]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
* Positionable and dynamically visible toolbars for the XForms frontend.
[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 "Toolbars.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         : toolbars_(new Toolbars(*this)),
57           intl_(new Intl),
58           autosave_timeout_(new Timeout(5000)),
59           lyxfunc_(new LyXFunc(this)),
60           dialogs_(new Dialogs(*this)),
61           controlcommand_(new ControlCommandBuffer(*this))
62 {
63         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
64 }
65
66
67 LyXView::~LyXView()
68 {
69 }
70
71
72 void LyXView::init()
73 {
74         updateLayoutChoice();
75         updateMenubar();
76
77         // Start autosave timer
78         if (lyxrc.autosave) {
79                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
80                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
81                 autosave_timeout_->start();
82         }
83
84         intl_->InitKeyMapper(lyxrc.use_kbmap);
85 }
86
87
88 Buffer * LyXView::buffer() const
89 {
90         return bufferview_->buffer();
91 }
92
93
94 boost::shared_ptr<BufferView> const & LyXView::view() const
95 {
96         return bufferview_;
97 }
98
99
100 void LyXView::setLayout(string const & layout)
101 {
102         toolbars_->setLayout(layout);
103 }
104
105
106 void LyXView::updateToolbars()
107 {
108         bool const math = bufferview_->cursor().inMathed();
109         bool const table =
110                 getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
111         toolbars_->update(math, table);
112 }
113
114
115 void LyXView::updateMenubar()
116 {
117         menubar_->update();
118 }
119
120
121 void LyXView::autoSave()
122 {
123         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
124
125         if (view()->available()) {
126                 ::AutoSave(view().get());
127         }
128 }
129
130
131 void LyXView::resetAutosaveTimer()
132 {
133         if (lyxrc.autosave)
134                 autosave_timeout_->restart();
135 }
136
137
138 void LyXView::updateLayoutChoice()
139 {
140         // don't show any layouts without a buffer
141         if (!view()->buffer()) {
142                 toolbars_->clearLayoutList();
143                 return;
144         }
145
146         // update the layout display
147         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
148                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
149         }
150
151         if (bufferview_->cursor().inMathed())
152                 return;
153
154         string const & layout =
155                 bufferview_->cursor().paragraph().layout()->name();
156
157         if (layout != current_layout) {
158                 toolbars_->setLayout(layout);
159                 current_layout = layout;
160         }
161 }
162
163
164 void LyXView::updateWindowTitle()
165 {
166         static string last_title = "LyX";
167         string maximize_title = "LyX";
168         string minimize_title = "LyX";
169
170         if (view()->available()) {
171                 string const cur_title = buffer()->fileName();
172                 if (!cur_title.empty()) {
173                         maximize_title += ": " + MakeDisplayPath(cur_title, 30);
174                         minimize_title = OnlyFilename(cur_title);
175                         if (!buffer()->isClean()) {
176                                 maximize_title += _(" (changed)");
177                                 minimize_title += '*';
178                         }
179                         if (buffer()->isReadonly())
180                                 maximize_title += _(" (read only)");
181                 }
182         }
183
184         if (maximize_title != last_title) {
185                 setWindowTitle(maximize_title, minimize_title);
186                 last_title = maximize_title;
187         }
188 }
189
190
191 void LyXView::dispatch(FuncRequest const & cmd)
192 {
193         getLyXFunc().dispatch(cmd);
194 }
195
196
197 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
198 {
199         Buffer const * buffer_ptr = 0;
200         if (inset) {
201                 buffer_ptr = bufferview_->buffer();
202                 bufferview_->update();
203         }
204         return buffer_ptr;
205 }