]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBase.C
remove defaults stuff, let Qt handle no toolbar
[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
14 #include "FormBase.h"
15
16 #include "ControlButtons.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 "support/BoostFormat.h"
25
26 #include "support/LAssert.h"
27 #include "support/filetools.h" //  LibFileSearch
28
29 #include FORMS_H_LOCATION
30
31 extern "C" {
32
33 // These should be in forms.h but aren't
34 void fl_show_tooltip(const char *, int, int);
35
36 void fl_hide_tooltip();
37
38 // Callback function invoked by xforms when the dialog is closed by the
39 // window manager.
40 static int C_WMHideCB(FL_FORM * form, void *);
41
42 // Callback function invoked by the xforms pre-handler routine.
43 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
44
45 } // extern "C"
46
47
48 FormBase::FormBase(string const & t, bool allowResize)
49         : ViewBase(),
50           warning_posted_(false), message_widget_(0),
51           minw_(0), minh_(0), allow_resize_(allowResize),
52           title_(t), icon_pixmap_(0), icon_mask_(0),
53           tooltips_(new Tooltips())
54 {}
55
56
57 FormBase::~FormBase()
58 {
59         if (icon_pixmap_)
60                 XFreePixmap(fl_get_display(), icon_pixmap_);
61
62         delete tooltips_;
63 }
64
65
66 bool FormBase::isVisible() const
67 {
68         return form() && form()->visible;
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::bcview()
86 {
87         return static_cast<xformsBC &>(bc().view());
88 }
89
90
91 void FormBase::prepare_to_show()
92 {
93         double const scale = get_scale_to_fit(form());
94         if (scale > 1.001)
95                 scale_form_horizontally(form(), scale);
96
97         // work around dumb xforms sizing bug
98         minw_ = form()->w;
99         minh_ = form()->h;
100
101         fl_set_form_atclose(form(), C_WMHideCB, 0);
102
103         // set the title for the minimized form
104         if (!getController().IconifyWithMain())
105                 fl_winicontitle(form()->window, title_.c_str());
106
107         //  assign an icon to the form
108         string const iconname = LibFileSearch("images", "lyx", "xpm");
109         if (!iconname.empty()) {
110                 unsigned int w, h;
111                 icon_pixmap_ = fl_read_pixmapfile(fl_root,
112                                                   iconname.c_str(),
113                                                   &w,
114                                                   &h,
115                                                   &icon_mask_,
116                                                   0, 0, 0);
117                 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
118         }
119 }
120
121
122 void FormBase::show()
123 {
124         // build() is/should be called from the controller, so form() should
125         // always exist.
126         lyx::Assert(form());
127
128         // we use minw_ to flag whether the dialog has ever been shown.
129         // In turn, prepare_to_show() initialises various bits 'n' pieces
130         // (including minw_).
131         if (minw_ == 0) {
132                 prepare_to_show();
133         }
134
135         // make sure the form is up to date.
136         fl_freeze_form(form());
137         update();
138         fl_unfreeze_form(form());
139
140         if (form()->visible) {
141                 fl_raise_form(form());
142                 /* This XMapWindow() will hopefully ensure that
143                  * iconified dialogs are de-iconified. Mad props
144                  * out to those crazy Xlib guys for forgetting a
145                  * XDeiconifyWindow(). At least WindowMaker, when
146                  * being notified of the redirected MapRequest will
147                  * specifically de-iconify. From source, fvwm2 seems
148                  * to do the same.
149                  */
150                 XMapWindow(fl_get_display(), form()->window);
151         } else {
152                 // calls to fl_set_form_minsize/maxsize apply only to the next
153                 // fl_show_form(), so this comes first.
154                 fl_set_form_minsize(form(), minw_, minh_);
155                 if (!allow_resize_)
156                         fl_set_form_maxsize(form(), minw_, minh_);
157
158                 string const maximize_title = "LyX: " + title_;
159                 int const iconify_policy =
160                         getController().IconifyWithMain() ? FL_TRANSIENT : 0;
161
162                 fl_show_form(form(),
163                              FL_PLACE_MOUSE | FL_FREE_SIZE,
164                              iconify_policy,
165                              maximize_title.c_str());
166         }
167 }
168
169
170 void FormBase::hide()
171 {
172         // xforms sometimes tries to process a hint-type MotionNotify, and
173         // use XQueryPointer, without verifying if the window still exists.
174         // So we try to clear out motion events in the queue before the
175         // DestroyNotify
176         XSync(fl_get_display(), false);
177
178         if (form() && form()->visible)
179                 fl_hide_form(form());
180 }
181
182
183 void FormBase::setPrehandler(FL_OBJECT * ob)
184 {
185         lyx::Assert(ob);
186         fl_set_object_prehandler(ob, C_PrehandlerCB);
187 }
188
189
190 void FormBase::setMessageWidget(FL_OBJECT * ob)
191 {
192         lyx::Assert(ob && ob->objclass == FL_TEXT);
193         message_widget_ = ob;
194         fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
195 }
196
197
198 void FormBase::InputCB(FL_OBJECT * ob, long data)
199 {
200         // It is possible to set the choice to 0 when using the
201         // keyboard shortcuts. This work-around deals with the problem.
202         if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
203                 fl_set_choice(ob, 1);
204         }
205
206         bc().input(input(ob, data));
207 }
208
209
210 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
211 {
212         return ButtonPolicy::SMI_VALID;
213 }
214
215
216 // preemptive handler for feedback messages
217 void FormBase::MessageCB(FL_OBJECT * ob, int event)
218 {
219         lyx::Assert(ob);
220
221         switch (event) {
222         case FL_ENTER:
223         {
224                 string const feedback = getFeedback(ob);
225                 if (feedback.empty() && warning_posted_)
226                         break;
227
228                 warning_posted_ = false;
229                 postMessage(getFeedback(ob));
230                 break;
231         }
232
233         case FL_LEAVE:
234                 if (!warning_posted_)
235                         clearMessage();
236                 break;
237
238         default:
239                 break;
240         }
241 }
242
243
244 void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
245 {
246         lyx::Assert(ob);
247
248         if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
249                 // Trigger an input event when pasting in an xforms input object
250                 // using the middle mouse button.
251                 InputCB(ob, 0);
252                 return;
253         }
254
255         if (message_widget_) {
256                 switch (event) {
257                 case FL_ENTER:
258                 case FL_LEAVE:
259                         // Post feedback as the mouse enters the object,
260                         // remove it as the mouse leaves.
261                         MessageCB(ob, event);
262                         break;
263                 }
264         }
265
266         // Tooltips are not displayed on browser widgets due to an xforms' bug.
267         // I have a fix, but it's not yet in the xforms sources.
268         // This is a work-around:
269         if (ob->objclass == FL_BROWSER) {
270                 switch (event) {
271                 case FL_ENTER:
272                         if (ob->tooltip && *(ob->tooltip)) {
273                                 int const x = ob->form->x + ob->x;
274                                 int const y = ob->form->y + ob->y + ob->h + 1;
275                                 fl_show_tooltip(ob->tooltip, x, y);
276                         }
277                         break;
278                 case FL_LEAVE:
279                 case FL_PUSH:
280                 case FL_KEYPRESS:
281                         fl_hide_tooltip();
282                         break;
283                 }
284         }
285 }
286
287
288 void FormBase::postWarning(string const & warning)
289 {
290         warning_posted_ = true;
291         postMessage(warning);
292 }
293
294
295 void FormBase::clearMessage()
296 {
297         lyx::Assert(message_widget_);
298
299         warning_posted_ = false;
300
301         string const existing = message_widget_->label
302                 ? message_widget_->label : string();
303         if (existing.empty())
304                 return;
305
306         // This trick is needed to get xforms to clear the label...
307         fl_set_object_label(message_widget_, "");
308         fl_hide_object(message_widget_);
309 }
310
311
312 void FormBase::postMessage(string const & message)
313 {
314         lyx::Assert(message_widget_);
315
316         int const width = message_widget_->w - 10;
317 #if USE_BOOST_FORMAT
318         boost::format fmter = warning_posted_ ?
319                 boost::format(_("WARNING! %1$s")) :
320                 boost::format("%1$s");
321
322         string const str = formatted(boost::io::str(fmter % message),
323                                      width, FL_NORMAL_SIZE);
324 #else
325         string const tmp = warning_posted_ ?
326                 _("WARNING!") + string(" ") + message :
327                 message;
328
329         string const str = formatted(tmp, width, FL_NORMAL_SIZE);
330 #endif
331
332         fl_set_object_label(message_widget_, str.c_str());
333         FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
334         fl_set_object_lcol(message_widget_, label_color);
335
336         if (!message_widget_->visible)
337                 fl_show_object(message_widget_);
338 }
339
340
341 namespace {
342
343 FormBase * GetForm(FL_OBJECT * ob)
344 {
345         lyx::Assert(ob && ob->form && ob->form->u_vdata);
346         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
347         return ptr;
348 }
349
350 } // namespace anon
351
352
353 extern "C" {
354
355 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
356 {
357         GetForm(ob)->getController().ApplyButton();
358 }
359
360
361 void C_FormBaseOKCB(FL_OBJECT * ob, long)
362 {
363         GetForm(ob)->getController().OKButton();
364 }
365
366
367 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
368 {
369         FormBase * form = GetForm(ob);
370         form->getController().CancelButton();
371 }
372
373
374 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
375 {
376         GetForm(ob)->getController().RestoreButton();
377 }
378
379
380 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
381 {
382         GetForm(ob)->InputCB(ob, d);
383 }
384
385
386 static int C_WMHideCB(FL_FORM * form, void *)
387 {
388         // Close the dialog cleanly, even if the WM is used to do so.
389         lyx::Assert(form && form->u_vdata);
390         FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
391         ptr->getController().CancelButton();
392         return FL_CANCEL;
393 }
394
395 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
396                           FL_Coord, FL_Coord, int key, void *)
397 {
398         // Note that the return value is important in the pre-emptive handler.
399         // Don't return anything other than 0.
400         lyx::Assert(ob);
401
402         // Don't Assert this one, as it can happen quite naturally when things
403         // are being deleted in the d-tor.
404         //Assert(ob->form);
405         if (!ob->form) return 0;
406
407         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
408
409         if (ptr)
410                 ptr->PrehandlerCB(ob, event, key);
411
412         return 0;
413 }
414
415 } // extern "C"