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