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 lyx::support::bformat;
29 using lyx::support::LibFileSearch;
36 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
37 // These should be in forms.h but aren't
38 void fl_show_tooltip(const char *, int, int);
39 void fl_hide_tooltip();
42 // Callback function invoked by xforms when the dialog is closed by the
44 static int C_WMHideCB(FL_FORM * form, void *);
46 // Callback function invoked by the xforms pre-handler routine.
47 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
52 FormBase::FormBase(string const & t, bool allowResize)
54 warning_posted_(false), message_widget_(0),
55 minw_(0), minh_(0), allow_resize_(allowResize),
56 icon_pixmap_(0), icon_mask_(0),
57 tooltips_(new Tooltips)
64 XFreePixmap(fl_get_display(), icon_pixmap_);
70 bool FormBase::isVisible() const
72 return form() && form()->visible;
76 Tooltips & FormBase::tooltips()
82 void FormBase::redraw()
84 if (form() && form()->visible)
85 fl_redraw_form(form());
89 xformsBC & FormBase::bcview()
91 return static_cast<xformsBC &>(bc().view());
95 void FormBase::prepare_to_show()
97 double const scale = get_scale_to_fit(form());
99 scale_form_horizontally(form(), scale);
101 // work around dumb xforms sizing bug
105 fl_set_form_atclose(form(), C_WMHideCB, 0);
107 // set the title for the minimized form
108 if (!getController().IconifyWithMain())
109 fl_winicontitle(form()->window, getTitle().c_str());
111 // assign an icon to the form
112 string const iconname = LibFileSearch("images", "lyx", "xpm");
113 if (!iconname.empty()) {
115 icon_pixmap_ = fl_read_pixmapfile(fl_root,
121 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
126 void FormBase::show()
128 // build() is/should be called from the controller, so form() should
130 BOOST_ASSERT(form());
132 // we use minw_ to flag whether the dialog has ever been shown.
133 // In turn, prepare_to_show() initialises various bits 'n' pieces
134 // (including minw_).
139 // make sure the form is up to date.
140 fl_freeze_form(form());
142 fl_unfreeze_form(form());
144 if (form()->visible) {
145 fl_raise_form(form());
146 /* This XMapWindow() will hopefully ensure that
147 * iconified dialogs are de-iconified. Mad props
148 * out to those crazy Xlib guys for forgetting a
149 * XDeiconifyWindow(). At least WindowMaker, when
150 * being notified of the redirected MapRequest will
151 * specifically de-iconify. From source, fvwm2 seems
154 XMapWindow(fl_get_display(), form()->window);
156 // calls to fl_set_form_minsize/maxsize apply only to the next
157 // fl_show_form(), so this comes first.
158 fl_set_form_minsize(form(), minw_, minh_);
160 fl_set_form_maxsize(form(), minw_, minh_);
162 string const maximize_title = "LyX: " + getTitle();
163 int const iconify_policy =
164 getController().IconifyWithMain() ? FL_TRANSIENT : 0;
167 FL_PLACE_MOUSE | FL_FREE_SIZE,
169 maximize_title.c_str());
174 void FormBase::hide()
176 // xforms sometimes tries to process a hint-type MotionNotify, and
177 // use XQueryPointer, without verifying if the window still exists.
178 // So we try to clear out motion events in the queue before the
180 XSync(fl_get_display(), false);
182 if (form() && form()->visible)
183 fl_hide_form(form());
187 void FormBase::setPrehandler(FL_OBJECT * ob)
190 fl_set_object_prehandler(ob, C_PrehandlerCB);
194 void FormBase::setMessageWidget(FL_OBJECT * ob)
196 BOOST_ASSERT(ob && ob->objclass == FL_TEXT);
197 message_widget_ = ob;
198 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
202 void FormBase::InputCB(FL_OBJECT * ob, long data)
204 // It is possible to set the choice to 0 when using the
205 // keyboard shortcuts. This work-around deals with the problem.
206 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
207 fl_set_choice(ob, 1);
210 bc().input(input(ob, data));
214 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
216 return ButtonPolicy::SMI_VALID;
220 // preemptive handler for feedback messages
221 void FormBase::MessageCB(FL_OBJECT * ob, int event)
228 string const feedback = getFeedback(ob);
229 if (feedback.empty() && warning_posted_)
232 warning_posted_ = false;
233 postMessage(getFeedback(ob));
238 if (!warning_posted_)
248 void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
252 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
253 // Trigger an input event when pasting in an xforms input object
254 // using the middle mouse button.
259 if (message_widget_) {
263 // Post feedback as the mouse enters the object,
264 // remove it as the mouse leaves.
265 MessageCB(ob, event);
270 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
271 // Tooltips are not displayed on browser widgets due to an xforms' bug.
272 // This is a work-around:
273 if (ob->objclass == FL_BROWSER) {
276 if (ob->tooltip && *(ob->tooltip)) {
277 int const x = ob->form->x + ob->x;
278 int const y = ob->form->y + ob->y + ob->h + 1;
279 fl_show_tooltip(ob->tooltip, x, y);
293 void FormBase::postWarning(string const & warning)
295 warning_posted_ = true;
296 postMessage(warning);
300 void FormBase::clearMessage()
302 BOOST_ASSERT(message_widget_);
304 warning_posted_ = false;
306 string const existing = message_widget_->label
307 ? message_widget_->label : string();
308 if (existing.empty())
311 // This trick is needed to get xforms to clear the label...
312 fl_set_object_label(message_widget_, "");
313 fl_hide_object(message_widget_);
317 void FormBase::postMessage(string const & message)
319 BOOST_ASSERT(message_widget_);
321 int const width = message_widget_->w - 10;
322 string const tmp = warning_posted_ ?
323 bformat(_("WARNING! %1$s"), message) :
326 string const str = formatted(tmp, width, FL_NORMAL_SIZE);
328 fl_set_object_label(message_widget_, str.c_str());
329 FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
330 fl_set_object_lcol(message_widget_, label_color);
332 if (!message_widget_->visible)
333 fl_show_object(message_widget_);
339 FormBase * GetForm(FL_OBJECT * ob)
341 BOOST_ASSERT(ob && ob->form && ob->form->u_vdata);
342 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
351 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
353 GetForm(ob)->getController().ApplyButton();
357 void C_FormBaseOKCB(FL_OBJECT * ob, long)
359 GetForm(ob)->getController().OKButton();
363 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
365 FormBase * form = GetForm(ob);
366 form->getController().CancelButton();
370 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
372 GetForm(ob)->getController().RestoreButton();
376 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
378 GetForm(ob)->InputCB(ob, d);
382 static int C_WMHideCB(FL_FORM * form, void *)
384 // Close the dialog cleanly, even if the WM is used to do so.
385 BOOST_ASSERT(form && form->u_vdata);
386 FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
387 ptr->getController().CancelButton();
391 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
392 FL_Coord, FL_Coord, int key, void *)
394 // Note that the return value is important in the pre-emptive handler.
395 // Don't return anything other than 0.
398 // Don't Assert this one, as it can happen quite naturally when things
399 // are being deleted in the d-tor.
400 //BOOST_ASSERT(ob->form);
401 if (!ob->form) return 0;
403 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
406 ptr->PrehandlerCB(ob, event, key);