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