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