]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.cpp
* restore lastfile feature.
[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
16 #include "Dialogs.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 #include "support/Timeout.h"
47
48 #include <boost/bind.hpp>
49
50
51 #ifdef HAVE_SYS_TIME_H
52 # include <sys/time.h>
53 #endif
54 #ifdef HAVE_UNISTD_H
55 # include <unistd.h>
56 #endif
57
58 using std::endl;
59 using std::string;
60
61 namespace lyx {
62
63 using support::bformat;
64 using support::FileName;
65 using support::makeDisplayPath;
66 using support::onlyFilename;
67
68 namespace frontend {
69
70 docstring current_layout;
71
72 LyXView::LyXView(int id)
73         : toolbars_(new Toolbars(*this)),
74           autosave_timeout_(new Timeout(5000)),
75           dialogs_(new Dialogs(*this)),
76           controlcommand_(new ControlCommandBuffer(*this)), id_(id)
77 {
78         // Start autosave timer
79         if (lyxrc.autosave) {
80                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
81                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
82                 autosave_timeout_->start();
83         }
84 }
85
86
87 LyXView::~LyXView()
88 {
89         disconnectBuffer();
90         disconnectBufferView();
91 }
92
93
94 Buffer * LyXView::buffer()
95 {
96         WorkArea * work_area = currentWorkArea();
97         if (work_area)
98                 return &work_area->bufferView().buffer();
99         return 0;
100 }
101
102
103 Buffer const * LyXView::buffer() const
104 {
105         WorkArea const * work_area = currentWorkArea();
106         if (work_area)
107                 return &work_area->bufferView().buffer();
108         return 0;
109 }
110
111
112 void LyXView::setBuffer(Buffer * newBuffer)
113 {
114         BOOST_ASSERT(newBuffer);
115         busy(true);
116
117         WorkArea * wa = workArea(*newBuffer);
118         if (wa == 0) {
119                 updateLabels(*newBuffer->getMasterBuffer());
120                 wa = addWorkArea(*newBuffer);
121         } else
122                 //Disconnect the old buffer...there's no new one.
123                 disconnectBuffer();
124         connectBuffer(*newBuffer);
125         connectBufferView(wa->bufferView());
126         setCurrentWorkArea(wa);
127
128         busy(false);
129 }
130
131
132 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
133 {
134         busy(true);
135
136         Buffer * newBuffer = checkAndLoadLyXFile(filename);
137
138         if (!newBuffer) {
139                 message(_("Document not loaded."));
140                 updateStatusBar();
141                 busy(false);
142                 return 0;
143         }
144
145         WorkArea * wa = addWorkArea(*newBuffer);
146
147         // scroll to the position when the file was last closed
148         if (lyxrc.use_lastfilepos) {
149                 pit_type pit;
150                 pos_type pos;
151                 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
152                 // if successfully move to pit (returned par_id is not zero),
153                 // update metrics and reset font
154                 BufferView & bv = wa->bufferView();
155                 if (bv.moveToPosition(pit, pos, 0, 0).get<1>()) {
156                         if (bv.fitCursor())
157                                 bv.updateMetrics(false);
158                         newBuffer->text().setCurrentFont(bv.cursor());
159                 }
160         }
161
162         if (tolastfiles)
163                 LyX::ref().session().lastFiles().add(filename);
164
165         busy(false);
166         return newBuffer;
167 }
168
169
170 void LyXView::connectBuffer(Buffer & buf)
171 {
172         if (errorsConnection_.connected())
173                 disconnectBuffer();
174
175         bufferStructureChangedConnection_ =
176                 buf.getMasterBuffer()->structureChanged.connect(
177                         boost::bind(&LyXView::updateToc, this));
178
179         errorsConnection_ =
180                 buf.errors.connect(
181                         boost::bind(&LyXView::showErrorList, this, _1));
182
183         messageConnection_ =
184                 buf.message.connect(
185                         boost::bind(&LyXView::message, this, _1));
186
187         busyConnection_ =
188                 buf.busy.connect(
189                         boost::bind(&LyXView::busy, this, _1));
190
191         titleConnection_ =
192                 buf.updateTitles.connect(
193                         boost::bind(&LyXView::updateWindowTitle, this));
194
195         timerConnection_ =
196                 buf.resetAutosaveTimers.connect(
197                         boost::bind(&LyXView::resetAutosaveTimer, this));
198
199         readonlyConnection_ =
200                 buf.readonly.connect(
201                         boost::bind(&LyXView::showReadonly, this, _1));
202 }
203
204
205 void LyXView::disconnectBuffer()
206 {
207         errorsConnection_.disconnect();
208         bufferStructureChangedConnection_.disconnect();
209         messageConnection_.disconnect();
210         busyConnection_.disconnect();
211         titleConnection_.disconnect();
212         timerConnection_.disconnect();
213         readonlyConnection_.disconnect();
214         layout_changed_connection_.disconnect();
215 }
216
217
218 void LyXView::connectBufferView(BufferView & bv)
219 {
220         message_connection_ = bv.message.connect(
221                         boost::bind(&LyXView::message, this, _1));
222         show_dialog_connection_ = bv.showDialog.connect(
223                         boost::bind(&LyXView::showDialog, this, _1));
224         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
225                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
226         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
227                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
228         update_dialog_connection_ = bv.updateDialog.connect(
229                         boost::bind(&LyXView::updateDialog, this, _1, _2));
230         layout_changed_connection_ = bv.layoutChanged.connect(
231                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
232 }
233
234
235 void LyXView::disconnectBufferView()
236 {
237         message_connection_.disconnect();
238         show_dialog_connection_.disconnect();
239         show_dialog_with_data_connection_.disconnect();
240         show_inset_dialog_connection_.disconnect();
241         update_dialog_connection_.disconnect();
242 }
243
244
245 void LyXView::showErrorList(string const & error_type)
246 {
247         ErrorList & el = buffer()->errorList(error_type);
248         if (!el.empty()) {
249                 getDialogs().show("errorlist", error_type);
250         }
251 }
252
253
254 void LyXView::showDialog(string const & name)
255 {
256         getDialogs().show(name);
257 }
258
259
260 void LyXView::showDialogWithData(string const & name, string const & data)
261 {
262         getDialogs().show(name, data);
263 }
264
265
266 void LyXView::showInsetDialog(string const & name, string const & data,
267                 Inset * inset)
268 {
269         getDialogs().show(name, data, inset);
270 }
271
272
273 void LyXView::updateDialog(string const & name, string const & data)
274 {
275         if (getDialogs().visible(name))
276                 getDialogs().update(name, data);
277 }
278
279
280 void LyXView::showReadonly(bool)
281 {
282         updateWindowTitle();
283         getDialogs().updateBufferDependent(false);
284 }
285
286
287 BufferView * LyXView::view()
288 {
289         WorkArea * wa = currentWorkArea();
290         return wa? &wa->bufferView() : 0;
291 }
292
293
294 void LyXView::updateToc()
295 {
296         updateDialog("toc", "");
297 }
298
299
300 void LyXView::updateToolbars()
301 {
302         WorkArea * wa = currentWorkArea();
303         if (wa) {
304                 bool const math =
305                         wa->bufferView().cursor().inMathed();
306                 bool const table =
307                         lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
308                 bool const review =
309                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
310                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
311
312                 toolbars_->update(math, table, review);
313         } else
314                 toolbars_->update(false, false, false);
315
316         // update redaonly status of open dialogs. This could also be in
317         // updateMenubar(), but since updateToolbars() and updateMenubar()
318         // are always called together it is only here.
319         getDialogs().checkStatus();
320 }
321
322
323 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
324 {
325         return toolbars_->getToolbarInfo(name);
326 }
327
328
329 void LyXView::toggleToolbarState(string const & name, bool allowauto)
330 {
331         // it is possible to get current toolbar status like this,...
332         // but I decide to obey the order of ToolbarBackend::flags
333         // and disregard real toolbar status.
334         // toolbars_->saveToolbarInfo();
335         //
336         // toggle state on/off/auto
337         toolbars_->toggleToolbarState(name, allowauto);
338         // update toolbar
339         updateToolbars();
340 }
341
342
343 void LyXView::updateMenubar()
344 {
345         menubar_->update();
346 }
347
348
349 void LyXView::autoSave()
350 {
351         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
352
353         if (buffer())
354                 lyx::autoSave(view());
355 }
356
357
358 void LyXView::resetAutosaveTimer()
359 {
360         if (lyxrc.autosave)
361                 autosave_timeout_->restart();
362 }
363
364
365 void LyXView::updateLayoutChoice()
366 {
367         // Don't show any layouts without a buffer
368         if (!buffer()) {
369                 toolbars_->clearLayoutList();
370                 return;
371         }
372
373         // Update the layout display
374         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
375                 current_layout = buffer()->params().getTextClass().defaultLayoutName();
376         }
377
378         docstring const & layout = currentWorkArea()->bufferView().cursor().
379                 innerParagraph().layout()->name();
380
381         if (layout != current_layout) {
382                 toolbars_->setLayout(layout);
383                 current_layout = layout;
384         }
385 }
386
387
388 void LyXView::updateWindowTitle()
389 {
390         docstring maximize_title = lyx::from_ascii("LyX");
391         docstring minimize_title = lyx::from_ascii("LyX");
392
393         Buffer * buf = buffer();
394         if (buf) {
395                 string const cur_title = buf->fileName();
396                 if (!cur_title.empty()) {
397                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
398                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
399                         if (!buf->isClean()) {
400                                 maximize_title += _(" (changed)");
401                                 minimize_title += lyx::char_type('*');
402                         }
403                         if (buf->isReadonly())
404                                 maximize_title += _(" (read only)");
405                 }
406         }
407
408         setWindowTitle(maximize_title, minimize_title);
409 }
410
411
412 void LyXView::dispatch(FuncRequest const & cmd)
413 {
414         string const argument = to_utf8(cmd.argument());
415         switch(cmd.action) {
416                 case LFUN_BUFFER_SWITCH:
417                         setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
418                         break;
419                 default:
420                         theLyXFunc().setLyXView(this);
421                         lyx::dispatch(cmd);
422         }
423 }
424
425
426 Buffer const * const LyXView::updateInset(Inset const * inset)
427 {
428         WorkArea * work_area = currentWorkArea();
429         if (!work_area)
430                 return 0;
431
432         if (inset) {
433                 BOOST_ASSERT(work_area);
434                 work_area->scheduleRedraw();
435         }
436         return &work_area->bufferView().buffer();
437 }
438
439 } // namespace frontend
440 } // namespace lyx