]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
forgot this
[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, string const & data)
246 {
247         getDialogs().show(name, data);
248 }
249
250
251 void LyXView::showInsetDialog(string const & name, string const & data,
252                 InsetBase * inset)
253 {
254         getDialogs().show(name, data, inset);
255 }
256
257
258 void LyXView::updateDialog(string const & name, string const & data)
259 {
260         if (getDialogs().visible(name))
261                 getDialogs().update(name, data);
262 }
263
264
265 void LyXView::showReadonly(bool)
266 {
267         updateWindowTitle();
268         getDialogs().updateBufferDependent(false);
269 }
270
271
272 BufferView * LyXView::view() const
273 {
274         return &work_area_->bufferView();
275 }
276
277
278 void LyXView::updateToolbars()
279 {
280         bool const math = work_area_->bufferView().cursor().inMathed();
281         bool const table =
282                 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
283         toolbars_->update(math, table);
284         // update redaonly status of open dialogs. This could also be in
285         // updateMenubar(), but since updateToolbars() and updateMenubar()
286         // are always called together it is only here.
287         getDialogs().checkStatus();
288 }
289
290
291 void LyXView::updateMenubar()
292 {
293         menubar_->update();
294 }
295
296
297 void LyXView::autoSave()
298 {
299         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
300
301         if (view()->buffer()) {
302                 ::autoSave(view());
303         }
304 }
305
306
307 void LyXView::resetAutosaveTimer()
308 {
309         if (lyxrc.autosave)
310                 autosave_timeout_->restart();
311 }
312
313
314 void LyXView::updateLayoutChoice()
315 {
316         // Don't show any layouts without a buffer
317         if (!view()->buffer()) {
318                 toolbars_->clearLayoutList();
319                 return;
320         }
321
322         // Update the layout display
323         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
324                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
325         }
326
327         if (work_area_->bufferView().cursor().inMathed())
328                 return;
329
330         string const & layout =
331                 work_area_->bufferView().cursor().paragraph().layout()->name();
332
333         if (layout != current_layout) {
334                 toolbars_->setLayout(layout);
335                 current_layout = layout;
336         }
337 }
338
339
340 void LyXView::updateWindowTitle()
341 {
342         static docstring last_title = lyx::from_ascii("LyX");
343         docstring maximize_title = lyx::from_ascii("LyX");
344         docstring minimize_title = lyx::from_ascii("LyX");
345
346         if (view()->buffer()) {
347                 string const cur_title = buffer()->fileName();
348                 if (!cur_title.empty()) {
349                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
350                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
351                         if (!buffer()->isClean()) {
352                                 maximize_title += _(" (changed)");
353                                 minimize_title += lyx::char_type('*');
354                         }
355                         if (buffer()->isReadonly())
356                                 maximize_title += _(" (read only)");
357                 }
358         }
359
360         if (maximize_title != last_title) {
361                 setWindowTitle(maximize_title, minimize_title);
362                 last_title = maximize_title;
363         }
364 }
365
366
367 void LyXView::dispatch(FuncRequest const & cmd)
368 {
369         theLyXFunc().setLyXView(this);
370         lyx::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 }