]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBase.C
John's possible fix to the Bad Window problems that some people have
[lyx.git] / src / frontends / xforms / FormBase.C
1 /* This file is part of
2  * ====================================================== 
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 2000-2001 The LyX Team.
7  *
8  * ======================================================
9  *
10  * \author Angus Leeming <a.leeming@ic.ac.uk>
11  */
12
13 #include <config.h>
14
15 #ifdef __GNUG__
16 #pragma implementation
17 #endif
18
19 #include "Dialogs.h"
20 #include "FormBase.h"
21 #include "xformsBC.h"
22 #include "GUIRunTime.h"
23 #include "support/LAssert.h"
24 #include "Tooltips.h"
25 #include "xforms_helpers.h" // formatted
26
27 extern "C" {
28
29 // Callback function invoked by xforms when the dialog is closed by the
30 // window manager
31 static int C_FormBaseWMHideCB(FL_FORM * form, void *);
32
33 // Use this to diaplay feedback messages or to trigger an input event on paste
34 // with the middle mouse button
35 static int C_FormBasePrehandler(FL_OBJECT * ob, int event,
36                                 FL_Coord, FL_Coord, int key, void *);
37
38 } // extern "C"
39
40
41 FormBase::FormBase(ControlButtons & c, string const & t, bool allowResize)
42         : ViewBC<xformsBC>(c), minw_(0), minh_(0), allow_resize_(allowResize),
43           title_(t), warning_posted_(false), tooltip_level_(NO_TOOLTIP)
44
45 {
46         tooltip_ = new Tooltips;
47         tooltip_->getTooltip.connect(SigC::slot(this, &FormBase::getTooltip));
48 }
49
50
51 FormBase::~FormBase()
52 {
53         delete tooltip_;
54 }
55
56 void FormBase::redraw()
57 {
58         if (form() && form()->visible)
59                 fl_redraw_form(form());
60 }
61
62
63 void FormBase::show()
64 {
65         if (!form()) {
66                 build();
67         }
68
69         // use minw_ to flag whether the dialog has ever been shown
70         // (Needed now that build() is/should be called from the controller)
71         if (minw_ == 0) {
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_FormBaseWMHideCB, 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
110
111 void FormBase::hide()
112 {
113         // xforms sometimes tries to process a hint-type MotionNotify, and
114         // use XQueryPointer, without verifying if the window still exists.
115         // So we try to clear out motion events in the queue before the
116         // DestroyNotify
117         XSync(fl_get_display(), false);
118         GUIRunTime::processEvents();
119
120         if (form() && form()->visible)
121                 fl_hide_form(form());
122 }
123
124
125 void FormBase::InputCB(FL_OBJECT * ob, long data)
126 {
127         // It is possible to set the choice to 0 when using the
128         // keyboard shortcuts. This work-around deals with the problem.
129         if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
130                 fl_set_choice(ob, 1);
131         }
132
133         bc().input(input(ob, data));
134 }
135
136
137 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
138 {
139         return ButtonPolicy::SMI_VALID;
140 }
141
142
143 // preemptive handler for feedback messages
144 void FormBase::FeedbackCB(FL_OBJECT * ob, int event)
145 {
146         lyx::Assert(ob);
147
148         switch (event) {
149         case FL_ENTER:
150                 warning_posted_ = false;
151                 feedback(ob);
152                 break;
153
154         case FL_LEAVE:
155                 if (!warning_posted_)
156                         clear_feedback();
157                 break;
158
159         default:
160                 break;
161         }
162 }
163
164
165 void FormBase::setTooltipHandler(FL_OBJECT * ob)
166 {
167         tooltip_->activateTooltip(ob);
168 }
169
170
171 string FormBase::getTooltip(FL_OBJECT const * ob)
172 {
173         lyx::Assert(ob);
174
175         switch (tooltip_level_) {
176         case VERBOSE_TOOLTIP: 
177         {
178                 string str = getVerboseTooltip(ob);
179                 if (!str.empty())
180                         return formatted(_(str), 400);
181                 // else, fall through
182         }
183         
184         case MINIMAL_TOOLTIP:
185                 return getMinimalTooltip(ob);
186                 
187         case NO_TOOLTIP:
188         default:
189                 return string();
190         }
191         
192 }
193                 
194
195 /// Fill the tooltips chooser with the standard descriptions
196 void FormBase::fillTooltipChoice(FL_OBJECT * ob)
197 {
198         lyx::Assert(ob && ob->objclass == FL_CHOICE);
199
200         fl_clear_choice(ob);
201         fl_addto_choice(ob, _(" None | Normal | Verbose "));
202
203         switch (tooltip_level_) {
204         case NO_TOOLTIP:
205                 fl_set_choice(ob, 1);
206                 break;
207         case MINIMAL_TOOLTIP:
208                 fl_set_choice(ob, 2);
209                 break;
210         case VERBOSE_TOOLTIP:
211                 fl_set_choice(ob, 3);
212                 break;
213         }
214 }
215
216
217 void FormBase::setTooltipLevel(FL_OBJECT * ob)
218 {
219         lyx::Assert(ob && ob->objclass == FL_CHOICE &&
220                     fl_get_choice_maxitems(ob) == 3);
221
222         switch (fl_get_choice(ob)) {
223         case 1:
224                 tooltip_level_ = NO_TOOLTIP;
225                 break;
226         case 2:
227                 tooltip_level_ = MINIMAL_TOOLTIP;
228                 break;
229         case 3:
230                 tooltip_level_ = VERBOSE_TOOLTIP;
231                 break;
232         }
233 }
234
235
236 void FormBase::setPrehandler(FL_OBJECT * ob)
237 {
238         lyx::Assert(ob);
239         fl_set_object_prehandler(ob, C_FormBasePrehandler);
240 }
241
242
243 void FormBase::setWarningPosted(bool warning)
244 {
245         warning_posted_ = warning;
246 }
247
248
249 namespace {
250
251 FormBase * GetForm(FL_OBJECT * ob)
252 {
253         lyx::Assert(ob && ob->form && ob->form->u_vdata);
254         FormBase * pre = static_cast<FormBase *>(ob->form->u_vdata);
255         return pre;
256 }
257
258 } // namespace anon
259
260
261 extern "C" {
262
263 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
264 {
265         GetForm(ob)->ApplyButton();
266 }
267
268
269 void C_FormBaseOKCB(FL_OBJECT * ob, long)
270 {
271         GetForm(ob)->OKButton();
272 }
273
274
275 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
276 {
277         FormBase * form = GetForm(ob);
278         form->CancelButton();
279 }
280
281
282 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
283 {
284         GetForm(ob)->RestoreButton();
285 }
286
287
288 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
289 {
290         GetForm(ob)->InputCB(ob, d);
291 }
292
293
294 static int C_FormBaseWMHideCB(FL_FORM * form, void *)
295 {
296         // Close the dialog cleanly, even if the WM is used to do so.
297         lyx::Assert(form && form->u_vdata);
298         FormBase * pre = static_cast<FormBase *>(form->u_vdata);
299         pre->CancelButton();
300         return FL_CANCEL;
301 }
302
303
304 static int C_FormBasePrehandler(FL_OBJECT * ob, int event,
305                                 FL_Coord, FL_Coord, int key, void *)
306 {
307         // Note that the return value is important in the pre-emptive handler.
308         // Don't return anything other than 0.
309         lyx::Assert(ob);
310
311         // Don't Assert this one, as it can happen quite naturally when things
312         // are being deleted in the d-tor.
313         //Assert(ob->form);
314         if (!ob->form) return 0;
315
316         FormBase * pre = static_cast<FormBase *>(ob->form->u_vdata);
317         if (!pre) return 0;
318
319         if (event == FL_PUSH && key == 2 && ob->objclass == FL_INPUT) {
320                 // Trigger an input event when pasting in an xforms input object
321                 // using the middle mouse button.
322                 pre->InputCB(ob, 0);
323
324         } else if (event == FL_ENTER || event == FL_LEAVE) {
325                 // Post feedback as the mouse enters the object,
326                 // remove it as the mouse leaves.
327                 pre->FeedbackCB(ob, event);
328         }
329
330         return 0;
331 }
332  
333 } // extern "C"