]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBase.C
fix crash with "save as"
[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 #if FL_VERSION > 0 || FL_REVISION >= 89
36
37 // These should be in forms.h but aren't
38 void fl_show_tooltip(const char *, int, int);
39
40 void fl_hide_tooltip();
41
42 #endif
43
44 // Callback function invoked by xforms when the dialog is closed by the
45 // window manager.
46 static int C_WMHideCB(FL_FORM * form, void *);
47
48 // Callback function invoked by the xforms pre-handler routine.
49 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
50
51 } // extern "C"
52
53
54 FormBase::FormBase(string const & t, bool allowResize)
55         : ViewBase(),
56           warning_posted_(false), message_widget_(0),
57           minw_(0), minh_(0), allow_resize_(allowResize),
58           title_(t), icon_pixmap_(0), icon_mask_(0),
59           tooltips_(new Tooltips())
60 {}
61
62
63 FormBase::~FormBase()
64 {
65         if (icon_pixmap_)
66                 XFreePixmap(fl_get_display(), icon_pixmap_);
67
68         delete tooltips_;
69 }
70
71
72 Tooltips & FormBase::tooltips()
73 {
74         return *tooltips_;
75 }
76
77
78 void FormBase::redraw()
79 {
80         if (form() && form()->visible)
81                 fl_redraw_form(form());
82 }
83
84
85 xformsBC & FormBase::bc()
86 {
87         return static_cast<xformsBC &>(getController().bc());
88         // return dynamic_cast<GUIbc &>(controller_ptr_->bc());
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 #if FL_VERSION > 0 || FL_REVISION >= 89
174         // Does no harm if none is visible and ensures that the tooltip form
175         // is hidden should the dialog be closed from the keyboard.
176         fl_hide_tooltip();
177 #endif
178
179         // xforms sometimes tries to process a hint-type MotionNotify, and
180         // use XQueryPointer, without verifying if the window still exists.
181         // So we try to clear out motion events in the queue before the
182         // DestroyNotify
183         XSync(fl_get_display(), false);
184
185         if (form() && form()->visible)
186                 fl_hide_form(form());
187 }
188
189
190 void FormBase::setPrehandler(FL_OBJECT * ob)
191 {
192         lyx::Assert(ob);
193         fl_set_object_prehandler(ob, C_PrehandlerCB);
194 }
195
196
197 void FormBase::setMessageWidget(FL_OBJECT * ob)
198 {
199         lyx::Assert(ob && ob->objclass == FL_TEXT);
200         message_widget_ = ob;
201         fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
202 }
203
204
205 void FormBase::InputCB(FL_OBJECT * ob, long data)
206 {
207         // It is possible to set the choice to 0 when using the
208         // keyboard shortcuts. This work-around deals with the problem.
209         if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
210                 fl_set_choice(ob, 1);
211         }
212
213         bc().input(input(ob, data));
214 }
215
216
217 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
218 {
219         return ButtonPolicy::SMI_VALID;
220 }
221
222
223 // preemptive handler for feedback messages
224 void FormBase::MessageCB(FL_OBJECT * ob, int event)
225 {
226         lyx::Assert(ob);
227
228         switch (event) {
229         case FL_ENTER:
230         {
231                 string const feedback = getFeedback(ob);
232                 if (feedback.empty() && warning_posted_)
233                         break;
234
235                 warning_posted_ = false;
236                 postMessage(getFeedback(ob));
237                 break;
238         }
239
240         case FL_LEAVE:
241                 if (!warning_posted_)
242                         clearMessage();
243                 break;
244
245         default:
246                 break;
247         }
248 }
249
250
251 void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
252 {
253         lyx::Assert(ob);
254
255         if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
256                 // Trigger an input event when pasting in an xforms input object
257                 // using the middle mouse button.
258                 InputCB(ob, 0);
259                 return;
260         }
261
262
263         if (event != FL_ENTER && event != FL_LEAVE)
264                 return;
265
266         if (ob->objclass == FL_TABFOLDER) {
267                 // This prehandler is used to work-around an xforms bug and
268                 // ensures that the form->x, form->y coords of the active
269                 // tabfolder are up to date.
270
271                 // The tabfolder itself can be very narrow, being just
272                 // the visible border to the tabs.
273                 // We thus use both FL_ENTER and FL_LEAVE as flags,
274                 // in case the FL_ENTER event is not caught.
275
276                 FL_FORM * const folder = fl_get_active_folder(ob);
277                 if (folder && folder->window) {
278                         fl_get_winorigin(folder->window,
279                                          &(folder->x), &(folder->y));
280                 }
281
282         }
283
284         if (message_widget_) {
285                 // Post feedback as the mouse enters the object,
286                 // remove it as the mouse leaves.
287                 MessageCB(ob, event);
288         }
289
290 #if FL_VERSION > 0 || FL_REVISION >= 89
291         // Tooltips are not displayed on browser widgets due to an xforms' bug.
292         // This is a work-around:
293         if (ob->objclass == FL_BROWSER) {
294                 if (event == FL_ENTER && ob->tooltip && *(ob->tooltip)) {
295                         fl_show_tooltip(ob->tooltip, ob->form->x + ob->x,
296                                         ob->form->y + ob->y + ob->h + 1);
297                 } else if (event == FL_LEAVE) {
298                         fl_hide_tooltip();
299                 }
300         }
301 #endif
302 }
303
304
305 void FormBase::postWarning(string const & warning)
306 {
307         warning_posted_ = true;
308         postMessage(warning);
309 }
310
311
312 void FormBase::clearMessage()
313 {
314         lyx::Assert(message_widget_);
315
316         warning_posted_ = false;
317
318         string const existing = message_widget_->label
319                 ? message_widget_->label : string();
320         if (existing.empty())
321                 return;
322
323         // This trick is needed to get xforms to clear the label...
324         fl_set_object_label(message_widget_, "");
325         fl_hide_object(message_widget_);
326 }
327
328
329 void FormBase::postMessage(string const & message)
330 {
331         lyx::Assert(message_widget_);
332
333         int const width = message_widget_->w - 10;
334 #if USE_BOOST_FORMAT
335         boost::format fmter = warning_posted_ ?
336                 boost::format(_("WARNING! %1$s")) :
337                 boost::format("%1$s");
338
339         string const str = formatted(boost::io::str(fmter % message),
340                                      width, FL_NORMAL_SIZE);
341 #else
342         string const tmp = warning_posted_ ?
343                 _("WARNING!") + string(" ") + message :
344                 message;
345
346         string const str = formatted(tmp, width, FL_NORMAL_SIZE);
347 #endif
348
349         fl_set_object_label(message_widget_, str.c_str());
350         FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
351         fl_set_object_lcol(message_widget_, label_color);
352
353         if (!message_widget_->visible)
354                 fl_show_object(message_widget_);
355 }
356
357
358 namespace {
359
360 FormBase * GetForm(FL_OBJECT * ob)
361 {
362         lyx::Assert(ob && ob->form && ob->form->u_vdata);
363         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
364         return ptr;
365 }
366
367 } // namespace anon
368
369
370 extern "C" {
371
372 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
373 {
374         GetForm(ob)->getController().ApplyButton();
375 }
376
377
378 void C_FormBaseOKCB(FL_OBJECT * ob, long)
379 {
380         GetForm(ob)->getController().OKButton();
381 }
382
383
384 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
385 {
386         FormBase * form = GetForm(ob);
387         form->getController().CancelButton();
388 }
389
390
391 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
392 {
393         GetForm(ob)->getController().RestoreButton();
394 }
395
396
397 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
398 {
399         GetForm(ob)->InputCB(ob, d);
400 }
401
402
403 static int C_WMHideCB(FL_FORM * form, void *)
404 {
405         // Close the dialog cleanly, even if the WM is used to do so.
406         lyx::Assert(form && form->u_vdata);
407         FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
408         ptr->getController().CancelButton();
409         return FL_CANCEL;
410 }
411
412 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
413                           FL_Coord, FL_Coord, int key, void *)
414 {
415         // Note that the return value is important in the pre-emptive handler.
416         // Don't return anything other than 0.
417         lyx::Assert(ob);
418
419         // Don't Assert this one, as it can happen quite naturally when things
420         // are being deleted in the d-tor.
421         //Assert(ob->form);
422         if (!ob->form) return 0;
423
424         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
425
426         if (ptr)
427                 ptr->PrehandlerCB(ob, event, key);
428
429         return 0;
430 }
431
432 } // extern "C"