]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
This commit creates a error_lists map member inside the Buffer class.
[lyx.git] / src / frontends / LyXView.C
1 /**
2  * \file LyXView.C
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 #include "Gui.h"
16 #include "Dialogs.h"
17 #include "Timeout.h"
18 #include "Toolbars.h"
19 #include "Menubar.h"
20 #include "WorkArea.h"
21
22 #include "buffer.h"
23 #include "bufferparams.h"
24 #include "BufferView.h"
25 #include "bufferview_funcs.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 "lyx_cb.h"
33 #include "lyxfunc.h"
34 #include "lyxrc.h"
35 #include "lyxtext.h"
36 #include "MenuBackend.h"
37 #include "paragraph.h"
38
39 #include "controllers/ControlCommandBuffer.h"
40
41 #include "support/lstrings.h"
42 #include "support/filetools.h" // OnlyFilename()
43
44 #include <boost/bind.hpp>
45
46 #ifdef HAVE_SYS_TIME_H
47 # include <sys/time.h>
48 #endif
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #endif
52
53 using lyx::frontend::Gui;
54 using lyx::frontend::WorkArea;
55
56 using lyx::support::bformat;
57 using lyx::support::makeDisplayPath;
58 using lyx::support::onlyFilename;
59
60 using std::endl;
61 using std::string;
62
63 using lyx::frontend::ControlCommandBuffer;
64
65 string current_layout;
66
67 Gui & LyXView::gui()
68 {
69         return owner_;
70 }
71
72
73 LyXView::LyXView(Gui & owner)
74         : work_area_(0),
75           owner_(owner),
76           toolbars_(new Toolbars(*this)),
77           intl_(new Intl),
78           autosave_timeout_(new Timeout(5000)),
79           lyxfunc_(new LyXFunc(this)),
80           dialogs_(new Dialogs(*this)),
81           controlcommand_(new ControlCommandBuffer(*this))
82 {
83         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
84 }
85
86 LyXView::~LyXView()
87 {
88 }
89
90
91 void LyXView::setWorkArea(WorkArea * work_area)
92 {
93         work_area_ = work_area;
94 }
95
96
97 void LyXView::redrawWorkArea()
98 {
99         work_area_->redraw();
100         updateStatusBar();
101 }
102
103
104 WorkArea * LyXView::workArea()
105 {
106         return work_area_;
107 }
108
109
110 void LyXView::init()
111 {
112         updateLayoutChoice();
113         updateMenubar();
114
115         // Start autosave timer
116         if (lyxrc.autosave) {
117                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
118                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
119                 autosave_timeout_->start();
120         }
121
122         intl_->initKeyMapper(lyxrc.use_kbmap);
123 }
124
125
126 Buffer * LyXView::buffer() const
127 {
128         return work_area_->bufferView().buffer();
129 }
130
131
132 void LyXView::setBuffer(Buffer * b)
133 {
134         if (work_area_->bufferView().buffer())
135                 disconnectBuffer();
136
137         work_area_->bufferView().setBuffer(b);
138         updateMenubar();
139         updateToolbars();
140         updateLayoutChoice();
141         updateWindowTitle();
142         if (b) {
143                 connectBuffer(*b);
144                 setLayout(work_area_->bufferView().firstLayout());
145         }
146         redrawWorkArea();
147 }
148
149
150 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
151 {
152         if (work_area_->bufferView().buffer())
153                 disconnectBuffer();
154
155         bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
156         showErrorList("Parse");
157
158         updateMenubar();
159         updateToolbars();
160         updateLayoutChoice();
161         updateWindowTitle();
162         if (loaded) {
163                 connectBuffer(*work_area_->bufferView().buffer());
164                 setLayout(work_area_->bufferView().firstLayout());
165         }
166         redrawWorkArea();
167         return loaded;
168 }
169
170
171 void LyXView::connectBuffer(Buffer & buf)
172 {
173         if (errorsConnection_.connected())
174                 disconnectBuffer();
175
176         errorsConnection_ =
177                 buf.errors.connect(
178                         boost::bind(&LyXView::showErrorList, this, _1));
179
180         messageConnection_ =
181                 buf.message.connect(
182                         boost::bind(&LyXView::message, this, _1));
183
184         busyConnection_ =
185                 buf.busy.connect(
186                         boost::bind(&LyXView::busy, this, _1));
187
188         titleConnection_ =
189                 buf.updateTitles.connect(
190                         boost::bind(&LyXView::updateWindowTitle, this));
191
192         timerConnection_ =
193                 buf.resetAutosaveTimers.connect(
194                         boost::bind(&LyXView::resetAutosaveTimer, this));
195
196         readonlyConnection_ =
197                 buf.readonly.connect(
198                         boost::bind(&LyXView::showReadonly, this, _1));
199
200         closingConnection_ =
201                 buf.closing.connect(
202                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
203 }
204
205
206 void LyXView::disconnectBuffer()
207 {
208         messageConnection_.disconnect();
209         busyConnection_.disconnect();
210         titleConnection_.disconnect();
211         timerConnection_.disconnect();
212         readonlyConnection_.disconnect();
213         closingConnection_.disconnect();
214 }
215
216
217 void LyXView::showErrorList(string const & error_type)
218 {
219         ErrorList & el = buffer()->errorList(error_type);
220         if (!el.empty()) {
221                 getDialogs().show("errorlist", error_type);
222         }
223 }
224
225
226 void LyXView::showReadonly(bool)
227 {
228         updateWindowTitle();
229         getDialogs().updateBufferDependent(false);
230 }
231
232
233 BufferView * LyXView::view() const
234 {
235         return &work_area_->bufferView();
236 }
237
238
239 void LyXView::setLayout(string const & layout)
240 {
241         toolbars_->setLayout(layout);
242 }
243
244
245 void LyXView::updateToolbars()
246 {
247         bool const math = work_area_->bufferView().cursor().inMathed();
248         bool const table =
249                 getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
250         toolbars_->update(math, table);
251         // update redaonly status of open dialogs. This could also be in
252         // updateMenubar(), but since updateToolbars() and updateMenubar()
253         // are always called together it is only here.
254         getDialogs().checkStatus();
255 }
256
257
258 void LyXView::updateMenubar()
259 {
260         menubar_->update();
261 }
262
263
264 void LyXView::autoSave()
265 {
266         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
267
268         if (view()->available()) {
269                 ::autoSave(view());
270         }
271 }
272
273
274 void LyXView::resetAutosaveTimer()
275 {
276         if (lyxrc.autosave)
277                 autosave_timeout_->restart();
278 }
279
280
281 void LyXView::updateLayoutChoice()
282 {
283         // Don't show any layouts without a buffer
284         if (!view()->buffer()) {
285                 toolbars_->clearLayoutList();
286                 return;
287         }
288
289         // Update the layout display
290         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
291                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
292         }
293
294         if (work_area_->bufferView().cursor().inMathed())
295                 return;
296
297         string const & layout =
298                 work_area_->bufferView().cursor().paragraph().layout()->name();
299
300         if (layout != current_layout) {
301                 toolbars_->setLayout(layout);
302                 current_layout = layout;
303         }
304 }
305
306
307 void LyXView::updateWindowTitle()
308 {
309         static string last_title = "LyX";
310         string maximize_title = "LyX";
311         string minimize_title = "LyX";
312
313         if (view()->available()) {
314                 string const cur_title = buffer()->fileName();
315                 if (!cur_title.empty()) {
316                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
317                         minimize_title = onlyFilename(cur_title);
318                         if (!buffer()->isClean()) {
319                                 maximize_title += _(" (changed)");
320                                 minimize_title += '*';
321                         }
322                         if (buffer()->isReadonly())
323                                 maximize_title += _(" (read only)");
324                 }
325         }
326
327         if (maximize_title != last_title) {
328                 setWindowTitle(maximize_title, minimize_title);
329                 last_title = maximize_title;
330         }
331 }
332
333
334 void LyXView::dispatch(FuncRequest const & cmd)
335 {
336         getLyXFunc().dispatch(cmd);
337 }
338
339
340 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
341 {
342         Buffer const * buffer_ptr = 0;
343         if (inset) {
344                 buffer_ptr = work_area_->bufferView().buffer();
345                 // No FitCursor:
346                 work_area_->bufferView().update(Update::Force);
347         }
348         return buffer_ptr;
349 }