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