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