]> git.lyx.org Git - features.git/blob - src/frontends/LyXView.cpp
Whitespace cleanup
[features.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 #include "Dialogs.h"
16 #include "Timeout.h"
17 #include "Toolbars.h"
18 #include "Menubar.h"
19 #include "WorkArea.h"
20 #include "Gui.h"
21
22 #include "Buffer.h"
23 //FIXME Bug 3701
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 "LyXFunc.h"
36 #include "LyXRC.h"
37 #include "Text.h"
38 #include "MenuBackend.h"
39 #include "Paragraph.h"
40
41 #include "controllers/ControlCommandBuffer.h"
42
43 #include "support/lstrings.h"
44 #include "support/filetools.h" // OnlyFilename()
45
46 #include <boost/bind.hpp>
47
48
49 namespace lyx {
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 frontend::WorkArea;
59
60 using support::bformat;
61 using support::FileName;
62 using support::makeDisplayPath;
63 using support::onlyFilename;
64
65 using std::endl;
66 using std::string;
67
68 using lyx::frontend::ControlCommandBuffer;
69
70 string current_layout;
71
72
73 LyXView::LyXView(int id)
74         : work_area_(0),
75           toolbars_(new Toolbars(*this)),
76           autosave_timeout_(new Timeout(5000)),
77           dialogs_(new Dialogs(*this)),
78           controlcommand_(new ControlCommandBuffer(*this)), id_(id)
79 {
80         // Start autosave timer
81         if (lyxrc.autosave) {
82                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
83                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
84                 autosave_timeout_->start();
85         }
86 }
87
88
89 LyXView::~LyXView()
90 {
91         disconnectBuffer();
92 }
93
94
95 // FIXME, there's only one WorkArea per LyXView possible for now.
96 void LyXView::setWorkArea(WorkArea * work_area)
97 {
98         BOOST_ASSERT(work_area);
99         work_area_ = work_area;
100         work_area_ids_.clear();
101         work_area_ids_.push_back(work_area_->id());
102 }
103
104
105 // FIXME, there's only one WorkArea per LyXView possible for now.
106 WorkArea const * LyXView::currentWorkArea() const
107 {
108         return work_area_;
109 }
110
111
112 // FIXME, there's only one WorkArea per LyXView possible for now.
113 WorkArea * LyXView::currentWorkArea()
114 {
115         return work_area_;
116 }
117
118
119 Buffer * LyXView::buffer() const
120 {
121         BOOST_ASSERT(work_area_);
122         return work_area_->bufferView().buffer();
123 }
124
125
126 void LyXView::setBuffer(Buffer * b)
127 {
128         busy(true);
129
130         BOOST_ASSERT(work_area_);
131         if (work_area_->bufferView().buffer())
132                 disconnectBuffer();
133
134         //FIXME Bug 3701
135         if (!b) // && theBufferList().empty()
136                 getDialogs().hideBufferDependent();
137
138         work_area_->bufferView().setBuffer(b);
139         // Make sure the TOC is updated before anything else.
140         updateToc();
141
142         if (work_area_->bufferView().buffer()) {
143                 // Buffer-dependent dialogs should be updated or
144                 // hidden. This should go here because some dialogs (eg ToC)
145                 // require bv_->text.
146                 getDialogs().updateBufferDependent(true);
147                 connectBuffer(*work_area_->bufferView().buffer());
148         }
149
150         if (quitting)
151                 return;
152
153         updateMenubar();
154         updateToolbars();
155         updateLayoutChoice();
156         updateWindowTitle();
157         updateStatusBar();
158         updateTab();
159         busy(false);
160         work_area_->redraw();
161 }
162
163
164 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
165 {
166         busy(true);
167
168         BOOST_ASSERT(work_area_);
169         bool const hadBuffer = work_area_->bufferView().buffer();
170         if (hadBuffer)
171                 disconnectBuffer();
172
173         bool const loaded =
174                 work_area_->bufferView().loadLyXFile(filename, tolastfiles);
175
176         updateToc();
177         updateMenubar();
178         updateToolbars();
179         updateLayoutChoice();
180         updateWindowTitle();
181         updateTab();
182         if (loaded) {
183                 connectBuffer(*work_area_->bufferView().buffer());
184                 showErrorList("Parse");
185         } else if (hadBuffer)
186                 //Need to reconnect the buffer if the load failed
187                 connectBuffer(*work_area_->bufferView().buffer());
188         updateStatusBar();
189         busy(false);
190         work_area_->redraw();
191         return loaded;
192 }
193
194
195 void LyXView::connectBuffer(Buffer & buf)
196 {
197         if (errorsConnection_.connected())
198                 disconnectBuffer();
199
200         BOOST_ASSERT(work_area_);
201         bufferChangedConnection_ =
202                 buf.changed.connect(
203                         boost::bind(&WorkArea::redraw, work_area_));
204
205         bufferStructureChangedConnection_ =
206                 buf.getMasterBuffer()->structureChanged.connect(
207                         boost::bind(&LyXView::updateToc, this));
208
209         errorsConnection_ =
210                 buf.errors.connect(
211                         boost::bind(&LyXView::showErrorList, this, _1));
212
213         messageConnection_ =
214                 buf.message.connect(
215                         boost::bind(&LyXView::message, this, _1));
216
217         busyConnection_ =
218                 buf.busy.connect(
219                         boost::bind(&LyXView::busy, this, _1));
220
221         titleConnection_ =
222                 buf.updateTitles.connect(
223                         boost::bind(&LyXView::updateWindowTitle, this));
224
225         timerConnection_ =
226                 buf.resetAutosaveTimers.connect(
227                         boost::bind(&LyXView::resetAutosaveTimer, this));
228
229         readonlyConnection_ =
230                 buf.readonly.connect(
231                         boost::bind(&LyXView::showReadonly, this, _1));
232
233         closingConnection_ =
234                 buf.closing.connect(
235                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
236 }
237
238
239 void LyXView::disconnectBuffer()
240 {
241         errorsConnection_.disconnect();
242         bufferChangedConnection_.disconnect();
243         bufferStructureChangedConnection_.disconnect();
244         messageConnection_.disconnect();
245         busyConnection_.disconnect();
246         titleConnection_.disconnect();
247         timerConnection_.disconnect();
248         readonlyConnection_.disconnect();
249         closingConnection_.disconnect();
250         layout_changed_connection_.disconnect();
251 }
252
253
254 void LyXView::connectBufferView(BufferView & bv)
255 {
256         show_dialog_connection_ = bv.showDialog.connect(
257                         boost::bind(&LyXView::showDialog, this, _1));
258         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
259                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
260         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
261                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
262         update_dialog_connection_ = bv.updateDialog.connect(
263                         boost::bind(&LyXView::updateDialog, this, _1, _2));
264         layout_changed_connection_ = bv.layoutChanged.connect(
265                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
266 }
267
268
269 void LyXView::disconnectBufferView()
270 {
271         show_dialog_connection_.disconnect();
272         show_dialog_with_data_connection_.disconnect();
273         show_inset_dialog_connection_.disconnect();
274         update_dialog_connection_.disconnect();
275 }
276
277
278 void LyXView::showErrorList(string const & error_type)
279 {
280         ErrorList & el = buffer()->errorList(error_type);
281         if (!el.empty()) {
282                 getDialogs().show("errorlist", error_type);
283         }
284 }
285
286
287 void LyXView::showDialog(string const & name)
288 {
289         getDialogs().show(name);
290 }
291
292
293 void LyXView::showDialogWithData(string const & name, string const & data)
294 {
295         getDialogs().show(name, data);
296 }
297
298
299 void LyXView::showInsetDialog(string const & name, string const & data,
300                 Inset * inset)
301 {
302         getDialogs().show(name, data, inset);
303 }
304
305
306 void LyXView::updateDialog(string const & name, string const & data)
307 {
308         if (getDialogs().visible(name))
309                 getDialogs().update(name, data);
310 }
311
312
313 void LyXView::showReadonly(bool)
314 {
315         updateWindowTitle();
316         getDialogs().updateBufferDependent(false);
317 }
318
319
320 BufferView * LyXView::view() const
321 {
322         BOOST_ASSERT(work_area_);
323         return &work_area_->bufferView();
324 }
325
326
327 void LyXView::updateToc()
328 {
329         updateDialog("toc", "");
330 }
331
332
333 void LyXView::updateToolbars()
334 {
335         BOOST_ASSERT(work_area_);
336         bool const math =
337                 work_area_->bufferView().cursor().inMathed();
338         bool const table =
339                 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
340         bool const review =
341                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
342                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
343
344         toolbars_->update(math, table, review);
345         // update redaonly status of open dialogs. This could also be in
346         // updateMenubar(), but since updateToolbars() and updateMenubar()
347         // are always called together it is only here.
348         getDialogs().checkStatus();
349 }
350
351
352 ToolbarInfo::Flags LyXView::getToolbarState(string const & name)
353 {
354         return toolbars_->getToolbarState(name);
355 }
356
357
358 void LyXView::toggleToolbarState(string const & name)
359 {
360         // it is possible to get current toolbar status like this,...
361         // but I decide to obey the order of ToolbarBackend::flags
362         // and disregard real toolbar status.
363         // toolbars_->saveToolbarInfo();
364         //
365         // toggle state on/off/auto
366         toolbars_->toggleToolbarState(name);
367         // update toolbar
368         updateToolbars();
369 }
370
371
372 void LyXView::updateMenubar()
373 {
374         menubar_->update();
375 }
376
377
378 void LyXView::autoSave()
379 {
380         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
381
382         if (view()->buffer())
383                 lyx::autoSave(view());
384 }
385
386
387 void LyXView::resetAutosaveTimer()
388 {
389         if (lyxrc.autosave)
390                 autosave_timeout_->restart();
391 }
392
393
394 void LyXView::updateLayoutChoice()
395 {
396         // Don't show any layouts without a buffer
397         if (!view()->buffer()) {
398                 toolbars_->clearLayoutList();
399                 return;
400         }
401
402         // Update the layout display
403         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
404                 current_layout = buffer()->params().getTextClass().defaultLayoutName();
405         }
406
407         BOOST_ASSERT(work_area_);
408         if (work_area_->bufferView().cursor().inMathed())
409                 return;
410
411         string const & layout =
412                 work_area_->bufferView().cursor().paragraph().layout()->name();
413
414         if (layout != current_layout) {
415                 toolbars_->setLayout(layout);
416                 current_layout = layout;
417         }
418 }
419
420
421 void LyXView::updateWindowTitle()
422 {
423         docstring maximize_title = lyx::from_ascii("LyX");
424         docstring minimize_title = lyx::from_ascii("LyX");
425
426         if (view()->buffer()) {
427                 string const cur_title = buffer()->fileName();
428                 if (!cur_title.empty()) {
429                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
430                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
431                         if (!buffer()->isClean()) {
432                                 maximize_title += _(" (changed)");
433                                 minimize_title += lyx::char_type('*');
434                         }
435                         if (buffer()->isReadonly())
436                                 maximize_title += _(" (read only)");
437                 }
438         }
439
440         setWindowTitle(maximize_title, minimize_title);
441         updateTab();
442 }
443
444
445 void LyXView::dispatch(FuncRequest const & cmd)
446 {
447         theLyXFunc().setLyXView(this);
448         lyx::dispatch(cmd);
449 }
450
451
452 Buffer const * const LyXView::updateInset(Inset const * inset) const
453 {
454         Buffer const * buffer_ptr = 0;
455         if (inset) {
456                 BOOST_ASSERT(work_area_);
457                 work_area_->scheduleRedraw();
458
459                 buffer_ptr = work_area_->bufferView().buffer();
460         }
461         return buffer_ptr;
462 }
463
464
465 } // namespace lyx