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
15 #include "ControlButtons.h"
17 #include "ButtonController.h"
18 #include "xforms_resize.h"
20 #include "xforms_helpers.h" // formatted
22 #include "gettext.h" // _()
23 #include "support/BoostFormat.h"
25 #include "support/LAssert.h"
26 #include "support/filetools.h" // LibFileSearch
28 #include "lyx_forms.h"
32 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
33 // These should be in forms.h but aren't
34 void fl_show_tooltip(const char *, int, int);
35 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()
172 // xforms sometimes tries to process a hint-type MotionNotify, and
173 // use XQueryPointer, without verifying if the window still exists.
174 // So we try to clear out motion events in the queue before the
176 XSync(fl_get_display(), false);
178 if (form() && form()->visible)
179 fl_hide_form(form());
183 void FormBase::setPrehandler(FL_OBJECT * ob)
186 fl_set_object_prehandler(ob, C_PrehandlerCB);
190 void FormBase::setMessageWidget(FL_OBJECT * ob)
192 lyx::Assert(ob && ob->objclass == FL_TEXT);
193 message_widget_ = ob;
194 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
198 void FormBase::InputCB(FL_OBJECT * ob, long data)
200 // It is possible to set the choice to 0 when using the
201 // keyboard shortcuts. This work-around deals with the problem.
202 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
203 fl_set_choice(ob, 1);
206 bc().input(input(ob, data));
210 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
212 return ButtonPolicy::SMI_VALID;
216 // preemptive handler for feedback messages
217 void FormBase::MessageCB(FL_OBJECT * ob, int event)
224 string const feedback = getFeedback(ob);
225 if (feedback.empty() && warning_posted_)
228 warning_posted_ = false;
229 postMessage(getFeedback(ob));
234 if (!warning_posted_)
244 void FormBase::PrehandlerCB(FL_OBJECT * ob, int event, int key)
248 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
249 // Trigger an input event when pasting in an xforms input object
250 // using the middle mouse button.
255 if (message_widget_) {
259 // Post feedback as the mouse enters the object,
260 // remove it as the mouse leaves.
261 MessageCB(ob, event);
266 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
267 // Tooltips are not displayed on browser widgets due to an xforms' bug.
268 // This is a work-around:
269 if (ob->objclass == FL_BROWSER) {
272 if (ob->tooltip && *(ob->tooltip)) {
273 int const x = ob->form->x + ob->x;
274 int const y = ob->form->y + ob->y + ob->h + 1;
275 fl_show_tooltip(ob->tooltip, x, y);
289 void FormBase::postWarning(string const & warning)
291 warning_posted_ = true;
292 postMessage(warning);
296 void FormBase::clearMessage()
298 lyx::Assert(message_widget_);
300 warning_posted_ = false;
302 string const existing = message_widget_->label
303 ? message_widget_->label : string();
304 if (existing.empty())
307 // This trick is needed to get xforms to clear the label...
308 fl_set_object_label(message_widget_, "");
309 fl_hide_object(message_widget_);
313 void FormBase::postMessage(string const & message)
315 lyx::Assert(message_widget_);
317 int const width = message_widget_->w - 10;
319 boost::format fmter = warning_posted_ ?
320 boost::format(_("WARNING! %1$s")) :
321 boost::format("%1$s");
323 string const str = formatted(STRCONV(boost::io::str(fmter % message)),
324 width, FL_NORMAL_SIZE);
326 string const tmp = warning_posted_ ?
327 _("WARNING!") + string(" ") + message :
330 string const str = formatted(tmp, width, FL_NORMAL_SIZE);
333 fl_set_object_label(message_widget_, str.c_str());
334 FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
335 fl_set_object_lcol(message_widget_, label_color);
337 if (!message_widget_->visible)
338 fl_show_object(message_widget_);
344 FormBase * GetForm(FL_OBJECT * ob)
346 lyx::Assert(ob && ob->form && ob->form->u_vdata);
347 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
356 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
358 GetForm(ob)->getController().ApplyButton();
362 void C_FormBaseOKCB(FL_OBJECT * ob, long)
364 GetForm(ob)->getController().OKButton();
368 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
370 FormBase * form = GetForm(ob);
371 form->getController().CancelButton();
375 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
377 GetForm(ob)->getController().RestoreButton();
381 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
383 GetForm(ob)->InputCB(ob, d);
387 static int C_WMHideCB(FL_FORM * form, void *)
389 // Close the dialog cleanly, even if the WM is used to do so.
390 lyx::Assert(form && form->u_vdata);
391 FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
392 ptr->getController().CancelButton();
396 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
397 FL_Coord, FL_Coord, int key, void *)
399 // Note that the return value is important in the pre-emptive handler.
400 // Don't return anything other than 0.
403 // Don't Assert this one, as it can happen quite naturally when things
404 // are being deleted in the d-tor.
406 if (!ob->form) return 0;
408 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
411 ptr->PrehandlerCB(ob, event, key);