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