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/LAssert.h"
25 #include "support/lstrings.h"
27 #include "lyx_forms.h"
29 using namespace lyx::support;
34 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
35 // These should be in forms.h but aren't
36 void fl_show_tooltip(const char *, int, int);
37 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 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::bcview()
89 return static_cast<xformsBC &>(bc().view());
93 void FormBase::prepare_to_show()
95 double const scale = get_scale_to_fit(form());
97 scale_form_horizontally(form(), scale);
99 // work around dumb xforms sizing bug
103 fl_set_form_atclose(form(), C_WMHideCB, 0);
105 // set the title for the minimized form
106 if (!getController().IconifyWithMain())
107 fl_winicontitle(form()->window, getTitle().c_str());
109 // assign an icon to the form
110 string const iconname = LibFileSearch("images", "lyx", "xpm");
111 if (!iconname.empty()) {
113 icon_pixmap_ = fl_read_pixmapfile(fl_root,
119 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
124 void FormBase::show()
126 // build() is/should be called from the controller, so form() should
130 // we use minw_ to flag whether the dialog has ever been shown.
131 // In turn, prepare_to_show() initialises various bits 'n' pieces
132 // (including minw_).
137 // make sure the form is up to date.
138 fl_freeze_form(form());
140 fl_unfreeze_form(form());
142 if (form()->visible) {
143 fl_raise_form(form());
144 /* This XMapWindow() will hopefully ensure that
145 * iconified dialogs are de-iconified. Mad props
146 * out to those crazy Xlib guys for forgetting a
147 * XDeiconifyWindow(). At least WindowMaker, when
148 * being notified of the redirected MapRequest will
149 * specifically de-iconify. From source, fvwm2 seems
152 XMapWindow(fl_get_display(), form()->window);
154 // calls to fl_set_form_minsize/maxsize apply only to the next
155 // fl_show_form(), so this comes first.
156 fl_set_form_minsize(form(), minw_, minh_);
158 fl_set_form_maxsize(form(), minw_, minh_);
160 string const maximize_title = "LyX: " + getTitle();
161 int const iconify_policy =
162 getController().IconifyWithMain() ? FL_TRANSIENT : 0;
165 FL_PLACE_MOUSE | FL_FREE_SIZE,
167 maximize_title.c_str());
172 void FormBase::hide()
174 // xforms sometimes tries to process a hint-type MotionNotify, and
175 // use XQueryPointer, without verifying if the window still exists.
176 // So we try to clear out motion events in the queue before the
178 XSync(fl_get_display(), false);
180 if (form() && form()->visible)
181 fl_hide_form(form());
185 void FormBase::setPrehandler(FL_OBJECT * ob)
188 fl_set_object_prehandler(ob, C_PrehandlerCB);
192 void FormBase::setMessageWidget(FL_OBJECT * ob)
194 Assert(ob && ob->objclass == FL_TEXT);
195 message_widget_ = ob;
196 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
200 void FormBase::InputCB(FL_OBJECT * ob, long data)
202 // It is possible to set the choice to 0 when using the
203 // keyboard shortcuts. This work-around deals with the problem.
204 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
205 fl_set_choice(ob, 1);
208 bc().input(input(ob, data));
212 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
214 return ButtonPolicy::SMI_VALID;
218 // preemptive handler for feedback messages
219 void FormBase::MessageCB(FL_OBJECT * ob, int event)
226 string const feedback = getFeedback(ob);
227 if (feedback.empty() && warning_posted_)
230 warning_posted_ = false;
231 postMessage(getFeedback(ob));
236 if (!warning_posted_)
246 void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
250 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
251 // Trigger an input event when pasting in an xforms input object
252 // using the middle mouse button.
257 if (message_widget_) {
261 // Post feedback as the mouse enters the object,
262 // remove it as the mouse leaves.
263 MessageCB(ob, event);
268 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
269 // Tooltips are not displayed on browser widgets due to an xforms' bug.
270 // This is a work-around:
271 if (ob->objclass == FL_BROWSER) {
274 if (ob->tooltip && *(ob->tooltip)) {
275 int const x = ob->form->x + ob->x;
276 int const y = ob->form->y + ob->y + ob->h + 1;
277 fl_show_tooltip(ob->tooltip, x, y);
291 void FormBase::postWarning(string const & warning)
293 warning_posted_ = true;
294 postMessage(warning);
298 void FormBase::clearMessage()
300 Assert(message_widget_);
302 warning_posted_ = false;
304 string const existing = message_widget_->label
305 ? message_widget_->label : string();
306 if (existing.empty())
309 // This trick is needed to get xforms to clear the label...
310 fl_set_object_label(message_widget_, "");
311 fl_hide_object(message_widget_);
315 void FormBase::postMessage(string const & message)
317 Assert(message_widget_);
319 int const width = message_widget_->w - 10;
320 string const tmp = warning_posted_ ?
321 bformat(_("WARNING! %1$s"), message) :
324 string const str = formatted(tmp, width, FL_NORMAL_SIZE);
326 fl_set_object_label(message_widget_, str.c_str());
327 FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
328 fl_set_object_lcol(message_widget_, label_color);
330 if (!message_widget_->visible)
331 fl_show_object(message_widget_);
337 FormBase * GetForm(FL_OBJECT * ob)
339 Assert(ob && ob->form && ob->form->u_vdata);
340 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
349 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
351 GetForm(ob)->getController().ApplyButton();
355 void C_FormBaseOKCB(FL_OBJECT * ob, long)
357 GetForm(ob)->getController().OKButton();
361 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
363 FormBase * form = GetForm(ob);
364 form->getController().CancelButton();
368 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
370 GetForm(ob)->getController().RestoreButton();
374 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
376 GetForm(ob)->InputCB(ob, d);
380 static int C_WMHideCB(FL_FORM * form, void *)
382 // Close the dialog cleanly, even if the WM is used to do so.
383 Assert(form && form->u_vdata);
384 FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
385 ptr->getController().CancelButton();
389 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
390 FL_Coord, FL_Coord, int key, void *)
392 // Note that the return value is important in the pre-emptive handler.
393 // Don't return anything other than 0.
396 // Don't Assert this one, as it can happen quite naturally when things
397 // are being deleted in the d-tor.
399 if (!ob->form) return 0;
401 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
404 ptr->PrehandlerCB(ob, event, key);