]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.cpp
pimpl not needed here
[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                 LastFilePosSection::FilePos filepos =
146                         LyX::ref().session().lastFilePos().load(filename);
147                 // if successfully move to pit (returned par_id is not zero),
148                 // update metrics and reset font
149                 wa->bufferView().moveToPosition(filepos.pit, filepos.pos, 0, 0);
150         }
151
152         if (tolastfiles)
153                 LyX::ref().session().lastFiles().add(filename);
154
155         setBusy(false);
156         return newBuffer;
157 }
158
159
160 void LyXView::connectBuffer(Buffer & buf)
161 {
162         buf.setGuiDelegate(this);
163 }
164
165
166 void LyXView::disconnectBuffer()
167 {
168         if (WorkArea * work_area = currentWorkArea())
169                 work_area->bufferView().setGuiDelegate(0);
170 }
171
172
173 void LyXView::connectBufferView(BufferView & bv)
174 {
175         bv.setGuiDelegate(this);
176 }
177
178
179 void LyXView::disconnectBufferView()
180 {
181         if (WorkArea * work_area = currentWorkArea())
182                 work_area->bufferView().setGuiDelegate(0);
183 }
184
185
186 void LyXView::showErrorList(string const & error_type)
187 {
188         ErrorList & el = buffer()->errorList(error_type);
189         if (!el.empty())
190                 getDialogs().show("errorlist", error_type);
191 }
192
193
194 void LyXView::showDialog(string const & name)
195 {
196         getDialogs().show(name);
197 }
198
199
200 void LyXView::showDialogWithData(string const & name, string const & data)
201 {
202         getDialogs().show(name, data);
203 }
204
205
206 void LyXView::showInsetDialog(string const & name, string const & data,
207                 Inset * inset)
208 {
209         getDialogs().show(name, data, inset);
210 }
211
212
213 void LyXView::updateDialog(string const & name, string const & data)
214 {
215         if (getDialogs().visible(name))
216                 getDialogs().update(name, data);
217 }
218
219
220 void LyXView::setReadOnly(bool)
221 {
222         updateWindowTitle();
223         getDialogs().updateBufferDependent(false);
224 }
225
226
227 BufferView * LyXView::view()
228 {
229         WorkArea * wa = currentWorkArea();
230         return wa? &wa->bufferView() : 0;
231 }
232
233
234 void LyXView::updateToc()
235 {
236         updateDialog("toc", "");
237 }
238
239
240 void LyXView::updateEmbeddedFiles()
241 {
242         updateDialog("embedding", "");
243 }
244
245
246 void LyXView::autoSave()
247 {
248         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
249
250         if (buffer())
251                 view()->buffer().autoSave();
252 }
253
254
255 void LyXView::resetAutosaveTimer()
256 {
257         if (lyxrc.autosave)
258                 autosave_timeout_->restart();
259 }
260
261
262 void LyXView::updateWindowTitle()
263 {
264         docstring maximize_title = from_ascii("LyX");
265         docstring minimize_title = from_ascii("LyX");
266
267         Buffer * buf = buffer();
268         if (buf) {
269                 string const cur_title = buf->absFileName();
270                 if (!cur_title.empty()) {
271                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
272                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
273                         if (!buf->isClean()) {
274                                 maximize_title += _(" (changed)");
275                                 minimize_title += char_type('*');
276                         }
277                         if (buf->isReadonly())
278                                 maximize_title += _(" (read only)");
279                 }
280         }
281
282         setWindowTitle(maximize_title, minimize_title);
283 }
284
285
286 void LyXView::dispatch(FuncRequest const & cmd)
287 {
288         string const argument = to_utf8(cmd.argument());
289         switch(cmd.action) {
290                 case LFUN_BUFFER_SWITCH:
291                         setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
292                         break;
293                 default:
294                         theLyXFunc().setLyXView(this);
295                         lyx::dispatch(cmd);
296         }
297 }
298
299
300 Buffer const * LyXView::updateInset(Inset const * inset)
301 {
302         WorkArea * work_area = currentWorkArea();
303         if (!work_area)
304                 return 0;
305
306         if (inset) {
307                 BOOST_ASSERT(work_area);
308                 work_area->scheduleRedraw();
309         }
310         return &work_area->bufferView().buffer();
311 }
312
313 } // namespace frontend
314 } // namespace lyx