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