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