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