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