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