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