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