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 #if FL_VERSION > 0 || FL_REVISION >= 89
37 // These should be in forms.h but aren't
38 void fl_show_tooltip(const char *, int, int);
40 void fl_hide_tooltip();
44 // Callback function invoked by xforms when the dialog is closed by the
46 static int C_WMHideCB(FL_FORM * form, void *);
48 // Callback function invoked by the xforms pre-handler routine.
49 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
54 FormBase::FormBase(string const & t, bool allowResize)
56 warning_posted_(false), message_widget_(0),
57 minw_(0), minh_(0), allow_resize_(allowResize),
58 title_(t), icon_pixmap_(0), icon_mask_(0),
59 tooltips_(new Tooltips())
66 XFreePixmap(fl_get_display(), icon_pixmap_);
72 Tooltips & FormBase::tooltips()
78 void FormBase::redraw()
80 if (form() && form()->visible)
81 fl_redraw_form(form());
85 xformsBC & FormBase::bc()
87 return static_cast<xformsBC &>(getController().bc());
88 // return dynamic_cast<GUIbc &>(controller_ptr_->bc());
92 void FormBase::prepare_to_show()
94 double const scale = get_scale_to_fit(form());
96 scale_form_horizontally(form(), scale);
98 // work around dumb xforms sizing bug
102 fl_set_form_atclose(form(), C_WMHideCB, 0);
104 // set the title for the minimized form
105 if (!getController().IconifyWithMain())
106 fl_winicontitle(form()->window, title_.c_str());
108 // assign an icon to the form
109 string const iconname = LibFileSearch("images", "lyx", "xpm");
110 if (!iconname.empty()) {
112 icon_pixmap_ = fl_read_pixmapfile(fl_root,
118 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
123 void FormBase::show()
125 // build() is/should be called from the controller, so form() should
129 // we use minw_ to flag whether the dialog has ever been shown.
130 // In turn, prepare_to_show() initialises various bits 'n' pieces
131 // (including minw_).
136 // make sure the form is up to date.
137 fl_freeze_form(form());
139 fl_unfreeze_form(form());
141 if (form()->visible) {
142 fl_raise_form(form());
143 /* This XMapWindow() will hopefully ensure that
144 * iconified dialogs are de-iconified. Mad props
145 * out to those crazy Xlib guys for forgetting a
146 * XDeiconifyWindow(). At least WindowMaker, when
147 * being notified of the redirected MapRequest will
148 * specifically de-iconify. From source, fvwm2 seems
151 XMapWindow(fl_get_display(), form()->window);
153 // calls to fl_set_form_minsize/maxsize apply only to the next
154 // fl_show_form(), so this comes first.
155 fl_set_form_minsize(form(), minw_, minh_);
157 fl_set_form_maxsize(form(), minw_, minh_);
159 string const maximize_title = "LyX: " + title_;
160 int const iconify_policy =
161 getController().IconifyWithMain() ? FL_TRANSIENT : 0;
164 FL_PLACE_MOUSE | FL_FREE_SIZE,
166 maximize_title.c_str());
171 void FormBase::hide()
173 #if FL_VERSION > 0 || FL_REVISION >= 89
174 // Does no harm if none is visible and ensures that the tooltip form
175 // is hidden should the dialog be closed from the keyboard.
179 // xforms sometimes tries to process a hint-type MotionNotify, and
180 // use XQueryPointer, without verifying if the window still exists.
181 // So we try to clear out motion events in the queue before the
183 XSync(fl_get_display(), false);
185 if (form() && form()->visible)
186 fl_hide_form(form());
190 void FormBase::setPrehandler(FL_OBJECT * ob)
193 fl_set_object_prehandler(ob, C_PrehandlerCB);
197 void FormBase::setMessageWidget(FL_OBJECT * ob)
199 lyx::Assert(ob && ob->objclass == FL_TEXT);
200 message_widget_ = ob;
201 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
205 void FormBase::InputCB(FL_OBJECT * ob, long data)
207 // It is possible to set the choice to 0 when using the
208 // keyboard shortcuts. This work-around deals with the problem.
209 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
210 fl_set_choice(ob, 1);
213 bc().input(input(ob, data));
217 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
219 return ButtonPolicy::SMI_VALID;
223 // preemptive handler for feedback messages
224 void FormBase::MessageCB(FL_OBJECT * ob, int event)
231 string const feedback = getFeedback(ob);
232 if (feedback.empty() && warning_posted_)
235 warning_posted_ = false;
236 postMessage(getFeedback(ob));
241 if (!warning_posted_)
251 void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
255 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
256 // Trigger an input event when pasting in an xforms input object
257 // using the middle mouse button.
263 if (event != FL_ENTER && event != FL_LEAVE)
266 if (ob->objclass == FL_TABFOLDER) {
267 // This prehandler is used to work-around an xforms bug and
268 // ensures that the form->x, form->y coords of the active
269 // tabfolder are up to date.
271 // The tabfolder itself can be very narrow, being just
272 // the visible border to the tabs.
273 // We thus use both FL_ENTER and FL_LEAVE as flags,
274 // in case the FL_ENTER event is not caught.
276 FL_FORM * const folder = fl_get_active_folder(ob);
277 if (folder && folder->window) {
278 fl_get_winorigin(folder->window,
279 &(folder->x), &(folder->y));
284 if (message_widget_) {
285 // Post feedback as the mouse enters the object,
286 // remove it as the mouse leaves.
287 MessageCB(ob, event);
290 #if FL_VERSION > 0 || FL_REVISION >= 89
291 // Tooltips are not displayed on browser widgets due to an xforms' bug.
292 // This is a work-around:
293 if (ob->objclass == FL_BROWSER) {
294 if (event == FL_ENTER && ob->tooltip && *(ob->tooltip)) {
295 fl_show_tooltip(ob->tooltip, ob->form->x + ob->x,
296 ob->form->y + ob->y + ob->h + 1);
297 } else if (event == FL_LEAVE) {
305 void FormBase::postWarning(string const & warning)
307 warning_posted_ = true;
308 postMessage(warning);
312 void FormBase::clearMessage()
314 lyx::Assert(message_widget_);
316 warning_posted_ = false;
318 string const existing = message_widget_->label
319 ? message_widget_->label : string();
320 if (existing.empty())
323 // This trick is needed to get xforms to clear the label...
324 fl_set_object_label(message_widget_, "");
325 fl_hide_object(message_widget_);
329 void FormBase::postMessage(string const & message)
331 lyx::Assert(message_widget_);
333 int const width = message_widget_->w - 10;
335 boost::format fmter = warning_posted_ ?
336 boost::format(_("WARNING! %1$s")) :
337 boost::format("%1$s");
339 string const str = formatted(boost::io::str(fmter % message),
340 width, FL_NORMAL_SIZE);
342 string const tmp = warning_posted_ ?
343 _("WARNING!") + string(" ") + message :
346 string const str = formatted(tmp, width, FL_NORMAL_SIZE);
349 fl_set_object_label(message_widget_, str.c_str());
350 FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
351 fl_set_object_lcol(message_widget_, label_color);
353 if (!message_widget_->visible)
354 fl_show_object(message_widget_);
360 FormBase * GetForm(FL_OBJECT * ob)
362 lyx::Assert(ob && ob->form && ob->form->u_vdata);
363 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
372 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
374 GetForm(ob)->getController().ApplyButton();
378 void C_FormBaseOKCB(FL_OBJECT * ob, long)
380 GetForm(ob)->getController().OKButton();
384 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
386 FormBase * form = GetForm(ob);
387 form->getController().CancelButton();
391 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
393 GetForm(ob)->getController().RestoreButton();
397 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
399 GetForm(ob)->InputCB(ob, d);
403 static int C_WMHideCB(FL_FORM * form, void *)
405 // Close the dialog cleanly, even if the WM is used to do so.
406 lyx::Assert(form && form->u_vdata);
407 FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
408 ptr->getController().CancelButton();
412 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
413 FL_Coord, FL_Coord, int key, void *)
415 // Note that the return value is important in the pre-emptive handler.
416 // Don't return anything other than 0.
419 // Don't Assert this one, as it can happen quite naturally when things
420 // are being deleted in the d-tor.
422 if (!ob->form) return 0;
424 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
427 ptr->PrehandlerCB(ob, event, key);