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