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 "GUIRunTime.h"
23 #include "support/LAssert.h"
25 #include "xforms_helpers.h" // formatted
29 // Callback function invoked by xforms when the dialog is closed by the
31 static int C_FormBaseWMHideCB(FL_FORM * form, void *);
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 *);
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)
46 tooltip_ = new Tooltips;
47 tooltip_->getTooltip.connect(SigC::slot(this, &FormBase::getTooltip));
56 void FormBase::redraw()
58 if (form() && form()->visible)
59 fl_redraw_form(form());
69 // use minw_ to flag whether the dialog has ever been shown
70 // (Needed now that build() is/should be called from the controller)
74 // work around dumb xforms sizing bug
78 fl_set_form_atclose(form(), C_FormBaseWMHideCB, 0);
81 fl_freeze_form(form());
82 update(); // make sure its up-to-date
83 fl_unfreeze_form(form());
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
95 XMapWindow(fl_get_display(), form()->window);
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_);
101 fl_set_form_maxsize(form(), minw_, minh_);
104 FL_PLACE_MOUSE | FL_FREE_SIZE,
105 (controller_.IconifyWithMain() ? FL_TRANSIENT : 0),
111 void FormBase::hide()
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
117 XSync(fl_get_display(), false);
118 GUIRunTime::processEvents();
120 if (form() && form()->visible)
121 fl_hide_form(form());
125 void FormBase::InputCB(FL_OBJECT * ob, long data)
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);
133 bc().input(input(ob, data));
137 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
139 return ButtonPolicy::SMI_VALID;
143 // preemptive handler for feedback messages
144 void FormBase::FeedbackCB(FL_OBJECT * ob, int event)
150 warning_posted_ = false;
155 if (!warning_posted_)
165 void FormBase::setTooltipHandler(FL_OBJECT * ob)
167 tooltip_->activateTooltip(ob);
171 string FormBase::getTooltip(FL_OBJECT const * ob)
175 switch (tooltip_level_) {
176 case VERBOSE_TOOLTIP:
178 string str = getVerboseTooltip(ob);
180 return formatted(_(str), 400);
181 // else, fall through
184 case MINIMAL_TOOLTIP:
185 return getMinimalTooltip(ob);
195 /// Fill the tooltips chooser with the standard descriptions
196 void FormBase::fillTooltipChoice(FL_OBJECT * ob)
198 lyx::Assert(ob && ob->objclass == FL_CHOICE);
201 fl_addto_choice(ob, _(" None | Normal | Verbose "));
203 switch (tooltip_level_) {
205 fl_set_choice(ob, 1);
207 case MINIMAL_TOOLTIP:
208 fl_set_choice(ob, 2);
210 case VERBOSE_TOOLTIP:
211 fl_set_choice(ob, 3);
217 void FormBase::setTooltipLevel(FL_OBJECT * ob)
219 lyx::Assert(ob && ob->objclass == FL_CHOICE &&
220 fl_get_choice_maxitems(ob) == 3);
222 switch (fl_get_choice(ob)) {
224 tooltip_level_ = NO_TOOLTIP;
227 tooltip_level_ = MINIMAL_TOOLTIP;
230 tooltip_level_ = VERBOSE_TOOLTIP;
236 void FormBase::setPrehandler(FL_OBJECT * ob)
239 fl_set_object_prehandler(ob, C_FormBasePrehandler);
243 void FormBase::setWarningPosted(bool warning)
245 warning_posted_ = warning;
251 FormBase * GetForm(FL_OBJECT * ob)
253 lyx::Assert(ob && ob->form && ob->form->u_vdata);
254 FormBase * pre = static_cast<FormBase *>(ob->form->u_vdata);
263 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
265 GetForm(ob)->ApplyButton();
269 void C_FormBaseOKCB(FL_OBJECT * ob, long)
271 GetForm(ob)->OKButton();
275 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
277 FormBase * form = GetForm(ob);
278 form->CancelButton();
282 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
284 GetForm(ob)->RestoreButton();
288 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
290 GetForm(ob)->InputCB(ob, d);
294 static int C_FormBaseWMHideCB(FL_FORM * form, void *)
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);
304 static int C_FormBasePrehandler(FL_OBJECT * ob, int event,
305 FL_Coord, FL_Coord, int key, void *)
307 // Note that the return value is important in the pre-emptive handler.
308 // Don't return anything other than 0.
311 // Don't Assert this one, as it can happen quite naturally when things
312 // are being deleted in the d-tor.
314 if (!ob->form) return 0;
316 FormBase * pre = static_cast<FormBase *>(ob->form->u_vdata);
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.
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);