1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 2000-2001 The LyX Team.
8 * ======================================================
10 * \author Angus Leeming <a.leeming@ic.ac.uk>
16 #pragma implementation
22 #include "support/LAssert.h"
23 #include "xforms_helpers.h" // formatted
29 int TooltipHandler(FL_OBJECT *ob, int event);
31 void TooltipTimerCB(FL_OBJECT * timer, long data);
37 static int C_FormBaseTooltipHandler(FL_OBJECT * ob, int event,
38 FL_Coord, FL_Coord, int, void *)
40 return TooltipHandler(ob, event);
43 static void C_FormBaseTooltipTimerCB(FL_OBJECT * ob, long data)
45 TooltipTimerCB(ob, data);
50 #endif // FL_REVISION < 89
55 // Callback function invoked by xforms when the dialog is closed by the
57 static int C_FormBaseWMHideCB(FL_FORM * form, void *);
59 // Use this to diaplay feedback messages or to trigger an input event on paste
60 // with the middle mouse button
61 static int C_FormBasePrehandler(FL_OBJECT * ob, int event,
62 FL_Coord, FL_Coord, int key, void *);
67 FormBase::FormBase(ControlButtons & c, string const & t, bool allowResize)
68 : ViewBC<xformsBC>(c), minw_(0), minh_(0), allow_resize_(allowResize),
69 title_(t), warning_posted_(false), tooltip_level_(VERBOSE_TOOLTIP)
78 void FormBase::redraw()
80 if (form() && form()->visible)
81 fl_redraw_form(form());
91 // use minw_ to flag whether the dialog has ever been shown
92 // (Needed now that build() is/should be called from the controller)
96 // work around dumb xforms sizing bug
100 fl_set_form_atclose(form(), C_FormBaseWMHideCB, 0);
103 fl_freeze_form(form());
104 update(); // make sure its up-to-date
105 fl_unfreeze_form(form());
107 if (form()->visible) {
108 fl_raise_form(form());
109 /* This XMapWindow() will hopefully ensure that
110 * iconified dialogs are de-iconified. Mad props
111 * out to those crazy Xlib guys for forgetting a
112 * XDeiconifyWindow(). At least WindowMaker, when
113 * being notified of the redirected MapRequest will
114 * specifically de-iconify. From source, fvwm2 seems
117 XMapWindow(fl_get_display(), form()->window);
119 // calls to fl_set_form_minsize/maxsize apply only to the next
120 // fl_show_form(), so this comes first.
121 fl_set_form_minsize(form(), minw_, minh_);
123 fl_set_form_maxsize(form(), minw_, minh_);
126 FL_PLACE_MOUSE | FL_FREE_SIZE,
127 (controller_.IconifyWithMain() ? FL_TRANSIENT : 0),
133 void FormBase::hide()
135 if (form() && form()->visible)
136 fl_hide_form(form());
140 void FormBase::InputCB(FL_OBJECT * ob, long data)
142 // It is possible to set the choice to 0 when using the
143 // keyboard shortcuts. This work-around deals with the problem.
144 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
145 fl_set_choice(ob, 1);
148 bc().input(input(ob, data));
152 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
154 return ButtonPolicy::SMI_VALID;
158 // preemptive handler for feedback messages
159 void FormBase::FeedbackCB(FL_OBJECT * ob, int event)
165 warning_posted_ = false;
170 if (!warning_posted_)
180 void FormBase::setTooltipHandler(FL_OBJECT * ob)
185 if (!tooltip_timer_) {
186 fl_addto_form(form());
187 tooltip_timer_ = fl_add_timer(FL_HIDDEN_TIMER, 0, 0, 0, 0, "");
191 fl_set_object_posthandler(ob, C_FormBaseTooltipHandler);
192 ob->u_cdata = reinterpret_cast<char *>(tooltip_timer_);
195 string const help(getTooltip(ob));
197 fl_set_object_helper(ob, help.c_str());
198 #endif // FL_REVISION < 89
202 string const FormBase::getTooltip(FL_OBJECT * ob) const
206 switch (tooltip_level_) {
207 case VERBOSE_TOOLTIP:
209 string str = getVerboseTooltip(ob);
211 return formatted(_(str), 400);
212 // else, fall through
215 case MINIMAL_TOOLTIP:
216 return getMinimalTooltip(ob);
226 void FormBase::setTooltipLevel(TooltipLevel level)
228 tooltip_level_ = level;
232 void FormBase::setPrehandler(FL_OBJECT * ob)
235 fl_set_object_prehandler(ob, C_FormBasePrehandler);
239 void FormBase::setWarningPosted(bool warning)
241 warning_posted_ = warning;
247 FormBase * GetForm(FL_OBJECT * ob)
249 lyx::Assert(ob && ob->form && ob->form->u_vdata);
250 FormBase * pre = static_cast<FormBase *>(ob->form->u_vdata);
259 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
261 GetForm(ob)->ApplyButton();
265 void C_FormBaseOKCB(FL_OBJECT * ob, long)
267 GetForm(ob)->OKButton();
271 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
273 FormBase * form = GetForm(ob);
274 form->CancelButton();
278 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
280 GetForm(ob)->RestoreButton();
284 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
286 GetForm(ob)->InputCB(ob, d);
290 static int C_FormBaseWMHideCB(FL_FORM * form, void *)
292 // Close the dialog cleanly, even if the WM is used to do so.
293 lyx::Assert(form && form->u_vdata);
294 FormBase * pre = static_cast<FormBase *>(form->u_vdata);
300 static int C_FormBasePrehandler(FL_OBJECT * ob, int event,
301 FL_Coord, FL_Coord, int key, void *)
303 // Note that the return value is important in the pre-emptive handler.
304 // Don't return anything other than 0.
307 // Don't Assert this one, as it can happen quite naturally when things
308 // are being deleted in the d-tor.
310 if (!ob->form) return 0;
312 FormBase * pre = static_cast<FormBase *>(ob->form->u_vdata);
315 if (event == FL_PUSH && key == 2 && ob->objclass == FL_INPUT) {
316 // Trigger an input event when pasting in an xforms input object
317 // using the middle mouse button.
320 } else if (event == FL_ENTER || event == FL_LEAVE){
321 // Post feedback as the mouse enters the object,
322 // remove it as the mouse leaves.
323 pre->FeedbackCB(ob, event);
336 void TooltipTimerCB(FL_OBJECT * timer, long data)
338 FL_OBJECT * ob = reinterpret_cast<FL_OBJECT*>(data);
339 lyx::Assert(ob && ob->form);
341 string const help = GetForm(timer)->getTooltip(ob);
345 fl_show_oneliner(help.c_str(),
347 ob->form->y + ob->y + ob->h);
351 // post_handler for bubble-help (Matthias)
352 int TooltipHandler(FL_OBJECT *ob, int event)
355 FL_OBJECT * timer = reinterpret_cast<FL_OBJECT *>(ob->u_cdata);
358 // We do not test for empty help here, since this can never happen
359 if (event == FL_ENTER){
360 fl_set_object_callback(timer,
361 C_FormBaseTooltipTimerCB,
362 reinterpret_cast<long>(ob));
363 fl_set_timer(timer, 1);
365 else if (event != FL_MOTION){
366 fl_set_timer(timer, 0);
374 #endif // FL_REVISION < 89