]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.cpp
Streamlining CollapseStatus stuff
[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
16 #include "Dialogs.h"
17 #include "Toolbars.h"
18 #include "Menubar.h"
19 #include "WorkArea.h"
20 #include "Gui.h"
21
22 #include "Buffer.h"
23 #include "buffer_funcs.h"
24 #include "BufferList.h"
25 #include "BufferParams.h"
26 #include "BufferView.h"
27 #include "bufferview_funcs.h"
28 #include "Cursor.h"
29 #include "debug.h"
30 #include "ErrorList.h"
31 #include "FuncRequest.h"
32 #include "gettext.h"
33 #include "Intl.h"
34 #include "callback.h"
35 #include "LyX.h"
36 #include "LyXFunc.h"
37 #include "LyXRC.h"
38 #include "Text.h"
39 #include "MenuBackend.h"
40 #include "Paragraph.h"
41
42 #include "controllers/ControlCommandBuffer.h"
43
44 #include "support/lstrings.h"
45 #include "support/filetools.h" // OnlyFilename()
46 #include "support/Timeout.h"
47
48 #include <boost/bind.hpp>
49
50
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
53 #endif
54 #ifdef HAVE_UNISTD_H
55 # include <unistd.h>
56 #endif
57
58 using std::endl;
59 using std::string;
60
61 namespace lyx {
62
63 using support::bformat;
64 using support::FileName;
65 using support::makeDisplayPath;
66 using support::onlyFilename;
67
68 namespace frontend {
69
70 docstring current_layout;
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, bool child_document)
126 {
127         busy(true);
128
129         BOOST_ASSERT(work_area_);
130         Buffer * oldBuffer = work_area_->bufferView().buffer();
131         // parentfilename will be used in case when we switch to a child
132         // document (hence when child_document is true)
133         string parentfilename;
134         if (oldBuffer)
135                 parentfilename = oldBuffer->fileName();
136
137         if (!b && theBufferList().empty())
138                 getDialogs().hideBufferDependent();
139
140         Buffer * newBuffer = work_area_->bufferView().setBuffer(b);
141
142         if (newBuffer) {
143                 //Are we closing an oldBuffer which was a child document?
144                 if (!b && oldBuffer && oldBuffer->getMasterBuffer() != oldBuffer)
145                         // Update the labels and section numbering of its master Buffer.
146                         updateLabels(*oldBuffer->getMasterBuffer());
147                 //Are we opening a new child document?
148                 else if (child_document && newBuffer->getMasterBuffer() != oldBuffer) {
149                         // Set the parent name of the child document.
150                         // This makes insertion of citations and references in the child work,
151                         // when the target is in the parent or another child document.
152                         newBuffer->setParentName(parentfilename);
153                         // Update the labels and section numbering to the new master Buffer.
154                         updateLabels(*newBuffer->getMasterBuffer());
155                 }
156                 //Now that all the updating of the old buffer has been done, we can
157                 //connect the new buffer. Note that this will also disconnect the old 
158                 //buffer, if such there is.
159                 //FIXME Is it clear that this should go right here? Or should it go
160                 //earlier before the previous if (in which case we'd remove the "else")?
161                 connectBuffer(*newBuffer);
162
163                 /* FIXME: We need to rebuild the Toc dialog before the others even
164                 if it will be rebuilt again in the next line. This avoid a crash when
165                 other dialogs are rebuilt before the Toc dialog. The reason is
166                 that closing a Buffer triggers an update of all opened dialogs
167                 when dispatching LFUN_DIALOG_UPDATE (hence the patch).
168                 The path is as following:
169                         setBuffer() -> updateBufferDependent() -> RestoreButton() -> LFUN
170                 The problem here is that the Toc dialog has not been
171                 reconstructed (because it comes after in the list of dialogs). */
172                 updateToc();
173
174                 // Buffer-dependent dialogs should be updated or
175                 // hidden. This should go here because some dialogs (eg ToC)
176                 // require bv_->text.
177                 getDialogs().updateBufferDependent(true);
178         } else
179                 //Disconnect the old buffer...there's no new one.
180                 disconnectBuffer();
181
182         if (quitting)
183                 return;
184
185         updateMenubar();
186         updateToolbars();
187         updateLayoutChoice();
188         updateWindowTitle();
189         updateStatusBar();
190         updateTab();
191         busy(false);
192         work_area_->redraw();
193 }
194
195
196 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles,
197                 bool child_document, bool auto_open)
198 {
199         busy(true);
200
201         BOOST_ASSERT(work_area_);
202         string parentfilename;
203         Buffer * oldBuffer = work_area_->bufferView().buffer();
204         if (oldBuffer)
205                 parentfilename = oldBuffer->fileName();
206
207         bool alreadyLoaded = checkIfLoaded(filename);
208         Buffer * newBuffer = checkAndLoadLyXFile(filename);
209
210         if (!newBuffer) {
211                 message(_("Document not loaded."));
212                 updateStatusBar();
213                 busy(false);
214                 work_area_->redraw();
215                 return false;
216         }
217
218         if (child_document && newBuffer != oldBuffer) {
219                 // Set the parent name of the child document.
220                 // This makes insertion of citations and references in the child work,
221                 // when the target is in the parent or another child document.
222                 newBuffer->setParentName(parentfilename);
223                 message(bformat(_("Opening child document %1$s..."),
224                         makeDisplayPath(filename.absFilename())));
225         }
226
227         // Update the labels and section numbering.
228         updateLabels(*newBuffer->getMasterBuffer());
229
230         bool const parse_error = !newBuffer->errorList("Parse").empty();
231         bool const need_switch = parse_error || !auto_open;
232         if (need_switch) {
233                 setBuffer(newBuffer, child_document);
234                 if (!alreadyLoaded) {
235                         if (parse_error)
236                                 showErrorList("Parse");
237                         // scroll to the position when the file was last closed
238                         if (lyxrc.use_lastfilepos) {
239                                 pit_type pit;
240                                 pos_type pos;
241                                 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
242                                 // if successfully move to pit (returned par_id is not zero),
243                                 // update metrics and reset font
244                                 if (work_area_->bufferView().moveToPosition(pit, pos, 0, 0).get<1>()) {
245                                         if (work_area_->bufferView().fitCursor())
246                                                 work_area_->bufferView().updateMetrics(false);
247                                         newBuffer->text().setCurrentFont(work_area_->bufferView().cursor());
248                                         updateMenubar();
249                                         updateToolbars();
250                                         updateLayoutChoice();
251                                         updateStatusBar();
252                                         work_area_->redraw();
253                                 }
254                         }
255                 if (tolastfiles)
256                         LyX::ref().session().lastFiles().add(filename);
257                 }
258         }
259
260         busy(false);
261         return true;
262 }
263
264
265 void LyXView::connectBuffer(Buffer & buf)
266 {
267         if (errorsConnection_.connected())
268                 disconnectBuffer();
269
270         BOOST_ASSERT(work_area_);
271         bufferChangedConnection_ =
272                 buf.changed.connect(
273                         boost::bind(&WorkArea::redraw, work_area_));
274
275         bufferStructureChangedConnection_ =
276                 buf.getMasterBuffer()->structureChanged.connect(
277                         boost::bind(&LyXView::updateToc, this));
278
279         errorsConnection_ =
280                 buf.errors.connect(
281                         boost::bind(&LyXView::showErrorList, this, _1));
282
283         messageConnection_ =
284                 buf.message.connect(
285                         boost::bind(&LyXView::message, this, _1));
286
287         busyConnection_ =
288                 buf.busy.connect(
289                         boost::bind(&LyXView::busy, this, _1));
290
291         titleConnection_ =
292                 buf.updateTitles.connect(
293                         boost::bind(&LyXView::updateWindowTitle, this));
294
295         timerConnection_ =
296                 buf.resetAutosaveTimers.connect(
297                         boost::bind(&LyXView::resetAutosaveTimer, this));
298
299         readonlyConnection_ =
300                 buf.readonly.connect(
301                         boost::bind(&LyXView::showReadonly, this, _1));
302
303         closingConnection_ =
304                 buf.closing.connect(
305                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0, false));
306 }
307
308
309 void LyXView::disconnectBuffer()
310 {
311         errorsConnection_.disconnect();
312         bufferChangedConnection_.disconnect();
313         bufferStructureChangedConnection_.disconnect();
314         messageConnection_.disconnect();
315         busyConnection_.disconnect();
316         titleConnection_.disconnect();
317         timerConnection_.disconnect();
318         readonlyConnection_.disconnect();
319         closingConnection_.disconnect();
320         layout_changed_connection_.disconnect();
321 }
322
323
324 void LyXView::connectBufferView(BufferView & bv)
325 {
326         show_dialog_connection_ = bv.showDialog.connect(
327                         boost::bind(&LyXView::showDialog, this, _1));
328         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
329                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
330         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
331                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
332         update_dialog_connection_ = bv.updateDialog.connect(
333                         boost::bind(&LyXView::updateDialog, this, _1, _2));
334         layout_changed_connection_ = bv.layoutChanged.connect(
335                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
336 }
337
338
339 void LyXView::disconnectBufferView()
340 {
341         show_dialog_connection_.disconnect();
342         show_dialog_with_data_connection_.disconnect();
343         show_inset_dialog_connection_.disconnect();
344         update_dialog_connection_.disconnect();
345 }
346
347
348 void LyXView::showErrorList(string const & error_type)
349 {
350         ErrorList & el = buffer()->errorList(error_type);
351         if (!el.empty()) {
352                 getDialogs().show("errorlist", error_type);
353         }
354 }
355
356
357 void LyXView::showDialog(string const & name)
358 {
359         getDialogs().show(name);
360 }
361
362
363 void LyXView::showDialogWithData(string const & name, string const & data)
364 {
365         getDialogs().show(name, data);
366 }
367
368
369 void LyXView::showInsetDialog(string const & name, string const & data,
370                 Inset * inset)
371 {
372         getDialogs().show(name, data, inset);
373 }
374
375
376 void LyXView::updateDialog(string const & name, string const & data)
377 {
378         if (getDialogs().visible(name))
379                 getDialogs().update(name, data);
380 }
381
382
383 void LyXView::showReadonly(bool)
384 {
385         updateWindowTitle();
386         getDialogs().updateBufferDependent(false);
387 }
388
389
390 BufferView * LyXView::view() const
391 {
392         BOOST_ASSERT(work_area_);
393         return &work_area_->bufferView();
394 }
395
396
397 void LyXView::updateToc()
398 {
399         updateDialog("toc", "");
400 }
401
402
403 void LyXView::updateToolbars()
404 {
405         BOOST_ASSERT(work_area_);
406         bool const math =
407                 work_area_->bufferView().cursor().inMathed();
408         bool const table =
409                 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
410         bool const review =
411                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
412                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
413
414         toolbars_->update(math, table, review);
415         // update redaonly status of open dialogs. This could also be in
416         // updateMenubar(), but since updateToolbars() and updateMenubar()
417         // are always called together it is only here.
418         getDialogs().checkStatus();
419 }
420
421
422 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
423 {
424         return toolbars_->getToolbarInfo(name);
425 }
426
427
428 void LyXView::toggleToolbarState(string const & name, bool allowauto)
429 {
430         // it is possible to get current toolbar status like this,...
431         // but I decide to obey the order of ToolbarBackend::flags
432         // and disregard real toolbar status.
433         // toolbars_->saveToolbarInfo();
434         //
435         // toggle state on/off/auto
436         toolbars_->toggleToolbarState(name, allowauto);
437         // update toolbar
438         updateToolbars();
439 }
440
441
442 void LyXView::updateMenubar()
443 {
444         menubar_->update();
445 }
446
447
448 void LyXView::autoSave()
449 {
450         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
451
452         if (view()->buffer())
453                 lyx::autoSave(view());
454 }
455
456
457 void LyXView::resetAutosaveTimer()
458 {
459         if (lyxrc.autosave)
460                 autosave_timeout_->restart();
461 }
462
463
464 void LyXView::updateLayoutChoice()
465 {
466         // Don't show any layouts without a buffer
467         if (!view()->buffer()) {
468                 toolbars_->clearLayoutList();
469                 return;
470         }
471
472         // Update the layout display
473         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
474                 current_layout = buffer()->params().getTextClass().defaultLayoutName();
475         }
476
477         BOOST_ASSERT(work_area_);
478         docstring const & layout = work_area_->bufferView().cursor().
479                 innerParagraph().layout()->name();
480
481         if (layout != current_layout) {
482                 toolbars_->setLayout(layout);
483                 current_layout = layout;
484         }
485 }
486
487
488 void LyXView::updateWindowTitle()
489 {
490         docstring maximize_title = lyx::from_ascii("LyX");
491         docstring minimize_title = lyx::from_ascii("LyX");
492
493         if (view()->buffer()) {
494                 string const cur_title = buffer()->fileName();
495                 if (!cur_title.empty()) {
496                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
497                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
498                         if (!buffer()->isClean()) {
499                                 maximize_title += _(" (changed)");
500                                 minimize_title += lyx::char_type('*');
501                         }
502                         if (buffer()->isReadonly())
503                                 maximize_title += _(" (read only)");
504                 }
505         }
506
507         setWindowTitle(maximize_title, minimize_title);
508         updateTab();
509 }
510
511
512 void LyXView::dispatch(FuncRequest const & cmd)
513 {
514         theLyXFunc().setLyXView(this);
515         lyx::dispatch(cmd);
516 }
517
518
519 Buffer const * const LyXView::updateInset(Inset const * inset) const
520 {
521         Buffer const * buffer_ptr = 0;
522         if (inset) {
523                 BOOST_ASSERT(work_area_);
524                 work_area_->scheduleRedraw();
525
526                 buffer_ptr = work_area_->bufferView().buffer();
527         }
528         return buffer_ptr;
529 }
530
531 } // namespace frontend
532 } // namespace lyx