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