]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.cpp
Fix crash on first load with mouse wheel: this was because the scrollbar was not...
[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         wa->redraw();
168
169         busy(false);
170         return newBuffer;
171 }
172
173
174 void LyXView::connectBuffer(Buffer & buf)
175 {
176         if (errorsConnection_.connected())
177                 disconnectBuffer();
178
179         bufferStructureChangedConnection_ =
180                 buf.getMasterBuffer()->structureChanged.connect(
181                         boost::bind(&LyXView::updateToc, this));
182
183         errorsConnection_ =
184                 buf.errors.connect(
185                         boost::bind(&LyXView::showErrorList, this, _1));
186
187         messageConnection_ =
188                 buf.message.connect(
189                         boost::bind(&LyXView::message, this, _1));
190
191         busyConnection_ =
192                 buf.busy.connect(
193                         boost::bind(&LyXView::busy, this, _1));
194
195         titleConnection_ =
196                 buf.updateTitles.connect(
197                         boost::bind(&LyXView::updateWindowTitle, this));
198
199         timerConnection_ =
200                 buf.resetAutosaveTimers.connect(
201                         boost::bind(&LyXView::resetAutosaveTimer, this));
202
203         readonlyConnection_ =
204                 buf.readonly.connect(
205                         boost::bind(&LyXView::showReadonly, this, _1));
206 }
207
208
209 void LyXView::disconnectBuffer()
210 {
211         errorsConnection_.disconnect();
212         bufferStructureChangedConnection_.disconnect();
213         messageConnection_.disconnect();
214         busyConnection_.disconnect();
215         titleConnection_.disconnect();
216         timerConnection_.disconnect();
217         readonlyConnection_.disconnect();
218         layout_changed_connection_.disconnect();
219 }
220
221
222 void LyXView::connectBufferView(BufferView & bv)
223 {
224         message_connection_ = bv.message.connect(
225                         boost::bind(&LyXView::message, this, _1));
226         show_dialog_connection_ = bv.showDialog.connect(
227                         boost::bind(&LyXView::showDialog, this, _1));
228         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
229                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
230         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
231                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
232         update_dialog_connection_ = bv.updateDialog.connect(
233                         boost::bind(&LyXView::updateDialog, this, _1, _2));
234         layout_changed_connection_ = bv.layoutChanged.connect(
235                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
236 }
237
238
239 void LyXView::disconnectBufferView()
240 {
241         message_connection_.disconnect();
242         show_dialog_connection_.disconnect();
243         show_dialog_with_data_connection_.disconnect();
244         show_inset_dialog_connection_.disconnect();
245         update_dialog_connection_.disconnect();
246 }
247
248
249 void LyXView::showErrorList(string const & error_type)
250 {
251         ErrorList & el = buffer()->errorList(error_type);
252         if (!el.empty()) {
253                 getDialogs().show("errorlist", error_type);
254         }
255 }
256
257
258 void LyXView::showDialog(string const & name)
259 {
260         getDialogs().show(name);
261 }
262
263
264 void LyXView::showDialogWithData(string const & name, string const & data)
265 {
266         getDialogs().show(name, data);
267 }
268
269
270 void LyXView::showInsetDialog(string const & name, string const & data,
271                 Inset * inset)
272 {
273         getDialogs().show(name, data, inset);
274 }
275
276
277 void LyXView::updateDialog(string const & name, string const & data)
278 {
279         if (getDialogs().visible(name))
280                 getDialogs().update(name, data);
281 }
282
283
284 void LyXView::showReadonly(bool)
285 {
286         updateWindowTitle();
287         getDialogs().updateBufferDependent(false);
288 }
289
290
291 BufferView * LyXView::view()
292 {
293         WorkArea * wa = currentWorkArea();
294         return wa? &wa->bufferView() : 0;
295 }
296
297
298 void LyXView::updateToc()
299 {
300         updateDialog("toc", "");
301 }
302
303
304 void LyXView::updateToolbars()
305 {
306         WorkArea * wa = currentWorkArea();
307         if (wa) {
308                 bool const math =
309                         wa->bufferView().cursor().inMathed();
310                 bool const table =
311                         lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
312                 bool const review =
313                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
314                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
315
316                 toolbars_->update(math, table, review);
317         } else
318                 toolbars_->update(false, false, false);
319
320         // update redaonly status of open dialogs. This could also be in
321         // updateMenubar(), but since updateToolbars() and updateMenubar()
322         // are always called together it is only here.
323         getDialogs().checkStatus();
324 }
325
326
327 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
328 {
329         return toolbars_->getToolbarInfo(name);
330 }
331
332
333 void LyXView::toggleToolbarState(string const & name, bool allowauto)
334 {
335         // it is possible to get current toolbar status like this,...
336         // but I decide to obey the order of ToolbarBackend::flags
337         // and disregard real toolbar status.
338         // toolbars_->saveToolbarInfo();
339         //
340         // toggle state on/off/auto
341         toolbars_->toggleToolbarState(name, allowauto);
342         // update toolbar
343         updateToolbars();
344 }
345
346
347 void LyXView::updateMenubar()
348 {
349         menubar_->update();
350 }
351
352
353 void LyXView::autoSave()
354 {
355         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
356
357         if (buffer())
358                 lyx::autoSave(view());
359 }
360
361
362 void LyXView::resetAutosaveTimer()
363 {
364         if (lyxrc.autosave)
365                 autosave_timeout_->restart();
366 }
367
368
369 void LyXView::updateLayoutChoice()
370 {
371         // Don't show any layouts without a buffer
372         if (!buffer()) {
373                 toolbars_->clearLayoutList();
374                 return;
375         }
376
377         // Update the layout display
378         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
379                 current_layout = buffer()->params().getTextClass().defaultLayoutName();
380         }
381
382         docstring const & layout = currentWorkArea()->bufferView().cursor().
383                 innerParagraph().layout()->name();
384
385         if (layout != current_layout) {
386                 toolbars_->setLayout(layout);
387                 current_layout = layout;
388         }
389 }
390
391
392 void LyXView::updateWindowTitle()
393 {
394         docstring maximize_title = lyx::from_ascii("LyX");
395         docstring minimize_title = lyx::from_ascii("LyX");
396
397         Buffer * buf = buffer();
398         if (buf) {
399                 string const cur_title = buf->fileName();
400                 if (!cur_title.empty()) {
401                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
402                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
403                         if (!buf->isClean()) {
404                                 maximize_title += _(" (changed)");
405                                 minimize_title += lyx::char_type('*');
406                         }
407                         if (buf->isReadonly())
408                                 maximize_title += _(" (read only)");
409                 }
410         }
411
412         setWindowTitle(maximize_title, minimize_title);
413 }
414
415
416 void LyXView::dispatch(FuncRequest const & cmd)
417 {
418         string const argument = to_utf8(cmd.argument());
419         switch(cmd.action) {
420                 case LFUN_BUFFER_SWITCH:
421                         setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
422                         break;
423                 default:
424                         theLyXFunc().setLyXView(this);
425                         lyx::dispatch(cmd);
426         }
427 }
428
429
430 Buffer const * const LyXView::updateInset(Inset const * inset)
431 {
432         WorkArea * work_area = currentWorkArea();
433         if (!work_area)
434                 return 0;
435
436         if (inset) {
437                 BOOST_ASSERT(work_area);
438                 work_area->scheduleRedraw();
439         }
440         return &work_area->bufferView().buffer();
441 }
442
443 } // namespace frontend
444 } // namespace lyx