2 * \file FormDialogView.C
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.
13 #include "FormDialogView.h"
16 #include "xforms_helpers.h" // formatted
17 #include "xforms_resize.h"
20 #include "controllers/ButtonController.h"
24 #include "support/filetools.h" // LibFileSearch
25 #include "support/lstrings.h"
27 #include "lyx_forms.h"
29 using lyx::support::bformat;
30 using lyx::support::LibFileSearch;
37 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
38 // These should be in forms.h but aren't
39 void fl_show_tooltip(const char *, int, int);
40 void fl_hide_tooltip();
43 // Callback function invoked by xforms when the dialog is closed by the
45 static int C_WMHideCB(FL_FORM * form, void *);
47 // Callback function invoked by the xforms pre-handler routine.
48 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
53 FormDialogView::FormDialogView(Dialog & parent,
54 string const & t, bool allowResize)
55 : Dialog::View(parent, t),
56 warning_posted_(false), message_widget_(0),
57 minw_(0), minh_(0), allow_resize_(allowResize),
58 icon_pixmap_(0), icon_mask_(0),
59 tooltips_(new Tooltips)
63 FormDialogView::~FormDialogView()
66 XFreePixmap(fl_get_display(), icon_pixmap_);
72 bool FormDialogView::isVisible() const
74 return form() && form()->visible;
78 Tooltips & FormDialogView::tooltips()
84 void FormDialogView::redraw()
86 if (form() && form()->visible)
87 fl_redraw_form(form());
91 xformsBC & FormDialogView::bcview()
93 return static_cast<xformsBC &>(dialog().bc().view());
97 void FormDialogView::prepare_to_show()
99 double const scale = get_scale_to_fit(form());
101 scale_form_horizontally(form(), scale);
103 // work around dumb xforms sizing bug
107 fl_set_form_atclose(form(), C_WMHideCB, 0);
109 // set the title for the minimized form
110 if (!lyxrc.dialogs_iconify_with_main)
111 fl_winicontitle(form()->window, getTitle().c_str());
113 // assign an icon to the form
114 string const iconname = LibFileSearch("images", "lyx", "xpm");
115 if (!iconname.empty()) {
117 icon_pixmap_ = fl_read_pixmapfile(fl_root,
123 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
128 void FormDialogView::show()
134 // we use minw_ to flag whether the dialog has ever been shown.
135 // In turn, prepare_to_show() initialises various bits 'n' pieces
136 // (including minw_).
141 // make sure the form is up to date.
142 fl_freeze_form(form());
144 fl_unfreeze_form(form());
146 if (form()->visible) {
147 fl_raise_form(form());
148 /* This XMapWindow() will hopefully ensure that
149 * iconified dialogs are de-iconified. Mad props
150 * out to those crazy Xlib guys for forgetting a
151 * XDeiconifyWindow(). At least WindowMaker, when
152 * being notified of the redirected MapRequest will
153 * specifically de-iconify. From source, fvwm2 seems
156 XMapWindow(fl_get_display(), form()->window);
158 // calls to fl_set_form_minsize/maxsize apply only to the next
159 // fl_show_form(), so this comes first.
160 fl_set_form_minsize(form(), minw_, minh_);
162 fl_set_form_maxsize(form(), minw_, minh_);
164 string const maximize_title = "LyX: " + getTitle();
165 int const iconify_policy = lyxrc.dialogs_iconify_with_main ?
169 FL_PLACE_MOUSE | FL_FREE_SIZE,
171 maximize_title.c_str());
176 void FormDialogView::hide()
178 // xforms sometimes tries to process a hint-type MotionNotify, and
179 // use XQueryPointer, without verifying if the window still exists.
180 // So we try to clear out motion events in the queue before the
182 XSync(fl_get_display(), false);
184 if (form() && form()->visible)
185 fl_hide_form(form());
189 void FormDialogView::setPrehandler(FL_OBJECT * ob)
192 fl_set_object_prehandler(ob, C_PrehandlerCB);
196 void FormDialogView::setMessageWidget(FL_OBJECT * ob)
198 BOOST_ASSERT(ob && ob->objclass == FL_TEXT);
199 message_widget_ = ob;
200 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
204 void FormDialogView::InputCB(FL_OBJECT * ob, long data)
206 // It is possible to set the choice to 0 when using the
207 // keyboard shortcuts. This work-around deals with the problem.
208 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
209 fl_set_choice(ob, 1);
212 bc().input(input(ob, data));
216 ButtonPolicy::SMInput FormDialogView::input(FL_OBJECT *, long)
218 return ButtonPolicy::SMI_VALID;
222 // preemptive handler for feedback messages
223 void FormDialogView::MessageCB(FL_OBJECT * ob, int event)
230 string const feedback = getFeedback(ob);
231 if (feedback.empty() && warning_posted_)
234 warning_posted_ = false;
235 postMessage(getFeedback(ob));
240 if (!warning_posted_)
250 void FormDialogView::PrehandlerCB(FL_OBJECT * ob, int event, int key)
254 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
255 // Trigger an input event when pasting in an xforms input
256 // object using the middle mouse button.
261 if (message_widget_) {
265 // Post feedback as the mouse enters the object,
266 // remove it as the mouse leaves.
267 MessageCB(ob, event);
272 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
273 // Tooltips are not displayed on browser widgets due to an xforms' bug.
274 // This is a work-around:
275 if (ob->objclass == FL_BROWSER) {
278 if (ob->tooltip && *(ob->tooltip)) {
279 int const x = ob->form->x + ob->x;
280 int const y = ob->form->y + ob->y + ob->h + 1;
281 fl_show_tooltip(ob->tooltip, x, y);
295 void FormDialogView::postWarning(string const & warning)
297 warning_posted_ = true;
298 postMessage(warning);
302 void FormDialogView::clearMessage()
304 BOOST_ASSERT(message_widget_);
306 warning_posted_ = false;
308 string const existing = message_widget_->label
309 ? message_widget_->label : string();
310 if (existing.empty())
313 // This trick is needed to get xforms to clear the label...
314 fl_set_object_label(message_widget_, "");
315 fl_hide_object(message_widget_);
319 void FormDialogView::postMessage(string const & message)
321 BOOST_ASSERT(message_widget_);
323 int const width = message_widget_->w - 10;
324 string const tmp = warning_posted_ ?
325 bformat(_("WARNING! %1$s"), message) :
328 string const str = formatted(tmp, width, FL_NORMAL_SIZE);
330 fl_set_object_label(message_widget_, str.c_str());
331 FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
332 fl_set_object_lcol(message_widget_, label_color);
334 if (!message_widget_->visible)
335 fl_show_object(message_widget_);
341 FormDialogView * GetForm(FL_OBJECT * ob)
343 BOOST_ASSERT(ob && ob->form && ob->form->u_vdata);
344 FormDialogView * ptr =
345 static_cast<FormDialogView *>(ob->form->u_vdata);
354 void C_FormDialogView_ApplyCB(FL_OBJECT * ob, long)
356 GetForm(ob)->dialog().ApplyButton();
360 void C_FormDialogView_OKCB(FL_OBJECT * ob, long)
362 GetForm(ob)->dialog().OKButton();
366 void C_FormDialogView_CancelCB(FL_OBJECT * ob, long)
368 FormDialogView * form = GetForm(ob);
369 form->dialog().CancelButton();
373 void C_FormDialogView_RestoreCB(FL_OBJECT * ob, long)
375 GetForm(ob)->dialog().RestoreButton();
379 void C_FormDialogView_InputCB(FL_OBJECT * ob, long d)
381 GetForm(ob)->InputCB(ob, d);
385 static int C_WMHideCB(FL_FORM * form, void *)
387 // Close the dialog cleanly, even if the WM is used to do so.
388 BOOST_ASSERT(form && form->u_vdata);
389 FormDialogView * ptr = static_cast<FormDialogView *>(form->u_vdata);
390 ptr->dialog().CancelButton();
394 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
395 FL_Coord, FL_Coord, int key, void *)
397 // Note that the return value is important in the pre-emptive handler.
398 // Don't return anything other than 0.
401 // Don't Assert this one, as it can happen quite naturally when things
402 // are being deleted in the d-tor.
403 //BOOST_ASSERT(ob->form);
404 if (!ob->form) return 0;
406 FormDialogView * ptr =
407 static_cast<FormDialogView *>(ob->form->u_vdata);
410 ptr->PrehandlerCB(ob, event, key);