]> git.lyx.org Git - lyx.git/blob - src/frontends/controllers/Dialog.C
6c0aee8d2cbad378cd92b2d35ab03d8b7caf9515
[lyx.git] / src / frontends / controllers / Dialog.C
1 /**
2  * \file Dialog.cpp
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 #include "Dialog.h"
14
15 #include "ButtonController.h"
16 #include "BCView.h"
17
18 #include "frontends/LyXView.h"
19
20 #include "funcrequest.h"
21 #include "FuncStatus.h"
22 #include "lyxfunc.h"
23
24 using lyx::docstring;
25
26 using std::string;
27
28 namespace lyx {
29 namespace frontend {
30
31 Dialog::Dialog(LyXView & lv, string const & name)
32         : is_closing_(false), kernel_(lv), name_(name),
33           bc_ptr_(new ButtonController)
34 {}
35
36
37 Dialog::~Dialog()
38 {}
39
40
41 void Dialog::ApplyButton()
42 {
43         apply();
44         bc().apply();
45 }
46
47
48 void Dialog::OKButton()
49 {
50         is_closing_ = true;
51         apply();
52         is_closing_ = false;
53         hide();
54         bc().ok();
55 }
56
57
58 void Dialog::CancelButton()
59 {
60         hide();
61         bc().cancel();
62 }
63
64
65 void Dialog::RestoreButton()
66 {
67         // Tell the kernel that a request to refresh the dialog's contents
68         // has been received. It's up to the kernel to supply the necessary
69         // info by calling Dialog::update().
70         kernel().updateDialog(name_);
71         bc().restore();
72 }
73
74
75 void Dialog::show(string const & data)
76 {
77         if (controller().isBufferDependent() && !kernel().isBufferAvailable())
78                 return;
79
80         if (!controller().initialiseParams(data)) {
81                 lyxerr << "Dialog \"" << name_
82                        << "\" failed to translate the data "
83                         "string passed to show()" << std::endl;
84                 return;
85         }
86
87         bc().readOnly(kernel().isBufferReadonly());
88         view().show();
89
90         // The widgets may not be valid, so refresh the button controller
91         bc().refresh();
92 }
93
94
95 void Dialog::update(string const & data)
96 {
97         if (controller().isBufferDependent() && !kernel().isBufferAvailable())
98                 return;
99
100         if (!controller().initialiseParams(data)) {
101                 lyxerr << "Dialog \"" << name_
102                        << "\" could not be initialized" << std::endl;
103                 return;
104         }
105
106         bc().readOnly(kernel().isBufferReadonly());
107         view().update();
108
109         // The widgets may not be valid, so refresh the button controller
110         bc().refresh();
111 }
112
113
114 void Dialog::hide()
115 {
116         if (!view().isVisible())
117                 return;
118
119         controller().clearParams();
120         view().hide();
121         kernel().disconnect(name());
122 }
123
124
125 void Dialog::apply()
126 {
127         if (controller().isBufferDependent()) {
128                 if (!kernel().isBufferAvailable() ||
129                     kernel().isBufferReadonly())
130                         return;
131         }
132
133         view().apply();
134         controller().dispatchParams();
135
136         if (controller().disconnectOnApply() && !is_closing_) {
137                 kernel().disconnect(name());
138                 controller().initialiseParams(string());
139                 view().update();
140         }
141 }
142
143
144 bool Dialog::isVisible() const
145 {
146         return view().isVisible();
147 }
148
149
150 void Dialog::redraw()
151 {
152         view().redraw();
153 }
154
155
156 ButtonController & Dialog::bc() const
157 {
158         BOOST_ASSERT(bc_ptr_.get());
159         return *bc_ptr_.get();
160 }
161
162
163 void Dialog::setController(Controller * i)
164 {
165         BOOST_ASSERT(i && !controller_ptr_.get());
166         controller_ptr_.reset(i);
167 }
168
169
170 void Dialog::setView(View * v)
171 {
172         BOOST_ASSERT(v && !view_ptr_.get());
173         view_ptr_.reset(v);
174 }
175
176
177 void Dialog::checkStatus()
178 {
179         // buffer independant dialogs are always active.
180         // This check allows us leave canApply unimplemented for some dialogs.
181         if (!controller().isBufferDependent())
182                 return;
183
184         // deactivate the dialog if we have no buffer
185         if (!kernel().isBufferAvailable()) {
186                 bc().readOnly(true);
187                 return;
188         }
189
190         // check whether this dialog may be active
191         if (controller().canApply()) {
192                 bool const readonly = kernel().isBufferReadonly();
193                 bc().readOnly(readonly);
194                 // refreshReadOnly() is too generous in _enabling_ widgets
195                 // update dialog to disable disabled widgets again
196                 if (!readonly)
197                         view().update();
198         } else
199                 bc().readOnly(true);
200 }
201
202
203 Dialog::Controller::Controller(Dialog & parent)
204         : parent_(parent)
205 {}
206
207
208 bool Dialog::Controller::canApply() const
209 {
210         FuncRequest const fr(getLfun(), dialog().name());
211         FuncStatus const fs(lyx::getStatus(fr));
212         return fs.enabled();
213 }
214
215
216 Dialog::Controller & Dialog::controller() const
217 {
218         BOOST_ASSERT(controller_ptr_.get());
219         return *controller_ptr_.get();
220 }
221
222
223 Dialog::View::View(Dialog & parent, docstring title) :
224         p_(parent), title_(title)
225 {}
226
227
228 Dialog::View & Dialog::view() const
229 {
230         BOOST_ASSERT(view_ptr_.get());
231         return *view_ptr_.get();
232 }
233
234
235 void Dialog::View::setTitle(docstring const & newtitle)
236 {
237         title_ = newtitle;
238 }
239
240
241 docstring const & Dialog::View::getTitle() const
242 {
243         return title_;
244 }
245
246
247 void Dialog::View::partialUpdate(int)
248 {}
249
250 } // namespace frontend
251 } // namespace lyx