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