]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
This commit replaces BufferView->LyXView->Gui->[selection,clipboard] with theApp...
[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 #ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h>
47 #endif
48 #ifdef HAVE_UNISTD_H
49 # include <unistd.h>
50 #endif
51
52 using lyx::frontend::WorkArea;
53
54 using lyx::docstring;
55 using lyx::support::bformat;
56 using lyx::support::makeDisplayPath;
57 using lyx::support::onlyFilename;
58
59 using std::endl;
60 using std::string;
61
62 using lyx::frontend::ControlCommandBuffer;
63
64 string current_layout;
65
66
67 LyXView::LyXView()
68         : work_area_(0),
69           toolbars_(new Toolbars(*this)),
70           autosave_timeout_(new Timeout(5000)),
71           lyxfunc_(new LyXFunc(this)),
72           dialogs_(new Dialogs(*this)),
73           controlcommand_(new ControlCommandBuffer(*this))
74 {
75         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
76 }
77
78
79 LyXView::~LyXView()
80 {
81 }
82
83
84 void LyXView::setWorkArea(WorkArea * work_area)
85 {
86         work_area_ = work_area;
87 }
88
89
90 void LyXView::redrawWorkArea()
91 {
92         work_area_->redraw();
93         updateStatusBar();
94 }
95
96
97 WorkArea * LyXView::workArea()
98 {
99         return work_area_;
100 }
101
102
103 void LyXView::init()
104 {
105         updateLayoutChoice();
106         updateMenubar();
107
108         // Start autosave timer
109         if (lyxrc.autosave) {
110                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
111                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
112                 autosave_timeout_->start();
113         }
114 }
115
116
117 Buffer * LyXView::buffer() const
118 {
119         return work_area_->bufferView().buffer();
120 }
121
122
123 void LyXView::setBuffer(Buffer * b)
124 {
125         if (work_area_->bufferView().buffer())
126                 disconnectBuffer();
127
128         if (!b)
129                 getDialogs().hideBufferDependent();
130
131         work_area_->bufferView().setBuffer(b);
132
133         if (work_area_->bufferView().buffer()) {
134                 // Buffer-dependent dialogs should be updated or
135                 // hidden. This should go here because some dialogs (eg ToC)
136                 // require bv_->text.
137                 getDialogs().updateBufferDependent(true);
138                 connectBuffer(*work_area_->bufferView().buffer());
139         }
140
141         updateMenubar();
142         updateToolbars();
143         updateLayoutChoice();
144         updateWindowTitle();
145         redrawWorkArea();
146 }
147
148
149 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
150 {
151         if (work_area_->bufferView().buffer())
152                 disconnectBuffer();
153
154         bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
155
156         updateMenubar();
157         updateToolbars();
158         updateLayoutChoice();
159         updateWindowTitle();
160         if (loaded) {
161                 connectBuffer(*work_area_->bufferView().buffer());
162                 showErrorList("Parse");
163         }
164         redrawWorkArea();
165         return loaded;
166 }
167
168
169 void LyXView::connectBuffer(Buffer & buf)
170 {
171         if (errorsConnection_.connected())
172                 disconnectBuffer();
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         messageConnection_.disconnect();
207         busyConnection_.disconnect();
208         titleConnection_.disconnect();
209         timerConnection_.disconnect();
210         readonlyConnection_.disconnect();
211         closingConnection_.disconnect();
212 }
213
214
215 void LyXView::connectBufferView(BufferView & bv)
216 {
217         show_dialog_connection_ = bv.showDialog.connect(
218                         boost::bind(&LyXView::showDialog, this, _1));
219         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
220                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
221         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
222                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
223         update_dialog_connection_ = bv.updateDialog.connect(
224                         boost::bind(&LyXView::updateDialog, this, _1, _2));
225 }
226
227
228 void LyXView::disconnectBufferView()
229 {
230         show_dialog_connection_.disconnect();
231         show_dialog_with_data_connection_.disconnect();
232         show_inset_dialog_connection_.disconnect();
233         update_dialog_connection_.disconnect();
234 }
235
236
237 void LyXView::showErrorList(string const & error_type)
238 {
239         ErrorList & el = buffer()->errorList(error_type);
240         if (!el.empty()) {
241                 getDialogs().show("errorlist", error_type);
242         }
243 }
244
245
246 void LyXView::showDialog(string const & name)
247 {
248         getDialogs().show(name);
249 }
250
251
252 void LyXView::showDialogWithData(string const & name,
253                                                                  string const & data)
254 {
255         getDialogs().show(name, data);
256 }
257
258
259 void LyXView::showInsetDialog(string const & name, string const & data,
260                                                           InsetBase * inset)
261 {
262         getDialogs().show(name, data, 0);
263 }
264
265
266 void LyXView::updateDialog(string const & name, string const & data)
267 {
268         if (getDialogs().visible(name))
269                 getDialogs().update(name, data);
270 }
271
272
273 void LyXView::showReadonly(bool)
274 {
275         updateWindowTitle();
276         getDialogs().updateBufferDependent(false);
277 }
278
279
280 BufferView * LyXView::view() const
281 {
282         return &work_area_->bufferView();
283 }
284
285
286 void LyXView::setLayout(string const & layout)
287 {
288         toolbars_->setLayout(layout);
289 }
290
291
292 void LyXView::updateToolbars()
293 {
294         bool const math = work_area_->bufferView().cursor().inMathed();
295         bool const table =
296                 getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
297         toolbars_->update(math, table);
298         // update redaonly status of open dialogs. This could also be in
299         // updateMenubar(), but since updateToolbars() and updateMenubar()
300         // are always called together it is only here.
301         getDialogs().checkStatus();
302 }
303
304
305 void LyXView::updateMenubar()
306 {
307         menubar_->update();
308 }
309
310
311 void LyXView::autoSave()
312 {
313         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
314
315         if (view()->buffer()) {
316                 ::autoSave(view());
317         }
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 += lyx::from_ascii(": ") + 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         getLyXFunc().dispatch(cmd);
384 }
385
386
387 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
388 {
389         Buffer const * buffer_ptr = 0;
390         if (inset) {
391                 buffer_ptr = work_area_->bufferView().buffer();
392                 // No FitCursor:
393                 work_area_->bufferView().update(Update::Force);
394         }
395         return buffer_ptr;
396 }