]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
Change _() to return a docstring. Fixup callers with the help of lyx::to_utf8.
[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         if (!b)
138                 getDialogs().hideBufferDependent();
139
140         work_area_->bufferView().setBuffer(b);
141
142         if (work_area_->bufferView().buffer()) {
143                 // Buffer-dependent dialogs should be updated or
144                 // hidden. This should go here because some dialogs (eg ToC)
145                 // require bv_->text.
146                 getDialogs().updateBufferDependent(true);
147                 connectBuffer(*work_area_->bufferView().buffer());
148         }
149
150         updateMenubar();
151         updateToolbars();
152         updateLayoutChoice();
153         updateWindowTitle();
154         redrawWorkArea();
155 }
156
157
158 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
159 {
160         if (work_area_->bufferView().buffer())
161                 disconnectBuffer();
162
163         bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
164
165         updateMenubar();
166         updateToolbars();
167         updateLayoutChoice();
168         updateWindowTitle();
169         if (loaded) {
170                 connectBuffer(*work_area_->bufferView().buffer());
171                 showErrorList("Parse");
172         }
173         redrawWorkArea();
174         return loaded;
175 }
176
177
178 void LyXView::connectBuffer(Buffer & buf)
179 {
180         if (errorsConnection_.connected())
181                 disconnectBuffer();
182
183         errorsConnection_ =
184                 buf.errors.connect(
185                         boost::bind(&LyXView::showErrorList, this, _1));
186
187         messageConnection_ =
188                 buf.message.connect(
189                         boost::bind(&LyXView::message, this, _1));
190
191         busyConnection_ =
192                 buf.busy.connect(
193                         boost::bind(&LyXView::busy, this, _1));
194
195         titleConnection_ =
196                 buf.updateTitles.connect(
197                         boost::bind(&LyXView::updateWindowTitle, this));
198
199         timerConnection_ =
200                 buf.resetAutosaveTimers.connect(
201                         boost::bind(&LyXView::resetAutosaveTimer, this));
202
203         readonlyConnection_ =
204                 buf.readonly.connect(
205                         boost::bind(&LyXView::showReadonly, this, _1));
206
207         closingConnection_ =
208                 buf.closing.connect(
209                         boost::bind(&LyXView::setBuffer, this, (Buffer *)0));
210 }
211
212
213 void LyXView::disconnectBuffer()
214 {
215         messageConnection_.disconnect();
216         busyConnection_.disconnect();
217         titleConnection_.disconnect();
218         timerConnection_.disconnect();
219         readonlyConnection_.disconnect();
220         closingConnection_.disconnect();
221 }
222
223
224 void LyXView::showErrorList(string const & error_type)
225 {
226         ErrorList & el = buffer()->errorList(error_type);
227         if (!el.empty()) {
228                 getDialogs().show("errorlist", error_type);
229         }
230 }
231
232
233 void LyXView::showReadonly(bool)
234 {
235         updateWindowTitle();
236         getDialogs().updateBufferDependent(false);
237 }
238
239
240 BufferView * LyXView::view() const
241 {
242         return &work_area_->bufferView();
243 }
244
245
246 void LyXView::setLayout(string const & layout)
247 {
248         toolbars_->setLayout(layout);
249 }
250
251
252 void LyXView::updateToolbars()
253 {
254         bool const math = work_area_->bufferView().cursor().inMathed();
255         bool const table =
256                 getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
257         toolbars_->update(math, table);
258         // update redaonly status of open dialogs. This could also be in
259         // updateMenubar(), but since updateToolbars() and updateMenubar()
260         // are always called together it is only here.
261         getDialogs().checkStatus();
262 }
263
264
265 void LyXView::updateMenubar()
266 {
267         menubar_->update();
268 }
269
270
271 void LyXView::autoSave()
272 {
273         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
274
275         if (view()->available()) {
276                 ::autoSave(view());
277         }
278 }
279
280
281 void LyXView::resetAutosaveTimer()
282 {
283         if (lyxrc.autosave)
284                 autosave_timeout_->restart();
285 }
286
287
288 void LyXView::updateLayoutChoice()
289 {
290         // Don't show any layouts without a buffer
291         if (!view()->buffer()) {
292                 toolbars_->clearLayoutList();
293                 return;
294         }
295
296         // Update the layout display
297         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
298                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
299         }
300
301         if (work_area_->bufferView().cursor().inMathed())
302                 return;
303
304         string const & layout =
305                 work_area_->bufferView().cursor().paragraph().layout()->name();
306
307         if (layout != current_layout) {
308                 toolbars_->setLayout(layout);
309                 current_layout = layout;
310         }
311 }
312
313
314 void LyXView::updateWindowTitle()
315 {
316         static string last_title = "LyX";
317         string maximize_title = "LyX";
318         string minimize_title = "LyX";
319
320         if (view()->available()) {
321                 string const cur_title = buffer()->fileName();
322                 if (!cur_title.empty()) {
323                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
324                         minimize_title = onlyFilename(cur_title);
325                         if (!buffer()->isClean()) {
326                                 maximize_title += lyx::to_utf8(_(" (changed)"));
327                                 minimize_title += '*';
328                         }
329                         if (buffer()->isReadonly())
330                                 maximize_title += lyx::to_utf8(_(" (read only)"));
331                 }
332         }
333
334         if (maximize_title != last_title) {
335                 setWindowTitle(maximize_title, minimize_title);
336                 last_title = maximize_title;
337         }
338 }
339
340
341 void LyXView::dispatch(FuncRequest const & cmd)
342 {
343         getLyXFunc().dispatch(cmd);
344 }
345
346
347 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
348 {
349         Buffer const * buffer_ptr = 0;
350         if (inset) {
351                 buffer_ptr = work_area_->bufferView().buffer();
352                 // No FitCursor:
353                 work_area_->bufferView().update(Update::Force);
354         }
355         return buffer_ptr;
356 }