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