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