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