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