]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBase.C
Introduce LFUN_PRINT.
[lyx.git] / src / frontends / xforms / FormBase.C
1 /**
2  * \file FormBase.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "FormBase.h"
14
15 #include "Tooltips.h"
16 #include "xforms_helpers.h" // formatted
17 #include "xforms_resize.h"
18 #include "xformsBC.h"
19
20 #include "controllers/ButtonController.h"
21 #include "controllers/ControlButtons.h"
22
23 #include "support/filetools.h" //  LibFileSearch
24 #include "support/lstrings.h"
25
26 #include "lyx_forms.h"
27
28 using lyx::support::bformat;
29 using lyx::support::LibFileSearch;
30
31 using std::string;
32
33
34 extern "C" {
35
36 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
37 // These should be in forms.h but aren't
38 void fl_show_tooltip(const char *, int, int);
39 void fl_hide_tooltip();
40 #endif
41
42 // Callback function invoked by xforms when the dialog is closed by the
43 // window manager.
44 static int C_WMHideCB(FL_FORM * form, void *);
45
46 // Callback function invoked by the xforms pre-handler routine.
47 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
48
49 } // extern "C"
50
51
52 FormBase::FormBase(string const & t, bool allowResize)
53         : ViewBase(t),
54           warning_posted_(false), message_widget_(0),
55           minw_(0), minh_(0), allow_resize_(allowResize),
56           icon_pixmap_(0), icon_mask_(0),
57           tooltips_(new Tooltips)
58 {}
59
60
61 FormBase::~FormBase()
62 {
63         if (icon_pixmap_)
64                 XFreePixmap(fl_get_display(), icon_pixmap_);
65
66         delete tooltips_;
67 }
68
69
70 bool FormBase::isVisible() const
71 {
72         return form() && form()->visible;
73 }
74
75
76 Tooltips & FormBase::tooltips()
77 {
78         return *tooltips_;
79 }
80
81
82 void FormBase::redraw()
83 {
84         if (form() && form()->visible)
85                 fl_redraw_form(form());
86 }
87
88
89 xformsBC & FormBase::bcview()
90 {
91         return static_cast<xformsBC &>(bc().view());
92 }
93
94
95 void FormBase::prepare_to_show()
96 {
97         double const scale = get_scale_to_fit(form());
98         if (scale > 1.001)
99                 scale_form_horizontally(form(), scale);
100
101         // work around dumb xforms sizing bug
102         minw_ = form()->w;
103         minh_ = form()->h;
104
105         fl_set_form_atclose(form(), C_WMHideCB, 0);
106
107         // set the title for the minimized form
108         if (!getController().IconifyWithMain())
109                 fl_winicontitle(form()->window, getTitle().c_str());
110
111         //  assign an icon to the form
112         string const iconname = LibFileSearch("images", "lyx", "xpm");
113         if (!iconname.empty()) {
114                 unsigned int w, h;
115                 icon_pixmap_ = fl_read_pixmapfile(fl_root,
116                                                   iconname.c_str(),
117                                                   &w,
118                                                   &h,
119                                                   &icon_mask_,
120                                                   0, 0, 0);
121                 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
122         }
123 }
124
125
126 void FormBase::show()
127 {
128         // build() is/should be called from the controller, so form() should
129         // always exist.
130         BOOST_ASSERT(form());
131
132         // we use minw_ to flag whether the dialog has ever been shown.
133         // In turn, prepare_to_show() initialises various bits 'n' pieces
134         // (including minw_).
135         if (minw_ == 0) {
136                 prepare_to_show();
137         }
138
139         // make sure the form is up to date.
140         fl_freeze_form(form());
141         update();
142         fl_unfreeze_form(form());
143
144         if (form()->visible) {
145                 fl_raise_form(form());
146                 /* This XMapWindow() will hopefully ensure that
147                  * iconified dialogs are de-iconified. Mad props
148                  * out to those crazy Xlib guys for forgetting a
149                  * XDeiconifyWindow(). At least WindowMaker, when
150                  * being notified of the redirected MapRequest will
151                  * specifically de-iconify. From source, fvwm2 seems
152                  * to do the same.
153                  */
154                 XMapWindow(fl_get_display(), form()->window);
155         } else {
156                 // calls to fl_set_form_minsize/maxsize apply only to the next
157                 // fl_show_form(), so this comes first.
158                 fl_set_form_minsize(form(), minw_, minh_);
159                 if (!allow_resize_)
160                         fl_set_form_maxsize(form(), minw_, minh_);
161
162                 string const maximize_title = "LyX: " + getTitle();
163                 int const iconify_policy =
164                         getController().IconifyWithMain() ? FL_TRANSIENT : 0;
165
166                 fl_show_form(form(),
167                              FL_PLACE_MOUSE | FL_FREE_SIZE,
168                              iconify_policy,
169                              maximize_title.c_str());
170         }
171 }
172
173
174 void FormBase::hide()
175 {
176         // xforms sometimes tries to process a hint-type MotionNotify, and
177         // use XQueryPointer, without verifying if the window still exists.
178         // So we try to clear out motion events in the queue before the
179         // DestroyNotify
180         XSync(fl_get_display(), false);
181
182         if (form() && form()->visible)
183                 fl_hide_form(form());
184 }
185
186
187 void FormBase::setPrehandler(FL_OBJECT * ob)
188 {
189         BOOST_ASSERT(ob);
190         fl_set_object_prehandler(ob, C_PrehandlerCB);
191 }
192
193
194 void FormBase::setMessageWidget(FL_OBJECT * ob)
195 {
196         BOOST_ASSERT(ob && ob->objclass == FL_TEXT);
197         message_widget_ = ob;
198         fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
199 }
200
201
202 void FormBase::InputCB(FL_OBJECT * ob, long data)
203 {
204         // It is possible to set the choice to 0 when using the
205         // keyboard shortcuts. This work-around deals with the problem.
206         if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
207                 fl_set_choice(ob, 1);
208         }
209
210         bc().input(input(ob, data));
211 }
212
213
214 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
215 {
216         return ButtonPolicy::SMI_VALID;
217 }
218
219
220 // preemptive handler for feedback messages
221 void FormBase::MessageCB(FL_OBJECT * ob, int event)
222 {
223         BOOST_ASSERT(ob);
224
225         switch (event) {
226         case FL_ENTER:
227         {
228                 string const feedback = getFeedback(ob);
229                 if (feedback.empty() && warning_posted_)
230                         break;
231
232                 warning_posted_ = false;
233                 postMessage(getFeedback(ob));
234                 break;
235         }
236
237         case FL_LEAVE:
238                 if (!warning_posted_)
239                         clearMessage();
240                 break;
241
242         default:
243                 break;
244         }
245 }
246
247
248 void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
249 {
250         BOOST_ASSERT(ob);
251
252         if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
253                 // Trigger an input event when pasting in an xforms input object
254                 // using the middle mouse button.
255                 InputCB(ob, 0);
256                 return;
257         }
258
259         if (message_widget_) {
260                 switch (event) {
261                 case FL_ENTER:
262                 case FL_LEAVE:
263                         // Post feedback as the mouse enters the object,
264                         // remove it as the mouse leaves.
265                         MessageCB(ob, event);
266                         break;
267                 }
268         }
269
270 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
271         // Tooltips are not displayed on browser widgets due to an xforms' bug.
272         // This is a work-around:
273         if (ob->objclass == FL_BROWSER) {
274                 switch (event) {
275                 case FL_ENTER:
276                         if (ob->tooltip && *(ob->tooltip)) {
277                                 int const x = ob->form->x + ob->x;
278                                 int const y = ob->form->y + ob->y + ob->h + 1;
279                                 fl_show_tooltip(ob->tooltip, x, y);
280                         }
281                         break;
282                 case FL_LEAVE:
283                 case FL_PUSH:
284                 case FL_KEYPRESS:
285                         fl_hide_tooltip();
286                         break;
287                 }
288         }
289 #endif
290 }
291
292
293 void FormBase::postWarning(string const & warning)
294 {
295         warning_posted_ = true;
296         postMessage(warning);
297 }
298
299
300 void FormBase::clearMessage()
301 {
302         BOOST_ASSERT(message_widget_);
303
304         warning_posted_ = false;
305
306         string const existing = message_widget_->label
307                 ? message_widget_->label : string();
308         if (existing.empty())
309                 return;
310
311         // This trick is needed to get xforms to clear the label...
312         fl_set_object_label(message_widget_, "");
313         fl_hide_object(message_widget_);
314 }
315
316
317 void FormBase::postMessage(string const & message)
318 {
319         BOOST_ASSERT(message_widget_);
320
321         int const width = message_widget_->w - 10;
322         string const tmp = warning_posted_ ?
323                 bformat(_("WARNING! %1$s"), message) :
324                 message;
325
326         string const str = formatted(tmp, width, FL_NORMAL_SIZE);
327
328         fl_set_object_label(message_widget_, str.c_str());
329         FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
330         fl_set_object_lcol(message_widget_, label_color);
331
332         if (!message_widget_->visible)
333                 fl_show_object(message_widget_);
334 }
335
336
337 namespace {
338
339 FormBase * GetForm(FL_OBJECT * ob)
340 {
341         BOOST_ASSERT(ob && ob->form && ob->form->u_vdata);
342         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
343         return ptr;
344 }
345
346 } // namespace anon
347
348
349 extern "C" {
350
351 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
352 {
353         GetForm(ob)->getController().ApplyButton();
354 }
355
356
357 void C_FormBaseOKCB(FL_OBJECT * ob, long)
358 {
359         GetForm(ob)->getController().OKButton();
360 }
361
362
363 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
364 {
365         FormBase * form = GetForm(ob);
366         form->getController().CancelButton();
367 }
368
369
370 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
371 {
372         GetForm(ob)->getController().RestoreButton();
373 }
374
375
376 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
377 {
378         GetForm(ob)->InputCB(ob, d);
379 }
380
381
382 static int C_WMHideCB(FL_FORM * form, void *)
383 {
384         // Close the dialog cleanly, even if the WM is used to do so.
385         BOOST_ASSERT(form && form->u_vdata);
386         FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
387         ptr->getController().CancelButton();
388         return FL_CANCEL;
389 }
390
391 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
392                           FL_Coord, FL_Coord, int key, void *)
393 {
394         // Note that the return value is important in the pre-emptive handler.
395         // Don't return anything other than 0.
396         BOOST_ASSERT(ob);
397
398         // Don't Assert this one, as it can happen quite naturally when things
399         // are being deleted in the d-tor.
400         //BOOST_ASSERT(ob->form);
401         if (!ob->form) return 0;
402
403         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
404
405         if (ptr)
406                 ptr->PrehandlerCB(ob, event, key);
407
408         return 0;
409 }
410
411 } // extern "C"