]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBase.C
Workaround for xforms bug, not hiding tooltip on dialog close.
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "FormBase.h"
18
19 #include "ControlButtons.h"
20 #include "xformsBC.h"
21 #include "xforms_resize.h"
22 #include "Tooltips.h"
23
24 #include "support/LAssert.h"
25 #include "support/filetools.h" //  LibFileSearch
26
27 #include FORMS_H_LOCATION
28
29 extern "C" {
30
31 // This should be in forms.h but isn't
32 void fl_hide_tooltip();
33
34 // Callback function invoked by xforms when the dialog is closed by the
35 // window manager
36 static int C_WMHideCB(FL_FORM * form, void *);
37
38 // Callback function invoked by the xforms pre- and post-handler routines
39 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
40
41 } // extern "C"
42
43
44 FormBase::FormBase(string const & t, bool allowResize)
45         : ViewBase(), minw_(0), minh_(0), allow_resize_(allowResize),
46           title_(t), icon_pixmap_(0), icon_mask_(0),
47           tooltips_(new Tooltips())
48 {}
49
50
51 FormBase::~FormBase()
52 {
53         if (icon_pixmap_)
54                 XFreePixmap(fl_get_display(), icon_pixmap_);
55
56         delete tooltips_;
57 }
58
59
60 Tooltips & FormBase::tooltips()
61 {
62         return *tooltips_;
63 }
64
65
66 void FormBase::redraw()
67 {
68         if (form() && form()->visible)
69                 fl_redraw_form(form());
70 }
71
72
73 xformsBC & FormBase::bc()
74 {
75         return static_cast<xformsBC &>(getController().bc());
76         // return dynamic_cast<GUIbc &>(controller_ptr_->bc());
77 }
78
79
80 void FormBase::prepare_to_show()
81 {
82         double const scale = scale_to_fit_tabs(form());
83         if (scale > 1.001)
84                 scale_form(form(), scale);
85
86         bc().refresh();
87
88         // work around dumb xforms sizing bug
89         minw_ = form()->w;
90         minh_ = form()->h;
91
92         fl_set_form_atclose(form(), C_WMHideCB, 0);
93
94         // set the title for the minimized form
95         if (!getController().IconifyWithMain())
96                 fl_winicontitle(form()->window, title_.c_str());
97
98         //  assign an icon to the form
99         string const iconname = LibFileSearch("images", "lyx", "xpm");
100         if (!iconname.empty()) {
101                 unsigned int w, h;
102                 icon_pixmap_ = fl_read_pixmapfile(fl_root,
103                                                   iconname.c_str(),
104                                                   &w,
105                                                   &h,
106                                                   &icon_mask_,
107                                                   0, 0, 0);
108                 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
109         }
110 }
111
112
113 void FormBase::show()
114 {
115         // build() is/should be called from the controller, so form() should
116         // always exist.
117         lyx::Assert(form());
118
119         // we use minw_ to flag whether the dialog has ever been shown.
120         // In turn, prepare_to_show() initialises various bits 'n' pieces
121         // (including minw_).
122         if (minw_ == 0) {
123                 prepare_to_show();
124         }
125
126         // make sure the form is up to date.
127         fl_freeze_form(form());
128         update();
129         fl_unfreeze_form(form());
130
131         if (form()->visible) {
132                 fl_raise_form(form());
133                 /* This XMapWindow() will hopefully ensure that
134                  * iconified dialogs are de-iconified. Mad props
135                  * out to those crazy Xlib guys for forgetting a
136                  * XDeiconifyWindow(). At least WindowMaker, when
137                  * being notified of the redirected MapRequest will
138                  * specifically de-iconify. From source, fvwm2 seems
139                  * to do the same.
140                  */
141                 XMapWindow(fl_get_display(), form()->window);
142         } else {
143                 // calls to fl_set_form_minsize/maxsize apply only to the next
144                 // fl_show_form(), so this comes first.
145                 fl_set_form_minsize(form(), minw_, minh_);
146                 if (!allow_resize_)
147                         fl_set_form_maxsize(form(), minw_, minh_);
148
149                 string const maximize_title = "LyX: " + title_;
150                 int const iconify_policy =
151                         getController().IconifyWithMain() ? FL_TRANSIENT : 0;
152
153                 fl_show_form(form(),
154                              FL_PLACE_MOUSE | FL_FREE_SIZE,
155                              iconify_policy,
156                              maximize_title.c_str());
157         }
158
159         // For some strange reason known only to xforms, the tooltips can only
160         // be set on a form that is already visible...
161         tooltips().set();
162 }
163
164
165 void FormBase::hide()
166 {
167         // Does no harm if none is visible and ensures that the tooltip form
168         // is hidden should the dialog be closed from the keyboard.
169         fl_hide_tooltip();
170
171         // xforms sometimes tries to process a hint-type MotionNotify, and
172         // use XQueryPointer, without verifying if the window still exists.
173         // So we try to clear out motion events in the queue before the
174         // DestroyNotify
175         XSync(fl_get_display(), false);
176
177         if (form() && form()->visible)
178                 fl_hide_form(form());
179 }
180
181
182 void FormBase::setPrehandler(FL_OBJECT * ob)
183 {
184         lyx::Assert(ob);
185         fl_set_object_prehandler(ob, C_PrehandlerCB);
186 }
187
188
189 void FormBase::InputCB(FL_OBJECT * ob, long data)
190 {
191         // It is possible to set the choice to 0 when using the
192         // keyboard shortcuts. This work-around deals with the problem.
193         if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
194                 fl_set_choice(ob, 1);
195         }
196
197         bc().input(input(ob, data));
198 }
199
200
201 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
202 {
203         return ButtonPolicy::SMI_VALID;
204 }
205
206
207 namespace {
208
209 FormBase * GetForm(FL_OBJECT * ob)
210 {
211         lyx::Assert(ob && ob->form && ob->form->u_vdata);
212         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
213         return ptr;
214 }
215
216 } // namespace anon
217
218
219 extern "C" {
220
221 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
222 {
223         GetForm(ob)->getController().ApplyButton();
224 }
225
226
227 void C_FormBaseOKCB(FL_OBJECT * ob, long)
228 {
229         GetForm(ob)->getController().OKButton();
230 }
231
232
233 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
234 {
235         FormBase * form = GetForm(ob);
236         form->getController().CancelButton();
237 }
238
239
240 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
241 {
242         GetForm(ob)->getController().RestoreButton();
243 }
244
245
246 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
247 {
248         GetForm(ob)->InputCB(ob, d);
249 }
250
251
252 static int C_WMHideCB(FL_FORM * form, void *)
253 {
254         // Close the dialog cleanly, even if the WM is used to do so.
255         lyx::Assert(form && form->u_vdata);
256         FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
257         ptr->getController().CancelButton();
258         return FL_CANCEL;
259 }
260
261 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
262                           FL_Coord, FL_Coord, int key, void *)
263 {
264         // Note that the return value is important in the pre-emptive handler.
265         // Don't return anything other than 0.
266         lyx::Assert(ob);
267
268         // Don't Assert this one, as it can happen quite naturally when things
269         // are being deleted in the d-tor.
270         //Assert(ob->form);
271         if (!ob->form) return 0;
272
273         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
274
275         if (ptr)
276                 ptr->PrehandlerCB(ob, event, key);
277
278         return 0;
279 }
280
281 } // extern "C"