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