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