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