]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.cpp
remove Dialog::title_, direct setting the title works as well.
[lyx.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         connectBuffer(*newBuffer);
122         connectBufferView(wa->bufferView());
123         setCurrentWorkArea(wa);
124
125         busy(false);
126 }
127
128
129 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
130 {
131         busy(true);
132
133         Buffer * newBuffer = checkAndLoadLyXFile(filename);
134
135         if (!newBuffer) {
136                 message(_("Document not loaded."));
137                 updateStatusBar();
138                 busy(false);
139                 return 0;
140         }
141
142         WorkArea * wa = workArea(*newBuffer);
143         if (wa == 0)
144                 wa = addWorkArea(*newBuffer);
145
146         // scroll to the position when the file was last closed
147         if (lyxrc.use_lastfilepos) {
148                 pit_type pit;
149                 pos_type pos;
150                 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
151                 // if successfully move to pit (returned par_id is not zero),
152                 // update metrics and reset font
153                 wa->bufferView().moveToPosition(pit, pos, 0, 0);
154         }
155
156         if (tolastfiles)
157                 LyX::ref().session().lastFiles().add(filename);
158
159         busy(false);
160         return newBuffer;
161 }
162
163
164 void LyXView::connectBuffer(Buffer & buf)
165 {
166         if (errorsConnection_.connected())
167                 disconnectBuffer();
168
169         bufferStructureChangedConnection_ =
170                 buf.getMasterBuffer()->structureChanged.connect(
171                         boost::bind(&LyXView::updateToc, this));
172
173         bufferEmbeddingChangedConnection_ =
174                 buf.embeddingChanged.connect(
175                         boost::bind(&LyXView::updateEmbeddedFiles, this));
176
177         errorsConnection_ =
178                 buf.errors.connect(
179                         boost::bind(&LyXView::showErrorList, this, _1));
180
181         messageConnection_ =
182                 buf.message.connect(
183                         boost::bind(&LyXView::message, this, _1));
184
185         busyConnection_ =
186                 buf.busy.connect(
187                         boost::bind(&LyXView::busy, this, _1));
188
189         titleConnection_ =
190                 buf.updateTitles.connect(
191                         boost::bind(&LyXView::updateWindowTitle, this));
192
193         timerConnection_ =
194                 buf.resetAutosaveTimers.connect(
195                         boost::bind(&LyXView::resetAutosaveTimer, this));
196
197         readonlyConnection_ =
198                 buf.readonly.connect(
199                         boost::bind(&LyXView::showReadonly, this, _1));
200 }
201
202
203 void LyXView::disconnectBuffer()
204 {
205         errorsConnection_.disconnect();
206         bufferStructureChangedConnection_.disconnect();
207         bufferEmbeddingChangedConnection_.disconnect();
208         messageConnection_.disconnect();
209         busyConnection_.disconnect();
210         titleConnection_.disconnect();
211         timerConnection_.disconnect();
212         readonlyConnection_.disconnect();
213         layout_changed_connection_.disconnect();
214 }
215
216
217 void LyXView::connectBufferView(BufferView & bv)
218 {
219         message_connection_ = bv.message.connect(
220                         boost::bind(&LyXView::message, this, _1));
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         message_connection_.disconnect();
237         show_dialog_connection_.disconnect();
238         show_dialog_with_data_connection_.disconnect();
239         show_inset_dialog_connection_.disconnect();
240         update_dialog_connection_.disconnect();
241 }
242
243
244 void LyXView::showErrorList(string const & error_type)
245 {
246         ErrorList & el = buffer()->errorList(error_type);
247         if (!el.empty()) {
248                 getDialogs().show("errorlist", error_type);
249         }
250 }
251
252
253 void LyXView::showDialog(string const & name)
254 {
255         getDialogs().show(name);
256 }
257
258
259 void LyXView::showDialogWithData(string const & name, string const & data)
260 {
261         getDialogs().show(name, data);
262 }
263
264
265 void LyXView::showInsetDialog(string const & name, string const & data,
266                 Inset * inset)
267 {
268         getDialogs().show(name, data, inset);
269 }
270
271
272 void LyXView::updateDialog(string const & name, string const & data)
273 {
274         if (getDialogs().visible(name))
275                 getDialogs().update(name, data);
276 }
277
278
279 void LyXView::showReadonly(bool)
280 {
281         updateWindowTitle();
282         getDialogs().updateBufferDependent(false);
283 }
284
285
286 BufferView * LyXView::view()
287 {
288         WorkArea * wa = currentWorkArea();
289         return wa? &wa->bufferView() : 0;
290 }
291
292
293 void LyXView::updateToc()
294 {
295         updateDialog("toc", "");
296 }
297
298
299 void LyXView::updateEmbeddedFiles()
300 {
301         updateDialog("embedding", "");
302 }
303
304
305 void LyXView::updateToolbars()
306 {
307         WorkArea * wa = currentWorkArea();
308         if (wa) {
309                 bool const math =
310                         wa->bufferView().cursor().inMathed();
311                 bool const table =
312                         lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
313                 bool const review =
314                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
315                         lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
316
317                 toolbars_->update(math, table, review);
318         } else
319                 toolbars_->update(false, false, false);
320
321         // update redaonly status of open dialogs.
322         getDialogs().checkStatus();
323 }
324
325
326 ToolbarInfo * LyXView::getToolbarInfo(string const & name)
327 {
328         return toolbars_->getToolbarInfo(name);
329 }
330
331
332 void LyXView::toggleToolbarState(string const & name, bool allowauto)
333 {
334         // it is possible to get current toolbar status like this,...
335         // but I decide to obey the order of ToolbarBackend::flags
336         // and disregard real toolbar status.
337         // toolbars_->saveToolbarInfo();
338         //
339         // toggle state on/off/auto
340         toolbars_->toggleToolbarState(name, allowauto);
341         // update toolbar
342         updateToolbars();
343 }
344
345
346 void LyXView::autoSave()
347 {
348         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
349
350         if (buffer())
351                 lyx::autoSave(view());
352 }
353
354
355 void LyXView::resetAutosaveTimer()
356 {
357         if (lyxrc.autosave)
358                 autosave_timeout_->restart();
359 }
360
361
362 void LyXView::updateLayoutChoice()
363 {
364         // Don't show any layouts without a buffer
365         if (!buffer()) {
366                 toolbars_->clearLayoutList();
367                 return;
368         }
369
370         // Update the layout display
371         if (toolbars_->updateLayoutList(buffer()->params().getTextClass_ptr())) {
372                 current_layout = buffer()->params().getTextClass().defaultLayoutName();
373         }
374
375         docstring const & layout = currentWorkArea()->bufferView().cursor().
376                 innerParagraph().layout()->name();
377
378         if (layout != current_layout) {
379                 toolbars_->setLayout(layout);
380                 current_layout = layout;
381         }
382 }
383
384
385 void LyXView::updateWindowTitle()
386 {
387         docstring maximize_title = lyx::from_ascii("LyX");
388         docstring minimize_title = lyx::from_ascii("LyX");
389
390         Buffer * buf = buffer();
391         if (buf) {
392                 string const cur_title = buf->fileName();
393                 if (!cur_title.empty()) {
394                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
395                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
396                         if (!buf->isClean()) {
397                                 maximize_title += _(" (changed)");
398                                 minimize_title += lyx::char_type('*');
399                         }
400                         if (buf->isReadonly())
401                                 maximize_title += _(" (read only)");
402                 }
403         }
404
405         setWindowTitle(maximize_title, minimize_title);
406 }
407
408
409 void LyXView::dispatch(FuncRequest const & cmd)
410 {
411         string const argument = to_utf8(cmd.argument());
412         switch(cmd.action) {
413                 case LFUN_BUFFER_SWITCH:
414                         setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
415                         break;
416                 default:
417                         theLyXFunc().setLyXView(this);
418                         lyx::dispatch(cmd);
419         }
420 }
421
422
423 Buffer const * const LyXView::updateInset(Inset const * inset)
424 {
425         WorkArea * work_area = currentWorkArea();
426         if (!work_area)
427                 return 0;
428
429         if (inset) {
430                 BOOST_ASSERT(work_area);
431                 work_area->scheduleRedraw();
432         }
433         return &work_area->bufferView().buffer();
434 }
435
436
437 void LyXView::openLayoutList()
438 {
439         toolbars_->openLayoutList();
440 }
441
442
443 bool LyXView::isToolbarVisible(std::string const & id)
444 {
445         return toolbars_->visible(id);
446 }
447
448 } // namespace frontend
449 } // namespace lyx