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