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"
33 using support::bformat;
34 using support::LibFileSearch;
40 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
41 // These should be in forms.h but aren't
42 void fl_show_tooltip(const char *, int, int);
43 void fl_hide_tooltip();
46 // Callback function invoked by xforms when the dialog is closed by the
48 static int C_WMHideCB(FL_FORM * form, void *);
50 // Callback function invoked by the xforms pre-handler routine.
51 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
56 FormDialogView::FormDialogView(Dialog & parent,
57 string const & t, bool allowResize)
58 : Dialog::View(parent, t),
59 warning_posted_(false), message_widget_(0),
60 minw_(0), minh_(0), allow_resize_(allowResize),
61 icon_pixmap_(0), icon_mask_(0),
62 tooltips_(new Tooltips)
66 FormDialogView::~FormDialogView()
69 XFreePixmap(fl_get_display(), icon_pixmap_);
75 bool FormDialogView::isVisible() const
77 return form() && form()->visible;
81 Tooltips & FormDialogView::tooltips()
87 void FormDialogView::redraw()
89 if (form() && form()->visible)
90 fl_redraw_form(form());
94 xformsBC & FormDialogView::bcview()
96 return static_cast<xformsBC &>(dialog().bc().view());
100 void FormDialogView::prepare_to_show()
102 double const scale = get_scale_to_fit(form());
104 scale_form_horizontally(form(), scale);
106 // work around dumb xforms sizing bug
110 fl_set_form_atclose(form(), C_WMHideCB, 0);
112 // set the title for the minimized form
113 if (!lyxrc.dialogs_iconify_with_main)
114 fl_winicontitle(form()->window, getTitle().c_str());
116 // assign an icon to the form
117 string const iconname = LibFileSearch("images", "lyx", "xpm");
118 if (!iconname.empty()) {
120 icon_pixmap_ = fl_read_pixmapfile(fl_root,
126 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
131 void FormDialogView::show()
137 // we use minw_ to flag whether the dialog has ever been shown.
138 // In turn, prepare_to_show() initialises various bits 'n' pieces
139 // (including minw_).
144 // make sure the form is up to date.
145 fl_freeze_form(form());
147 fl_unfreeze_form(form());
149 if (form()->visible) {
150 fl_raise_form(form());
151 /* This XMapWindow() will hopefully ensure that
152 * iconified dialogs are de-iconified. Mad props
153 * out to those crazy Xlib guys for forgetting a
154 * XDeiconifyWindow(). At least WindowMaker, when
155 * being notified of the redirected MapRequest will
156 * specifically de-iconify. From source, fvwm2 seems
159 XMapWindow(fl_get_display(), form()->window);
161 // calls to fl_set_form_minsize/maxsize apply only to the next
162 // fl_show_form(), so this comes first.
163 fl_set_form_minsize(form(), minw_, minh_);
165 fl_set_form_maxsize(form(), minw_, minh_);
167 string const maximize_title = "LyX: " + getTitle();
168 int const iconify_policy = lyxrc.dialogs_iconify_with_main ?
172 FL_PLACE_MOUSE | FL_FREE_SIZE,
174 maximize_title.c_str());
179 void FormDialogView::hide()
181 // xforms sometimes tries to process a hint-type MotionNotify, and
182 // use XQueryPointer, without verifying if the window still exists.
183 // So we try to clear out motion events in the queue before the
185 XSync(fl_get_display(), false);
187 if (form() && form()->visible)
188 fl_hide_form(form());
192 void FormDialogView::setPrehandler(FL_OBJECT * ob)
195 fl_set_object_prehandler(ob, C_PrehandlerCB);
199 void FormDialogView::setMessageWidget(FL_OBJECT * ob)
201 BOOST_ASSERT(ob && ob->objclass == FL_TEXT);
202 message_widget_ = ob;
203 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
207 void FormDialogView::InputCB(FL_OBJECT * ob, long data)
209 // It is possible to set the choice to 0 when using the
210 // keyboard shortcuts. This work-around deals with the problem.
211 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
212 fl_set_choice(ob, 1);
215 bc().input(input(ob, data));
219 ButtonPolicy::SMInput FormDialogView::input(FL_OBJECT *, long)
221 return ButtonPolicy::SMI_VALID;
225 // preemptive handler for feedback messages
226 void FormDialogView::MessageCB(FL_OBJECT * ob, int event)
233 string const feedback = getFeedback(ob);
234 if (feedback.empty() && warning_posted_)
237 warning_posted_ = false;
238 postMessage(getFeedback(ob));
243 if (!warning_posted_)
253 void FormDialogView::PrehandlerCB(FL_OBJECT * ob, int event, int key)
257 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
258 // Trigger an input event when pasting in an xforms input
259 // object using the middle mouse button.
264 if (message_widget_) {
268 // Post feedback as the mouse enters the object,
269 // remove it as the mouse leaves.
270 MessageCB(ob, event);
275 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
276 // Tooltips are not displayed on browser widgets due to an xforms' bug.
277 // This is a work-around:
278 if (ob->objclass == FL_BROWSER) {
281 if (ob->tooltip && *(ob->tooltip)) {
282 int const x = ob->form->x + ob->x;
283 int const y = ob->form->y + ob->y + ob->h + 1;
284 fl_show_tooltip(ob->tooltip, x, y);
298 void FormDialogView::postWarning(string const & warning)
300 warning_posted_ = true;
301 postMessage(warning);
305 void FormDialogView::clearMessage()
307 BOOST_ASSERT(message_widget_);
309 warning_posted_ = false;
311 string const existing = message_widget_->label
312 ? message_widget_->label : string();
313 if (existing.empty())
316 // This trick is needed to get xforms to clear the label...
317 fl_set_object_label(message_widget_, "");
318 fl_hide_object(message_widget_);
322 void FormDialogView::postMessage(string const & message)
324 BOOST_ASSERT(message_widget_);
326 int const width = message_widget_->w - 10;
327 string const tmp = warning_posted_ ?
328 bformat(_("WARNING! %1$s"), message) :
331 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 FormDialogView * GetForm(FL_OBJECT * ob)
346 BOOST_ASSERT(ob && ob->form && ob->form->u_vdata);
347 FormDialogView * ptr =
348 static_cast<FormDialogView *>(ob->form->u_vdata);
357 void C_FormDialogView_ApplyCB(FL_OBJECT * ob, long)
359 GetForm(ob)->dialog().ApplyButton();
363 void C_FormDialogView_OKCB(FL_OBJECT * ob, long)
365 GetForm(ob)->dialog().OKButton();
369 void C_FormDialogView_CancelCB(FL_OBJECT * ob, long)
371 FormDialogView * form = GetForm(ob);
372 form->dialog().CancelButton();
376 void C_FormDialogView_RestoreCB(FL_OBJECT * ob, long)
378 GetForm(ob)->dialog().RestoreButton();
382 void C_FormDialogView_InputCB(FL_OBJECT * ob, long d)
384 GetForm(ob)->InputCB(ob, d);
388 static int C_WMHideCB(FL_FORM * form, void *)
390 // Close the dialog cleanly, even if the WM is used to do so.
391 BOOST_ASSERT(form && form->u_vdata);
392 FormDialogView * ptr = static_cast<FormDialogView *>(form->u_vdata);
393 ptr->dialog().CancelButton();
397 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
398 FL_Coord, FL_Coord, int key, void *)
400 // Note that the return value is important in the pre-emptive handler.
401 // Don't return anything other than 0.
404 // Don't Assert this one, as it can happen quite naturally when things
405 // are being deleted in the d-tor.
406 //BOOST_ASSERT(ob->form);
407 if (!ob->form) return 0;
409 FormDialogView * ptr =
410 static_cast<FormDialogView *>(ob->form->u_vdata);
413 ptr->PrehandlerCB(ob, event, key);
420 } // namespace frontend