3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS
14 #pragma implementation
19 #include "ControlButtons.h"
21 #include "xforms_resize.h"
23 #include "xforms_helpers.h" // formatted
25 #include "gettext.h" // _()
26 #include "BoostFormat.h"
28 #include "support/LAssert.h"
29 #include "support/filetools.h" // LibFileSearch
31 #include FORMS_H_LOCATION
35 // These should be in forms.h but aren't
36 void fl_show_tooltip(const char *, int, int);
38 void fl_hide_tooltip();
40 // Callback function invoked by xforms when the dialog is closed by the
42 static int C_WMHideCB(FL_FORM * form, void *);
44 // Callback function invoked by the xforms pre-handler routine.
45 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
50 FormBase::FormBase(string const & t, bool allowResize)
52 warning_posted_(false), message_widget_(0),
53 minw_(0), minh_(0), allow_resize_(allowResize),
54 title_(t), icon_pixmap_(0), icon_mask_(0),
55 tooltips_(new Tooltips())
62 XFreePixmap(fl_get_display(), icon_pixmap_);
68 bool FormBase::isVisible() const
70 return form() && form()->visible;
74 Tooltips & FormBase::tooltips()
80 void FormBase::redraw()
82 if (form() && form()->visible)
83 fl_redraw_form(form());
87 xformsBC & FormBase::bc()
89 return static_cast<xformsBC &>(getController().bc());
90 // return dynamic_cast<GUIbc &>(controller_ptr_->bc());
94 void FormBase::prepare_to_show()
96 double const scale = get_scale_to_fit(form());
98 scale_form_horizontally(form(), scale);
100 // work around dumb xforms sizing bug
104 fl_set_form_atclose(form(), C_WMHideCB, 0);
106 // set the title for the minimized form
107 if (!getController().IconifyWithMain())
108 fl_winicontitle(form()->window, title_.c_str());
110 // assign an icon to the form
111 string const iconname = LibFileSearch("images", "lyx", "xpm");
112 if (!iconname.empty()) {
114 icon_pixmap_ = fl_read_pixmapfile(fl_root,
120 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
125 void FormBase::show()
127 // build() is/should be called from the controller, so form() should
131 // we use minw_ to flag whether the dialog has ever been shown.
132 // In turn, prepare_to_show() initialises various bits 'n' pieces
133 // (including minw_).
138 // make sure the form is up to date.
139 fl_freeze_form(form());
141 fl_unfreeze_form(form());
143 if (form()->visible) {
144 fl_raise_form(form());
145 /* This XMapWindow() will hopefully ensure that
146 * iconified dialogs are de-iconified. Mad props
147 * out to those crazy Xlib guys for forgetting a
148 * XDeiconifyWindow(). At least WindowMaker, when
149 * being notified of the redirected MapRequest will
150 * specifically de-iconify. From source, fvwm2 seems
153 XMapWindow(fl_get_display(), form()->window);
155 // calls to fl_set_form_minsize/maxsize apply only to the next
156 // fl_show_form(), so this comes first.
157 fl_set_form_minsize(form(), minw_, minh_);
159 fl_set_form_maxsize(form(), minw_, minh_);
161 string const maximize_title = "LyX: " + title_;
162 int const iconify_policy =
163 getController().IconifyWithMain() ? FL_TRANSIENT : 0;
166 FL_PLACE_MOUSE | FL_FREE_SIZE,
168 maximize_title.c_str());
173 void FormBase::hide()
176 // Does no harm if none is visible and ensures that the tooltip form
177 // is hidden should the dialog be closed from the keyboard.
181 // xforms sometimes tries to process a hint-type MotionNotify, and
182 // use XQueryPointer, without verifying if the window still exists.
183 // So we try to clear out motion events in the queue before the
185 XSync(fl_get_display(), false);
187 if (form() && form()->visible)
188 fl_hide_form(form());
192 void FormBase::setPrehandler(FL_OBJECT * ob)
195 fl_set_object_prehandler(ob, C_PrehandlerCB);
199 void FormBase::setMessageWidget(FL_OBJECT * ob)
201 lyx::Assert(ob && ob->objclass == FL_TEXT);
202 message_widget_ = ob;
203 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
207 void FormBase::InputCB(FL_OBJECT * ob, long data)
209 // It is possible to set the choice to 0 when using the
210 // keyboard shortcuts. This work-around deals with the problem.
211 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
212 fl_set_choice(ob, 1);
215 bc().input(input(ob, data));
219 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
221 return ButtonPolicy::SMI_VALID;
225 // preemptive handler for feedback messages
226 void FormBase::MessageCB(FL_OBJECT * ob, int event)
233 string const feedback = getFeedback(ob);
234 if (feedback.empty() && warning_posted_)
237 warning_posted_ = false;
238 postMessage(getFeedback(ob));
243 if (!warning_posted_)
253 void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
257 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
258 // Trigger an input event when pasting in an xforms input object
259 // using the middle mouse button.
267 if (message_widget_) {
268 // Post feedback as the mouse enters the object,
269 // remove it as the mouse leaves.
270 MessageCB(ob, event);
274 if (ob->objclass == FL_TABFOLDER) {
275 // This prehandler is used to work-around an xforms
276 // bug and ensures that the form->x, form->y coords of
277 // the active tabfolder are up to date.
279 // The tabfolder itself can be very narrow, being just
280 // the visible border to the tabs.
281 // We thus use both FL_ENTER and FL_LEAVE as flags,
282 // in case the FL_ENTER event is not caught.
284 FL_FORM * const folder = fl_get_active_folder(ob);
285 if (folder && folder->window) {
286 fl_get_winorigin(folder->window,
287 &(folder->x), &(folder->y));
294 // Tooltips are not displayed on browser widgets due to an xforms' bug.
295 // I have a fix, but it's not yet in the xforms sources.
296 // This is a work-around:
299 if (ob->objclass == FL_BROWSER &&
300 ob->tooltip && *(ob->tooltip)) {
301 int const x = ob->form->x + ob->x;
302 int const y = ob->form->y + ob->y + ob->h + 1;
303 fl_show_tooltip(ob->tooltip, x, y);
309 if (ob->objclass == FL_BROWSER)
316 void FormBase::postWarning(string const & warning)
318 warning_posted_ = true;
319 postMessage(warning);
323 void FormBase::clearMessage()
325 lyx::Assert(message_widget_);
327 warning_posted_ = false;
329 string const existing = message_widget_->label
330 ? message_widget_->label : string();
331 if (existing.empty())
334 // This trick is needed to get xforms to clear the label...
335 fl_set_object_label(message_widget_, "");
336 fl_hide_object(message_widget_);
340 void FormBase::postMessage(string const & message)
342 lyx::Assert(message_widget_);
344 int const width = message_widget_->w - 10;
346 boost::format fmter = warning_posted_ ?
347 boost::format(_("WARNING! %1$s")) :
348 boost::format("%1$s");
350 string const str = formatted(boost::io::str(fmter % message),
351 width, FL_NORMAL_SIZE);
353 string const tmp = warning_posted_ ?
354 _("WARNING!") + string(" ") + message :
357 string const str = formatted(tmp, width, FL_NORMAL_SIZE);
360 fl_set_object_label(message_widget_, str.c_str());
361 FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
362 fl_set_object_lcol(message_widget_, label_color);
364 if (!message_widget_->visible)
365 fl_show_object(message_widget_);
371 FormBase * GetForm(FL_OBJECT * ob)
373 lyx::Assert(ob && ob->form && ob->form->u_vdata);
374 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
383 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
385 GetForm(ob)->getController().ApplyButton();
389 void C_FormBaseOKCB(FL_OBJECT * ob, long)
391 GetForm(ob)->getController().OKButton();
395 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
397 FormBase * form = GetForm(ob);
398 form->getController().CancelButton();
402 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
404 GetForm(ob)->getController().RestoreButton();
408 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
410 GetForm(ob)->InputCB(ob, d);
414 static int C_WMHideCB(FL_FORM * form, void *)
416 // Close the dialog cleanly, even if the WM is used to do so.
417 lyx::Assert(form && form->u_vdata);
418 FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
419 ptr->getController().CancelButton();
423 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
424 FL_Coord, FL_Coord, int key, void *)
426 // Note that the return value is important in the pre-emptive handler.
427 // Don't return anything other than 0.
430 // Don't Assert this one, as it can happen quite naturally when things
431 // are being deleted in the d-tor.
433 if (!ob->form) return 0;
435 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
438 ptr->PrehandlerCB(ob, event, key);