]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormDialogView.C
ditch FileInfo -> use boost.filesystem
[lyx.git] / src / frontends / xforms / FormDialogView.C
1 /**
2  * \file FormDialogView.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 #include "FormDialogView.h"
14
15 #include "Tooltips.h"
16 #include "xforms_helpers.h" // formatted
17 #include "xforms_resize.h"
18 #include "xformsBC.h"
19
20 #include "controllers/ButtonController.h"
21
22 #include "lyxrc.h"
23
24 #include "support/filetools.h" //  LibFileSearch
25 #include "support/lstrings.h"
26
27 #include "lyx_forms.h"
28
29 using std::string;
30
31 namespace lyx {
32
33 using support::bformat;
34 using support::LibFileSearch;
35
36 namespace frontend {
37
38 extern "C" {
39
40 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
41 // These should be in forms.h but aren't
42 void fl_show_tooltip(const char *, int, int);
43 void fl_hide_tooltip();
44 #endif
45
46 // Callback function invoked by xforms when the dialog is closed by the
47 // window manager.
48 static int C_WMHideCB(FL_FORM * form, void *);
49
50 // Callback function invoked by the xforms pre-handler routine.
51 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
52
53 } // extern "C"
54
55
56 FormDialogView::FormDialogView(Dialog & parent,
57                                string const & t, bool allowResize)
58         : Dialog::View(parent, t),
59           warning_posted_(false), message_widget_(0),
60           minw_(0), minh_(0), allow_resize_(allowResize),
61           icon_pixmap_(0), icon_mask_(0),
62           tooltips_(new Tooltips)
63 {}
64
65
66 FormDialogView::~FormDialogView()
67 {
68         if (icon_pixmap_)
69                 XFreePixmap(fl_get_display(), icon_pixmap_);
70
71         delete tooltips_;
72 }
73
74
75 bool FormDialogView::isVisible() const
76 {
77         return form() && form()->visible;
78 }
79
80
81 Tooltips & FormDialogView::tooltips()
82 {
83         return *tooltips_;
84 }
85
86
87 void FormDialogView::redraw()
88 {
89         if (form() && form()->visible)
90                 fl_redraw_form(form());
91 }
92
93
94 xformsBC & FormDialogView::bcview()
95 {
96         return static_cast<xformsBC &>(dialog().bc().view());
97 }
98
99
100 void FormDialogView::prepare_to_show()
101 {
102         double const scale = get_scale_to_fit(form());
103         if (scale > 1.001)
104                 scale_form_horizontally(form(), scale);
105
106         // work around dumb xforms sizing bug
107         minw_ = form()->w;
108         minh_ = form()->h;
109
110         fl_set_form_atclose(form(), C_WMHideCB, 0);
111
112         // set the title for the minimized form
113         if (!lyxrc.dialogs_iconify_with_main)
114                 fl_winicontitle(form()->window, getTitle().c_str());
115
116         //  assign an icon to the form
117         string const iconname = LibFileSearch("images", "lyx", "xpm");
118         if (!iconname.empty()) {
119                 unsigned int w, h;
120                 icon_pixmap_ = fl_read_pixmapfile(fl_root,
121                                                   iconname.c_str(),
122                                                   &w,
123                                                   &h,
124                                                   &icon_mask_,
125                                                   0, 0, 0);
126                 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
127         }
128 }
129
130
131 void FormDialogView::show()
132 {
133         if (!form()) {
134                 build();
135         }
136
137         // we use minw_ to flag whether the dialog has ever been shown.
138         // In turn, prepare_to_show() initialises various bits 'n' pieces
139         // (including minw_).
140         if (minw_ == 0) {
141                 prepare_to_show();
142         }
143
144         // make sure the form is up to date.
145         fl_freeze_form(form());
146         update();
147         fl_unfreeze_form(form());
148
149         if (form()->visible) {
150                 fl_raise_form(form());
151                 /* This XMapWindow() will hopefully ensure that
152                  * iconified dialogs are de-iconified. Mad props
153                  * out to those crazy Xlib guys for forgetting a
154                  * XDeiconifyWindow(). At least WindowMaker, when
155                  * being notified of the redirected MapRequest will
156                  * specifically de-iconify. From source, fvwm2 seems
157                  * to do the same.
158                  */
159                 XMapWindow(fl_get_display(), form()->window);
160         } else {
161                 // calls to fl_set_form_minsize/maxsize apply only to the next
162                 // fl_show_form(), so this comes first.
163                 fl_set_form_minsize(form(), minw_, minh_);
164                 if (!allow_resize_)
165                         fl_set_form_maxsize(form(), minw_, minh_);
166
167                 string const maximize_title = "LyX: " + getTitle();
168                 int const iconify_policy = lyxrc.dialogs_iconify_with_main ?
169                         FL_TRANSIENT : 0;
170
171                 fl_show_form(form(),
172                              FL_PLACE_MOUSE | FL_FREE_SIZE,
173                              iconify_policy,
174                              maximize_title.c_str());
175         }
176 }
177
178
179 void FormDialogView::hide()
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 FormDialogView::setPrehandler(FL_OBJECT * ob)
193 {
194         BOOST_ASSERT(ob);
195         fl_set_object_prehandler(ob, C_PrehandlerCB);
196 }
197
198
199 void FormDialogView::setMessageWidget(FL_OBJECT * ob)
200 {
201         BOOST_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 FormDialogView::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 FormDialogView::input(FL_OBJECT *, long)
220 {
221         return ButtonPolicy::SMI_VALID;
222 }
223
224
225 // preemptive handler for feedback messages
226 void FormDialogView::MessageCB(FL_OBJECT * ob, int event)
227 {
228         BOOST_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 FormDialogView::PrehandlerCB(FL_OBJECT * ob, int event, int key)
254 {
255         BOOST_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
259                 // object using the middle mouse button.
260                 InputCB(ob, 0);
261                 return;
262         }
263
264         if (message_widget_) {
265                 switch (event) {
266                 case FL_ENTER:
267                 case FL_LEAVE:
268                         // Post feedback as the mouse enters the object,
269                         // remove it as the mouse leaves.
270                         MessageCB(ob, event);
271                         break;
272                 }
273         }
274
275 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
276         // Tooltips are not displayed on browser widgets due to an xforms' bug.
277         // This is a work-around:
278         if (ob->objclass == FL_BROWSER) {
279                 switch (event) {
280                 case FL_ENTER:
281                         if (ob->tooltip && *(ob->tooltip)) {
282                                 int const x = ob->form->x + ob->x;
283                                 int const y = ob->form->y + ob->y + ob->h + 1;
284                                 fl_show_tooltip(ob->tooltip, x, y);
285                         }
286                         break;
287                 case FL_LEAVE:
288                 case FL_PUSH:
289                 case FL_KEYPRESS:
290                         fl_hide_tooltip();
291                         break;
292                 }
293         }
294 #endif
295 }
296
297
298 void FormDialogView::postWarning(string const & warning)
299 {
300         warning_posted_ = true;
301         postMessage(warning);
302 }
303
304
305 void FormDialogView::clearMessage()
306 {
307         BOOST_ASSERT(message_widget_);
308
309         warning_posted_ = false;
310
311         string const existing = message_widget_->label
312                 ? message_widget_->label : string();
313         if (existing.empty())
314                 return;
315
316         // This trick is needed to get xforms to clear the label...
317         fl_set_object_label(message_widget_, "");
318         fl_hide_object(message_widget_);
319 }
320
321
322 void FormDialogView::postMessage(string const & message)
323 {
324         BOOST_ASSERT(message_widget_);
325
326         int const width = message_widget_->w - 10;
327         string const tmp = warning_posted_ ?
328                 bformat(_("WARNING! %1$s"), message) :
329                 message;
330
331         string const str = formatted(tmp, width, FL_NORMAL_SIZE);
332
333         fl_set_object_label(message_widget_, str.c_str());
334         FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
335         fl_set_object_lcol(message_widget_, label_color);
336
337         if (!message_widget_->visible)
338                 fl_show_object(message_widget_);
339 }
340
341
342 namespace {
343
344 FormDialogView * GetForm(FL_OBJECT * ob)
345 {
346         BOOST_ASSERT(ob && ob->form && ob->form->u_vdata);
347         FormDialogView * ptr =
348                 static_cast<FormDialogView *>(ob->form->u_vdata);
349         return ptr;
350 }
351
352 } // namespace anon
353
354
355 extern "C" {
356
357 void C_FormDialogView_ApplyCB(FL_OBJECT * ob, long)
358 {
359         GetForm(ob)->dialog().ApplyButton();
360 }
361
362
363 void C_FormDialogView_OKCB(FL_OBJECT * ob, long)
364 {
365         GetForm(ob)->dialog().OKButton();
366 }
367
368
369 void C_FormDialogView_CancelCB(FL_OBJECT * ob, long)
370 {
371         FormDialogView * form = GetForm(ob);
372         form->dialog().CancelButton();
373 }
374
375
376 void C_FormDialogView_RestoreCB(FL_OBJECT * ob, long)
377 {
378         GetForm(ob)->dialog().RestoreButton();
379 }
380
381
382 void C_FormDialogView_InputCB(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         BOOST_ASSERT(form && form->u_vdata);
392         FormDialogView * ptr = static_cast<FormDialogView *>(form->u_vdata);
393         ptr->dialog().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         BOOST_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         //BOOST_ASSERT(ob->form);
407         if (!ob->form) return 0;
408
409         FormDialogView * ptr =
410                 static_cast<FormDialogView *>(ob->form->u_vdata);
411
412         if (ptr)
413                 ptr->PrehandlerCB(ob, event, key);
414
415         return 0;
416 }
417
418 } // extern "C"
419
420 } // namespace frontend
421 } // namespace lyx