]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/XFormsView.C
ditch FileInfo -> use boost.filesystem
[lyx.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 "XMiniBuffer.h"
17
18 #include "BufferView.h"
19 #include "debug.h"
20 #include "lyxfunc.h"
21 #include "MenuBackend.h"
22
23 #include "frontends/Dialogs.h"
24 #include "frontends/Toolbars.h"
25
26 #include "support/filetools.h"        // OnlyFilename()
27
28 #include <boost/bind.hpp>
29
30 using boost::shared_ptr;
31
32 using std::abs;
33 using std::endl;
34 using std::string;
35
36 //extern void AutoSave(BufferView *);
37 extern void QuitLyX();
38
39 namespace lyx {
40
41 using support::LibFileSearch;
42
43 namespace frontend {
44
45 extern "C" {
46
47 static
48 int C_XFormsView_atCloseMainFormCB(FL_FORM * form, void * p)
49 {
50         return XFormsView::atCloseMainFormCB(form, p);
51 }
52
53 }
54
55
56 void print_metrics(std::ostream & os, std::string const & name, Box const & box)
57 {
58         os << name << " metrics:"
59            << "\tx = " << box.xorigin()
60            << "\ty = " << box.yorigin()
61            << "\tw = " << box.width()
62            << "\th = " << box.height() << '\n';
63 }
64
65
66 XFormsView::XFormsView(int width, int height)
67         : LyXView(),
68           window_(Box(width, height)),
69           icon_pixmap_(0), icon_mask_(0)
70 {
71         int const air = 2;
72
73         // Logical layout of the boxes making up the LyX window.
74         shared_ptr<Box> top = window_.children().push_back(Box(0,0));
75         shared_ptr<Box> middle = window_.children().push_back(Box(0,0));
76         middle->set(Box::Horizontal);
77         shared_ptr<Box> bottom = window_.children().push_back(Box(0,0));
78
79         shared_ptr<Box> left = middle->children().push_back(Box(air,0));
80         shared_ptr<Box> center = middle->children().push_back(Box(0,0));
81         center->set(Box::Expand);
82         shared_ptr<Box> right = middle->children().push_back(Box(air,0));
83
84         // Define accessors to the various boxes.
85         box_map_[Top]    = top;
86         box_map_[Bottom] = bottom;
87         box_map_[Left]   = left;
88         box_map_[Center] = center;
89         box_map_[Right]  = right;
90
91         // Define the XForms components making up the window.
92         // Each uses the layout engine defined above to control its
93         // dimensions.
94         form_ = fl_bgn_form(FL_NO_BOX, width, height);
95         form_->u_vdata = this;
96         fl_set_form_atclose(form_, C_XFormsView_atCloseMainFormCB, 0);
97
98         FL_OBJECT * obj = fl_add_box(FL_FLAT_BOX, 0, 0, width, height, "");
99         fl_set_object_color(obj, FL_MCOL, FL_MCOL);
100
101         menubar_.reset(new XFormsMenubar(this, menubackend));
102         getToolbars().init();
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(true);
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         updateToolbars();
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 shared_ptr<Box> XFormsView::getBox(Position pos) const
151 {
152         BoxMap::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(bool resize_form)
202 {
203         FL_FORM * form = getForm();
204
205         // We don't want the window to be downsized.
206         if (!resize_form)
207                 window_.setMinimumDimensions(form->w, form->h);
208
209         window_.updateMetrics();
210
211         fl_freeze_form(form);
212
213         if (resize_form)
214                 fl_set_form_size(form, window_.width(), window_.height());
215
216         // Emit a signal so that all daughter widgets are layed-out
217         // correctly.
218         metricsUpdated();
219         fl_unfreeze_form(form);
220 }
221
222
223 bool XFormsView::hasFocus() const
224 {
225         // No real implementation needed for now
226         return true;
227 }
228
229
230 void XFormsView::setWindowTitle(string const & title, string const & icon_title)
231 {
232         fl_set_form_title(getForm(), title.c_str());
233         fl_winicontitle(form_->window, icon_title.c_str());
234 }
235
236
237 void XFormsView::message(string const & str)
238 {
239         minibuffer_->message(str);
240 }
241
242
243 void XFormsView::clearMessage()
244 {
245         message(getLyXFunc().viewStatusMessage());
246 }
247
248
249 void XFormsView::show_view_state()
250 {
251         message(getLyXFunc().viewStatusMessage());
252 }
253
254
255 void XFormsView::busy(bool yes) const
256 {
257         if (yes) {
258                 view()->hideCursor();
259
260                 static Cursor cursor;
261                 static bool cursor_undefined = true;
262
263                 if (cursor_undefined) {
264                         cursor = XCreateFontCursor(fl_get_display(), XC_watch);
265                         XFlush(fl_get_display());
266                         cursor_undefined = false;
267                 }
268
269                 /// set the cursor to the watch for all forms and the canvas
270                 XDefineCursor(fl_get_display(), getForm()->window, cursor);
271
272                 XFlush(fl_get_display());
273
274                 /// we only need to deactivate to prevent resetting the cursor
275                 /// to I-beam over the workarea
276                 fl_deactivate_all_forms();
277         } else {
278                 /// reset the cursor from the watch for all forms and the canvas
279
280                 XUndefineCursor(fl_get_display(), getForm()->window);
281
282                 XFlush(fl_get_display());
283                 fl_activate_all_forms();
284         }
285 }
286
287 } // namespace frontend
288 } // namespace lyx