]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.cpp
Move Color::color enum to ColorCode.h
[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 "WorkArea.h"
18 #include "Gui.h"
19
20 #include "Buffer.h"
21 #include "buffer_funcs.h"
22 #include "BufferList.h"
23 #include "BufferParams.h"
24 #include "BufferView.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 "Layout.h"
32 #include "LyX.h"
33 #include "LyXFunc.h"
34 #include "LyXRC.h"
35 #include "MenuBackend.h"
36 #include "Paragraph.h"
37 #include "Session.h"
38 #include "Text.h"
39
40 #include "support/lstrings.h"
41 #include "support/filetools.h" // OnlyFilename()
42 #include "support/Timeout.h"
43
44 #include <boost/bind.hpp>
45
46
47 #ifdef HAVE_SYS_TIME_H
48 # include <sys/time.h>
49 #endif
50 #ifdef HAVE_UNISTD_H
51 # include <unistd.h>
52 #endif
53
54 using std::endl;
55 using std::string;
56
57 namespace lyx {
58
59 using support::bformat;
60 using support::FileName;
61 using support::makeDisplayPath;
62 using support::onlyFilename;
63
64 namespace frontend {
65
66 LyXView::LyXView(int id)
67         : autosave_timeout_(new Timeout(5000)),
68           dialogs_(new Dialogs(*this)),
69           id_(id)
70 {
71         // Start autosave timer
72         if (lyxrc.autosave) {
73                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
74                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
75                 autosave_timeout_->start();
76         }
77 }
78
79
80 LyXView::~LyXView()
81 {
82         delete dialogs_;
83         delete autosave_timeout_;
84 }
85
86
87 Buffer * LyXView::buffer()
88 {
89         WorkArea * work_area = currentWorkArea();
90         if (work_area)
91                 return &work_area->bufferView().buffer();
92         return 0;
93 }
94
95
96 Buffer const * LyXView::buffer() const
97 {
98         WorkArea const * work_area = currentWorkArea();
99         if (work_area)
100                 return &work_area->bufferView().buffer();
101         return 0;
102 }
103
104
105 void LyXView::setBuffer(Buffer * newBuffer)
106 {
107         BOOST_ASSERT(newBuffer);
108         setBusy(true);
109
110         WorkArea * wa = workArea(*newBuffer);
111         if (wa == 0) {
112                 updateLabels(*newBuffer->masterBuffer());
113                 wa = addWorkArea(*newBuffer);
114         } else {
115                 //Disconnect the old buffer...there's no new one.
116                 disconnectBuffer();
117         }
118         connectBuffer(*newBuffer);
119         connectBufferView(wa->bufferView());
120         setCurrentWorkArea(wa);
121
122         setBusy(false);
123 }
124
125
126 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
127 {
128         setBusy(true);
129
130         Buffer * newBuffer = checkAndLoadLyXFile(filename);
131
132         if (!newBuffer) {
133                 message(_("Document not loaded."));
134                 updateStatusBar();
135                 setBusy(false);
136                 return 0;
137         }
138
139         WorkArea * wa = workArea(*newBuffer);
140         if (wa == 0)
141                 wa = addWorkArea(*newBuffer);
142
143         // scroll to the position when the file was last closed
144         if (lyxrc.use_lastfilepos) {
145                 pit_type pit;
146                 pos_type pos;
147                 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
148                 // if successfully move to pit (returned par_id is not zero),
149                 // update metrics and reset font
150                 wa->bufferView().moveToPosition(pit, pos, 0, 0);
151         }
152
153         if (tolastfiles)
154                 LyX::ref().session().lastFiles().add(filename);
155
156         setBusy(false);
157         return newBuffer;
158 }
159
160
161 void LyXView::connectBuffer(Buffer & buf)
162 {
163         buf.setGuiDelegate(this);
164 }
165
166
167 void LyXView::disconnectBuffer()
168 {
169         if (WorkArea * work_area = currentWorkArea())
170                 work_area->bufferView().setGuiDelegate(0);
171 }
172
173
174 void LyXView::connectBufferView(BufferView & bv)
175 {
176         bv.setGuiDelegate(this);
177 }
178
179
180 void LyXView::disconnectBufferView()
181 {
182         if (WorkArea * work_area = currentWorkArea())
183                 work_area->bufferView().setGuiDelegate(0);
184 }
185
186
187 void LyXView::showErrorList(string const & error_type)
188 {
189         ErrorList & el = buffer()->errorList(error_type);
190         if (!el.empty())
191                 getDialogs().show("errorlist", error_type);
192 }
193
194
195 void LyXView::showDialog(string const & name)
196 {
197         getDialogs().show(name);
198 }
199
200
201 void LyXView::showDialogWithData(string const & name, string const & data)
202 {
203         getDialogs().show(name, data);
204 }
205
206
207 void LyXView::showInsetDialog(string const & name, string const & data,
208                 Inset * inset)
209 {
210         getDialogs().show(name, data, inset);
211 }
212
213
214 void LyXView::updateDialog(string const & name, string const & data)
215 {
216         if (getDialogs().visible(name))
217                 getDialogs().update(name, data);
218 }
219
220
221 void LyXView::setReadOnly(bool)
222 {
223         updateWindowTitle();
224         getDialogs().updateBufferDependent(false);
225 }
226
227
228 BufferView * LyXView::view()
229 {
230         WorkArea * wa = currentWorkArea();
231         return wa? &wa->bufferView() : 0;
232 }
233
234
235 void LyXView::updateToc()
236 {
237         updateDialog("toc", "");
238 }
239
240
241 void LyXView::updateEmbeddedFiles()
242 {
243         updateDialog("embedding", "");
244 }
245
246
247 void LyXView::autoSave()
248 {
249         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
250
251         if (buffer())
252                 view()->buffer().autoSave();
253 }
254
255
256 void LyXView::resetAutosaveTimer()
257 {
258         if (lyxrc.autosave)
259                 autosave_timeout_->restart();
260 }
261
262
263 void LyXView::updateWindowTitle()
264 {
265         docstring maximize_title = from_ascii("LyX");
266         docstring minimize_title = from_ascii("LyX");
267
268         Buffer * buf = buffer();
269         if (buf) {
270                 string const cur_title = buf->absFileName();
271                 if (!cur_title.empty()) {
272                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
273                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
274                         if (!buf->isClean()) {
275                                 maximize_title += _(" (changed)");
276                                 minimize_title += char_type('*');
277                         }
278                         if (buf->isReadonly())
279                                 maximize_title += _(" (read only)");
280                 }
281         }
282
283         setWindowTitle(maximize_title, minimize_title);
284 }
285
286
287 void LyXView::dispatch(FuncRequest const & cmd)
288 {
289         string const argument = to_utf8(cmd.argument());
290         switch(cmd.action) {
291                 case LFUN_BUFFER_SWITCH:
292                         setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
293                         break;
294                 default:
295                         theLyXFunc().setLyXView(this);
296                         lyx::dispatch(cmd);
297         }
298 }
299
300
301 Buffer const * LyXView::updateInset(Inset const * inset)
302 {
303         WorkArea * work_area = currentWorkArea();
304         if (!work_area)
305                 return 0;
306
307         if (inset) {
308                 BOOST_ASSERT(work_area);
309                 work_area->scheduleRedraw();
310         }
311         return &work_area->bufferView().buffer();
312 }
313
314 } // namespace frontend
315 } // namespace lyx