]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.cpp
the delegate patch
[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 "callback.h"
26 #include "Cursor.h"
27 #include "debug.h"
28 #include "ErrorList.h"
29 #include "FuncRequest.h"
30 #include "gettext.h"
31 #include "Intl.h"
32 #include "Layout.h"
33 #include "LyX.h"
34 #include "LyXFunc.h"
35 #include "LyXRC.h"
36 #include "MenuBackend.h"
37 #include "Paragraph.h"
38 #include "Session.h"
39 #include "Text.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         buf.setGuiDelegate(this);
167 }
168
169
170 void LyXView::disconnectBuffer()
171 {
172         if (WorkArea * work_area = currentWorkArea())
173                 work_area->bufferView().setGuiDelegate(0);
174 }
175
176
177 void LyXView::connectBufferView(BufferView & bv)
178 {
179         bv.setGuiDelegate(this);
180 }
181
182
183 void LyXView::disconnectBufferView()
184 {
185         if (WorkArea * work_area = currentWorkArea())
186                 work_area->bufferView().setGuiDelegate(0);
187 }
188
189
190 void LyXView::showErrorList(string const & error_type)
191 {
192         ErrorList & el = buffer()->errorList(error_type);
193         if (!el.empty())
194                 getDialogs().show("errorlist", error_type);
195 }
196
197
198 void LyXView::showDialog(string const & name)
199 {
200         getDialogs().show(name);
201 }
202
203
204 void LyXView::showDialogWithData(string const & name, string const & data)
205 {
206         getDialogs().show(name, data);
207 }
208
209
210 void LyXView::showInsetDialog(string const & name, string const & data,
211                 Inset * inset)
212 {
213         getDialogs().show(name, data, inset);
214 }
215
216
217 void LyXView::updateDialog(string const & name, string const & data)
218 {
219         if (getDialogs().visible(name))
220                 getDialogs().update(name, data);
221 }
222
223
224 void LyXView::showReadonly(bool)
225 {
226         updateWindowTitle();
227         getDialogs().updateBufferDependent(false);
228 }
229
230
231 BufferView * LyXView::view()
232 {
233         WorkArea * wa = currentWorkArea();
234         return wa? &wa->bufferView() : 0;
235 }
236
237
238 void LyXView::updateToc()
239 {
240         updateDialog("toc", "");
241 }
242
243
244 void LyXView::updateEmbeddedFiles()
245 {
246         updateDialog("embedding", "");
247 }
248
249
250 void LyXView::autoSave()
251 {
252         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
253
254         if (buffer())
255                 lyx::autoSave(view());
256 }
257
258
259 void LyXView::resetAutosaveTimer()
260 {
261         if (lyxrc.autosave)
262                 autosave_timeout_->restart();
263 }
264
265
266 void LyXView::updateWindowTitle()
267 {
268         docstring maximize_title = from_ascii("LyX");
269         docstring minimize_title = from_ascii("LyX");
270
271         Buffer * buf = buffer();
272         if (buf) {
273                 string const cur_title = buf->fileName();
274                 if (!cur_title.empty()) {
275                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
276                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
277                         if (!buf->isClean()) {
278                                 maximize_title += _(" (changed)");
279                                 minimize_title += char_type('*');
280                         }
281                         if (buf->isReadonly())
282                                 maximize_title += _(" (read only)");
283                 }
284         }
285
286         setWindowTitle(maximize_title, minimize_title);
287 }
288
289
290 void LyXView::dispatch(FuncRequest const & cmd)
291 {
292         string const argument = to_utf8(cmd.argument());
293         switch(cmd.action) {
294                 case LFUN_BUFFER_SWITCH:
295                         setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
296                         break;
297                 default:
298                         theLyXFunc().setLyXView(this);
299                         lyx::dispatch(cmd);
300         }
301 }
302
303
304 Buffer const * LyXView::updateInset(Inset const * inset)
305 {
306         WorkArea * work_area = currentWorkArea();
307         if (!work_area)
308                 return 0;
309
310         if (inset) {
311                 BOOST_ASSERT(work_area);
312                 work_area->scheduleRedraw();
313         }
314         return &work_area->bufferView().buffer();
315 }
316
317
318
319 void LyXView::changed()
320 {
321         if (WorkArea * wa = currentWorkArea())
322                 wa->redraw();
323 }
324
325
326 void LyXView::closing(Buffer *)
327 {
328         if (WorkArea * wa = currentWorkArea())
329                 removeWorkArea(wa);
330 }
331
332
333 } // namespace frontend
334 } // namespace lyx