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