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