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