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());
148 if (dialog().controller().exitEarly())
151 if (form()->visible) {
152 fl_raise_form(form());
153 /* This XMapWindow() will hopefully ensure that
154 * iconified dialogs are de-iconified. Mad props
155 * out to those crazy Xlib guys for forgetting a
156 * XDeiconifyWindow(). At least WindowMaker, when
157 * being notified of the redirected MapRequest will
158 * specifically de-iconify. From source, fvwm2 seems
161 XMapWindow(fl_get_display(), form()->window);
163 // calls to fl_set_form_minsize/maxsize apply only to the next
164 // fl_show_form(), so this comes first.
165 fl_set_form_minsize(form(), minw_, minh_);
167 fl_set_form_maxsize(form(), minw_, minh_);
169 string const maximize_title = "LyX: " + getTitle();
170 int const iconify_policy = lyxrc.dialogs_iconify_with_main ?
174 FL_PLACE_MOUSE | FL_FREE_SIZE,
176 maximize_title.c_str());
181 void FormDialogView::hide()
183 // xforms sometimes tries to process a hint-type MotionNotify, and
184 // use XQueryPointer, without verifying if the window still exists.
185 // So we try to clear out motion events in the queue before the
187 XSync(fl_get_display(), false);
189 if (form() && form()->visible)
190 fl_hide_form(form());
194 void FormDialogView::setPrehandler(FL_OBJECT * ob)
197 fl_set_object_prehandler(ob, C_PrehandlerCB);
201 void FormDialogView::setMessageWidget(FL_OBJECT * ob)
203 BOOST_ASSERT(ob && ob->objclass == FL_TEXT);
204 message_widget_ = ob;
205 fl_set_object_lsize(message_widget_, FL_NORMAL_SIZE);
209 void FormDialogView::InputCB(FL_OBJECT * ob, long data)
211 // It is possible to set the choice to 0 when using the
212 // keyboard shortcuts. This work-around deals with the problem.
213 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
214 fl_set_choice(ob, 1);
217 bc().input(input(ob, data));
221 ButtonPolicy::SMInput FormDialogView::input(FL_OBJECT *, long)
223 return ButtonPolicy::SMI_VALID;
227 // preemptive handler for feedback messages
228 void FormDialogView::MessageCB(FL_OBJECT * ob, int event)
235 string const feedback = getFeedback(ob);
236 if (feedback.empty() && warning_posted_)
239 warning_posted_ = false;
240 postMessage(getFeedback(ob));
245 if (!warning_posted_)
255 void FormDialogView::PrehandlerCB(FL_OBJECT * ob, int event, int key)
259 if (ob->objclass == FL_INPUT && event == FL_PUSH && key == 2) {
260 // Trigger an input event when pasting in an xforms input
261 // object using the middle mouse button.
266 if (message_widget_) {
270 // Post feedback as the mouse enters the object,
271 // remove it as the mouse leaves.
272 MessageCB(ob, event);
277 #if FL_VERSION == 0 || (FL_REVISION == 0 && FL_FIXLEVEL == 0)
278 // Tooltips are not displayed on browser widgets due to an xforms' bug.
279 // This is a work-around:
280 if (ob->objclass == FL_BROWSER) {
283 if (ob->tooltip && *(ob->tooltip)) {
284 int const x = ob->form->x + ob->x;
285 int const y = ob->form->y + ob->y + ob->h + 1;
286 fl_show_tooltip(ob->tooltip, x, y);
300 void FormDialogView::postWarning(string const & warning)
302 warning_posted_ = true;
303 postMessage(warning);
307 void FormDialogView::clearMessage()
309 BOOST_ASSERT(message_widget_);
311 warning_posted_ = false;
313 string const existing = message_widget_->label
314 ? message_widget_->label : string();
315 if (existing.empty())
318 // This trick is needed to get xforms to clear the label...
319 fl_set_object_label(message_widget_, "");
320 fl_hide_object(message_widget_);
324 void FormDialogView::postMessage(string const & message)
326 BOOST_ASSERT(message_widget_);
328 int const width = message_widget_->w - 10;
329 string const tmp = warning_posted_ ?
330 bformat(_("WARNING! %1$s"), message) :
333 string const str = formatted(tmp, width, FL_NORMAL_SIZE);
335 fl_set_object_label(message_widget_, str.c_str());
336 FL_COLOR const label_color = warning_posted_ ? FL_RED : FL_LCOL;
337 fl_set_object_lcol(message_widget_, label_color);
339 if (!message_widget_->visible)
340 fl_show_object(message_widget_);
346 FormDialogView * GetForm(FL_OBJECT * ob)
348 BOOST_ASSERT(ob && ob->form && ob->form->u_vdata);
349 FormDialogView * ptr =
350 static_cast<FormDialogView *>(ob->form->u_vdata);
359 void C_FormDialogView_ApplyCB(FL_OBJECT * ob, long)
361 GetForm(ob)->dialog().ApplyButton();
365 void C_FormDialogView_OKCB(FL_OBJECT * ob, long)
367 GetForm(ob)->dialog().OKButton();
371 void C_FormDialogView_CancelCB(FL_OBJECT * ob, long)
373 FormDialogView * form = GetForm(ob);
374 form->dialog().CancelButton();
378 void C_FormDialogView_RestoreCB(FL_OBJECT * ob, long)
380 GetForm(ob)->dialog().RestoreButton();
384 void C_FormDialogView_InputCB(FL_OBJECT * ob, long d)
386 GetForm(ob)->InputCB(ob, d);
390 static int C_WMHideCB(FL_FORM * form, void *)
392 // Close the dialog cleanly, even if the WM is used to do so.
393 BOOST_ASSERT(form && form->u_vdata);
394 FormDialogView * ptr = static_cast<FormDialogView *>(form->u_vdata);
395 ptr->dialog().CancelButton();
399 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
400 FL_Coord, FL_Coord, int key, void *)
402 // Note that the return value is important in the pre-emptive handler.
403 // Don't return anything other than 0.
406 // Don't Assert this one, as it can happen quite naturally when things
407 // are being deleted in the d-tor.
408 //BOOST_ASSERT(ob->form);
409 if (!ob->form) return 0;
411 FormDialogView * ptr =
412 static_cast<FormDialogView *>(ob->form->u_vdata);
415 ptr->PrehandlerCB(ob, event, key);
422 } // namespace frontend