]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
This commit introduces Application_pimpl and cleanup the header includes of the affec...
[lyx.git] / src / frontends / LyXView.C
1 /**
2  * \file LyXView.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "LyXView.h"
15 #include "Dialogs.h"
16 #include "Timeout.h"
17 #include "Toolbars.h"
18 #include "Menubar.h"
19 #include "WorkArea.h"
20
21 #include "buffer.h"
22 #include "bufferparams.h"
23 #include "BufferView.h"
24 #include "bufferview_funcs.h"
25 #include "cursor.h"
26 #include "debug.h"
27 #include "errorlist.h"
28 #include "funcrequest.h"
29 #include "gettext.h"
30 #include "intl.h"
31 #include "lyx_cb.h"
32 #include "lyxfunc.h"
33 #include "lyxrc.h"
34 #include "lyxtext.h"
35 #include "MenuBackend.h"
36 #include "paragraph.h"
37
38 #include "controllers/ControlCommandBuffer.h"
39
40 #include "support/lstrings.h"
41 #include "support/filetools.h" // OnlyFilename()
42
43 #include <boost/bind.hpp>
44
45 #ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h>
47 #endif
48 #ifdef HAVE_UNISTD_H
49 # include <unistd.h>
50 #endif
51
52 using lyx::frontend::WorkArea;
53
54 using lyx::docstring;
55 using lyx::support::bformat;
56 using lyx::support::makeDisplayPath;
57 using lyx::support::onlyFilename;
58
59 using std::endl;
60 using std::string;
61
62 using lyx::frontend::ControlCommandBuffer;
63
64 string current_layout;
65
66
67 LyXView::LyXView()
68         : work_area_(0),
69           toolbars_(new Toolbars(*this)),
70           autosave_timeout_(new Timeout(5000)),
71           dialogs_(new Dialogs(*this)),
72           controlcommand_(new ControlCommandBuffer(*this))
73 {
74         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
75 }
76
77
78 LyXView::~LyXView()
79 {
80 }
81
82
83 void LyXView::setWorkArea(WorkArea * work_area)
84 {
85         work_area_ = work_area;
86 }
87
88
89 void LyXView::redrawWorkArea()
90 {
91         work_area_->redraw();
92         updateStatusBar();
93 }
94
95
96 WorkArea * LyXView::workArea()
97 {
98         return work_area_;
99 }
100
101
102 void LyXView::init()
103 {
104         updateLayoutChoice();
105         updateMenubar();
106
107         // Start autosave timer
108         if (lyxrc.autosave) {
109                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
110                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
111                 autosave_timeout_->start();
112         }
113 }
114
115
116 Buffer * LyXView::buffer() const
117 {
118         return work_area_->bufferView().buffer();
119 }
120
121
122 void LyXView::setBuffer(Buffer * b)
123 {
124         if (work_area_->bufferView().buffer())
125                 disconnectBuffer();
126
127         if (!b)
128                 getDialogs().hideBufferDependent();
129
130         work_area_->bufferView().setBuffer(b);
131
132         if (work_area_->bufferView().buffer()) {
133                 // Buffer-dependent dialogs should be updated or
134                 // hidden. This should go here because some dialogs (eg ToC)
135                 // require bv_->text.
136                 getDialogs().updateBufferDependent(true);
137                 connectBuffer(*work_area_->bufferView().buffer());
138         }
139
140         updateMenubar();
141         updateToolbars();
142         updateLayoutChoice();
143         updateWindowTitle();
144         redrawWorkArea();
145 }
146
147
148 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
149 {
150         if (work_area_->bufferView().buffer())
151                 disconnectBuffer();
152
153         bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
154
155         updateMenubar();
156         updateToolbars();
157         updateLayoutChoice();
158         updateWindowTitle();
159         if (loaded) {
160                 connectBuffer(*work_area_->bufferView().buffer());
161                 showErrorList("Parse");
162         }
163         redrawWorkArea();
164         return loaded;
165 }
166
167
168 void LyXView::connectBuffer(Buffer & buf)
169 {
170         if (errorsConnection_.connected())
171                 disconnectBuffer();
172
173         errorsConnection_ =
174                 buf.errors.connect(
175                         boost::bind(&LyXView::showErrorList, this, _1));
176
177         messageConnection_ =
178                 buf.message.connect(
179                         boost::bind(&LyXView::message, this, _1));
180
181         busyConnection_ =
182                 buf.busy.connect(
183                         boost::bind(&LyXView::busy, this, _1));
184
185         titleConnection_ =
186                 buf.updateTitles.connect(
187                         boost::bind(&LyXView::updateWindowTitle, this));
188
189         timerConnection_ =
190                 buf.resetAutosaveTimers.connect(
191                         boost::bind(&LyXView::resetAutosaveTimer, this));
192
193         readonlyConnection_ =
194                 buf.readonly.connect(
195                         boost::bind(&LyXView::showReadonly, this, _1));
196
197         closingConnection_ =
198                 buf.closing.connect(
199                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
200 }
201
202
203 void LyXView::disconnectBuffer()
204 {
205         messageConnection_.disconnect();
206         busyConnection_.disconnect();
207         titleConnection_.disconnect();
208         timerConnection_.disconnect();
209         readonlyConnection_.disconnect();
210         closingConnection_.disconnect();
211         layout_changed_connection_.disconnect();
212 }
213
214
215 void LyXView::connectBufferView(BufferView & bv)
216 {
217         show_dialog_connection_ = bv.showDialog.connect(
218                         boost::bind(&LyXView::showDialog, this, _1));
219         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
220                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
221         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
222                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
223         update_dialog_connection_ = bv.updateDialog.connect(
224                         boost::bind(&LyXView::updateDialog, this, _1, _2));
225         layout_changed_connection_ = bv.layoutChanged.connect(
226                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
227 }
228
229
230 void LyXView::disconnectBufferView()
231 {
232         show_dialog_connection_.disconnect();
233         show_dialog_with_data_connection_.disconnect();
234         show_inset_dialog_connection_.disconnect();
235         update_dialog_connection_.disconnect();
236 }
237
238
239 void LyXView::showErrorList(string const & error_type)
240 {
241         ErrorList & el = buffer()->errorList(error_type);
242         if (!el.empty()) {
243                 getDialogs().show("errorlist", error_type);
244         }
245 }
246
247
248 void LyXView::showDialog(string const & name)
249 {
250         getDialogs().show(name);
251 }
252
253
254 void LyXView::showDialogWithData(string const & name,
255                                                                  string const & data)
256 {
257         getDialogs().show(name, data);
258 }
259
260
261 void LyXView::showInsetDialog(string const & name, string const & data,
262                                                           InsetBase * inset)
263 {
264         getDialogs().show(name, data, 0);
265 }
266
267
268 void LyXView::updateDialog(string const & name, string const & data)
269 {
270         if (getDialogs().visible(name))
271                 getDialogs().update(name, data);
272 }
273
274
275 void LyXView::showReadonly(bool)
276 {
277         updateWindowTitle();
278         getDialogs().updateBufferDependent(false);
279 }
280
281
282 BufferView * LyXView::view() const
283 {
284         return &work_area_->bufferView();
285 }
286
287
288 void LyXView::updateToolbars()
289 {
290         bool const math = work_area_->bufferView().cursor().inMathed();
291         bool const table =
292                 getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
293         toolbars_->update(math, table);
294         // update redaonly status of open dialogs. This could also be in
295         // updateMenubar(), but since updateToolbars() and updateMenubar()
296         // are always called together it is only here.
297         getDialogs().checkStatus();
298 }
299
300
301 void LyXView::updateMenubar()
302 {
303         menubar_->update();
304 }
305
306
307 void LyXView::autoSave()
308 {
309         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
310
311         if (view()->buffer()) {
312                 ::autoSave(view());
313         }
314 }
315
316
317 void LyXView::resetAutosaveTimer()
318 {
319         if (lyxrc.autosave)
320                 autosave_timeout_->restart();
321 }
322
323
324 void LyXView::updateLayoutChoice()
325 {
326         // Don't show any layouts without a buffer
327         if (!view()->buffer()) {
328                 toolbars_->clearLayoutList();
329                 return;
330         }
331
332         // Update the layout display
333         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
334                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
335         }
336
337         if (work_area_->bufferView().cursor().inMathed())
338                 return;
339
340         string const & layout =
341                 work_area_->bufferView().cursor().paragraph().layout()->name();
342
343         if (layout != current_layout) {
344                 toolbars_->setLayout(layout);
345                 current_layout = layout;
346         }
347 }
348
349
350 void LyXView::updateWindowTitle()
351 {
352         static docstring last_title = lyx::from_ascii("LyX");
353         docstring maximize_title = lyx::from_ascii("LyX");
354         docstring minimize_title = lyx::from_ascii("LyX");
355
356         if (view()->buffer()) {
357                 string const cur_title = buffer()->fileName();
358                 if (!cur_title.empty()) {
359                         maximize_title += lyx::from_ascii(": ") + makeDisplayPath(cur_title, 30);
360                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
361                         if (!buffer()->isClean()) {
362                                 maximize_title += _(" (changed)");
363                                 minimize_title += lyx::char_type('*');
364                         }
365                         if (buffer()->isReadonly())
366                                 maximize_title += _(" (read only)");
367                 }
368         }
369
370         if (maximize_title != last_title) {
371                 setWindowTitle(maximize_title, minimize_title);
372                 last_title = maximize_title;
373         }
374 }
375
376
377 void LyXView::dispatch(FuncRequest const & cmd)
378 {
379         getLyXFunc().dispatch(cmd);
380 }
381
382
383 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
384 {
385         Buffer const * buffer_ptr = 0;
386         if (inset) {
387                 buffer_ptr = work_area_->bufferView().buffer();
388                 // No FitCursor:
389                 work_area_->bufferView().update(Update::Force);
390         }
391         return buffer_ptr;
392 }