]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBase.C
change some __GNUG_ to __GNUG__
[lyx.git] / src / frontends / xforms / FormBase.C
1 /**
2  * \file FormBase.C
3  * Copyright 2000-2001 The LyX Team.
4  * See the file COPYING.
5  *
6  * \author Angus Leeming, a.leeming@ic.ac.uk
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "Dialogs.h"
16 #include "FormBase.h"
17 #include "xformsBC.h"
18 #include "GUIRunTime.h"
19 #include "Tooltips.h"
20 #include "support/LAssert.h"
21
22 extern "C" {
23
24 // Callback function invoked by xforms when the dialog is closed by the
25 // window manager
26 static int C_WMHideCB(FL_FORM * form, void *);
27
28 // Callback function invoked by the xforms pre- and post-handler routines
29 static int C_PrehandlerCB(FL_OBJECT *, int, FL_Coord, FL_Coord, int, void *);
30
31 } // extern "C"
32
33
34 FormBase::FormBase(ControlButtons & c, string const & t, bool allowResize)
35         : ViewBC<xformsBC>(c), minw_(0), minh_(0), allow_resize_(allowResize),
36           title_(t), tooltips_(new Tooltips)
37 {}
38
39
40 FormBase::~FormBase()
41 {
42         delete tooltips_;
43 }
44
45
46 Tooltips & FormBase::tooltips()
47 {
48         return *tooltips_;
49 }
50
51
52 void FormBase::redraw()
53 {
54         if (form() && form()->visible)
55                 fl_redraw_form(form());
56 }
57
58
59 void FormBase::show()
60 {
61         if (!form()) {
62                 build();
63         }
64
65         // use minw_ to flag whether the dialog has ever been shown
66         // (Needed now that build() is/should be called from the controller)
67         if (minw_ == 0) {
68                 bc().refresh();
69
70                 // work around dumb xforms sizing bug
71                 minw_ = form()->w;
72                 minh_ = form()->h;
73
74                 fl_set_form_atclose(form(), C_WMHideCB, 0);
75         }
76
77         fl_freeze_form(form());
78         update();  // make sure its up-to-date
79         fl_unfreeze_form(form());
80
81         if (form()->visible) {
82                 fl_raise_form(form());
83                 /* This XMapWindow() will hopefully ensure that
84                  * iconified dialogs are de-iconified. Mad props
85                  * out to those crazy Xlib guys for forgetting a
86                  * XDeiconifyWindow(). At least WindowMaker, when
87                  * being notified of the redirected MapRequest will
88                  * specifically de-iconify. From source, fvwm2 seems
89                  * to do the same.
90                  */
91                 XMapWindow(fl_get_display(), form()->window);
92         } else {
93                 // calls to fl_set_form_minsize/maxsize apply only to the next
94                 // fl_show_form(), so this comes first.
95                 fl_set_form_minsize(form(), minw_, minh_);
96                 if (!allow_resize_)
97                         fl_set_form_maxsize(form(), minw_, minh_);
98
99                 fl_show_form(form(),
100                              FL_PLACE_MOUSE | FL_FREE_SIZE,
101                              (controller_.IconifyWithMain() ? FL_TRANSIENT : 0),
102                              title_.c_str());
103         }
104
105         tooltips().set();
106 }
107
108
109 void FormBase::hide()
110 {
111         // xforms sometimes tries to process a hint-type MotionNotify, and
112         // use XQueryPointer, without verifying if the window still exists.
113         // So we try to clear out motion events in the queue before the
114         // DestroyNotify
115         XSync(fl_get_display(), false);
116         GUIRunTime::processEvents();
117
118         if (form() && form()->visible)
119                 fl_hide_form(form());
120 }
121
122
123 void FormBase::setPrehandler(FL_OBJECT * ob)
124 {
125         lyx::Assert(ob);
126         fl_set_object_prehandler(ob, C_PrehandlerCB);
127 }
128
129
130 void FormBase::InputCB(FL_OBJECT * ob, long data)
131 {
132         // It is possible to set the choice to 0 when using the
133         // keyboard shortcuts. This work-around deals with the problem.
134         if (ob && ob->objclass == FL_CHOICE && fl_get_choice(ob) < 1) {
135                 fl_set_choice(ob, 1);
136         }
137
138         bc().input(input(ob, data));
139 }
140
141
142 ButtonPolicy::SMInput FormBase::input(FL_OBJECT *, long)
143 {
144         return ButtonPolicy::SMI_VALID;
145 }
146
147
148 namespace {
149
150 FormBase * GetForm(FL_OBJECT * ob)
151 {
152         lyx::Assert(ob && ob->form && ob->form->u_vdata);
153         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
154         return ptr;
155 }
156
157 } // namespace anon
158
159
160 extern "C" {
161
162 void C_FormBaseApplyCB(FL_OBJECT * ob, long)
163 {
164         GetForm(ob)->ApplyButton();
165 }
166
167
168 void C_FormBaseOKCB(FL_OBJECT * ob, long)
169 {
170         GetForm(ob)->OKButton();
171 }
172
173
174 void C_FormBaseCancelCB(FL_OBJECT * ob, long)
175 {
176         FormBase * form = GetForm(ob);
177         form->CancelButton();
178 }
179
180
181 void C_FormBaseRestoreCB(FL_OBJECT * ob, long)
182 {
183         GetForm(ob)->RestoreButton();
184 }
185
186
187 void C_FormBaseInputCB(FL_OBJECT * ob, long d)
188 {
189         GetForm(ob)->InputCB(ob, d);
190 }
191
192
193 static int C_WMHideCB(FL_FORM * form, void *)
194 {
195         // Close the dialog cleanly, even if the WM is used to do so.
196         lyx::Assert(form && form->u_vdata);
197         FormBase * ptr = static_cast<FormBase *>(form->u_vdata);
198         ptr->CancelButton();
199         return FL_CANCEL;
200 }
201
202 static int C_PrehandlerCB(FL_OBJECT * ob, int event,
203                           FL_Coord, FL_Coord, int key, void *)
204 {
205         // Note that the return value is important in the pre-emptive handler.
206         // Don't return anything other than 0.
207         lyx::Assert(ob);
208
209         // Don't Assert this one, as it can happen quite naturally when things
210         // are being deleted in the d-tor.
211         //Assert(ob->form);
212         if (!ob->form) return 0;
213
214         FormBase * ptr = static_cast<FormBase *>(ob->form->u_vdata);
215
216         if (ptr)
217                 ptr->PrehandlerCB(ob, event, key);
218
219         return 0;
220 }
221
222 } // extern "C"