]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.cpp
Allow dissolution of insets inside mathed
[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         delete dialogs_;
84         delete autosave_timeout_;
85 }
86
87
88 Buffer * LyXView::buffer()
89 {
90         WorkArea * work_area = currentWorkArea();
91         if (work_area)
92                 return &work_area->bufferView().buffer();
93         return 0;
94 }
95
96
97 Buffer const * LyXView::buffer() const
98 {
99         WorkArea const * work_area = currentWorkArea();
100         if (work_area)
101                 return &work_area->bufferView().buffer();
102         return 0;
103 }
104
105
106 void LyXView::setBuffer(Buffer * newBuffer)
107 {
108         BOOST_ASSERT(newBuffer);
109         busy(true);
110
111         WorkArea * wa = workArea(*newBuffer);
112         if (wa == 0) {
113                 updateLabels(*newBuffer->getMasterBuffer());
114                 wa = addWorkArea(*newBuffer);
115         } else {
116                 //Disconnect the old buffer...there's no new one.
117                 disconnectBuffer();
118         }
119         connectBuffer(*newBuffer);
120         connectBufferView(wa->bufferView());
121         setCurrentWorkArea(wa);
122
123         busy(false);
124 }
125
126
127 Buffer * LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
128 {
129         busy(true);
130
131         Buffer * newBuffer = checkAndLoadLyXFile(filename);
132
133         if (!newBuffer) {
134                 message(_("Document not loaded."));
135                 updateStatusBar();
136                 busy(false);
137                 return 0;
138         }
139
140         WorkArea * wa = workArea(*newBuffer);
141         if (wa == 0)
142                 wa = addWorkArea(*newBuffer);
143
144         // scroll to the position when the file was last closed
145         if (lyxrc.use_lastfilepos) {
146                 pit_type pit;
147                 pos_type pos;
148                 boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
149                 // if successfully move to pit (returned par_id is not zero),
150                 // update metrics and reset font
151                 wa->bufferView().moveToPosition(pit, pos, 0, 0);
152         }
153
154         if (tolastfiles)
155                 LyX::ref().session().lastFiles().add(filename);
156
157         busy(false);
158         return newBuffer;
159 }
160
161
162 void LyXView::connectBuffer(Buffer & buf)
163 {
164         buf.setGuiDelegate(this);
165 }
166
167
168 void LyXView::disconnectBuffer()
169 {
170         if (WorkArea * work_area = currentWorkArea())
171                 work_area->bufferView().setGuiDelegate(0);
172 }
173
174
175 void LyXView::connectBufferView(BufferView & bv)
176 {
177         bv.setGuiDelegate(this);
178 }
179
180
181 void LyXView::disconnectBufferView()
182 {
183         if (WorkArea * work_area = currentWorkArea())
184                 work_area->bufferView().setGuiDelegate(0);
185 }
186
187
188 void LyXView::showErrorList(string const & error_type)
189 {
190         ErrorList & el = buffer()->errorList(error_type);
191         if (!el.empty())
192                 getDialogs().show("errorlist", error_type);
193 }
194
195
196 void LyXView::showDialog(string const & name)
197 {
198         getDialogs().show(name);
199 }
200
201
202 void LyXView::showDialogWithData(string const & name, string const & data)
203 {
204         getDialogs().show(name, data);
205 }
206
207
208 void LyXView::showInsetDialog(string const & name, string const & data,
209                 Inset * inset)
210 {
211         getDialogs().show(name, data, inset);
212 }
213
214
215 void LyXView::updateDialog(string const & name, string const & data)
216 {
217         if (getDialogs().visible(name))
218                 getDialogs().update(name, data);
219 }
220
221
222 void LyXView::showReadonly(bool)
223 {
224         updateWindowTitle();
225         getDialogs().updateBufferDependent(false);
226 }
227
228
229 BufferView * LyXView::view()
230 {
231         WorkArea * wa = currentWorkArea();
232         return wa? &wa->bufferView() : 0;
233 }
234
235
236 void LyXView::updateToc()
237 {
238         updateDialog("toc", "");
239 }
240
241
242 void LyXView::updateEmbeddedFiles()
243 {
244         updateDialog("embedding", "");
245 }
246
247
248 void LyXView::autoSave()
249 {
250         LYXERR(Debug::INFO) << "Running autoSave()" << endl;
251
252         if (buffer())
253                 lyx::autoSave(view());
254 }
255
256
257 void LyXView::resetAutosaveTimer()
258 {
259         if (lyxrc.autosave)
260                 autosave_timeout_->restart();
261 }
262
263
264 void LyXView::updateWindowTitle()
265 {
266         docstring maximize_title = from_ascii("LyX");
267         docstring minimize_title = from_ascii("LyX");
268
269         Buffer * buf = buffer();
270         if (buf) {
271                 string const cur_title = buf->fileName();
272                 if (!cur_title.empty()) {
273                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
274                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
275                         if (!buf->isClean()) {
276                                 maximize_title += _(" (changed)");
277                                 minimize_title += char_type('*');
278                         }
279                         if (buf->isReadonly())
280                                 maximize_title += _(" (read only)");
281                 }
282         }
283
284         setWindowTitle(maximize_title, minimize_title);
285 }
286
287
288 void LyXView::dispatch(FuncRequest const & cmd)
289 {
290         string const argument = to_utf8(cmd.argument());
291         switch(cmd.action) {
292                 case LFUN_BUFFER_SWITCH:
293                         setBuffer(theBufferList().getBuffer(to_utf8(cmd.argument())));
294                         break;
295                 default:
296                         theLyXFunc().setLyXView(this);
297                         lyx::dispatch(cmd);
298         }
299 }
300
301
302 Buffer const * LyXView::updateInset(Inset const * inset)
303 {
304         WorkArea * work_area = currentWorkArea();
305         if (!work_area)
306                 return 0;
307
308         if (inset) {
309                 BOOST_ASSERT(work_area);
310                 work_area->scheduleRedraw();
311         }
312         return &work_area->bufferView().buffer();
313 }
314
315 } // namespace frontend
316 } // namespace lyx