]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/FormBase.C
fix a couple of hard crashes, constify local variables, whitespace changes, some...
[lyx.git] / src / frontends / xforms / FormBase.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  *
5  *           LyX, The Document Processor
6  *
7  *           Copyright 2000 The LyX Team.
8  *
9  * ======================================================
10  */
11
12 #include <config.h>
13
14 #include FORMS_H_LOCATION
15
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19
20 #include "Dialogs.h"
21 #include "FormBase.h"
22 #include "LyXView.h"
23 #include "xform_macros.h"
24 #include "support/LAssert.h"
25 //#include "debug.h"
26
27 // The current scheme muddles debugging.
28 // Can we please use some other means to create these functions?
29 // I really don't like to use the preprossessor for this.
30 // My suggestion: First of all move these functions into their own
31 // file (that can be included here if wanted, and use m4 to expand
32 // that file. So create a m4 function to do the expansion, a file
33 // that contains the calls to to this function and a script to run
34 // it and create the C++ file with the expanded functions. (Lgb)
35 // Possible startoff point:
36 // define([C_RETURNCB],[extern "C" int C_$1$2(FL_FORM * ob, void * d) { return $1::$2(ob, d); }])
37
38 C_RETURNCB (FormBase, WMHideCB)
39 C_GENERICCB(FormBase, ApplyCB)
40 C_GENERICCB(FormBase, OKCB)
41 C_GENERICCB(FormBase, CancelCB)
42 C_GENERICCB(FormBase, InputCB)
43 C_GENERICCB(FormBase, RestoreCB)
44
45
46 FormBase::FormBase(LyXView * lv, Dialogs * d, string const & t,
47                    ButtonPolicy * bp, char const * close, char const * cancel)
48         : lv_(lv), bc_(bp, cancel, close), d_(d), h_(0), title(t), bp_(bp),
49           minw_(0), minh_(0)
50 {
51         Assert(lv && d && bp);
52         Dialogs::redrawGUI.connect(slot(this, &FormBase::redraw));
53 }
54
55
56 FormBase::~FormBase()
57 {
58         //lyxerr << "bp_[" << bp_ << "]" << endl;
59         delete bp_;
60 }
61
62
63 void FormBase::redraw()
64 {
65         if (form() && form()->visible)
66                 fl_redraw_form(form());
67 }
68
69
70 void FormBase::connect()
71 {
72         fl_set_form_minsize(form(), minw_, minh_);
73 }
74
75
76 void FormBase::show()
77 {
78         if (!form()) {
79                 build();
80                 fl_set_form_atclose(form(),
81                                     C_FormBaseWMHideCB, 0);
82         }
83
84         fl_freeze_form(form());
85         update();  // make sure its up-to-date
86         fl_unfreeze_form(form());
87
88         if (form()->visible) {
89                 fl_raise_form(form());
90         } else {
91                 // calls to fl_set_form_minsize/maxsize apply only to the next
92                 // fl_show_form(), so connect() comes first.
93                 connect();
94                 fl_show_form(form(),
95                              FL_PLACE_MOUSE | FL_FREE_SIZE,
96                              FL_TRANSIENT,
97                              title.c_str());
98         }
99 }
100
101
102 void FormBase::hide()
103 {
104         if (form() && form()->visible) {
105                 // some dialogs might do things to the form first
106                 // such as the nested tabfolder problem in Preferences
107                 disconnect();
108                 fl_hide_form(form());
109         }
110 }
111
112
113 int FormBase::WMHideCB(FL_FORM * form, void *)
114 {
115         Assert(form);
116         // Ensure that the signals (u and h) are disconnected even if the
117         // window manager is used to close the dialog.
118         FormBase * pre = static_cast<FormBase*>(form->u_vdata);
119         Assert(pre);
120         pre->hide();
121         pre->bc_.hide();
122         return FL_CANCEL;
123 }
124
125
126 void FormBase::ApplyCB(FL_OBJECT * ob, long)
127 {
128         Assert(ob && ob->form);
129         FormBase * pre = static_cast<FormBase*>(ob->form->u_vdata);
130         Assert(pre);
131         pre->apply();
132         pre->bc_.apply();
133 }
134
135
136 void FormBase::OKCB(FL_OBJECT * ob, long)
137 {
138         Assert(ob && ob->form);
139         FormBase * pre = static_cast<FormBase*>(ob->form->u_vdata);
140         Assert(pre);
141         pre->ok();
142         pre->bc_.ok();
143 }
144
145
146 void FormBase::CancelCB(FL_OBJECT * ob, long)
147 {
148         Assert(ob && ob->form);
149         FormBase * pre = static_cast<FormBase*>(ob->form->u_vdata);
150         Assert(pre);
151         pre->cancel();
152         pre->bc_.cancel();
153 }
154
155
156 void FormBase::InputCB(FL_OBJECT * ob, long data)
157 {
158         Assert(ob && ob->form);
159         FormBase * pre = static_cast<FormBase*>(ob->form->u_vdata);
160         Assert(ob);
161         pre->bc_.valid(pre->input(ob, data));
162 }
163
164
165 void FormBase::RestoreCB(FL_OBJECT * ob, long)
166 {
167         Assert(ob && ob->form);
168         FormBase * pre = static_cast<FormBase*>(ob->form->u_vdata);
169         Assert(ob);
170         pre->restore();
171         pre->bc_.undoAll();
172 }
173
174
175 FormBaseBI::FormBaseBI(LyXView * lv, Dialogs * d, string const & t,
176                        ButtonPolicy * bp,
177                        char const * close, char const * cancel)
178         : FormBase(lv, d, t, bp, close, cancel)
179 {}
180
181
182 void FormBaseBI::connect()
183 {
184         h_ = d_->hideAll.connect(slot(this, &FormBaseBI::hide));
185         FormBase::connect();
186 }
187
188
189 void FormBaseBI::disconnect()
190 {
191         h_.disconnect();
192 }
193
194
195 FormBaseBD::FormBaseBD(LyXView * lv, Dialogs * d, string const & t,
196                        ButtonPolicy * bp,
197                        char const * close, char const * cancel)
198         : FormBase(lv, d, t, bp, close, cancel),
199           u_(0)
200 {}
201
202
203 void FormBaseBD::connect()
204 {
205         u_ = d_->updateBufferDependent.
206                  connect(slot(this, &FormBaseBD::updateSlot));
207         h_ = d_->hideBufferDependent.
208                  connect(slot(this, &FormBaseBD::hide));
209         FormBase::connect();
210 }
211
212
213 void FormBaseBD::disconnect()
214 {
215         u_.disconnect();
216         h_.disconnect();
217 }