]> git.lyx.org Git - features.git/blob - src/frontends/LyXView.cpp
Remove warnings reported with gcc 4.3:
[features.git] / src / frontends / LyXView.cpp
1 /**
2  * \file LyXView.cpp
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
16 #include "Dialogs.h"
17 #include "Toolbars.h"
18 #include "WorkArea.h"
19 #include "Gui.h"
20
21 #include "Buffer.h"
22 #include "buffer_funcs.h"
23 #include "BufferList.h"
24 #include "BufferParams.h"
25 #include "BufferView.h"
26 #include "bufferview_funcs.h"
27 #include "Cursor.h"
28 #include "debug.h"
29 #include "ErrorList.h"
30 #include "FuncRequest.h"
31 #include "gettext.h"
32 #include "Intl.h"
33 #include "callback.h"
34 #include "LyX.h"
35 #include "LyXFunc.h"
36 #include "LyXRC.h"
37 #include "Text.h"
38 #include "MenuBackend.h"
39 #include "Paragraph.h"
40
41 #include "support/lstrings.h"
42 #include "support/filetools.h" // OnlyFilename()
43 #include "support/Timeout.h"
44
45 #include <boost/bind.hpp>
46
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 std::endl;
56 using std::string;
57
58 namespace lyx {
59
60 using support::bformat;
61 using support::FileName;
62 using support::makeDisplayPath;
63 using support::onlyFilename;
64
65 namespace frontend {
66
67 docstring current_layout;
68
69 LyXView::LyXView(int id)
70         : toolbars_(new Toolbars(*this)),
71           autosave_timeout_(new Timeout(5000)),
72           dialogs_(new Dialogs(*this)),
73           id_(id)
74 {
75         // Start autosave timer
76         if (lyxrc.autosave) {
77                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
78                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
79                 autosave_timeout_->start();
80         }
81 }
82
83
84 LyXView::~LyXView()
85 {
86         disconnectBuffer();
87         disconnectBufferView();
88 }
89
90
91 Buffer * LyXView::buffer()
92 {
93         WorkArea * work_area = currentWorkArea();
94         if (work_area)
95                 return &work_area->bufferView().buffer();
96         return 0;
97 }
98
99
100 Buffer const * LyXView::buffer() const
101 {
102         WorkArea const * work_area = currentWorkArea();
103         if (work_area)
104                 return &work_area->bufferView().buffer();
105         return 0;
106 }
107
108
109 void LyXView::setBuffer(Buffer * newBuffer)
110 {
111         BOOST_ASSERT(newBuffer);
112         busy(true);
113
114         WorkArea * wa = workArea(*newBuffer);
115         if (wa == 0) {
116                 updateLabels(*newBuffer->getMasterBuffer());
117                 wa = addWorkArea(*newBuffer);
118         } else {
119                 //Disconnect the old buffer...there's no new one.
120                 disconnectBuffer();
121         }
122         connectBuffer(*newBuffer);
123         connectBufferView(wa->bufferView());
124         setCurrentWorkArea(wa);
125
126         busy(false);
127 }
128
129
130 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
131 {
132         busy(true);
133
134         Buffer * newBuffer = checkAndLoadLyXFile(filename);
135
136         if (!newBuffer) {
137                 message(_("Document not loaded."));
138                 updateStatusBar();
139                 busy(false);
140                 return 0;
141         }
142
143         WorkArea * wa = workArea(*newBuffer);
144         if (wa == 0)
145                 wa = addWorkArea(*newBuffer);
146
147         // scroll to the position when the file was last closed
148         if (lyxrc.use_lastfilepos) {
149                 pit_type pit;
150                 pos_type pos;
151                 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
152                 // if successfully move to pit (returned par_id is not zero),
153                 // update metrics and reset font
154                 wa->bufferView().moveToPosition(pit, pos, 0, 0);
155         }
156
157         if (tolastfiles)
158                 LyX::ref().session().lastFiles().add(filename);
159
160         busy(false);
161         return newBuffer;
162 }
163
164
165 void LyXView::connectBuffer(Buffer & buf)
166 {
167         if (errorsConnection_.connected())
168                 disconnectBuffer();
169
170         bufferStructureChangedConnection_ =
171                 buf.getMasterBuffer()->structureChanged.connect(
172                         boost::bind(&LyXView::updateToc, this));
173
174         bufferEmbeddingChangedConnection_ =
175                 buf.embeddingChanged.connect(
176                         boost::bind(&LyXView::updateEmbeddedFiles, this));
177
178         errorsConnection_ =
179                 buf.errors.connect(
180                         boost::bind(&LyXView::showErrorList, this, _1));
181
182         messageConnection_ =
183                 buf.message.connect(
184                         boost::bind(&LyXView::message, this, _1));
185
186         busyConnection_ =
187                 buf.busy.connect(
188                         boost::bind(&LyXView::busy, this, _1));
189
190         titleConnection_ =
191                 buf.updateTitles.connect(
192                         boost::bind(&LyXView::updateWindowTitle, this));
193
194         timerConnection_ =
195                 buf.resetAutosaveTimers.connect(
196                         boost::bind(&LyXView::resetAutosaveTimer, this));
197
198         readonlyConnection_ =
199                 buf.readonly.connect(
200                         boost::bind(&LyXView::showReadonly, this, _1));
201 }
202
203
204 void LyXView::disconnectBuffer()
205 {
206         errorsConnection_.disconnect();
207         bufferStructureChangedConnection_.disconnect();
208         bufferEmbeddingChangedConnection_.disconnect();
209         messageConnection_.disconnect();
210         busyConnection_.disconnect();
211         titleConnection_.disconnect();
212         timerConnection_.disconnect();
213         readonlyConnection_.disconnect();
214         layout_changed_connection_.disconnect();
215 }
216
217
218 void LyXView::connectBufferView(BufferView & bv)
219 {
220         message_connection_ = bv.message.connect(
221                         boost::bind(&LyXView::message, this, _1));
222         show_dialog_connection_ = bv.showDialog.connect(
223                         boost::bind(&LyXView::showDialog, this, _1));
224         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
225                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
226         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
227                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
228         update_dialog_connection_ = bv.updateDialog.connect(
229                         boost::bind(&LyXView::updateDialog, this, _1, _2));
230         layout_changed_connection_ = bv.layoutChanged.connect(
231                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
232 }
233
234
235 void LyXView::disconnectBufferView()
236 {
237         message_connection_.disconnect();
238         show_dialog_connection_.disconnect();
239         show_dialog_with_data_connection_.disconnect();
240         show_inset_dialog_connection_.disconnect();
241         update_dialog_connection_.disconnect();
242 }
243
244
245 void LyXView::showErrorList(string const & error_type)
246 {
247         ErrorList & el = buffer()->errorList(error_type);
248         if (!el.empty()) {
249                 getDialogs().show("errorlist", error_type);
250         }
251 }
252
253
254 void LyXView::showDialog(string const & name)
255 {
256         getDialogs().show(name);
257 }
258
259
260 void LyXView::showDialogWithData(string const & name, string const & data)
261 {
262         getDialogs().show(name, data);
263 }
264
265
266 void LyXView::showInsetDialog(string const & name, string const & data,
267                 Inset * inset)
268 {
269         getDialogs().show(name, data, inset);
270 }
271
272
273 void LyXView::updateDialog(string const & name, string const & data)
274 {
275         if (getDialogs().visible(name))
276                 getDialogs().update(name, data);
277 }
278
279
280 void LyXView::showReadonly(bool)
281 {
282         updateWindowTitle();
283         getDialogs().updateBufferDependent(false);
284 }
285
286
287 BufferView * LyXView::view()
288 {
289         WorkArea * wa = currentWorkArea();
290         return wa? &wa->bufferView() : 0;
291 }
292
293
294 void LyXView::updateToc()
295 {
296         updateDialog("toc", "");
297 }
298
299
300 void LyXView::updateEmbeddedFiles()
301 {
302         updateDialog("embedding", "");
303 }
304
305
306 void LyXView::updateToolbars()
307 {
308         WorkArea * wa = currentWorkArea();
309         if (wa) {
310                 bool const math =
311                         wa->bufferView().cursor().inMathed();
312                 bool const table =
313                         lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
314                 bool const review =
315                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
316                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
317
318                 toolbars_->update(math, table, review);
319         } else
320                 toolbars_->update(false, false, false);
321
322         // update redaonly status of open dialogs.
323         getDialogs().checkStatus();
324 }
325
326
327 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
328 {
329         return toolbars_->getToolbarInfo(name);
330 }
331
332
333 void LyXView::toggleToolbarState(string const & name, bool allowauto)
334 {
335         // it is possible to get current toolbar status like this,...
336         // but I decide to obey the order of ToolbarBackend::flags
337         // and disregard real toolbar status.
338         // toolbars_->saveToolbarInfo();
339         //
340         // toggle state on/off/auto
341         toolbars_->toggleToolbarState(name, allowauto);
342         // update toolbar
343         updateToolbars();
344 }
345
346
347 void LyXView::autoSave()
348 {
349         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
350
351         if (buffer())
352                 lyx::autoSave(view());
353 }
354
355
356 void LyXView::resetAutosaveTimer()
357 {
358         if (lyxrc.autosave)
359                 autosave_timeout_->restart();
360 }
361
362
363 void LyXView::updateLayoutChoice()
364 {
365         // Don't show any layouts without a buffer
366         if (!buffer()) {
367                 toolbars_->clearLayoutList();
368                 return;
369         }
370
371         // Update the layout display
372         if (toolbars_->updateLayoutList(buffer()->params().getTextClass_ptr())) {
373                 current_layout = buffer()->params().getTextClass().defaultLayoutName();
374         }
375
376         docstring const & layout = currentWorkArea()->bufferView().cursor().
377                 innerParagraph().layout()->name();
378
379         if (layout != current_layout) {
380                 toolbars_->setLayout(layout);
381                 current_layout = layout;
382         }
383 }
384
385
386 void LyXView::updateWindowTitle()
387 {
388         docstring maximize_title = from_ascii("LyX");
389         docstring minimize_title = from_ascii("LyX");
390
391         Buffer * buf = buffer();
392         if (buf) {
393                 string const cur_title = buf->fileName();
394                 if (!cur_title.empty()) {
395                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
396                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
397                         if (!buf->isClean()) {
398                                 maximize_title += _(" (changed)");
399                                 minimize_title += char_type('*');
400                         }
401                         if (buf->isReadonly())
402                                 maximize_title += _(" (read only)");
403                 }
404         }
405
406         setWindowTitle(maximize_title, minimize_title);
407 }
408
409
410 void LyXView::dispatch(FuncRequest const & cmd)
411 {
412         string const argument = to_utf8(cmd.argument());
413         switch(cmd.action) {
414                 case LFUN_BUFFER_SWITCH:
415                         setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
416                         break;
417                 default:
418                         theLyXFunc().setLyXView(this);
419                         lyx::dispatch(cmd);
420         }
421 }
422
423
424 Buffer const * LyXView::updateInset(Inset const * inset)
425 {
426         WorkArea * work_area = currentWorkArea();
427         if (!work_area)
428                 return 0;
429
430         if (inset) {
431                 BOOST_ASSERT(work_area);
432                 work_area->scheduleRedraw();
433         }
434         return &work_area->bufferView().buffer();
435 }
436
437
438 void LyXView::openLayoutList()
439 {
440         toolbars_->openLayoutList();
441 }
442
443
444 bool LyXView::isToolbarVisible(std::string const & id)
445 {
446         return toolbars_->visible(id);
447 }
448
449 } // namespace frontend
450 } // namespace lyx