]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
move everything into namespace lyx
[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
21 #include "buffer.h"
22 #include "bufferparams.h"
23 #include "BufferView.h"
24 #include "bufferview_funcs.h"
25 #include "cursor.h"
26 #include "debug.h"
27 #include "errorlist.h"
28 #include "funcrequest.h"
29 #include "gettext.h"
30 #include "intl.h"
31 #include "lyx_cb.h"
32 #include "lyxfunc.h"
33 #include "lyxrc.h"
34 #include "lyxtext.h"
35 #include "MenuBackend.h"
36 #include "paragraph.h"
37
38 #include "controllers/ControlCommandBuffer.h"
39
40 #include "support/lstrings.h"
41 #include "support/filetools.h" // OnlyFilename()
42
43 #include <boost/bind.hpp>
44
45
46 namespace lyx {
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 lyx::frontend::WorkArea;
56
57 using lyx::docstring;
58 using lyx::support::bformat;
59 using lyx::support::makeDisplayPath;
60 using lyx::support::onlyFilename;
61
62 using std::endl;
63 using std::string;
64
65 using lyx::frontend::ControlCommandBuffer;
66
67 string current_layout;
68
69
70 LyXView::LyXView()
71         : work_area_(0),
72           toolbars_(new Toolbars(*this)),
73           autosave_timeout_(new Timeout(5000)),
74           dialogs_(new Dialogs(*this)),
75           controlcommand_(new ControlCommandBuffer(*this))
76 {
77         // Start autosave timer
78         if (lyxrc.autosave) {
79                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
80                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
81                 autosave_timeout_->start();
82         }
83 }
84
85
86 LyXView::~LyXView()
87 {
88 }
89
90
91 void LyXView::setWorkArea(WorkArea * work_area)
92 {
93         work_area_ = work_area;
94 }
95
96
97 void LyXView::redrawWorkArea()
98 {
99         work_area_->redraw();
100         updateStatusBar();
101 }
102
103
104 WorkArea * LyXView::workArea()
105 {
106         return work_area_;
107 }
108
109
110 Buffer * LyXView::buffer() const
111 {
112         return work_area_->bufferView().buffer();
113 }
114
115
116 void LyXView::setBuffer(Buffer * b)
117 {
118         if (work_area_->bufferView().buffer())
119                 disconnectBuffer();
120
121         if (!b)
122                 getDialogs().hideBufferDependent();
123
124         work_area_->bufferView().setBuffer(b);
125
126         if (work_area_->bufferView().buffer()) {
127                 // Buffer-dependent dialogs should be updated or
128                 // hidden. This should go here because some dialogs (eg ToC)
129                 // require bv_->text.
130                 getDialogs().updateBufferDependent(true);
131                 connectBuffer(*work_area_->bufferView().buffer());
132         }
133
134         updateMenubar();
135         updateToolbars();
136         updateLayoutChoice();
137         updateWindowTitle();
138         redrawWorkArea();
139 }
140
141
142 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
143 {
144         if (work_area_->bufferView().buffer())
145                 disconnectBuffer();
146
147         bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
148
149         updateMenubar();
150         updateToolbars();
151         updateLayoutChoice();
152         updateWindowTitle();
153         if (loaded) {
154                 connectBuffer(*work_area_->bufferView().buffer());
155                 showErrorList("Parse");
156         }
157         redrawWorkArea();
158         return loaded;
159 }
160
161
162 void LyXView::connectBuffer(Buffer & buf)
163 {
164         if (errorsConnection_.connected())
165                 disconnectBuffer();
166
167         errorsConnection_ =
168                 buf.errors.connect(
169                         boost::bind(&LyXView::showErrorList, this, _1));
170
171         messageConnection_ =
172                 buf.message.connect(
173                         boost::bind(&LyXView::message, this, _1));
174
175         busyConnection_ =
176                 buf.busy.connect(
177                         boost::bind(&LyXView::busy, this, _1));
178
179         titleConnection_ =
180                 buf.updateTitles.connect(
181                         boost::bind(&LyXView::updateWindowTitle, this));
182
183         timerConnection_ =
184                 buf.resetAutosaveTimers.connect(
185                         boost::bind(&LyXView::resetAutosaveTimer, this));
186
187         readonlyConnection_ =
188                 buf.readonly.connect(
189                         boost::bind(&LyXView::showReadonly, this, _1));
190
191         closingConnection_ =
192                 buf.closing.connect(
193                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
194 }
195
196
197 void LyXView::disconnectBuffer()
198 {
199         messageConnection_.disconnect();
200         busyConnection_.disconnect();
201         titleConnection_.disconnect();
202         timerConnection_.disconnect();
203         readonlyConnection_.disconnect();
204         closingConnection_.disconnect();
205         layout_changed_connection_.disconnect();
206 }
207
208
209 void LyXView::connectBufferView(BufferView & bv)
210 {
211         show_dialog_connection_ = bv.showDialog.connect(
212                         boost::bind(&LyXView::showDialog, this, _1));
213         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
214                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
215         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
216                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
217         update_dialog_connection_ = bv.updateDialog.connect(
218                         boost::bind(&LyXView::updateDialog, this, _1, _2));
219         layout_changed_connection_ = bv.layoutChanged.connect(
220                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
221 }
222
223
224 void LyXView::disconnectBufferView()
225 {
226         show_dialog_connection_.disconnect();
227         show_dialog_with_data_connection_.disconnect();
228         show_inset_dialog_connection_.disconnect();
229         update_dialog_connection_.disconnect();
230 }
231
232
233 void LyXView::showErrorList(string const & error_type)
234 {
235         ErrorList & el = buffer()->errorList(error_type);
236         if (!el.empty()) {
237                 getDialogs().show("errorlist", error_type);
238         }
239 }
240
241
242 void LyXView::showDialog(string const & name)
243 {
244         getDialogs().show(name);
245 }
246
247
248 void LyXView::showDialogWithData(string const & name, string const & data)
249 {
250         getDialogs().show(name, data);
251 }
252
253
254 void LyXView::showInsetDialog(string const & name, string const & data,
255                 InsetBase * inset)
256 {
257         getDialogs().show(name, data, inset);
258 }
259
260
261 void LyXView::updateDialog(string const & name, string const & data)
262 {
263         if (getDialogs().visible(name))
264                 getDialogs().update(name, data);
265 }
266
267
268 void LyXView::showReadonly(bool)
269 {
270         updateWindowTitle();
271         getDialogs().updateBufferDependent(false);
272 }
273
274
275 BufferView * LyXView::view() const
276 {
277         return &work_area_->bufferView();
278 }
279
280
281 void LyXView::updateToolbars()
282 {
283         bool const math = work_area_->bufferView().cursor().inMathed();
284         bool const table =
285                 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
286         toolbars_->update(math, table);
287         // update redaonly status of open dialogs. This could also be in
288         // updateMenubar(), but since updateToolbars() and updateMenubar()
289         // are always called together it is only here.
290         getDialogs().checkStatus();
291 }
292
293
294 void LyXView::updateMenubar()
295 {
296         menubar_->update();
297 }
298
299
300 void LyXView::autoSave()
301 {
302         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
303
304         if (view()->buffer())
305                 lyx::autoSave(view());
306 }
307
308
309 void LyXView::resetAutosaveTimer()
310 {
311         if (lyxrc.autosave)
312                 autosave_timeout_->restart();
313 }
314
315
316 void LyXView::updateLayoutChoice()
317 {
318         // Don't show any layouts without a buffer
319         if (!view()->buffer()) {
320                 toolbars_->clearLayoutList();
321                 return;
322         }
323
324         // Update the layout display
325         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
326                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
327         }
328
329         if (work_area_->bufferView().cursor().inMathed())
330                 return;
331
332         string const & layout =
333                 work_area_->bufferView().cursor().paragraph().layout()->name();
334
335         if (layout != current_layout) {
336                 toolbars_->setLayout(layout);
337                 current_layout = layout;
338         }
339 }
340
341
342 void LyXView::updateWindowTitle()
343 {
344         static docstring last_title = lyx::from_ascii("LyX");
345         docstring maximize_title = lyx::from_ascii("LyX");
346         docstring minimize_title = lyx::from_ascii("LyX");
347
348         if (view()->buffer()) {
349                 string const cur_title = buffer()->fileName();
350                 if (!cur_title.empty()) {
351                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
352                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
353                         if (!buffer()->isClean()) {
354                                 maximize_title += _(" (changed)");
355                                 minimize_title += lyx::char_type('*');
356                         }
357                         if (buffer()->isReadonly())
358                                 maximize_title += _(" (read only)");
359                 }
360         }
361
362         if (maximize_title != last_title) {
363                 setWindowTitle(maximize_title, minimize_title);
364                 last_title = maximize_title;
365         }
366 }
367
368
369 void LyXView::dispatch(FuncRequest const & cmd)
370 {
371         theLyXFunc().setLyXView(this);
372         lyx::dispatch(cmd);
373 }
374
375
376 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
377 {
378         Buffer const * buffer_ptr = 0;
379         if (inset) {
380                 buffer_ptr = work_area_->bufferView().buffer();
381                 // No FitCursor:
382                 work_area_->bufferView().update(Update::Force);
383         }
384         return buffer_ptr;
385 }
386
387
388 } // namespace lyx