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