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