]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
add some assertions.
[lyx.git] / src / frontends / LyXView.C
1 /**
2  * \file LyXView.C
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 #include "bufferparams.h"
24 #include "BufferView.h"
25 #include "bufferview_funcs.h"
26 #include "cursor.h"
27 #include "debug.h"
28 #include "errorlist.h"
29 #include "funcrequest.h"
30 #include "gettext.h"
31 #include "intl.h"
32 #include "lyx_cb.h"
33 #include "lyxfunc.h"
34 #include "lyxrc.h"
35 #include "lyxtext.h"
36 #include "MenuBackend.h"
37 #include "paragraph.h"
38
39 #include "controllers/ControlCommandBuffer.h"
40
41 #include "support/lstrings.h"
42 #include "support/filetools.h" // OnlyFilename()
43
44 #include <boost/bind.hpp>
45
46
47 namespace lyx {
48
49 #ifdef HAVE_SYS_TIME_H
50 # include <sys/time.h>
51 #endif
52 #ifdef HAVE_UNISTD_H
53 # include <unistd.h>
54 #endif
55
56 using frontend::WorkArea;
57
58 using support::bformat;
59 using support::FileName;
60 using support::makeDisplayPath;
61 using support::onlyFilename;
62
63 using std::endl;
64 using std::string;
65
66 using lyx::frontend::ControlCommandBuffer;
67
68 string current_layout;
69
70
71 LyXView::LyXView(int id)
72         : work_area_(0),
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 }
91
92
93 // FIXME, there's only one WorkArea per LyXView possible for now.
94 void LyXView::setWorkArea(WorkArea * work_area)
95 {
96         BOOST_ASSERT(work_area);
97         work_area_ = work_area;
98         work_area_ids_.clear();
99         work_area_ids_.push_back(work_area_->id());
100 }
101
102
103 Buffer * LyXView::buffer() const
104 {
105         BOOST_ASSERT(work_area_);
106         return work_area_->bufferView().buffer();
107 }
108
109
110 void LyXView::setBuffer(Buffer * b)
111 {
112         busy(true);
113
114         BOOST_ASSERT(work_area_);
115         if (work_area_->bufferView().buffer())
116                 disconnectBuffer();
117
118         if (!b)
119                 getDialogs().hideBufferDependent();
120
121         work_area_->bufferView().setBuffer(b);
122
123         if (work_area_->bufferView().buffer()) {
124                 // Buffer-dependent dialogs should be updated or
125                 // hidden. This should go here because some dialogs (eg ToC)
126                 // require bv_->text.
127                 getDialogs().updateBufferDependent(true);
128                 connectBuffer(*work_area_->bufferView().buffer());
129         }
130
131         if (quitting)
132                 return;
133
134         updateMenubar();
135         updateToolbars();
136         updateLayoutChoice();
137         updateWindowTitle();
138         updateStatusBar();
139         updateTab();
140         busy(false);
141         work_area_->redraw();
142 }
143
144
145 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
146 {
147         busy(true);
148
149         BOOST_ASSERT(work_area_);
150         if (work_area_->bufferView().buffer())
151                 disconnectBuffer();
152
153         bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
154
155         updateMenubar();
156         updateToolbars();
157         updateLayoutChoice();
158         updateWindowTitle();
159         updateTab();
160         if (loaded) {
161                 connectBuffer(*work_area_->bufferView().buffer());
162                 showErrorList("Parse");
163         }
164         updateStatusBar();
165         busy(false);
166         work_area_->redraw();
167         return loaded;
168 }
169
170
171 void LyXView::connectBuffer(Buffer & buf)
172 {
173         if (errorsConnection_.connected())
174                 disconnectBuffer();
175
176         BOOST_ASSERT(work_area_);
177         bufferChangedConnection_ =
178                 buf.changed.connect(
179                         boost::bind(&WorkArea::redraw, work_area_));
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         closingConnection_ =
206                 buf.closing.connect(
207                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
208 }
209
210
211 void LyXView::disconnectBuffer()
212 {
213         errorsConnection_.disconnect();
214         bufferChangedConnection_.disconnect();
215         messageConnection_.disconnect();
216         busyConnection_.disconnect();
217         titleConnection_.disconnect();
218         timerConnection_.disconnect();
219         readonlyConnection_.disconnect();
220         closingConnection_.disconnect();
221         layout_changed_connection_.disconnect();
222 }
223
224
225 void LyXView::connectBufferView(BufferView & bv)
226 {
227         show_dialog_connection_ = bv.showDialog.connect(
228                         boost::bind(&LyXView::showDialog, this, _1));
229         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
230                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
231         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
232                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
233         update_dialog_connection_ = bv.updateDialog.connect(
234                         boost::bind(&LyXView::updateDialog, this, _1, _2));
235         layout_changed_connection_ = bv.layoutChanged.connect(
236                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
237 }
238
239
240 void LyXView::disconnectBufferView()
241 {
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                 InsetBase * 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() const
292 {
293         BOOST_ASSERT(work_area_);
294         return &work_area_->bufferView();
295 }
296
297
298 void LyXView::updateToolbars()
299 {
300         BOOST_ASSERT(work_area_);
301         bool const math =
302                 work_area_->bufferView().cursor().inMathed();
303         bool const table =
304                 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
305         bool const review =
306                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
307                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
308                 
309         toolbars_->update(math, table, review);
310         // update redaonly status of open dialogs. This could also be in
311         // updateMenubar(), but since updateToolbars() and updateMenubar()
312         // are always called together it is only here.
313         getDialogs().checkStatus();
314 }
315
316
317 ToolbarBackend::Flags LyXView::getToolbarState(string const & name)
318 {
319         return toolbars_->getToolbarState(name);
320 }
321
322
323 void LyXView::toggleToolbarState(string const & name)
324 {
325         // it is possible to get current toolbar status like this,...
326         // but I decide to obey the order of ToolbarBackend::flags
327         // and disregard real toolbar status.
328         // toolbars_->saveToolbarInfo();
329         //
330         // toggle state on/off/auto
331         toolbars_->toggleToolbarState(name);
332         // update toolbar
333         updateToolbars();
334 }
335
336
337 void LyXView::updateMenubar()
338 {
339         menubar_->update();
340 }
341
342
343 void LyXView::autoSave()
344 {
345         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
346
347         if (view()->buffer())
348                 lyx::autoSave(view());
349 }
350
351
352 void LyXView::resetAutosaveTimer()
353 {
354         if (lyxrc.autosave)
355                 autosave_timeout_->restart();
356 }
357
358
359 void LyXView::updateLayoutChoice()
360 {
361         // Don't show any layouts without a buffer
362         if (!view()->buffer()) {
363                 toolbars_->clearLayoutList();
364                 return;
365         }
366
367         // Update the layout display
368         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
369                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
370         }
371
372         BOOST_ASSERT(work_area_);
373         if (work_area_->bufferView().cursor().inMathed())
374                 return;
375
376         string const & layout =
377                 work_area_->bufferView().cursor().paragraph().layout()->name();
378
379         if (layout != current_layout) {
380                 toolbars_->setLayout(layout);
381                 current_layout = layout;
382         }
383 }
384
385
386 void LyXView::updateWindowTitle()
387 {
388         docstring maximize_title = lyx::from_ascii("LyX");
389         docstring minimize_title = lyx::from_ascii("LyX");
390
391         if (view()->buffer()) {
392                 string const cur_title = buffer()->fileName();
393                 if (!cur_title.empty()) {
394                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
395                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
396                         if (!buffer()->isClean()) {
397                                 maximize_title += _(" (changed)");
398                                 minimize_title += lyx::char_type('*');
399                         }
400                         if (buffer()->isReadonly())
401                                 maximize_title += _(" (read only)");
402                 }
403         }
404
405         setWindowTitle(maximize_title, minimize_title);
406 }
407
408
409 void LyXView::dispatch(FuncRequest const & cmd)
410 {
411         theLyXFunc().setLyXView(this);
412         lyx::dispatch(cmd);
413 }
414
415
416 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
417 {
418         Buffer const * buffer_ptr = 0;
419         if (inset) {
420                 buffer_ptr = work_area_->bufferView().buffer();
421                 // No FitCursor:
422                 BOOST_ASSERT(work_area_);
423                 work_area_->bufferView().update(Update::Force);
424                 work_area_->redraw();
425         }
426         return buffer_ptr;
427 }
428
429
430 } // namespace lyx