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 // Callback function invoked by xforms when the dialog is closed by the
33 static int C_WMHideCB(FL_FORM * form, void *);
35 // Callback function invoked by the xforms pre- and post-handler routines
36 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
41 FormBase::FormBase(string const & t, bool allowResize)
42 : ViewBase(), minw_(0), minh_(0), allow_resize_(allowResize),
43 title_(t), icon_pixmap_(0), icon_mask_(0),
44 tooltips_(new Tooltips())
51 XFreePixmap(fl_get_display(), icon_pixmap_);
57 Tooltips & FormBase::tooltips()
63 void FormBase::redraw()
65 if (form() && form()->visible)
66 fl_redraw_form(form());
70 xformsBC & FormBase::bc()
72 return static_cast<xformsBC &>(getController().bc());
73 // return dynamic_cast<GUIbc &>(controller_ptr_->bc());
77 void FormBase::prepare_to_show()
79 double const scale = scale_to_fit_tabs(form());
81 scale_form(form(), scale);
85 // work around dumb xforms sizing bug
89 fl_set_form_atclose(form(), C_WMHideCB, 0);
91 // set the title for the minimized form
92 if (!getController().IconifyWithMain())
93 fl_winicontitle(form()->window, title_.c_str());
95 // assign an icon to the form
96 string const iconname = LibFileSearch("images", "lyx", "xpm");
97 if (!iconname.empty()) {
99 icon_pixmap_ = fl_read_pixmapfile(fl_root,
105 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
110 void FormBase::show()
112 // build() is/should be called from the controller, so form() should
116 // we use minw_ to flag whether the dialog has ever been shown.
117 // In turn, prepare_to_show() initialises various bits 'n' pieces
118 // (including minw_).
123 // make sure the form is up to date.
124 fl_freeze_form(form());
126 fl_unfreeze_form(form());
128 if (form()->visible) {
129 fl_raise_form(form());
130 /* This XMapWindow() will hopefully ensure that
131 * iconified dialogs are de-iconified. Mad props
132 * out to those crazy Xlib guys for forgetting a
133 * XDeiconifyWindow(). At least WindowMaker, when
134 * being notified of the redirected MapRequest will
135 * specifically de-iconify. From source, fvwm2 seems
138 XMapWindow(fl_get_display(), form()->window);
140 // calls to fl_set_form_minsize/maxsize apply only to the next
141 // fl_show_form(), so this comes first.
142 fl_set_form_minsize(form(), minw_, minh_);
144 fl_set_form_maxsize(form(), minw_, minh_);
146 string const maximize_title = "LyX: " + title_;
147 int const iconify_policy =
148 getController().IconifyWithMain() ? FL_TRANSIENT : 0;
151 FL_PLACE_MOUSE | FL_FREE_SIZE,
153 maximize_title.c_str());
156 // For some strange reason known only to xforms, the tooltips can only
157 // be set on a form that is already visible...
162 void FormBase::hide()
164 // xforms sometimes tries to process a hint-type MotionNotify, and
165 // use XQueryPointer, without verifying if the window still exists.
166 // So we try to clear out motion events in the queue before the
168 XSync(fl_get_display(), false);
170 if (form() && form()->visible)
171 fl_hide_form(form());
175 void FormBase::setPrehandler(FL_OBJECT * ob)
178 fl_set_object_prehandler(ob, C_PrehandlerCB);
182 void FormBase::InputCB(FL_OBJECT * ob, long data)
184 // It is possible to set the choice to 0 when using the
185 // keyboard shortcuts. This work-around deals with the problem.
186 if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
187 fl_set_choice(ob, 1);
190 bc().input(input(ob, data));
194 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
196 return ButtonPolicy::SMI_VALID;
202 FormBase * GetForm(FL_OBJECT * ob)
204 lyx::Assert(ob && ob->form && ob->form->u_vdata);
205 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
214 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
216 GetForm(ob)->getController().ApplyButton();
220 void C_FormBaseOKCB(FL_OBJECT * ob, long)
222 GetForm(ob)->getController().OKButton();
226 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
228 FormBase * form = GetForm(ob);
229 form->getController().CancelButton();
233 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
235 GetForm(ob)->getController().RestoreButton();
239 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
241 GetForm(ob)->InputCB(ob, d);
245 static int C_WMHideCB(FL_FORM * form, void *)
247 // Close the dialog cleanly, even if the WM is used to do so.
248 lyx::Assert(form && form->u_vdata);
249 FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
250 ptr->getController().CancelButton();
254 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
255 FL_Coord, FL_Coord, int key, void *)
257 // Note that the return value is important in the pre-emptive handler.
258 // Don't return anything other than 0.
261 // Don't Assert this one, as it can happen quite naturally when things
262 // are being deleted in the d-tor.
264 if (!ob->form) return 0;
266 FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
269 ptr->PrehandlerCB(ob, event, key);