]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
Extracted from r14281
[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
84 LyXView::~LyXView()
85 {
86 }
87
88
89 void LyXView::setWorkArea(WorkArea * work_area)
90 {
91         work_area_ = work_area;
92 }
93
94
95 void LyXView::redrawWorkArea()
96 {
97         work_area_->redraw();
98 }
99
100
101 void LyXView::init()
102 {
103         updateLayoutChoice();
104         updateMenubar();
105
106         // Start autosave timer
107         if (lyxrc.autosave) {
108                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
109                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
110                 autosave_timeout_->start();
111         }
112
113         intl_->initKeyMapper(lyxrc.use_kbmap);
114 }
115
116
117 Buffer * LyXView::buffer() const
118 {
119         return work_area_->bufferView().buffer();
120 }
121
122
123 BufferView * LyXView::view() const
124 {
125         return &work_area_->bufferView();
126 }
127
128
129 void LyXView::setLayout(string const & layout)
130 {
131         toolbars_->setLayout(layout);
132 }
133
134
135 void LyXView::updateToolbars()
136 {
137         bool const math = work_area_->bufferView().cursor().inMathed();
138         bool const table =
139                 getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
140         toolbars_->update(math, table);
141         // update redaonly status of open dialogs. This could also be in
142         // updateMenubar(), but since updateToolbars() and updateMenubar()
143         // are always called together it is only here.
144         getDialogs().checkStatus();
145 }
146
147
148 void LyXView::updateMenubar()
149 {
150         menubar_->update();
151 }
152
153
154 void LyXView::autoSave()
155 {
156         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
157
158         if (view()->available()) {
159                 ::autoSave(view());
160         }
161 }
162
163
164 void LyXView::resetAutosaveTimer()
165 {
166         if (lyxrc.autosave)
167                 autosave_timeout_->restart();
168 }
169
170
171 void LyXView::updateLayoutChoice()
172 {
173         // Don't show any layouts without a buffer
174         if (!view()->buffer()) {
175                 toolbars_->clearLayoutList();
176                 return;
177         }
178
179         // Update the layout display
180         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
181                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
182         }
183
184         if (work_area_->bufferView().cursor().inMathed())
185                 return;
186
187         string const & layout =
188                 work_area_->bufferView().cursor().paragraph().layout()->name();
189
190         if (layout != current_layout) {
191                 toolbars_->setLayout(layout);
192                 current_layout = layout;
193         }
194 }
195
196
197 void LyXView::updateWindowTitle()
198 {
199         static string last_title = "LyX";
200         string maximize_title = "LyX";
201         string minimize_title = "LyX";
202
203         if (view()->available()) {
204                 string const cur_title = buffer()->fileName();
205                 if (!cur_title.empty()) {
206                         maximize_title += ": " + makeDisplayPath(cur_title, 30);
207                         minimize_title = onlyFilename(cur_title);
208                         if (!buffer()->isClean()) {
209                                 maximize_title += _(" (changed)");
210                                 minimize_title += '*';
211                         }
212                         if (buffer()->isReadonly())
213                                 maximize_title += _(" (read only)");
214                 }
215         }
216
217         if (maximize_title != last_title) {
218                 setWindowTitle(maximize_title, minimize_title);
219                 last_title = maximize_title;
220         }
221 }
222
223
224 void LyXView::dispatch(FuncRequest const & cmd)
225 {
226         getLyXFunc().dispatch(cmd);
227 }
228
229
230 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
231 {
232         Buffer const * buffer_ptr = 0;
233         if (inset) {
234                 buffer_ptr = work_area_->bufferView().buffer();
235                 // No FitCursor:
236                 work_area_->bufferView().update(Update::Force);
237         }
238         return buffer_ptr;
239 }