]> git.lyx.org Git - features.git/blob - src/frontends/LyXView.cpp
* src/frontends/LyXView.h:
[features.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->getMasterBuffer() != 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                         // Update the labels and section numbering to the new master Buffer.
155                         updateLabels(*newBuffer->getMasterBuffer());
156                 }
157
158                 if (!b && oldBuffer && oldBuffer->getMasterBuffer() != oldBuffer)
159                         // We are closing oldBuffer which was a child document so we
160                         // must update the labels and section numbering of its master
161                         // Buffer.
162                         updateLabels(*oldBuffer->getMasterBuffer());
163
164                 connectBuffer(*newBuffer);
165
166                 /* FIXME: We need to rebuild the Toc dialog before the others even
167                 if it will be rebuilt again in the next line. This avoid a crash when
168                 other dialogs are rebuilt before the Toc dialog. The reason is
169                 that closing a Buffer triggers an update of all opened dialogs
170                 when dispatching LFUN_DIALOG_UPDATE (hence the patch).
171                 The path is as following:
172                         setBuffer() -> updateBufferDependent() -> RestoreButton() -> LFUN
173                 The problem here is that the Toc dialog has not been
174                 reconstructed (because it comes after in the list of dialogs). */
175                 updateToc();
176
177                 // Buffer-dependent dialogs should be updated or
178                 // hidden. This should go here because some dialogs (eg ToC)
179                 // require bv_->text.
180                 getDialogs().updateBufferDependent(true);
181         }
182
183         if (quitting)
184                 return;
185
186         updateMenubar();
187         updateToolbars();
188         updateLayoutChoice();
189         updateWindowTitle();
190         updateStatusBar();
191         updateTab();
192         busy(false);
193         work_area_->redraw();
194 }
195
196
197 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles,
198                 bool child_document, bool auto_open)
199 {
200         busy(true);
201
202         BOOST_ASSERT(work_area_);
203         string parentfilename;
204         Buffer * oldBuffer = work_area_->bufferView().buffer();
205         if (oldBuffer)
206                 parentfilename = oldBuffer->fileName();
207
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         if (parse_error || !auto_open) {
232                 setBuffer(newBuffer, child_document);
233                 showErrorList("Parse");
234         }
235
236         // scroll to the position when the file was last closed
237         if (!auto_open && lyxrc.use_lastfilepos) {
238                 pit_type pit;
239                 pos_type pos;
240                 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
241                 // if successfully move to pit (returned par_id is not zero),
242                 // update metrics and reset font
243                 if (work_area_->bufferView().moveToPosition(pit, pos, 0, 0).get<1>()) {
244                         if (work_area_->bufferView().fitCursor())
245                                 work_area_->bufferView().updateMetrics(false);
246                         newBuffer->text().setCurrentFont(work_area_->bufferView().cursor());
247                         updateMenubar();
248                         updateToolbars();
249                         updateLayoutChoice();
250                         updateStatusBar();
251                         work_area_->redraw();
252                 }
253         }
254
255         if (tolastfiles)
256                 LyX::ref().session().lastFiles().add(filename);
257
258         busy(false);
259         return true;
260 }
261
262
263 void LyXView::connectBuffer(Buffer & buf)
264 {
265         if (errorsConnection_.connected())
266                 disconnectBuffer();
267
268         BOOST_ASSERT(work_area_);
269         bufferChangedConnection_ =
270                 buf.changed.connect(
271                         boost::bind(&WorkArea::redraw, work_area_));
272
273         bufferStructureChangedConnection_ =
274                 buf.getMasterBuffer()->structureChanged.connect(
275                         boost::bind(&LyXView::updateToc, this));
276
277         errorsConnection_ =
278                 buf.errors.connect(
279                         boost::bind(&LyXView::showErrorList, this, _1));
280
281         messageConnection_ =
282                 buf.message.connect(
283                         boost::bind(&LyXView::message, this, _1));
284
285         busyConnection_ =
286                 buf.busy.connect(
287                         boost::bind(&LyXView::busy, this, _1));
288
289         titleConnection_ =
290                 buf.updateTitles.connect(
291                         boost::bind(&LyXView::updateWindowTitle, this));
292
293         timerConnection_ =
294                 buf.resetAutosaveTimers.connect(
295                         boost::bind(&LyXView::resetAutosaveTimer, this));
296
297         readonlyConnection_ =
298                 buf.readonly.connect(
299                         boost::bind(&LyXView::showReadonly, this, _1));
300
301         closingConnection_ =
302                 buf.closing.connect(
303                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0, false));
304 }
305
306
307 void LyXView::disconnectBuffer()
308 {
309         errorsConnection_.disconnect();
310         bufferChangedConnection_.disconnect();
311         bufferStructureChangedConnection_.disconnect();
312         messageConnection_.disconnect();
313         busyConnection_.disconnect();
314         titleConnection_.disconnect();
315         timerConnection_.disconnect();
316         readonlyConnection_.disconnect();
317         closingConnection_.disconnect();
318         layout_changed_connection_.disconnect();
319 }
320
321
322 void LyXView::connectBufferView(BufferView & bv)
323 {
324         show_dialog_connection_ = bv.showDialog.connect(
325                         boost::bind(&LyXView::showDialog, this, _1));
326         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
327                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
328         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
329                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
330         update_dialog_connection_ = bv.updateDialog.connect(
331                         boost::bind(&LyXView::updateDialog, this, _1, _2));
332         layout_changed_connection_ = bv.layoutChanged.connect(
333                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
334 }
335
336
337 void LyXView::disconnectBufferView()
338 {
339         show_dialog_connection_.disconnect();
340         show_dialog_with_data_connection_.disconnect();
341         show_inset_dialog_connection_.disconnect();
342         update_dialog_connection_.disconnect();
343 }
344
345
346 void LyXView::showErrorList(string const & error_type)
347 {
348         ErrorList & el = buffer()->errorList(error_type);
349         if (!el.empty()) {
350                 getDialogs().show("errorlist", error_type);
351         }
352 }
353
354
355 void LyXView::showDialog(string const & name)
356 {
357         getDialogs().show(name);
358 }
359
360
361 void LyXView::showDialogWithData(string const & name, string const & data)
362 {
363         getDialogs().show(name, data);
364 }
365
366
367 void LyXView::showInsetDialog(string const & name, string const & data,
368                 Inset * inset)
369 {
370         getDialogs().show(name, data, inset);
371 }
372
373
374 void LyXView::updateDialog(string const & name, string const & data)
375 {
376         if (getDialogs().visible(name))
377                 getDialogs().update(name, data);
378 }
379
380
381 void LyXView::showReadonly(bool)
382 {
383         updateWindowTitle();
384         getDialogs().updateBufferDependent(false);
385 }
386
387
388 BufferView * LyXView::view() const
389 {
390         BOOST_ASSERT(work_area_);
391         return &work_area_->bufferView();
392 }
393
394
395 void LyXView::updateToc()
396 {
397         updateDialog("toc", "");
398 }
399
400
401 void LyXView::updateToolbars()
402 {
403         BOOST_ASSERT(work_area_);
404         bool const math =
405                 work_area_->bufferView().cursor().inMathed();
406         bool const table =
407                 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
408         bool const review =
409                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
410                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
411
412         toolbars_->update(math, table, review);
413         // update redaonly status of open dialogs. This could also be in
414         // updateMenubar(), but since updateToolbars() and updateMenubar()
415         // are always called together it is only here.
416         getDialogs().checkStatus();
417 }
418
419
420 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
421 {
422         return toolbars_->getToolbarInfo(name);
423 }
424
425
426 void LyXView::toggleToolbarState(string const & name, bool allowauto)
427 {
428         // it is possible to get current toolbar status like this,...
429         // but I decide to obey the order of ToolbarBackend::flags
430         // and disregard real toolbar status.
431         // toolbars_->saveToolbarInfo();
432         //
433         // toggle state on/off/auto
434         toolbars_->toggleToolbarState(name, allowauto);
435         // update toolbar
436         updateToolbars();
437 }
438
439
440 void LyXView::updateMenubar()
441 {
442         menubar_->update();
443 }
444
445
446 void LyXView::autoSave()
447 {
448         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
449
450         if (view()->buffer())
451                 lyx::autoSave(view());
452 }
453
454
455 void LyXView::resetAutosaveTimer()
456 {
457         if (lyxrc.autosave)
458                 autosave_timeout_->restart();
459 }
460
461
462 void LyXView::updateLayoutChoice()
463 {
464         // Don't show any layouts without a buffer
465         if (!view()->buffer()) {
466                 toolbars_->clearLayoutList();
467                 return;
468         }
469
470         // Update the layout display
471         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
472                 current_layout = buffer()->params().getTextClass().defaultLayoutName();
473         }
474
475         BOOST_ASSERT(work_area_);
476         string const & layout = work_area_->bufferView().cursor().
477                 innerParagraph().layout()->name();
478
479         if (layout != current_layout) {
480                 toolbars_->setLayout(layout);
481                 current_layout = layout;
482         }
483 }
484
485
486 void LyXView::updateWindowTitle()
487 {
488         docstring maximize_title = lyx::from_ascii("LyX");
489         docstring minimize_title = lyx::from_ascii("LyX");
490
491         if (view()->buffer()) {
492                 string const cur_title = buffer()->fileName();
493                 if (!cur_title.empty()) {
494                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
495                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
496                         if (!buffer()->isClean()) {
497                                 maximize_title += _(" (changed)");
498                                 minimize_title += lyx::char_type('*');
499                         }
500                         if (buffer()->isReadonly())
501                                 maximize_title += _(" (read only)");
502                 }
503         }
504
505         setWindowTitle(maximize_title, minimize_title);
506         updateTab();
507 }
508
509
510 void LyXView::dispatch(FuncRequest const & cmd)
511 {
512         theLyXFunc().setLyXView(this);
513         lyx::dispatch(cmd);
514 }
515
516
517 Buffer const * const LyXView::updateInset(Inset const * inset) const
518 {
519         Buffer const * buffer_ptr = 0;
520         if (inset) {
521                 BOOST_ASSERT(work_area_);
522                 work_area_->scheduleRedraw();
523
524                 buffer_ptr = work_area_->bufferView().buffer();
525         }
526         return buffer_ptr;
527 }
528
529
530 } // namespace lyx