]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.cpp
Polish the Toc and labels updating when loading a child document.
[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 "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
47 #include <boost/bind.hpp>
48
49
50 namespace lyx {
51
52 #ifdef HAVE_SYS_TIME_H
53 # include <sys/time.h>
54 #endif
55 #ifdef HAVE_UNISTD_H
56 # include <unistd.h>
57 #endif
58
59 using frontend::WorkArea;
60
61 using support::bformat;
62 using support::FileName;
63 using support::makeDisplayPath;
64 using support::onlyFilename;
65
66 using std::endl;
67 using std::string;
68
69 using lyx::frontend::ControlCommandBuffer;
70
71 string current_layout;
72
73
74 LyXView::LyXView(int id)
75         : work_area_(0),
76           toolbars_(new Toolbars(*this)),
77           autosave_timeout_(new Timeout(5000)),
78           dialogs_(new Dialogs(*this)),
79           controlcommand_(new ControlCommandBuffer(*this)), id_(id)
80 {
81         // Start autosave timer
82         if (lyxrc.autosave) {
83                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
84                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
85                 autosave_timeout_->start();
86         }
87 }
88
89
90 LyXView::~LyXView()
91 {
92         disconnectBuffer();
93 }
94
95
96 // FIXME, there's only one WorkArea per LyXView possible for now.
97 void LyXView::setWorkArea(WorkArea * work_area)
98 {
99         BOOST_ASSERT(work_area);
100         work_area_ = work_area;
101         work_area_ids_.clear();
102         work_area_ids_.push_back(work_area_->id());
103 }
104
105
106 // FIXME, there's only one WorkArea per LyXView possible for now.
107 WorkArea const * LyXView::currentWorkArea() const
108 {
109         return work_area_;
110 }
111
112
113 // FIXME, there's only one WorkArea per LyXView possible for now.
114 WorkArea * LyXView::currentWorkArea()
115 {
116         return work_area_;
117 }
118
119
120 Buffer * LyXView::buffer() const
121 {
122         BOOST_ASSERT(work_area_);
123         return work_area_->bufferView().buffer();
124 }
125
126
127 void LyXView::setBuffer(Buffer * b, bool child_document)
128 {
129         busy(true);
130
131         BOOST_ASSERT(work_area_);
132         Buffer * oldBuffer = work_area_->bufferView().buffer();
133         // parentfilename will be used in case when we switch to a child
134         // document (hence when child_document is true)
135         string parentfilename;
136         if (oldBuffer) {
137                 parentfilename = oldBuffer->fileName();
138                 disconnectBuffer();
139         }
140
141         if (!b && theBufferList().empty())
142                 getDialogs().hideBufferDependent();
143
144         work_area_->bufferView().setBuffer(b);
145
146   //FIXME This would be a little simpler if setBuffer returned the buffer.
147         Buffer * newBuffer = work_area_->bufferView().buffer();
148         if (newBuffer) {
149                 if (child_document && newBuffer != oldBuffer) {
150                         // Set the parent name of the child document.
151                         // This makes insertion of citations and references in the child work,
152                         // when the target is in the parent or another child document.
153                         newBuffer->setParentName(parentfilename);
154                         // updateLabels() will emit Buffer::structureChanged() so better
155                         // connect it before.
156                         connectBuffer(*newBuffer);
157                         // Update the labels and section numbering.
158                         updateLabels(*newBuffer->getMasterBuffer());
159                 } else
160                         connectBuffer(*newBuffer);
161
162                 // Buffer-dependent dialogs should be updated or
163                 // hidden. This should go here because some dialogs (eg ToC)
164                 // require bv_->text.
165                 getDialogs().updateBufferDependent(true);
166         }
167
168         if (quitting)
169                 return;
170
171         updateMenubar();
172         updateToolbars();
173         updateLayoutChoice();
174         updateWindowTitle();
175         updateStatusBar();
176         updateTab();
177         busy(false);
178         work_area_->redraw();
179 }
180
181
182 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles,
183                 bool child_document, bool auto_open)
184 {
185         busy(true);
186
187         BOOST_ASSERT(work_area_);
188         string parentfilename;
189         Buffer * oldBuffer = work_area_->bufferView().buffer();
190         if (oldBuffer)
191                 parentfilename = oldBuffer->fileName();
192
193         Buffer * newBuffer =
194                 work_area_->bufferView().loadLyXFile(filename, auto_open);
195
196   if (!newBuffer) {
197                 message(_("Document not loaded."));
198     updateStatusBar();
199     busy(false);
200     work_area_->redraw();
201     return false;
202   }
203   
204   if (!auto_open) {
205     disconnectBuffer();
206     connectBuffer(*newBuffer);
207   }
208
209   showErrorList("Parse");
210
211   if (child_document && newBuffer != oldBuffer) {
212     // Set the parent name of the child document.
213     // This makes insertion of citations and references in the child work,
214     // when the target is in the parent or another child document.
215     newBuffer->setParentName(parentfilename);
216     message(bformat(_("Opening child document %1$s..."),
217       makeDisplayPath(filename.absFilename())));
218   }
219   
220   // Update the labels and section numbering.
221   updateLabels(*newBuffer->getMasterBuffer());
222
223   // scroll to the position when the file was last closed
224   if (!auto_open && lyxrc.use_lastfilepos) {
225     pit_type pit;
226     pos_type pos;
227     boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
228     // if successfully move to pit (returned par_id is not zero),
229     // update metrics and reset font
230     if (work_area_->bufferView().moveToPosition(pit, pos, 0, 0).get<1>()) {
231       if (work_area_->bufferView().fitCursor())
232         work_area_->bufferView().updateMetrics(false);
233       newBuffer->text().setCurrentFont(work_area_->bufferView().cursor());
234     }
235   }
236
237   if (tolastfiles)
238     LyX::ref().session().lastFiles().add(filename);
239
240   // FIXME We definitely don't have to do all of this if auto_open...
241   // The question is: What do we have to do here if we've loaded a new
242   // file but haven't switched buffers? I'm guessing we can skip the
243   // layout bit and the title.
244   // and if we have already switched buffers...won't all of this have been
245   // done already in setBuffer()? So why does it also need doing here?
246   // In fact, won't a lot of what's above already have been done?  E.g.,
247   // the connecting and disconnecting of buffers will have been done
248   // already in setBuffer(). 
249         // This bit of cleanup is post-1.5.0....
250         updateMenubar();
251         updateToolbars();
252         updateLayoutChoice();
253         updateWindowTitle();
254         updateTab();
255         updateStatusBar();
256         busy(false);
257         work_area_->redraw();
258         return true;
259 }
260
261
262 void LyXView::connectBuffer(Buffer & buf)
263 {
264         if (errorsConnection_.connected())
265                 disconnectBuffer();
266
267         BOOST_ASSERT(work_area_);
268         bufferChangedConnection_ =
269                 buf.changed.connect(
270                         boost::bind(&WorkArea::redraw, work_area_));
271
272         bufferStructureChangedConnection_ =
273                 buf.getMasterBuffer()->structureChanged.connect(
274                         boost::bind(&LyXView::updateToc, this));
275
276         errorsConnection_ =
277                 buf.errors.connect(
278                         boost::bind(&LyXView::showErrorList, this, _1));
279
280         messageConnection_ =
281                 buf.message.connect(
282                         boost::bind(&LyXView::message, this, _1));
283
284         busyConnection_ =
285                 buf.busy.connect(
286                         boost::bind(&LyXView::busy, this, _1));
287
288         titleConnection_ =
289                 buf.updateTitles.connect(
290                         boost::bind(&LyXView::updateWindowTitle, this));
291
292         timerConnection_ =
293                 buf.resetAutosaveTimers.connect(
294                         boost::bind(&LyXView::resetAutosaveTimer, this));
295
296         readonlyConnection_ =
297                 buf.readonly.connect(
298                         boost::bind(&LyXView::showReadonly, this, _1));
299
300         closingConnection_ =
301                 buf.closing.connect(
302                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0, false));
303 }
304
305
306 void LyXView::disconnectBuffer()
307 {
308         errorsConnection_.disconnect();
309         bufferChangedConnection_.disconnect();
310         bufferStructureChangedConnection_.disconnect();
311         messageConnection_.disconnect();
312         busyConnection_.disconnect();
313         titleConnection_.disconnect();
314         timerConnection_.disconnect();
315         readonlyConnection_.disconnect();
316         closingConnection_.disconnect();
317         layout_changed_connection_.disconnect();
318 }
319
320
321 void LyXView::connectBufferView(BufferView & bv)
322 {
323         show_dialog_connection_ = bv.showDialog.connect(
324                         boost::bind(&LyXView::showDialog, this, _1));
325         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
326                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
327         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
328                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
329         update_dialog_connection_ = bv.updateDialog.connect(
330                         boost::bind(&LyXView::updateDialog, this, _1, _2));
331         layout_changed_connection_ = bv.layoutChanged.connect(
332                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
333 }
334
335
336 void LyXView::disconnectBufferView()
337 {
338         show_dialog_connection_.disconnect();
339         show_dialog_with_data_connection_.disconnect();
340         show_inset_dialog_connection_.disconnect();
341         update_dialog_connection_.disconnect();
342 }
343
344
345 void LyXView::showErrorList(string const & error_type)
346 {
347         ErrorList & el = buffer()->errorList(error_type);
348         if (!el.empty()) {
349                 getDialogs().show("errorlist", error_type);
350         }
351 }
352
353
354 void LyXView::showDialog(string const & name)
355 {
356         getDialogs().show(name);
357 }
358
359
360 void LyXView::showDialogWithData(string const & name, string const & data)
361 {
362         getDialogs().show(name, data);
363 }
364
365
366 void LyXView::showInsetDialog(string const & name, string const & data,
367                 Inset * inset)
368 {
369         getDialogs().show(name, data, inset);
370 }
371
372
373 void LyXView::updateDialog(string const & name, string const & data)
374 {
375         if (getDialogs().visible(name))
376                 getDialogs().update(name, data);
377 }
378
379
380 void LyXView::showReadonly(bool)
381 {
382         updateWindowTitle();
383         getDialogs().updateBufferDependent(false);
384 }
385
386
387 BufferView * LyXView::view() const
388 {
389         BOOST_ASSERT(work_area_);
390         return &work_area_->bufferView();
391 }
392
393
394 void LyXView::updateToc()
395 {
396         updateDialog("toc", "");
397 }
398
399
400 void LyXView::updateToolbars()
401 {
402         BOOST_ASSERT(work_area_);
403         bool const math =
404                 work_area_->bufferView().cursor().inMathed();
405         bool const table =
406                 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
407         bool const review =
408                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
409                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
410
411         toolbars_->update(math, table, review);
412         // update redaonly status of open dialogs. This could also be in
413         // updateMenubar(), but since updateToolbars() and updateMenubar()
414         // are always called together it is only here.
415         getDialogs().checkStatus();
416 }
417
418
419 ToolbarInfo::Flags LyXView::getToolbarState(string const & name)
420 {
421         return toolbars_->getToolbarState(name);
422 }
423
424
425 void LyXView::toggleToolbarState(string const & name, bool allowauto)
426 {
427         // it is possible to get current toolbar status like this,...
428         // but I decide to obey the order of ToolbarBackend::flags
429         // and disregard real toolbar status.
430         // toolbars_->saveToolbarInfo();
431         //
432         // toggle state on/off/auto
433         toolbars_->toggleToolbarState(name, allowauto);
434         // update toolbar
435         updateToolbars();
436 }
437
438
439 void LyXView::updateMenubar()
440 {
441         menubar_->update();
442 }
443
444
445 void LyXView::autoSave()
446 {
447         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
448
449         if (view()->buffer())
450                 lyx::autoSave(view());
451 }
452
453
454 void LyXView::resetAutosaveTimer()
455 {
456         if (lyxrc.autosave)
457                 autosave_timeout_->restart();
458 }
459
460
461 void LyXView::updateLayoutChoice()
462 {
463         // Don't show any layouts without a buffer
464         if (!view()->buffer()) {
465                 toolbars_->clearLayoutList();
466                 return;
467         }
468
469         // Update the layout display
470         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
471                 current_layout = buffer()->params().getTextClass().defaultLayoutName();
472         }
473
474         BOOST_ASSERT(work_area_);
475         string const & layout = work_area_->bufferView().cursor().
476                 innerParagraph().layout()->name();
477
478         if (layout != current_layout) {
479                 toolbars_->setLayout(layout);
480                 current_layout = layout;
481         }
482 }
483
484
485 void LyXView::updateWindowTitle()
486 {
487         docstring maximize_title = lyx::from_ascii("LyX");
488         docstring minimize_title = lyx::from_ascii("LyX");
489
490         if (view()->buffer()) {
491                 string const cur_title = buffer()->fileName();
492                 if (!cur_title.empty()) {
493                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
494                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
495                         if (!buffer()->isClean()) {
496                                 maximize_title += _(" (changed)");
497                                 minimize_title += lyx::char_type('*');
498                         }
499                         if (buffer()->isReadonly())
500                                 maximize_title += _(" (read only)");
501                 }
502         }
503
504         setWindowTitle(maximize_title, minimize_title);
505         updateTab();
506 }
507
508
509 void LyXView::dispatch(FuncRequest const & cmd)
510 {
511         theLyXFunc().setLyXView(this);
512         lyx::dispatch(cmd);
513 }
514
515
516 Buffer const * const LyXView::updateInset(Inset const * inset) const
517 {
518         Buffer const * buffer_ptr = 0;
519         if (inset) {
520                 BOOST_ASSERT(work_area_);
521                 work_area_->scheduleRedraw();
522
523                 buffer_ptr = work_area_->bufferView().buffer();
524         }
525         return buffer_ptr;
526 }
527
528
529 } // namespace lyx