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