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 "xforms_helpers.h" // formatted
17 #include "xforms_resize.h"
20 #include "controllers/ButtonController.h"
21 #include "controllers/ControlButtons.h"
23 #include "support/filetools.h" // LibFileSearch
24 #include "support/lstrings.h"
26 #include "lyx_forms.h"
28 using namespace lyx::support;
33 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
34 // These should be in forms.h but aren't
35 void fl_show_tooltip(const char *, int, int);
36 void fl_hide_tooltip();
39 // Callback function invoked by xforms when the dialog is closed by the
41 static int C_WMHideCB(FL_FORM * form, void *);
43 // Callback function invoked by the xforms pre-handler routine.
44 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
49 FormBase::FormBase(string const & t, bool allowResize)
51 warning_posted_(false), message_widget_(0),
52 minw_(0), minh_(0), allow_resize_(allowResize),
53 icon_pixmap_(0), icon_mask_(0),
54 tooltips_(new Tooltips())
61 XFreePixmap(fl_get_display(), icon_pixmap_);
67 bool FormBase::isVisible() const
69 return form() && form()->visible;
73 Tooltips & FormBase::tooltips()
79 void FormBase::redraw()
81 if (form() && form()->visible)
82 fl_redraw_form(form());
86 xformsBC & FormBase::bcview()
88 return static_cast<xformsBC &>(bc().view());
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, getTitle().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
127 BOOST_ASSERT(form());
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: " + getTitle();
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 // xforms sometimes tries to process a hint-type MotionNotify, and
174 // use XQueryPointer, without verifying if the window still exists.
175 // So we try to clear out motion events in the queue before the
177 XSync(fl_get_display(), false);
179 if (form() && form()->visible)
180 fl_hide_form(form());
184 void FormBase::setPrehandler(FL_OBJECT * ob)
187 fl_set_object_prehandler(ob, C_PrehandlerCB);
191 void FormBase::setMessageWidget(FL_OBJECT * ob)
193 BOOST_ASSERT(ob && ob->objclass == FL_TEXT);
194 message_widget_ = ob;
195 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
199 void FormBase::InputCB(FL_OBJECT * ob, long data)
201 // It is possible to set the choice to 0 when using the
202 // keyboard shortcuts. This work-around deals with the problem.
203 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
204 fl_set_choice(ob, 1);
207 bc().input(input(ob, data));
211 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
213 return ButtonPolicy::SMI_VALID;
217 // preemptive handler for feedback messages
218 void FormBase::MessageCB(FL_OBJECT * ob, int event)
225 string const feedback = getFeedback(ob);
226 if (feedback.empty() && warning_posted_)
229 warning_posted_ = false;
230 postMessage(getFeedback(ob));
235 if (!warning_posted_)
245 void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
249 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
250 // Trigger an input event when pasting in an xforms input object
251 // using the middle mouse button.
256 if (message_widget_) {
260 // Post feedback as the mouse enters the object,
261 // remove it as the mouse leaves.
262 MessageCB(ob, event);
267 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
268 // Tooltips are not displayed on browser widgets due to an xforms' bug.
269 // This is a work-around:
270 if (ob->objclass == FL_BROWSER) {
273 if (ob->tooltip && *(ob->tooltip)) {
274 int const x = ob->form->x + ob->x;
275 int const y = ob->form->y + ob->y + ob->h + 1;
276 fl_show_tooltip(ob->tooltip, x, y);
290 void FormBase::postWarning(string const & warning)
292 warning_posted_ = true;
293 postMessage(warning);
297 void FormBase::clearMessage()
299 BOOST_ASSERT(message_widget_);
301 warning_posted_ = false;
303 string const existing = message_widget_->label
304 ? message_widget_->label : string();
305 if (existing.empty())
308 // This trick is needed to get xforms to clear the label...
309 fl_set_object_label(message_widget_, "");
310 fl_hide_object(message_widget_);
314 void FormBase::postMessage(string const & message)
316 BOOST_ASSERT(message_widget_);
318 int const width = message_widget_->w - 10;
319 string const tmp = warning_posted_ ?
320 bformat(_("WARNING! %1$s"), message) :
323 string const str = formatted(tmp, width, FL_NORMAL_SIZE);
325 fl_set_object_label(message_widget_, str.c_str());
326 FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
327 fl_set_object_lcol(message_widget_, label_color);
329 if (!message_widget_->visible)
330 fl_show_object(message_widget_);
336 FormBase * GetForm(FL_OBJECT * ob)
338 BOOST_ASSERT(ob && ob->form && ob->form->u_vdata);
339 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
348 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
350 GetForm(ob)->getController().ApplyButton();
354 void C_FormBaseOKCB(FL_OBJECT * ob, long)
356 GetForm(ob)->getController().OKButton();
360 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
362 FormBase * form = GetForm(ob);
363 form->getController().CancelButton();
367 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
369 GetForm(ob)->getController().RestoreButton();
373 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
375 GetForm(ob)->InputCB(ob, d);
379 static int C_WMHideCB(FL_FORM * form, void *)
381 // Close the dialog cleanly, even if the WM is used to do so.
382 BOOST_ASSERT(form && form->u_vdata);
383 FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
384 ptr->getController().CancelButton();
388 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
389 FL_Coord, FL_Coord, int key, void *)
391 // Note that the return value is important in the pre-emptive handler.
392 // Don't return anything other than 0.
395 // Don't Assert this one, as it can happen quite naturally when things
396 // are being deleted in the d-tor.
397 //BOOST_ASSERT(ob->form);
398 if (!ob->form) return 0;
400 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
403 ptr->PrehandlerCB(ob, event, key);