]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormDialogView.C
remove defaults stuff, let Qt handle no toolbar
[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 // These should be in forms.h but aren't
35 void fl_show_tooltip(const char *, int, int);
36
37 void fl_hide_tooltip();
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 FormDialogView::FormDialogView(Dialog & parent,
50                                string const & t, bool allowResize)
51         : Dialog::View(parent),
52           warning_posted_(false), message_widget_(0),
53           minw_(0), minh_(0), allow_resize_(allowResize),
54           title_(t), icon_pixmap_(0), icon_mask_(0),
55           tooltips_(new Tooltips())
56 {}
57
58
59 FormDialogView::~FormDialogView()
60 {
61         if (icon_pixmap_)
62                 XFreePixmap(fl_get_display(), icon_pixmap_);
63
64         delete tooltips_;
65 }
66
67
68 bool FormDialogView::isVisible() const
69 {
70         return form() && form()->visible;
71 }
72
73
74 Tooltips & FormDialogView::tooltips()
75 {
76         return *tooltips_;
77 }
78
79
80 void FormDialogView::redraw()
81 {
82         if (form() && form()->visible)
83                 fl_redraw_form(form());
84 }
85
86
87 xformsBC & FormDialogView::bcview()
88 {
89         return static_cast<xformsBC &>(dialog().bc().view());
90 }
91
92
93 void FormDialogView::prepare_to_show()
94 {
95         double const scale = get_scale_to_fit(form());
96         if (scale > 1.001)
97                 scale_form_horizontally(form(), scale);
98
99         // work around dumb xforms sizing bug
100         minw_ = form()->w;
101         minh_ = form()->h;
102
103         fl_set_form_atclose(form(), C_WMHideCB, 0);
104
105         // set the title for the minimized form
106         if (!lyxrc.dialogs_iconify_with_main)
107                 fl_winicontitle(form()->window, title_.c_str());
108
109         //  assign an icon to the form
110         string const iconname = LibFileSearch("images", "lyx", "xpm");
111         if (!iconname.empty()) {
112                 unsigned int w, h;
113                 icon_pixmap_ = fl_read_pixmapfile(fl_root,
114                                                   iconname.c_str(),
115                                                   &w,
116                                                   &h,
117                                                   &icon_mask_,
118                                                   0, 0, 0);
119                 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
120         }
121 }
122
123
124 void FormDialogView::show()
125 {
126         if (!form()) {
127                 build();
128         }
129
130         // we use minw_ to flag whether the dialog has ever been shown.
131         // In turn, prepare_to_show() initialises various bits 'n' pieces
132         // (including minw_).
133         if (minw_ == 0) {
134                 prepare_to_show();
135         }
136
137         // make sure the form is up to date.
138         fl_freeze_form(form());
139         update();
140         fl_unfreeze_form(form());
141
142         if (form()->visible) {
143                 fl_raise_form(form());
144                 /* This XMapWindow() will hopefully ensure that
145                  * iconified dialogs are de-iconified. Mad props
146                  * out to those crazy Xlib guys for forgetting a
147                  * XDeiconifyWindow(). At least WindowMaker, when
148                  * being notified of the redirected MapRequest will
149                  * specifically de-iconify. From source, fvwm2 seems
150                  * to do the same.
151                  */
152                 XMapWindow(fl_get_display(), form()->window);
153         } else {
154                 // calls to fl_set_form_minsize/maxsize apply only to the next
155                 // fl_show_form(), so this comes first.
156                 fl_set_form_minsize(form(), minw_, minh_);
157                 if (!allow_resize_)
158                         fl_set_form_maxsize(form(), minw_, minh_);
159
160                 string const maximize_title = "LyX: " + title_;
161                 int const iconify_policy = lyxrc.dialogs_iconify_with_main ?
162                         FL_TRANSIENT : 0;
163
164                 fl_show_form(form(),
165                              FL_PLACE_MOUSE | FL_FREE_SIZE,
166                              iconify_policy,
167                              maximize_title.c_str());
168         }
169 }
170
171
172 void FormDialogView::hide()
173 {
174         // xforms sometimes tries to process a hint-type MotionNotify, and
175         // use XQueryPointer, without verifying if the window still exists.
176         // So we try to clear out motion events in the queue before the
177         // DestroyNotify
178         XSync(fl_get_display(), false);
179
180         if (form() && form()->visible)
181                 fl_hide_form(form());
182 }
183
184
185 void FormDialogView::setPrehandler(FL_OBJECT * ob)
186 {
187         lyx::Assert(ob);
188         fl_set_object_prehandler(ob, C_PrehandlerCB);
189 }
190
191
192 void FormDialogView::setMessageWidget(FL_OBJECT * ob)
193 {
194         lyx::Assert(ob && ob->objclass == FL_TEXT);
195         message_widget_ = ob;
196         fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
197 }
198
199
200 void FormDialogView::InputCB(FL_OBJECT * ob, long data)
201 {
202         // It is possible to set the choice to 0 when using the
203         // keyboard shortcuts. This work-around deals with the problem.
204         if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
205                 fl_set_choice(ob, 1);
206         }
207
208         bc().input(input(ob, data));
209 }
210
211
212 ButtonPolicy::SMInput FormDialogView::input(FL_OBJECT *, long)
213 {
214         return ButtonPolicy::SMI_VALID;
215 }
216
217
218 // preemptive handler for feedback messages
219 void FormDialogView::MessageCB(FL_OBJECT * ob, int event)
220 {
221         lyx::Assert(ob);
222
223         switch (event) {
224         case FL_ENTER:
225         {
226                 string const feedback = getFeedback(ob);
227                 if (feedback.empty() && warning_posted_)
228                         break;
229
230                 warning_posted_ = false;
231                 postMessage(getFeedback(ob));
232                 break;
233         }
234
235         case FL_LEAVE:
236                 if (!warning_posted_)
237                         clearMessage();
238                 break;
239
240         default:
241                 break;
242         }
243 }
244
245
246 void FormDialogView::PrehandlerCB(FL_OBJECT * ob, int event, int key)
247 {
248         lyx::Assert(ob);
249
250         if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
251                 // Trigger an input event when pasting in an xforms input
252                 // object using the middle mouse button.
253                 InputCB(ob, 0);
254                 return;
255         }
256
257         if (message_widget_) {
258                 switch (event) {
259                 case FL_ENTER:
260                 case FL_LEAVE:
261                         // Post feedback as the mouse enters the object,
262                         // remove it as the mouse leaves.
263                         MessageCB(ob, event);
264                         break;
265                 }
266         }
267
268         // Tooltips are not displayed on browser widgets due to an xforms' bug.
269         // I have a fix, but it's not yet in the xforms sources.
270         // This is a work-around:
271         if (ob->objclass == FL_BROWSER) {
272                 switch (event) {
273                 case FL_ENTER:
274                         if (ob->tooltip && *(ob->tooltip)) {
275                                 int const x = ob->form->x + ob->x;
276                                 int const y = ob->form->y + ob->y + ob->h + 1;
277                                 fl_show_tooltip(ob->tooltip, x, y);
278                         }
279                         break;
280                 case FL_LEAVE:
281                 case FL_PUSH:
282                 case FL_KEYPRESS:
283                         fl_hide_tooltip();
284                         break;
285                 }
286         }
287 }
288
289
290 void FormDialogView::postWarning(string const & warning)
291 {
292         warning_posted_ = true;
293         postMessage(warning);
294 }
295
296
297 void FormDialogView::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 FormDialogView::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 FormDialogView * GetForm(FL_OBJECT * ob)
346 {
347         lyx::Assert(ob && ob->form && ob->form->u_vdata);
348         FormDialogView * ptr =
349                 static_cast<FormDialogView *>(ob->form->u_vdata);
350         return ptr;
351 }
352
353 } // namespace anon
354
355
356 extern "C" {
357
358 void C_FormDialogView_ApplyCB(FL_OBJECT * ob, long)
359 {
360         GetForm(ob)->dialog().ApplyButton();
361 }
362
363
364 void C_FormDialogView_OKCB(FL_OBJECT * ob, long)
365 {
366         GetForm(ob)->dialog().OKButton();
367 }
368
369
370 void C_FormDialogView_CancelCB(FL_OBJECT * ob, long)
371 {
372         FormDialogView * form = GetForm(ob);
373         form->dialog().CancelButton();
374 }
375
376
377 void C_FormDialogView_RestoreCB(FL_OBJECT * ob, long)
378 {
379         GetForm(ob)->dialog().RestoreButton();
380 }
381
382
383 void C_FormDialogView_InputCB(FL_OBJECT * ob, long d)
384 {
385         GetForm(ob)->InputCB(ob, d);
386 }
387
388
389 static int C_WMHideCB(FL_FORM * form, void *)
390 {
391         // Close the dialog cleanly, even if the WM is used to do so.
392         lyx::Assert(form && form->u_vdata);
393         FormDialogView * ptr = static_cast<FormDialogView *>(form->u_vdata);
394         ptr->dialog().CancelButton();
395         return FL_CANCEL;
396 }
397
398 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
399                           FL_Coord, FL_Coord, int key, void *)
400 {
401         // Note that the return value is important in the pre-emptive handler.
402         // Don't return anything other than 0.
403         lyx::Assert(ob);
404
405         // Don't Assert this one, as it can happen quite naturally when things
406         // are being deleted in the d-tor.
407         //Assert(ob->form);
408         if (!ob->form) return 0;
409
410         FormDialogView * ptr =
411                 static_cast<FormDialogView *>(ob->form->u_vdata);
412
413         if (ptr)
414                 ptr->PrehandlerCB(ob, event, key);
415
416         return 0;
417 }
418
419 } // extern "C"