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