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