]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBase.C
Fix leaking pixmap icon.
[lyx.git] / src / frontends / xforms / FormBase.C
1 /**
2  * \file FormBase.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming 
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "FormBase.h"
18
19 #include "ControlButtons.h"
20 #include "xformsBC.h"
21 #include "xforms_resize.h"
22 #include "Tooltips.h"
23
24 #include "support/LAssert.h"
25 #include "support/filetools.h" //  LibFileSearch
26
27 #include FORMS_H_LOCATION
28
29 extern "C" {
30
31 // Callback function invoked by xforms when the dialog is closed by the
32 // window manager
33 static int C_WMHideCB(FL_FORM * form, void *);
34
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 *);
37
38 } // extern "C"
39
40
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())
45 {}
46
47
48 FormBase::~FormBase()
49 {
50         if (icon_pixmap_)
51                 XFreePixmap(fl_get_display(), icon_pixmap_);
52
53         delete tooltips_;
54 }
55
56
57 Tooltips & FormBase::tooltips()
58 {
59         return *tooltips_;
60 }
61
62
63 void FormBase::redraw()
64 {
65         if (form() && form()->visible)
66                 fl_redraw_form(form());
67 }
68
69
70 xformsBC & FormBase::bc()
71 {
72         return static_cast<xformsBC &>(getController().bc());
73         // return dynamic_cast<GUIbc &>(controller_ptr_->bc());
74 }
75
76
77 void FormBase::prepare_to_show()
78 {
79         double const scale = scale_to_fit_tabs(form());
80         if (scale > 1.001)
81                 scale_form(form(), scale);
82
83         bc().refresh();
84
85         // work around dumb xforms sizing bug
86         minw_ = form()->w;
87         minh_ = form()->h;
88
89         fl_set_form_atclose(form(), C_WMHideCB, 0);
90
91         // set the title for the minimized form
92         if (!getController().IconifyWithMain())
93                 fl_winicontitle(form()->window, title_.c_str());
94
95         //  assign an icon to the form
96         string const iconname = LibFileSearch("images", "lyx", "xpm");
97         if (!iconname.empty()) {
98                 unsigned int w, h;
99                 icon_pixmap_ = fl_read_pixmapfile(fl_root,
100                                                   iconname.c_str(),
101                                                   &w,
102                                                   &h,
103                                                   &icon_mask_,
104                                                   0, 0, 0);
105                 fl_set_form_icon(form(), icon_pixmap_, icon_mask_);
106         }
107 }
108
109
110 void FormBase::show()
111 {
112         // build() is/should be called from the controller, so form() should
113         // always exist.
114         lyx::Assert(form());
115
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_).
119         if (minw_ == 0) {
120                 prepare_to_show();
121         }
122
123         // make sure the form is up to date.
124         fl_freeze_form(form());
125         update();
126         fl_unfreeze_form(form());
127
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
136                  * to do the same.
137                  */
138                 XMapWindow(fl_get_display(), form()->window);
139         } else {
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_);
143                 if (!allow_resize_)
144                         fl_set_form_maxsize(form(), minw_, minh_);
145
146                 string const maximize_title = "LyX: " + title_;
147                 int const iconify_policy =
148                         getController().IconifyWithMain() ? FL_TRANSIENT : 0;
149
150                 fl_show_form(form(),
151                              FL_PLACE_MOUSE | FL_FREE_SIZE,
152                              iconify_policy,
153                              maximize_title.c_str());
154         }
155
156         // For some strange reason known only to xforms, the tooltips can only
157         // be set on a form that is already visible...
158         tooltips().set();
159 }
160
161
162 void FormBase::hide()
163 {
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
167         // DestroyNotify
168         XSync(fl_get_display(), false);
169
170         if (form() && form()->visible)
171                 fl_hide_form(form());
172 }
173
174
175 void FormBase::setPrehandler(FL_OBJECT * ob)
176 {
177         lyx::Assert(ob);
178         fl_set_object_prehandler(ob, C_PrehandlerCB);
179 }
180
181
182 void FormBase::InputCB(FL_OBJECT * ob, long data)
183 {
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);
188         }
189
190         bc().input(input(ob, data));
191 }
192
193
194 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
195 {
196         return ButtonPolicy::SMI_VALID;
197 }
198
199
200 namespace {
201
202 FormBase * GetForm(FL_OBJECT * ob)
203 {
204         lyx::Assert(ob && ob->form && ob->form->u_vdata);
205         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
206         return ptr;
207 }
208
209 } // namespace anon
210
211
212 extern "C" {
213
214 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
215 {
216         GetForm(ob)->getController().ApplyButton();
217 }
218
219
220 void C_FormBaseOKCB(FL_OBJECT * ob, long)
221 {
222         GetForm(ob)->getController().OKButton();
223 }
224
225
226 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
227 {
228         FormBase * form = GetForm(ob);
229         form->getController().CancelButton();
230 }
231
232
233 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
234 {
235         GetForm(ob)->getController().RestoreButton();
236 }
237
238
239 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
240 {
241         GetForm(ob)->InputCB(ob, d);
242 }
243
244
245 static int C_WMHideCB(FL_FORM * form, void *)
246 {
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();
251         return FL_CANCEL;
252 }
253
254 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
255                           FL_Coord, FL_Coord, int key, void *)
256 {
257         // Note that the return value is important in the pre-emptive handler.
258         // Don't return anything other than 0.
259         lyx::Assert(ob);
260
261         // Don't Assert this one, as it can happen quite naturally when things
262         // are being deleted in the d-tor.
263         //Assert(ob->form);
264         if (!ob->form) return 0;
265
266         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
267
268         if (ptr)
269                 ptr->PrehandlerCB(ob, event, key);
270
271         return 0;
272 }
273
274 } // extern "C"