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