]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
54dad90f3eaed780cb5069692e5dc6ce49715dca
[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 "funcrequest.h"
29 #include "gettext.h"
30 #include "intl.h"
31 #include "lyx_cb.h"
32 #include "lyxfunc.h"
33 #include "lyxrc.h"
34 #include "lyxtext.h"
35 #include "MenuBackend.h"
36 #include "paragraph.h"
37
38 #include "controllers/ControlCommandBuffer.h"
39
40 #include "support/filetools.h" // OnlyFilename()
41
42 #include <boost/bind.hpp>
43
44 #ifdef HAVE_SYS_TIME_H
45 # include <sys/time.h>
46 #endif
47 #ifdef HAVE_UNISTD_H
48 # include <unistd.h>
49 #endif
50
51 using lyx::frontend::Gui;
52 using lyx::frontend::WorkArea;
53
54 using lyx::support::makeDisplayPath;
55 using lyx::support::onlyFilename;
56
57 using std::endl;
58 using std::string;
59
60 using lyx::frontend::ControlCommandBuffer;
61
62 string current_layout;
63
64 Gui & LyXView::gui()
65 {
66         return owner_;
67 }
68
69
70 LyXView::LyXView(Gui & owner)
71         : work_area_(0),
72           owner_(owner),
73           toolbars_(new Toolbars(*this)),
74           intl_(new Intl),
75           autosave_timeout_(new Timeout(5000)),
76           lyxfunc_(new LyXFunc(this)),
77           dialogs_(new Dialogs(*this)),
78           controlcommand_(new ControlCommandBuffer(*this))
79 {
80         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
81 }
82
83 LyXView::~LyXView()
84 {
85 }
86
87
88 void LyXView::setWorkArea(WorkArea * work_area)
89 {
90         work_area_ = work_area;
91 }
92
93
94 void LyXView::redrawWorkArea()
95 {
96         work_area_->redraw();
97         updateStatusBar();
98 }
99
100
101 WorkArea * LyXView::workArea()
102 {
103         return work_area_;
104 }
105
106
107 void LyXView::init()
108 {
109         updateLayoutChoice();
110         updateMenubar();
111
112         // Start autosave timer
113         if (lyxrc.autosave) {
114                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
115                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
116                 autosave_timeout_->start();
117         }
118
119         intl_->initKeyMapper(lyxrc.use_kbmap);
120 }
121
122
123 Buffer * LyXView::buffer() const
124 {
125         return work_area_->bufferView().buffer();
126 }
127
128
129 void LyXView::setBuffer(Buffer * b)
130 {
131         work_area_->bufferView().setBuffer(b);
132         updateMenubar();
133         updateToolbars();
134         updateLayoutChoice();
135         updateWindowTitle();
136         if (b)
137                 setLayout(work_area_->bufferView().firstLayout());
138         redrawWorkArea();
139 }
140
141
142 bool LyXView::loadLyXFile(string const & filename, bool tolastfiles)
143 {
144         bool loaded = work_area_->bufferView().loadLyXFile(filename, tolastfiles);
145         updateMenubar();
146         updateToolbars();
147         updateLayoutChoice();
148         updateWindowTitle();
149         if (loaded)
150                 setLayout(work_area_->bufferView().firstLayout());
151         redrawWorkArea();
152         return loaded;
153 }
154
155 BufferView * LyXView::view() const
156 {
157         return &work_area_->bufferView();
158 }
159
160
161 void LyXView::setLayout(string const & layout)
162 {
163         toolbars_->setLayout(layout);
164 }
165
166
167 void LyXView::updateToolbars()
168 {
169         bool const math = work_area_->bufferView().cursor().inMathed();
170         bool const table =
171                 getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
172         toolbars_->update(math, table);
173         // update redaonly status of open dialogs. This could also be in
174         // updateMenubar(), but since updateToolbars() and updateMenubar()
175         // are always called together it is only here.
176         getDialogs().checkStatus();
177 }
178
179
180 void LyXView::updateMenubar()
181 {
182         menubar_->update();
183 }
184
185
186 void LyXView::autoSave()
187 {
188         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
189
190         if (view()->available()) {
191                 ::autoSave(view());
192         }
193 }
194
195
196 void LyXView::resetAutosaveTimer()
197 {
198         if (lyxrc.autosave)
199                 autosave_timeout_->restart();
200 }
201
202
203 void LyXView::updateLayoutChoice()
204 {
205         // Don't show any layouts without a buffer
206         if (!view()->buffer()) {
207                 toolbars_->clearLayoutList();
208                 return;
209         }
210
211         // Update the layout display
212         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
213                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
214         }
215
216         if (work_area_->bufferView().cursor().inMathed())
217                 return;
218
219         string const & layout =
220                 work_area_->bufferView().cursor().paragraph().layout()->name();
221
222         if (layout != current_layout) {
223                 toolbars_->setLayout(layout);
224                 current_layout = layout;
225         }
226 }
227
228
229 void LyXView::updateWindowTitle()
230 {
231         static string last_title = "LyX";
232         string maximize_title = "LyX";
233         string minimize_title = "LyX";
234
235         if (view()->available()) {
236                 string const cur_title = buffer()->fileName();
237                 if (!cur_title.empty()) {
238                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
239                         minimize_title = onlyFilename(cur_title);
240                         if (!buffer()->isClean()) {
241                                 maximize_title += _(" (changed)");
242                                 minimize_title += '*';
243                         }
244                         if (buffer()->isReadonly())
245                                 maximize_title += _(" (read only)");
246                 }
247         }
248
249         if (maximize_title != last_title) {
250                 setWindowTitle(maximize_title, minimize_title);
251                 last_title = maximize_title;
252         }
253 }
254
255
256 void LyXView::dispatch(FuncRequest const & cmd)
257 {
258         getLyXFunc().dispatch(cmd);
259 }
260
261
262 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
263 {
264         Buffer const * buffer_ptr = 0;
265         if (inset) {
266                 buffer_ptr = work_area_->bufferView().buffer();
267                 // No FitCursor:
268                 work_area_->bufferView().update(Update::Force);
269         }
270         return buffer_ptr;
271 }