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