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