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