]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBaseDeprecated.C
Rob's dialog clean-up and Martin's 'disfucation' of insetgraphics.
[lyx.git] / src / frontends / xforms / FormBaseDeprecated.C
1 /**
2  * \file FormBaseDeprecated.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 "Dialogs.h"
18 #include "FormBaseDeprecated.h"
19 #include "xformsBC.h"
20 #include "xforms_resize.h"
21 #include "Tooltips.h"
22 #include FORMS_H_LOCATION
23
24 #include "lyxrc.h"
25
26 #include "frontends/LyXView.h"
27
28 #include "support/LAssert.h"
29 #include "support/filetools.h" //  LibFileSearch
30
31 #include <boost/bind.hpp>
32
33 extern "C" {
34
35 // Callback function invoked by xforms when the dialog is closed by the
36 // window manager
37 static int C_WMHideCB(FL_FORM *, void *);
38
39 // Callback function invoked by the xforms pre- and post-handler routines
40 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
41
42 } // extern "C"
43
44
45 FormBaseDeprecated::FormBaseDeprecated(LyXView & lv, Dialogs & d,
46                                        string const & t, bool allowResize)
47         : lv_(lv), d_(d), title_(t),
48           minw_(0), minh_(0), allow_resize_(allowResize),
49           tooltips_(new Tooltips())
50 {}
51
52
53 FormBaseDeprecated::~FormBaseDeprecated()
54 {
55         delete tooltips_;
56 }
57
58
59 Tooltips & FormBaseDeprecated::tooltips()
60 {
61         return *tooltips_;
62 }
63
64
65 void FormBaseDeprecated::redraw()
66 {
67         if (form() && form()->visible)
68                 fl_redraw_form(form());
69 }
70
71
72 void FormBaseDeprecated::connect()
73 {
74         fl_set_form_minsize(form(), minw_, minh_);
75         r_ = d_.redrawGUI().connect(boost::bind(&FormBaseDeprecated::redraw, this));
76 }
77
78
79 void FormBaseDeprecated::disconnect()
80 {
81         h_.disconnect();
82         r_.disconnect();
83 }
84
85
86 void FormBaseDeprecated::show()
87 {
88         if (!form()) {
89                 build();
90
91                 double const scale = scale_to_fit_tabs(form());
92                 if (scale > 1.001)
93                         scale_form(form(), scale);
94
95                 bc().refresh();
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
104         fl_freeze_form(form());
105         update();
106         fl_unfreeze_form(form());
107
108         if (form()->visible) {
109                 fl_raise_form(form());
110                 /* This XMapWindow() will hopefully ensure that
111                  * iconified dialogs are de-iconified. Mad props
112                  * out to those crazy Xlib guys for forgetting a
113                  * XDeiconifyWindow(). At least WindowMaker, when
114                  * being notified of the redirected MapRequest will
115                  * specifically de-iconify. From source, fvwm2 seems
116                  * to do the same.
117                  */
118                 XMapWindow(fl_get_display(), form()->window);
119         } else {
120                 connect();
121
122                 // calls to fl_set_form_minsize/maxsize apply only to the next
123                 // fl_show_form(), so this comes first.
124                 fl_set_form_minsize(form(), minw_, minh_);
125                 if (!allow_resize_)
126                         fl_set_form_maxsize(form(), minw_, minh_);
127
128                 string const maximize_title = "LyX: " + title_;
129                 int const iconify_policy = lyxrc.dialogs_iconify_with_main ?
130                                                 FL_TRANSIENT : 0;
131
132                 fl_show_form(form(),
133                              FL_PLACE_MOUSE | FL_FREE_SIZE,
134                              iconify_policy,
135                              maximize_title.c_str());
136
137                 if (iconify_policy == 0) {
138                         // set title for minimized form
139                         string const minimize_title = title_;
140                         fl_winicontitle(form()->window, minimize_title.c_str());
141
142                         //  assign an icon to form
143                         string const iconname = LibFileSearch("images", "lyx", "xpm");
144                         if (!iconname.empty()) {
145                                 unsigned int w, h;
146                                 Pixmap icon_mask;
147                                 Pixmap const icon_p = fl_read_pixmapfile(fl_root,
148                                                         iconname.c_str(),
149                                                         &w,
150                                                         &h,
151                                                         &icon_mask,
152                                                         0, 0, 0); // this leaks
153                                 fl_set_form_icon(form(), icon_p, icon_mask);
154                         }
155                 }
156         }
157
158         tooltips().set();
159 }
160
161
162 void FormBaseDeprecated::hide()
163 {
164         // xforms sometimes tries to process a hint-type MotionNotify, and
165         // use XQueryPointer, without verifying if the window still exists.
166         // So we try to clear out motion events in the queue before the
167         // DestroyNotify
168         XSync(fl_get_display(), false);
169
170         if (form() && form()->visible) {
171                 // some dialogs might do things to the form first
172                 // such as the nested tabfolder problem in Preferences
173                 disconnect();
174                 fl_hide_form(form());
175         }
176 }
177
178
179 void FormBaseDeprecated::setPrehandler(FL_OBJECT * ob)
180 {
181         lyx::Assert(ob);
182         fl_set_object_prehandler(ob, C_PrehandlerCB);
183 }
184
185
186 void FormBaseDeprecated::WMHideCB()
187 {
188         hide();
189         bc().hide();
190 }
191
192
193 void FormBaseDeprecated::ApplyCB()
194 {
195         apply();
196         bc().apply();
197 }
198
199
200 void FormBaseDeprecated::OKCB()
201 {
202         ok();
203         bc().ok();
204 }
205
206
207 void FormBaseDeprecated::CancelCB()
208 {
209         cancel();
210         bc().cancel();
211 }
212
213
214 void FormBaseDeprecated::InputCB(FL_OBJECT * ob, long data)
215 {
216         // It is possible to set the choice to 0 when using the
217         // keyboard shortcuts. This work-around deals with the problem.
218         if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
219                 fl_set_choice(ob, 1);
220         }
221
222         bc().valid(input(ob, data));
223 }
224
225
226 void FormBaseDeprecated::RestoreCB()
227 {
228         bc().restore();
229         restore();
230 }
231
232
233 FormBaseBI::FormBaseBI(LyXView & lv, Dialogs & d, string const & t,
234                        bool allowResize)
235         : FormBaseDeprecated(lv, d, t, allowResize)
236 {}
237
238
239 void FormBaseBI::connect()
240 {
241         h_ = d_.hideAll.connect(boost::bind(&FormBaseBI::hide, this));
242         FormBaseDeprecated::connect();
243 }
244
245
246 FormBaseBD::FormBaseBD(LyXView & lv, Dialogs & d, string const & t,
247                        bool allowResize)
248         : FormBaseDeprecated(lv, d, t, allowResize)
249 {}
250
251
252 void FormBaseBD::connect()
253 {
254         u_ = d_.updateBufferDependent.
255                 connect(boost::bind(&FormBaseBD::updateSlot, this, _1));
256         h_ = d_.hideBufferDependent.
257                 connect(boost::bind(&FormBaseBD::hide, this));
258         FormBaseDeprecated::connect();
259 }
260
261
262 void FormBaseBD::disconnect()
263 {
264         u_.disconnect();
265         FormBaseDeprecated::disconnect();
266 }
267
268
269 namespace {
270
271 FormBaseDeprecated * GetForm(FL_OBJECT * ob)
272 {
273         lyx::Assert(ob && ob->form && ob->form->u_vdata);
274         FormBaseDeprecated * ptr =
275                 static_cast<FormBaseDeprecated *>(ob->form->u_vdata);
276         return ptr;
277 }
278
279 } // namespace anon
280
281
282 extern "C" {
283
284 void C_FormBaseDeprecatedApplyCB(FL_OBJECT * ob, long)
285 {
286         GetForm(ob)->ApplyCB();
287 }
288
289
290 void C_FormBaseDeprecatedOKCB(FL_OBJECT * ob, long)
291 {
292         GetForm(ob)->OKCB();
293 }
294
295
296 void C_FormBaseDeprecatedCancelCB(FL_OBJECT * ob, long)
297 {
298         GetForm(ob)->CancelCB();
299 }
300
301
302 void C_FormBaseDeprecatedInputCB(FL_OBJECT * ob, long d)
303 {
304         GetForm(ob)->InputCB(ob, d);
305 }
306
307
308 void C_FormBaseDeprecatedRestoreCB(FL_OBJECT * ob, long)
309 {
310         GetForm(ob)->RestoreCB();
311 }
312
313 static int C_WMHideCB(FL_FORM * form, void *)
314 {
315         // Close the dialog cleanly, even if the WM is used to do so.
316         lyx::Assert(form && form->u_vdata);
317         FormBaseDeprecated * ptr =
318                 static_cast<FormBaseDeprecated *>(form->u_vdata);
319         ptr->WMHideCB();
320         return FL_CANCEL;
321 }
322
323 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
324                           FL_Coord, FL_Coord, int key, void *)
325 {
326         // Note that the return value is important in the pre-emptive handler.
327         // Don't return anything other than 0.
328         lyx::Assert(ob);
329
330         // Don't Assert this one, as it can happen quite naturally when things
331         // are being deleted in the d-tor.
332         //Assert(ob->form);
333         if (!ob->form) return 0;
334
335         FormBaseDeprecated * ptr =
336                 static_cast<FormBaseDeprecated *>(ob->form->u_vdata);
337
338         if (ptr)
339                 ptr->PrehandlerCB(ob, event, key);
340
341         return 0;
342 }
343
344 } // extern "C"