]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
Fix strange bibtex problem by converting some more functions to use FileName
[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 "Dialogs.h"
16 #include "Timeout.h"
17 #include "Toolbars.h"
18 #include "Menubar.h"
19 #include "WorkArea.h"
20 #include "Gui.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
47 namespace lyx {
48
49 #ifdef HAVE_SYS_TIME_H
50 # include <sys/time.h>
51 #endif
52 #ifdef HAVE_UNISTD_H
53 # include <unistd.h>
54 #endif
55
56 using frontend::WorkArea;
57
58 using support::bformat;
59 using support::FileName;
60 using support::makeDisplayPath;
61 using support::onlyFilename;
62
63 using std::endl;
64 using std::string;
65
66 using lyx::frontend::ControlCommandBuffer;
67
68 string current_layout;
69
70
71 LyXView::LyXView(int id)
72         : work_area_(0),
73           toolbars_(new Toolbars(*this)),
74           autosave_timeout_(new Timeout(5000)),
75           dialogs_(new Dialogs(*this)),
76           controlcommand_(new ControlCommandBuffer(*this)), id_(id)
77 {
78         // Start autosave timer
79         if (lyxrc.autosave) {
80                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
81                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
82                 autosave_timeout_->start();
83         }
84 }
85
86
87 LyXView::~LyXView()
88 {
89         disconnectBuffer();
90 }
91
92
93 // FIXME, there's only one WorkArea per LyXView possible for now.
94 void LyXView::setWorkArea(WorkArea * work_area)
95 {
96         work_area_ = work_area;
97         work_area_ids_.clear();
98         work_area_ids_.push_back(work_area_->id());
99 }
100
101
102 Buffer * LyXView::buffer() const
103 {
104         return work_area_->bufferView().buffer();
105 }
106
107
108 void LyXView::setBuffer(Buffer * b)
109 {
110         busy(true);
111
112         if (work_area_->bufferView().buffer())
113                 disconnectBuffer();
114
115         if (!b)
116                 getDialogs().hideBufferDependent();
117
118         work_area_->bufferView().setBuffer(b);
119
120         if (work_area_->bufferView().buffer()) {
121                 // Buffer-dependent dialogs should be updated or
122                 // hidden. This should go here because some dialogs (eg ToC)
123                 // require bv_->text.
124                 getDialogs().updateBufferDependent(true);
125                 connectBuffer(*work_area_->bufferView().buffer());
126         }
127
128         if (quitting)
129                 return;
130
131         updateMenubar();
132         updateToolbars();
133         updateLayoutChoice();
134         updateWindowTitle();
135         updateStatusBar();
136         updateTab();
137         busy(false);
138         work_area_->redraw();
139 }
140
141
142 bool LyXView::loadLyXFile(FileName const & filename, bool tolastfiles)
143 {
144         busy(true);
145
146         if (work_area_->bufferView().buffer())
147                 disconnectBuffer();
148
149         bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
150
151         updateMenubar();
152         updateToolbars();
153         updateLayoutChoice();
154         updateWindowTitle();
155         updateTab();
156         if (loaded) {
157                 connectBuffer(*work_area_->bufferView().buffer());
158                 showErrorList("Parse");
159         }
160         updateStatusBar();
161         busy(false);
162         work_area_->redraw();
163         return loaded;
164 }
165
166
167 void LyXView::connectBuffer(Buffer & buf)
168 {
169         if (errorsConnection_.connected())
170                 disconnectBuffer();
171
172         bufferChangedConnection_ =
173                 buf.changed.connect(
174                         boost::bind(&WorkArea::redraw, work_area_));
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         errorsConnection_.disconnect();
209         bufferChangedConnection_.disconnect();
210         messageConnection_.disconnect();
211         busyConnection_.disconnect();
212         titleConnection_.disconnect();
213         timerConnection_.disconnect();
214         readonlyConnection_.disconnect();
215         closingConnection_.disconnect();
216         layout_changed_connection_.disconnect();
217 }
218
219
220 void LyXView::connectBufferView(BufferView & bv)
221 {
222         show_dialog_connection_ = bv.showDialog.connect(
223                         boost::bind(&LyXView::showDialog, this, _1));
224         show_dialog_with_data_connection_ = bv.showDialogWithData.connect(
225                         boost::bind(&LyXView::showDialogWithData, this, _1, _2));
226         show_inset_dialog_connection_ = bv.showInsetDialog.connect(
227                         boost::bind(&LyXView::showInsetDialog, this, _1, _2, _3));
228         update_dialog_connection_ = bv.updateDialog.connect(
229                         boost::bind(&LyXView::updateDialog, this, _1, _2));
230         layout_changed_connection_ = bv.layoutChanged.connect(
231                         boost::bind(&Toolbars::setLayout, toolbars_.get(), _1));
232 }
233
234
235 void LyXView::disconnectBufferView()
236 {
237         show_dialog_connection_.disconnect();
238         show_dialog_with_data_connection_.disconnect();
239         show_inset_dialog_connection_.disconnect();
240         update_dialog_connection_.disconnect();
241 }
242
243
244 void LyXView::showErrorList(string const & error_type)
245 {
246         ErrorList & el = buffer()->errorList(error_type);
247         if (!el.empty()) {
248                 getDialogs().show("errorlist", error_type);
249         }
250 }
251
252
253 void LyXView::showDialog(string const & name)
254 {
255         getDialogs().show(name);
256 }
257
258
259 void LyXView::showDialogWithData(string const & name, string const & data)
260 {
261         getDialogs().show(name, data);
262 }
263
264
265 void LyXView::showInsetDialog(string const & name, string const & data,
266                 InsetBase * inset)
267 {
268         getDialogs().show(name, data, inset);
269 }
270
271
272 void LyXView::updateDialog(string const & name, string const & data)
273 {
274         if (getDialogs().visible(name))
275                 getDialogs().update(name, data);
276 }
277
278
279 void LyXView::showReadonly(bool)
280 {
281         updateWindowTitle();
282         getDialogs().updateBufferDependent(false);
283 }
284
285
286 BufferView * LyXView::view() const
287 {
288         return &work_area_->bufferView();
289 }
290
291
292 void LyXView::updateToolbars()
293 {
294         bool const math =
295                 work_area_->bufferView().cursor().inMathed();
296         bool const table =
297                 lyx::getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
298         bool const review =
299                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).enabled() &&
300                 lyx::getStatus(FuncRequest(LFUN_CHANGES_TRACK)).onoff(true);
301                 
302         toolbars_->update(math, table, review);
303         // update redaonly status of open dialogs. This could also be in
304         // updateMenubar(), but since updateToolbars() and updateMenubar()
305         // are always called together it is only here.
306         getDialogs().checkStatus();
307 }
308
309
310 ToolbarBackend::Flags LyXView::getToolbarState(string const & name)
311 {
312         return toolbars_->getToolbarState(name);
313 }
314
315
316 void LyXView::toggleToolbarState(string const & name)
317 {
318         // it is possible to get current toolbar status like this,...
319         // but I decide to obey the order of ToolbarBackend::flags
320         // and disregard real toolbar status.
321         // toolbars_->saveToolbarInfo();
322         //
323         // toggle state on/off/auto
324         toolbars_->toggleToolbarState(name);
325         // update toolbar
326         updateToolbars();
327 }
328
329
330 void LyXView::updateMenubar()
331 {
332         menubar_->update();
333 }
334
335
336 void LyXView::autoSave()
337 {
338         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
339
340         if (view()->buffer())
341                 lyx::autoSave(view());
342 }
343
344
345 void LyXView::resetAutosaveTimer()
346 {
347         if (lyxrc.autosave)
348                 autosave_timeout_->restart();
349 }
350
351
352 void LyXView::updateLayoutChoice()
353 {
354         // Don't show any layouts without a buffer
355         if (!view()->buffer()) {
356                 toolbars_->clearLayoutList();
357                 return;
358         }
359
360         // Update the layout display
361         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
362                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
363         }
364
365         if (work_area_->bufferView().cursor().inMathed())
366                 return;
367
368         string const & layout =
369                 work_area_->bufferView().cursor().paragraph().layout()->name();
370
371         if (layout != current_layout) {
372                 toolbars_->setLayout(layout);
373                 current_layout = layout;
374         }
375 }
376
377
378 void LyXView::updateWindowTitle()
379 {
380         docstring maximize_title = lyx::from_ascii("LyX");
381         docstring minimize_title = lyx::from_ascii("LyX");
382
383         if (view()->buffer()) {
384                 string const cur_title = buffer()->fileName();
385                 if (!cur_title.empty()) {
386                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
387                         minimize_title = lyx::from_utf8(onlyFilename(cur_title));
388                         if (!buffer()->isClean()) {
389                                 maximize_title += _(" (changed)");
390                                 minimize_title += lyx::char_type('*');
391                         }
392                         if (buffer()->isReadonly())
393                                 maximize_title += _(" (read only)");
394                 }
395         }
396
397         setWindowTitle(maximize_title, minimize_title);
398 }
399
400
401 void LyXView::dispatch(FuncRequest const & cmd)
402 {
403         theLyXFunc().setLyXView(this);
404         lyx::dispatch(cmd);
405 }
406
407
408 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
409 {
410         Buffer const * buffer_ptr = 0;
411         if (inset) {
412                 buffer_ptr = work_area_->bufferView().buffer();
413                 // No FitCursor:
414                 work_area_->bufferView().update(Update::Force);
415                 work_area_->redraw();
416         }
417         return buffer_ptr;
418 }
419
420
421 } // namespace lyx