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