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"
17 #include "ButtonController.h"
18 #include "xforms_resize.h"
20 #include "xforms_helpers.h" // formatted
22 #include "gettext.h" // _()
25 #include "support/lstrings.h"
26 #include "support/LAssert.h"
27 #include "support/filetools.h" // LibFileSearch
29 #include "lyx_forms.h"
31 using namespace lyx::support;
35 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
36 // These should be in forms.h but aren't
37 void fl_show_tooltip(const char *, int, int);
38 void fl_hide_tooltip();
41 // Callback function invoked by xforms when the dialog is closed by the
43 static int C_WMHideCB(FL_FORM * form, void *);
45 // Callback function invoked by the xforms pre-handler routine.
46 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
51 FormDialogView::FormDialogView(Dialog & parent,
52 string const & t, bool allowResize)
53 : Dialog::View(parent, t),
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())
61 FormDialogView::~FormDialogView()
64 XFreePixmap(fl_get_display(), icon_pixmap_);
70 bool FormDialogView::isVisible() const
72 return form() && form()->visible;
76 Tooltips & FormDialogView::tooltips()
82 void FormDialogView::redraw()
84 if (form() && form()->visible)
85 fl_redraw_form(form());
89 xformsBC & FormDialogView::bcview()
91 return static_cast<xformsBC &>(dialog().bc().view());
95 void FormDialogView::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 (!lyxrc.dialogs_iconify_with_main)
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 FormDialogView::show()
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 = lyxrc.dialogs_iconify_with_main ?
167 FL_PLACE_MOUSE | FL_FREE_SIZE,
169 maximize_title.c_str());
174 void FormDialogView::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 FormDialogView::setPrehandler(FL_OBJECT * ob)
190 fl_set_object_prehandler(ob, C_PrehandlerCB);
194 void FormDialogView::setMessageWidget(FL_OBJECT * ob)
196 Assert(ob && ob->objclass == FL_TEXT);
197 message_widget_ = ob;
198 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
202 void FormDialogView::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 FormDialogView::input(FL_OBJECT *, long)
216 return ButtonPolicy::SMI_VALID;
220 // preemptive handler for feedback messages
221 void FormDialogView::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 FormDialogView::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
254 // object 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 FormDialogView::postWarning(string const & warning)
295 warning_posted_ = true;
296 postMessage(warning);
300 void FormDialogView::clearMessage()
302 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 FormDialogView::postMessage(string const & message)
319 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 FormDialogView * GetForm(FL_OBJECT * ob)
341 Assert(ob && ob->form && ob->form->u_vdata);
342 FormDialogView * ptr =
343 static_cast<FormDialogView *>(ob->form->u_vdata);
352 void C_FormDialogView_ApplyCB(FL_OBJECT * ob, long)
354 GetForm(ob)->dialog().ApplyButton();
358 void C_FormDialogView_OKCB(FL_OBJECT * ob, long)
360 GetForm(ob)->dialog().OKButton();
364 void C_FormDialogView_CancelCB(FL_OBJECT * ob, long)
366 FormDialogView * form = GetForm(ob);
367 form->dialog().CancelButton();
371 void C_FormDialogView_RestoreCB(FL_OBJECT * ob, long)
373 GetForm(ob)->dialog().RestoreButton();
377 void C_FormDialogView_InputCB(FL_OBJECT * ob, long d)
379 GetForm(ob)->InputCB(ob, d);
383 static int C_WMHideCB(FL_FORM * form, void *)
385 // Close the dialog cleanly, even if the WM is used to do so.
386 Assert(form && form->u_vdata);
387 FormDialogView * ptr = static_cast<FormDialogView *>(form->u_vdata);
388 ptr->dialog().CancelButton();
392 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
393 FL_Coord, FL_Coord, int key, void *)
395 // Note that the return value is important in the pre-emptive handler.
396 // Don't return anything other than 0.
399 // Don't Assert this one, as it can happen quite naturally when things
400 // are being deleted in the d-tor.
402 if (!ob->form) return 0;
404 FormDialogView * ptr =
405 static_cast<FormDialogView *>(ob->form->u_vdata);
408 ptr->PrehandlerCB(ob, event, key);