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 = scale_to_fit_tabs(form());
86 scale_form(form(), scale);
90 // work around dumb xforms sizing bug
94 fl_set_form_atclose(form(), C_WMHideCB, 0);
96 // set the title for the minimized form
97 if (!getController().IconifyWithMain())
98 fl_winicontitle(form()->window, title_.c_str());
100 // assign an icon to the form
101 string const iconname = LibFileSearch("images", "lyx", "xpm");
102 if (!iconname.empty()) {
104 icon_pixmap_ = fl_read_pixmapfile(fl_root,
110 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
115 void FormBase::show()
117 // build() is/should be called from the controller, so form() should
121 // we use minw_ to flag whether the dialog has ever been shown.
122 // In turn, prepare_to_show() initialises various bits 'n' pieces
123 // (including minw_).
128 // make sure the form is up to date.
129 fl_freeze_form(form());
131 fl_unfreeze_form(form());
133 if (form()->visible) {
134 fl_raise_form(form());
135 /* This XMapWindow() will hopefully ensure that
136 * iconified dialogs are de-iconified. Mad props
137 * out to those crazy Xlib guys for forgetting a
138 * XDeiconifyWindow(). At least WindowMaker, when
139 * being notified of the redirected MapRequest will
140 * specifically de-iconify. From source, fvwm2 seems
143 XMapWindow(fl_get_display(), form()->window);
145 // calls to fl_set_form_minsize/maxsize apply only to the next
146 // fl_show_form(), so this comes first.
147 fl_set_form_minsize(form(), minw_, minh_);
149 fl_set_form_maxsize(form(), minw_, minh_);
151 string const maximize_title = "LyX: " + title_;
152 int const iconify_policy =
153 getController().IconifyWithMain() ? FL_TRANSIENT : 0;
156 FL_PLACE_MOUSE | FL_FREE_SIZE,
158 maximize_title.c_str());
161 // For some strange reason known only to xforms, the tooltips can only
162 // be set on a form that is already visible...
167 void FormBase::hide()
169 #if FL_VERSION > 0 || FL_REVISION >= 89
170 // Does no harm if none is visible and ensures that the tooltip form
171 // is hidden should the dialog be closed from the keyboard.
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 FormBase::setPrehandler(FL_OBJECT * ob)
189 fl_set_object_prehandler(ob, C_PrehandlerCB);
193 void FormBase::InputCB(FL_OBJECT * ob, long data)
195 // It is possible to set the choice to 0 when using the
196 // keyboard shortcuts. This work-around deals with the problem.
197 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
198 fl_set_choice(ob, 1);
201 bc().input(input(ob, data));
205 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
207 return ButtonPolicy::SMI_VALID;
213 FormBase * GetForm(FL_OBJECT * ob)
215 lyx::Assert(ob && ob->form && ob->form->u_vdata);
216 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
225 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
227 GetForm(ob)->getController().ApplyButton();
231 void C_FormBaseOKCB(FL_OBJECT * ob, long)
233 GetForm(ob)->getController().OKButton();
237 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
239 FormBase * form = GetForm(ob);
240 form->getController().CancelButton();
244 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
246 GetForm(ob)->getController().RestoreButton();
250 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
252 GetForm(ob)->InputCB(ob, d);
256 static int C_WMHideCB(FL_FORM * form, void *)
258 // Close the dialog cleanly, even if the WM is used to do so.
259 lyx::Assert(form && form->u_vdata);
260 FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
261 ptr->getController().CancelButton();
265 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
266 FL_Coord, FL_Coord, int key, void *)
268 // Note that the return value is important in the pre-emptive handler.
269 // Don't return anything other than 0.
272 // Don't Assert this one, as it can happen quite naturally when things
273 // are being deleted in the d-tor.
275 if (!ob->form) return 0;
277 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
280 ptr->PrehandlerCB(ob, event, key);