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 #pragma implementation
19 #include "ControlButtons.h"
21 #include "xforms_resize.h"
24 #include "support/LAssert.h"
25 #include "support/filetools.h" // LibFileSearch
27 #include FORMS_H_LOCATION
31 #if FL_VERSION > 0 || FL_REVISION >= 89
32 // This should be in forms.h but isn't
33 void fl_hide_tooltip();
36 // Callback function invoked by xforms when the dialog is closed by the
38 static int C_WMHideCB(FL_FORM * form, void *);
40 // Callback function invoked by the xforms pre- and post-handler routines
41 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
46 FormBase::FormBase(string const & t, bool allowResize)
47 : ViewBase(), minw_(0), minh_(0), allow_resize_(allowResize),
48 title_(t), icon_pixmap_(0), icon_mask_(0),
49 tooltips_(new Tooltips())
56 XFreePixmap(fl_get_display(), icon_pixmap_);
62 Tooltips & FormBase::tooltips()
68 void FormBase::redraw()
70 if (form() && form()->visible)
71 fl_redraw_form(form());
75 xformsBC & FormBase::bc()
77 return static_cast<xformsBC &>(getController().bc());
78 // return dynamic_cast<GUIbc &>(controller_ptr_->bc());
82 void FormBase::prepare_to_show()
84 double const scale = get_scale_to_fit(form());
86 scale_form_horizontally(form(), scale);
88 // work around dumb xforms sizing bug
92 fl_set_form_atclose(form(), C_WMHideCB, 0);
94 // set the title for the minimized form
95 if (!getController().IconifyWithMain())
96 fl_winicontitle(form()->window, title_.c_str());
98 // assign an icon to the form
99 string const iconname = LibFileSearch("images", "lyx", "xpm");
100 if (!iconname.empty()) {
102 icon_pixmap_ = fl_read_pixmapfile(fl_root,
108 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
113 void FormBase::show()
115 // build() is/should be called from the controller, so form() should
119 // we use minw_ to flag whether the dialog has ever been shown.
120 // In turn, prepare_to_show() initialises various bits 'n' pieces
121 // (including minw_).
126 // make sure the form is up to date.
127 fl_freeze_form(form());
129 fl_unfreeze_form(form());
131 if (form()->visible) {
132 fl_raise_form(form());
133 /* This XMapWindow() will hopefully ensure that
134 * iconified dialogs are de-iconified. Mad props
135 * out to those crazy Xlib guys for forgetting a
136 * XDeiconifyWindow(). At least WindowMaker, when
137 * being notified of the redirected MapRequest will
138 * specifically de-iconify. From source, fvwm2 seems
141 XMapWindow(fl_get_display(), form()->window);
143 // calls to fl_set_form_minsize/maxsize apply only to the next
144 // fl_show_form(), so this comes first.
145 fl_set_form_minsize(form(), minw_, minh_);
147 fl_set_form_maxsize(form(), minw_, minh_);
149 string const maximize_title = "LyX: " + title_;
150 int const iconify_policy =
151 getController().IconifyWithMain() ? FL_TRANSIENT : 0;
154 FL_PLACE_MOUSE | FL_FREE_SIZE,
156 maximize_title.c_str());
161 void FormBase::hide()
163 #if FL_VERSION > 0 || FL_REVISION >= 89
164 // Does no harm if none is visible and ensures that the tooltip form
165 // is hidden should the dialog be closed from the keyboard.
169 // xforms sometimes tries to process a hint-type MotionNotify, and
170 // use XQueryPointer, without verifying if the window still exists.
171 // So we try to clear out motion events in the queue before the
173 XSync(fl_get_display(), false);
175 if (form() && form()->visible)
176 fl_hide_form(form());
180 void FormBase::setPrehandler(FL_OBJECT * ob)
183 fl_set_object_prehandler(ob, C_PrehandlerCB);
187 void FormBase::InputCB(FL_OBJECT * ob, long data)
189 // It is possible to set the choice to 0 when using the
190 // keyboard shortcuts. This work-around deals with the problem.
191 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
192 fl_set_choice(ob, 1);
195 bc().input(input(ob, data));
199 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
201 return ButtonPolicy::SMI_VALID;
207 FormBase * GetForm(FL_OBJECT * ob)
209 lyx::Assert(ob && ob->form && ob->form->u_vdata);
210 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
219 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
221 GetForm(ob)->getController().ApplyButton();
225 void C_FormBaseOKCB(FL_OBJECT * ob, long)
227 GetForm(ob)->getController().OKButton();
231 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
233 FormBase * form = GetForm(ob);
234 form->getController().CancelButton();
238 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
240 GetForm(ob)->getController().RestoreButton();
244 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
246 GetForm(ob)->InputCB(ob, d);
250 static int C_WMHideCB(FL_FORM * form, void *)
252 // Close the dialog cleanly, even if the WM is used to do so.
253 lyx::Assert(form && form->u_vdata);
254 FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
255 ptr->getController().CancelButton();
259 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
260 FL_Coord, FL_Coord, int key, void *)
262 // Note that the return value is important in the pre-emptive handler.
263 // Don't return anything other than 0.
266 // Don't Assert this one, as it can happen quite naturally when things
267 // are being deleted in the d-tor.
269 if (!ob->form) return 0;
271 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
274 ptr->PrehandlerCB(ob, event, key);