]> git.lyx.org Git - features.git/blob - src/frontends/xforms/XFormsView.C
f3d92b503ed358904b452770663e06363f4f4318
[features.git] / src / frontends / xforms / XFormsView.C
1 /**
2  * \file XFormsView.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "XFormsView.h"
14
15 #include "XFormsMenubar.h"
16 #include "XFormsToolbar.h"
17 #include "XMiniBuffer.h"
18
19 #include "BufferView.h"
20 #include "debug.h"
21 #include "lyxfunc.h"
22 #include "MenuBackend.h"
23
24 #include "frontends/Dialogs.h"
25
26 #include "support/filetools.h"        // OnlyFilename()
27
28 #include <boost/bind.hpp>
29
30 using lyx::frontend::Box;
31
32 using lyx::support::LibFileSearch;
33
34 using std::abs;
35 using std::endl;
36 using std::string;
37
38
39 //extern void AutoSave(BufferView *);
40 extern void QuitLyX();
41
42 extern "C" {
43
44 static
45 int C_XFormsView_atCloseMainFormCB(FL_FORM * form, void * p)
46 {
47         return XFormsView::atCloseMainFormCB(form, p);
48 }
49
50 }
51
52
53 void print_metrics(std::ostream & os, std::string const & name, Box const & box)
54 {
55         os << name << " metrics:"
56            << "\tx = " << box.xorigin()
57            << "\ty = " << box.yorigin()
58            << "\tw = " << box.width()
59            << "\th = " << box.height() << '\n';
60 }
61
62
63 XFormsView::XFormsView(int width, int height)
64         : LyXView(),
65           window_(Box(width, height)),
66           icon_pixmap_(0), icon_mask_(0)
67 {
68         int const air = 2;
69
70         // Logical layout of the boxes making up the LyX window.
71         Box & top    = window_.children().push_back(Box(0,0));
72         Box & middle = window_.children().push_back(Box(0,0));
73         middle.set(Box::Horizontal);
74         Box & bottom = window_.children().push_back(Box(0,0));
75
76         Box & left   = middle.children().push_back(Box(air,0));
77         Box & center = middle.children().push_back(Box(0,0));
78         center.set(Box::Expand);
79         Box & right  = middle.children().push_back(Box(air,0));
80
81         // Define accessors to the various boxes.
82         box_map_[Top]    = &top;
83         box_map_[Bottom] = &bottom;
84         box_map_[Left]   = &left;
85         box_map_[Center] = &center;
86         box_map_[Right]  = &right;
87
88         // Define the XForms components making up the window.
89         // Each uses the layout engine defined above to control its
90         // dimensions.
91         form_ = fl_bgn_form(FL_NO_BOX, width, height);
92         form_->u_vdata = this;
93         fl_set_form_atclose(form_, C_XFormsView_atCloseMainFormCB, 0);
94
95         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
96         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
97
98         menubar_.reset(new XFormsMenubar(this, menubackend));
99
100         toolbar_.reset(new XFormsToolbar(this));
101         toolbar_->init();
102
103         bufferview_.reset(new BufferView(this, width, height));
104         minibuffer_.reset(new XMiniBuffer(*this, *controlcommand_));
105
106         //  Assign an icon to the main form.
107         string const iconname = LibFileSearch("images", "lyx", "xpm");
108         if (!iconname.empty()) {
109                 unsigned int w, h;
110                 icon_pixmap_ = fl_read_pixmapfile(fl_root,
111                                            iconname.c_str(),
112                                            &w,
113                                            &h,
114                                            &icon_mask_,
115                                            0,
116                                            0,
117                                            0);
118                 fl_set_form_icon(form_, icon_pixmap_, icon_mask_);
119         }
120
121         fl_end_form();
122
123         // Update the layout so that all widgets fit.
124         window_.show();
125         updateMetrics();
126         
127         view_state_con =
128                 view_state_changed.connect(boost::bind(&XFormsView::show_view_state, this));
129         focus_con =
130                 focus_command_buffer.connect(boost::bind(&XMiniBuffer::focus, minibuffer_.get()));
131
132         // Make sure the buttons are disabled if needed.
133         updateToolbar();
134         redraw_con =
135                 getDialogs().redrawGUI().connect(boost::bind(&XFormsView::redraw, this));
136 }
137
138
139 XFormsView::~XFormsView()
140 {
141         if (icon_pixmap_)
142                 XFreePixmap(fl_get_display(), icon_pixmap_);
143
144         minibuffer_->freeze();
145         fl_hide_form(form_);
146         fl_free_form(form_);
147 }
148
149
150 Box & XFormsView::getBox(Position pos) const
151 {
152         std::map<Position, Box *>::const_iterator it = box_map_.find(pos);
153         BOOST_ASSERT(it != box_map_.end());
154         return *it->second;
155 }
156
157
158 /// Redraw the main form.
159 void XFormsView::redraw()
160 {
161         lyxerr[Debug::INFO] << "XFormsView::redraw()" << endl;
162         fl_redraw_form(getForm());
163         minibuffer_->redraw();
164 }
165
166
167 FL_FORM * XFormsView::getForm() const
168 {
169         return form_;
170 }
171
172
173 // Callback for close main form from window manager
174 int XFormsView::atCloseMainFormCB(FL_FORM *, void *)
175 {
176         QuitLyX();
177         return FL_IGNORE;
178 }
179
180
181 void XFormsView::show(int x, int y, string const & title)
182 {
183         FL_FORM * form = getForm();
184
185         fl_set_form_minsize(form, form->w, form->h);
186
187         int placement = FL_PLACE_CENTER | FL_FREE_SIZE;
188
189         // Did we get a valid geometry position ?
190         if (x >= 0 && y >= 0) {
191                 fl_set_form_position(form, x, y);
192                 placement = FL_PLACE_POSITION;
193         }
194
195         fl_show_form(form, placement, FL_FULLBORDER, title.c_str());
196
197         show_view_state();
198 }
199
200
201 void XFormsView::updateMetrics()
202 {
203         window_.updateMetrics();
204
205         FL_FORM * form = getForm();
206         fl_set_form_size(form, window_.width(), window_.height());
207
208         // Emit a signal so that all daughter widgets are layed-out
209         // correctly.
210         metricsUpdated();
211 }
212
213
214 void XFormsView::setWindowTitle(string const & title, string const & icon_title)
215 {
216         fl_set_form_title(getForm(), title.c_str());
217         fl_winicontitle(form_->window, icon_title.c_str());
218 }
219
220
221 void XFormsView::message(string const & str)
222 {
223         minibuffer_->message(str);
224 }
225
226
227 void XFormsView::clearMessage()
228 {
229         message(getLyXFunc().viewStatusMessage());
230 }
231
232
233 void XFormsView::show_view_state()
234 {
235         message(getLyXFunc().viewStatusMessage());
236 }
237
238
239 void XFormsView::busy(bool yes) const
240 {
241         if (yes) {
242                 view()->hideCursor();
243
244                 static Cursor cursor;
245                 static bool cursor_undefined = true;
246
247                 if (cursor_undefined) {
248                         cursor = XCreateFontCursor(fl_get_display(), XC_watch);
249                         XFlush(fl_get_display());
250                         cursor_undefined = false;
251                 }
252
253                 /// set the cursor to the watch for all forms and the canvas
254                 XDefineCursor(fl_get_display(), getForm()->window, cursor);
255
256                 XFlush(fl_get_display());
257
258                 /// we only need to deactivate to prevent resetting the cursor
259                 /// to I-beam over the workarea
260                 fl_deactivate_all_forms();
261         } else {
262                 /// reset the cursor from the watch for all forms and the canvas
263
264                 XUndefineCursor(fl_get_display(), getForm()->window);
265
266                 XFlush(fl_get_display());
267                 fl_activate_all_forms();
268         }
269 }