]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
split LyXFunc::getStatus() into inset specific chunks
[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 "Toolbar.h"
18 #include "Menubar.h"
19
20 #include "buffer.h"
21 #include "bufferparams.h"
22 #include "BufferView.h"
23 #include "bufferview_funcs.h"
24 #include "cursor.h"
25 #include "debug.h"
26 #include "funcrequest.h"
27 #include "gettext.h"
28 #include "intl.h"
29 #include "lyx_cb.h"
30 #include "lyxfunc.h"
31 #include "lyxrc.h"
32 #include "lyxtext.h"
33 #include "MenuBackend.h"
34 #include "paragraph.h"
35
36 #include "controllers/ControlCommandBuffer.h"
37
38 #include "support/filetools.h" // OnlyFilename()
39
40 #include <boost/bind.hpp>
41
42 #include <sys/time.h>
43 #include <unistd.h>
44
45 using lyx::support::MakeDisplayPath;
46 using lyx::support::OnlyFilename;
47
48 using std::endl;
49 using std::string;
50
51
52 string current_layout;
53
54
55 LyXView::LyXView()
56         : intl_(new Intl),
57           autosave_timeout_(new Timeout(5000)),
58           lyxfunc_(new LyXFunc(this)),
59           dialogs_(new Dialogs(*this)),
60           controlcommand_(new ControlCommandBuffer(*this))
61 {
62         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
63 }
64
65
66 LyXView::~LyXView()
67 {
68 }
69
70
71 void LyXView::init()
72 {
73         updateLayoutChoice();
74         updateMenubar();
75
76         // Start autosave timer
77         if (lyxrc.autosave) {
78                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
79                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
80                 autosave_timeout_->start();
81         }
82
83         intl_->InitKeyMapper(lyxrc.use_kbmap);
84 }
85
86
87 Buffer * LyXView::buffer() const
88 {
89         return bufferview_->buffer();
90 }
91
92
93 boost::shared_ptr<BufferView> const & LyXView::view() const
94 {
95         return bufferview_;
96 }
97
98
99 void LyXView::setLayout(string const & layout)
100 {
101         toolbar_->setLayout(layout);
102 }
103
104
105 void LyXView::updateToolbar()
106 {
107         bool const math = bufferview_->cursor().inMathed();
108         bool const table =
109                 getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
110         toolbar_->update(math, table);
111 }
112
113
114 void LyXView::updateMenubar()
115 {
116         menubar_->update();
117 }
118
119
120 void LyXView::autoSave()
121 {
122         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
123
124         if (view()->available()) {
125                 ::AutoSave(view().get());
126         }
127 }
128
129
130 void LyXView::resetAutosaveTimer()
131 {
132         if (lyxrc.autosave)
133                 autosave_timeout_->restart();
134 }
135
136
137 void LyXView::updateLayoutChoice()
138 {
139         // don't show any layouts without a buffer
140         if (!view()->buffer()) {
141                 toolbar_->clearLayoutList();
142                 return;
143         }
144
145         // update the layout display
146         if (toolbar_->updateLayoutList(buffer()->params().textclass)) {
147                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
148         }
149
150         if (bufferview_->cursor().inMathed())
151                 return;
152
153         string const & layout =
154                 bufferview_->cursor().paragraph().layout()->name();
155
156         if (layout != current_layout) {
157                 toolbar_->setLayout(layout);
158                 current_layout = layout;
159         }
160 }
161
162
163 void LyXView::updateWindowTitle()
164 {
165         static string last_title = "LyX";
166         string maximize_title = "LyX";
167         string minimize_title = "LyX";
168
169         if (view()->available()) {
170                 string const cur_title = buffer()->fileName();
171                 if (!cur_title.empty()) {
172                         maximize_title += ": " + MakeDisplayPath(cur_title, 30);
173                         minimize_title = OnlyFilename(cur_title);
174                         if (!buffer()->isClean()) {
175                                 maximize_title += _(" (changed)");
176                                 minimize_title += '*';
177                         }
178                         if (buffer()->isReadonly())
179                                 maximize_title += _(" (read only)");
180                 }
181         }
182
183         if (maximize_title != last_title) {
184                 setWindowTitle(maximize_title, minimize_title);
185                 last_title = maximize_title;
186         }
187 }
188
189
190 void LyXView::dispatch(FuncRequest const & cmd)
191 {
192         getLyXFunc().dispatch(cmd);
193 }
194
195
196 Buffer const * const LyXView::updateInset(InsetOld const * inset) const
197 {
198         Buffer const * buffer_ptr = 0;
199         if (inset) {
200                 buffer_ptr = bufferview_->buffer();
201                 bufferview_->update();
202         }
203         return buffer_ptr;
204 }