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