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
16 #include "ControlButtons.h"
18 #include "ButtonController.h"
19 #include "xforms_resize.h"
21 #include "xforms_helpers.h" // formatted
23 #include "gettext.h" // _()
24 #include "BoostFormat.h"
26 #include "support/LAssert.h"
27 #include "support/filetools.h" // LibFileSearch
29 #include FORMS_H_LOCATION
33 // These should be in forms.h but aren't
34 void fl_show_tooltip(const char *, int, int);
36 void fl_hide_tooltip();
38 // Callback function invoked by xforms when the dialog is closed by the
40 static int C_WMHideCB(FL_FORM * form, void *);
42 // Callback function invoked by the xforms pre-handler routine.
43 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
48 FormBase::FormBase(string const & t, bool allowResize)
50 warning_posted_(false), message_widget_(0),
51 minw_(0), minh_(0), allow_resize_(allowResize),
52 title_(t), icon_pixmap_(0), icon_mask_(0),
53 tooltips_(new Tooltips())
60 XFreePixmap(fl_get_display(), icon_pixmap_);
66 bool FormBase::isVisible() const
68 return form() && form()->visible;
72 Tooltips & FormBase::tooltips()
78 void FormBase::redraw()
80 if (form() && form()->visible)
81 fl_redraw_form(form());
85 xformsBC & FormBase::bcview()
87 return static_cast<xformsBC &>(bc().view());
91 void FormBase::prepare_to_show()
93 double const scale = get_scale_to_fit(form());
95 scale_form_horizontally(form(), scale);
97 // work around dumb xforms sizing bug
101 fl_set_form_atclose(form(), C_WMHideCB, 0);
103 // set the title for the minimized form
104 if (!getController().IconifyWithMain())
105 fl_winicontitle(form()->window, title_.c_str());
107 // assign an icon to the form
108 string const iconname = LibFileSearch("images", "lyx", "xpm");
109 if (!iconname.empty()) {
111 icon_pixmap_ = fl_read_pixmapfile(fl_root,
117 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
122 void FormBase::show()
124 // build() is/should be called from the controller, so form() should
128 // we use minw_ to flag whether the dialog has ever been shown.
129 // In turn, prepare_to_show() initialises various bits 'n' pieces
130 // (including minw_).
135 // make sure the form is up to date.
136 fl_freeze_form(form());
138 fl_unfreeze_form(form());
140 if (form()->visible) {
141 fl_raise_form(form());
142 /* This XMapWindow() will hopefully ensure that
143 * iconified dialogs are de-iconified. Mad props
144 * out to those crazy Xlib guys for forgetting a
145 * XDeiconifyWindow(). At least WindowMaker, when
146 * being notified of the redirected MapRequest will
147 * specifically de-iconify. From source, fvwm2 seems
150 XMapWindow(fl_get_display(), form()->window);
152 // calls to fl_set_form_minsize/maxsize apply only to the next
153 // fl_show_form(), so this comes first.
154 fl_set_form_minsize(form(), minw_, minh_);
156 fl_set_form_maxsize(form(), minw_, minh_);
158 string const maximize_title = "LyX: " + title_;
159 int const iconify_policy =
160 getController().IconifyWithMain() ? FL_TRANSIENT : 0;
163 FL_PLACE_MOUSE | FL_FREE_SIZE,
165 maximize_title.c_str());
170 void FormBase::hide()
173 // Does no harm if none is visible and ensures that the tooltip form
174 // is hidden should the dialog be closed from the keyboard.
178 // xforms sometimes tries to process a hint-type MotionNotify, and
179 // use XQueryPointer, without verifying if the window still exists.
180 // So we try to clear out motion events in the queue before the
182 XSync(fl_get_display(), false);
184 if (form() && form()->visible)
185 fl_hide_form(form());
189 void FormBase::setPrehandler(FL_OBJECT * ob)
192 fl_set_object_prehandler(ob, C_PrehandlerCB);
196 void FormBase::setMessageWidget(FL_OBJECT * ob)
198 lyx::Assert(ob && ob->objclass == FL_TEXT);
199 message_widget_ = ob;
200 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
204 void FormBase::InputCB(FL_OBJECT * ob, long data)
206 // It is possible to set the choice to 0 when using the
207 // keyboard shortcuts. This work-around deals with the problem.
208 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
209 fl_set_choice(ob, 1);
212 bc().input(input(ob, data));
216 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
218 return ButtonPolicy::SMI_VALID;
222 // preemptive handler for feedback messages
223 void FormBase::MessageCB(FL_OBJECT * ob, int event)
230 string const feedback = getFeedback(ob);
231 if (feedback.empty() && warning_posted_)
234 warning_posted_ = false;
235 postMessage(getFeedback(ob));
240 if (!warning_posted_)
250 void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
254 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
255 // Trigger an input event when pasting in an xforms input object
256 // using the middle mouse button.
264 if (message_widget_) {
265 // Post feedback as the mouse enters the object,
266 // remove it as the mouse leaves.
267 MessageCB(ob, event);
271 if (ob->objclass == FL_TABFOLDER) {
272 // This prehandler is used to work-around an xforms
273 // bug and ensures that the form->x, form->y coords of
274 // the active tabfolder are up to date.
276 // The tabfolder itself can be very narrow, being just
277 // the visible border to the tabs.
278 // We thus use both FL_ENTER and FL_LEAVE as flags,
279 // in case the FL_ENTER event is not caught.
281 FL_FORM * const folder = fl_get_active_folder(ob);
282 if (folder && folder->window) {
283 fl_get_winorigin(folder->window,
284 &(folder->x), &(folder->y));
291 // Tooltips are not displayed on browser widgets due to an xforms' bug.
292 // I have a fix, but it's not yet in the xforms sources.
293 // This is a work-around:
296 if (ob->objclass == FL_BROWSER &&
297 ob->tooltip && *(ob->tooltip)) {
298 int const x = ob->form->x + ob->x;
299 int const y = ob->form->y + ob->y + ob->h + 1;
300 fl_show_tooltip(ob->tooltip, x, y);
306 if (ob->objclass == FL_BROWSER)
313 void FormBase::postWarning(string const & warning)
315 warning_posted_ = true;
316 postMessage(warning);
320 void FormBase::clearMessage()
322 lyx::Assert(message_widget_);
324 warning_posted_ = false;
326 string const existing = message_widget_->label
327 ? message_widget_->label : string();
328 if (existing.empty())
331 // This trick is needed to get xforms to clear the label...
332 fl_set_object_label(message_widget_, "");
333 fl_hide_object(message_widget_);
337 void FormBase::postMessage(string const & message)
339 lyx::Assert(message_widget_);
341 int const width = message_widget_->w - 10;
343 boost::format fmter = warning_posted_ ?
344 boost::format(_("WARNING! %1$s")) :
345 boost::format("%1$s");
347 string const str = formatted(boost::io::str(fmter % message),
348 width, FL_NORMAL_SIZE);
350 string const tmp = warning_posted_ ?
351 _("WARNING!") + string(" ") + message :
354 string const str = formatted(tmp, width, FL_NORMAL_SIZE);
357 fl_set_object_label(message_widget_, str.c_str());
358 FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
359 fl_set_object_lcol(message_widget_, label_color);
361 if (!message_widget_->visible)
362 fl_show_object(message_widget_);
368 FormBase * GetForm(FL_OBJECT * ob)
370 lyx::Assert(ob && ob->form && ob->form->u_vdata);
371 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
380 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
382 GetForm(ob)->getController().ApplyButton();
386 void C_FormBaseOKCB(FL_OBJECT * ob, long)
388 GetForm(ob)->getController().OKButton();
392 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
394 FormBase * form = GetForm(ob);
395 form->getController().CancelButton();
399 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
401 GetForm(ob)->getController().RestoreButton();
405 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
407 GetForm(ob)->InputCB(ob, d);
411 static int C_WMHideCB(FL_FORM * form, void *)
413 // Close the dialog cleanly, even if the WM is used to do so.
414 lyx::Assert(form && form->u_vdata);
415 FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
416 ptr->getController().CancelButton();
420 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
421 FL_Coord, FL_Coord, int key, void *)
423 // Note that the return value is important in the pre-emptive handler.
424 // Don't return anything other than 0.
427 // Don't Assert this one, as it can happen quite naturally when things
428 // are being deleted in the d-tor.
430 if (!ob->form) return 0;
432 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
435 ptr->PrehandlerCB(ob, event, key);