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
14 #include "FormDialogView.h"
18 #include "ButtonController.h"
19 #include "xforms_resize.h"
21 #include "xforms_helpers.h" // formatted
23 #include "gettext.h" // _()
25 #include "support/BoostFormat.h"
27 #include "support/LAssert.h"
28 #include "support/filetools.h" // LibFileSearch
30 #include FORMS_H_LOCATION
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 FormDialogView::FormDialogView(Dialog & parent,
51 string const & t, bool allowResize)
52 : Dialog::View(parent),
53 warning_posted_(false), message_widget_(0),
54 minw_(0), minh_(0), allow_resize_(allowResize),
55 title_(t), icon_pixmap_(0), icon_mask_(0),
56 tooltips_(new Tooltips())
60 FormDialogView::~FormDialogView()
63 XFreePixmap(fl_get_display(), icon_pixmap_);
69 bool FormDialogView::isVisible() const
71 return form() && form()->visible;
75 Tooltips & FormDialogView::tooltips()
81 void FormDialogView::redraw()
83 if (form() && form()->visible)
84 fl_redraw_form(form());
88 xformsBC & FormDialogView::bcview()
90 return static_cast<xformsBC &>(dialog().bc().view());
94 void FormDialogView::prepare_to_show()
96 double const scale = get_scale_to_fit(form());
98 scale_form_horizontally(form(), scale);
100 // work around dumb xforms sizing bug
104 fl_set_form_atclose(form(), C_WMHideCB, 0);
106 // set the title for the minimized form
107 if (!lyxrc.dialogs_iconify_with_main)
108 fl_winicontitle(form()->window, title_.c_str());
110 // assign an icon to the form
111 string const iconname = LibFileSearch("images", "lyx", "xpm");
112 if (!iconname.empty()) {
114 icon_pixmap_ = fl_read_pixmapfile(fl_root,
120 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
125 void FormDialogView::show()
131 // we use minw_ to flag whether the dialog has ever been shown.
132 // In turn, prepare_to_show() initialises various bits 'n' pieces
133 // (including minw_).
138 // make sure the form is up to date.
139 fl_freeze_form(form());
141 fl_unfreeze_form(form());
143 if (form()->visible) {
144 fl_raise_form(form());
145 /* This XMapWindow() will hopefully ensure that
146 * iconified dialogs are de-iconified. Mad props
147 * out to those crazy Xlib guys for forgetting a
148 * XDeiconifyWindow(). At least WindowMaker, when
149 * being notified of the redirected MapRequest will
150 * specifically de-iconify. From source, fvwm2 seems
153 XMapWindow(fl_get_display(), form()->window);
155 // calls to fl_set_form_minsize/maxsize apply only to the next
156 // fl_show_form(), so this comes first.
157 fl_set_form_minsize(form(), minw_, minh_);
159 fl_set_form_maxsize(form(), minw_, minh_);
161 string const maximize_title = "LyX: " + title_;
162 int const iconify_policy = lyxrc.dialogs_iconify_with_main ?
166 FL_PLACE_MOUSE | FL_FREE_SIZE,
168 maximize_title.c_str());
173 void FormDialogView::hide()
175 // xforms sometimes tries to process a hint-type MotionNotify, and
176 // use XQueryPointer, without verifying if the window still exists.
177 // So we try to clear out motion events in the queue before the
179 XSync(fl_get_display(), false);
181 if (form() && form()->visible)
182 fl_hide_form(form());
186 void FormDialogView::setPrehandler(FL_OBJECT * ob)
189 fl_set_object_prehandler(ob, C_PrehandlerCB);
193 void FormDialogView::setMessageWidget(FL_OBJECT * ob)
195 lyx::Assert(ob && ob->objclass == FL_TEXT);
196 message_widget_ = ob;
197 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
201 void FormDialogView::InputCB(FL_OBJECT * ob, long data)
203 // It is possible to set the choice to 0 when using the
204 // keyboard shortcuts. This work-around deals with the problem.
205 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
206 fl_set_choice(ob, 1);
209 bc().input(input(ob, data));
213 ButtonPolicy::SMInput FormDialogView::input(FL_OBJECT *, long)
215 return ButtonPolicy::SMI_VALID;
219 // preemptive handler for feedback messages
220 void FormDialogView::MessageCB(FL_OBJECT * ob, int event)
227 string const feedback = getFeedback(ob);
228 if (feedback.empty() && warning_posted_)
231 warning_posted_ = false;
232 postMessage(getFeedback(ob));
237 if (!warning_posted_)
247 void FormDialogView::PrehandlerCB(FL_OBJECT * ob, int event, int key)
251 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
252 // Trigger an input event when pasting in an xforms input
253 // object using the middle mouse button.
258 if (message_widget_) {
262 // Post feedback as the mouse enters the object,
263 // remove it as the mouse leaves.
264 MessageCB(ob, event);
269 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
270 // Tooltips are not displayed on browser widgets due to an xforms' bug.
271 // This is a work-around:
272 if (ob->objclass == FL_BROWSER) {
275 if (ob->tooltip && *(ob->tooltip)) {
276 int const x = ob->form->x + ob->x;
277 int const y = ob->form->y + ob->y + ob->h + 1;
278 fl_show_tooltip(ob->tooltip, x, y);
292 void FormDialogView::postWarning(string const & warning)
294 warning_posted_ = true;
295 postMessage(warning);
299 void FormDialogView::clearMessage()
301 lyx::Assert(message_widget_);
303 warning_posted_ = false;
305 string const existing = message_widget_->label
306 ? message_widget_->label : string();
307 if (existing.empty())
310 // This trick is needed to get xforms to clear the label...
311 fl_set_object_label(message_widget_, "");
312 fl_hide_object(message_widget_);
316 void FormDialogView::postMessage(string const & message)
318 lyx::Assert(message_widget_);
320 int const width = message_widget_->w - 10;
322 boost::format fmter = warning_posted_ ?
323 boost::format(_("WARNING! %1$s")) :
324 boost::format("%1$s");
326 string const str = formatted(boost::io::str(fmter % message),
327 width, FL_NORMAL_SIZE);
329 string const tmp = warning_posted_ ?
330 _("WARNING!") + string(" ") + message :
333 string const str = formatted(tmp, width, FL_NORMAL_SIZE);
336 fl_set_object_label(message_widget_, str.c_str());
337 FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
338 fl_set_object_lcol(message_widget_, label_color);
340 if (!message_widget_->visible)
341 fl_show_object(message_widget_);
347 FormDialogView * GetForm(FL_OBJECT * ob)
349 lyx::Assert(ob && ob->form && ob->form->u_vdata);
350 FormDialogView * ptr =
351 static_cast<FormDialogView *>(ob->form->u_vdata);
360 void C_FormDialogView_ApplyCB(FL_OBJECT * ob, long)
362 GetForm(ob)->dialog().ApplyButton();
366 void C_FormDialogView_OKCB(FL_OBJECT * ob, long)
368 GetForm(ob)->dialog().OKButton();
372 void C_FormDialogView_CancelCB(FL_OBJECT * ob, long)
374 FormDialogView * form = GetForm(ob);
375 form->dialog().CancelButton();
379 void C_FormDialogView_RestoreCB(FL_OBJECT * ob, long)
381 GetForm(ob)->dialog().RestoreButton();
385 void C_FormDialogView_InputCB(FL_OBJECT * ob, long d)
387 GetForm(ob)->InputCB(ob, d);
391 static int C_WMHideCB(FL_FORM * form, void *)
393 // Close the dialog cleanly, even if the WM is used to do so.
394 lyx::Assert(form && form->u_vdata);
395 FormDialogView * ptr = static_cast<FormDialogView *>(form->u_vdata);
396 ptr->dialog().CancelButton();
400 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
401 FL_Coord, FL_Coord, int key, void *)
403 // Note that the return value is important in the pre-emptive handler.
404 // Don't return anything other than 0.
407 // Don't Assert this one, as it can happen quite naturally when things
408 // are being deleted in the d-tor.
410 if (!ob->form) return 0;
412 FormDialogView * ptr =
413 static_cast<FormDialogView *>(ob->form->u_vdata);
416 ptr->PrehandlerCB(ob, event, key);