]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.cpp
Move most of the Gui specific code in Toolbars to its new qt4 specialization GuiToolbars.
[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 "callback.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 "Layout.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 LyXView::LyXView(int id)
68         : autosave_timeout_(new Timeout(5000)),
69           dialogs_(new Dialogs(*this)),
70           id_(id)
71 {
72         // Start autosave timer
73         if (lyxrc.autosave) {
74                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
75                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
76                 autosave_timeout_->start();
77         }
78 }
79
80
81 LyXView::~LyXView()
82 {
83         disconnectBuffer();
84         disconnectBufferView();
85         delete dialogs_;
86         delete autosave_timeout_;
87 }
88
89
90 Buffer * LyXView::buffer()
91 {
92         WorkArea * work_area = currentWorkArea();
93         if (work_area)
94                 return &work_area->bufferView().buffer();
95         return 0;
96 }
97
98
99 Buffer const * LyXView::buffer() const
100 {
101         WorkArea const * work_area = currentWorkArea();
102         if (work_area)
103                 return &work_area->bufferView().buffer();
104         return 0;
105 }
106
107
108 void LyXView::setBuffer(Buffer * newBuffer)
109 {
110         BOOST_ASSERT(newBuffer);
111         busy(true);
112
113         WorkArea * wa = workArea(*newBuffer);
114         if (wa == 0) {
115                 updateLabels(*newBuffer->getMasterBuffer());
116                 wa = addWorkArea(*newBuffer);
117         } else {
118                 //Disconnect the old buffer...there's no new one.
119                 disconnectBuffer();
120         }
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 }
214
215
216 void LyXView::connectBufferView(BufferView & bv)
217 {
218         message_connection_ = bv.message.connect(
219                         boost::bind(&LyXView::message, this, _1));
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 }
229
230
231 void LyXView::disconnectBufferView()
232 {
233         message_connection_.disconnect();
234         show_dialog_connection_.disconnect();
235         show_dialog_with_data_connection_.disconnect();
236         show_inset_dialog_connection_.disconnect();
237         update_dialog_connection_.disconnect();
238 }
239
240
241 void LyXView::showErrorList(string const & error_type)
242 {
243         ErrorList & el = buffer()->errorList(error_type);
244         if (!el.empty()) {
245                 getDialogs().show("errorlist", error_type);
246         }
247 }
248
249
250 void LyXView::showDialog(string const & name)
251 {
252         getDialogs().show(name);
253 }
254
255
256 void LyXView::showDialogWithData(string const & name, string const & data)
257 {
258         getDialogs().show(name, data);
259 }
260
261
262 void LyXView::showInsetDialog(string const & name, string const & data,
263                 Inset * inset)
264 {
265         getDialogs().show(name, data, inset);
266 }
267
268
269 void LyXView::updateDialog(string const & name, string const & data)
270 {
271         if (getDialogs().visible(name))
272                 getDialogs().update(name, data);
273 }
274
275
276 void LyXView::showReadonly(bool)
277 {
278         updateWindowTitle();
279         getDialogs().updateBufferDependent(false);
280 }
281
282
283 BufferView * LyXView::view()
284 {
285         WorkArea * wa = currentWorkArea();
286         return wa? &wa->bufferView() : 0;
287 }
288
289
290 void LyXView::updateToc()
291 {
292         updateDialog("toc", "");
293 }
294
295
296 void LyXView::updateEmbeddedFiles()
297 {
298         updateDialog("embedding", "");
299 }
300
301
302 void LyXView::autoSave()
303 {
304         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
305
306         if (buffer())
307                 lyx::autoSave(view());
308 }
309
310
311 void LyXView::resetAutosaveTimer()
312 {
313         if (lyxrc.autosave)
314                 autosave_timeout_->restart();
315 }
316
317
318 void LyXView::updateWindowTitle()
319 {
320         docstring maximize_title = from_ascii("LyX");
321         docstring minimize_title = from_ascii("LyX");
322
323         Buffer * buf = buffer();
324         if (buf) {
325                 string const cur_title = buf->fileName();
326                 if (!cur_title.empty()) {
327                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
328                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
329                         if (!buf->isClean()) {
330                                 maximize_title += _(" (changed)");
331                                 minimize_title += char_type('*');
332                         }
333                         if (buf->isReadonly())
334                                 maximize_title += _(" (read only)");
335                 }
336         }
337
338         setWindowTitle(maximize_title, minimize_title);
339 }
340
341
342 void LyXView::dispatch(FuncRequest const & cmd)
343 {
344         string const argument = to_utf8(cmd.argument());
345         switch(cmd.action) {
346                 case LFUN_BUFFER_SWITCH:
347                         setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
348                         break;
349                 default:
350                         theLyXFunc().setLyXView(this);
351                         lyx::dispatch(cmd);
352         }
353 }
354
355
356 Buffer const * LyXView::updateInset(Inset const * inset)
357 {
358         WorkArea * work_area = currentWorkArea();
359         if (!work_area)
360                 return 0;
361
362         if (inset) {
363                 BOOST_ASSERT(work_area);
364                 work_area->scheduleRedraw();
365         }
366         return &work_area->bufferView().buffer();
367 }
368
369
370 } // namespace frontend
371 } // namespace lyx