]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
Small clean-up.
[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 "debug.h"
25 #include "funcrequest.h"
26 #include "gettext.h"
27 #include "intl.h"
28 #include "lyx_cb.h"
29 #include "lyxfunc.h"
30 #include "lyxrc.h"
31 #include "lyxtext.h"
32 #include "MenuBackend.h"
33 #include "paragraph.h"
34
35 #include "controllers/ControlCommandBuffer.h"
36
37 #include "mathed/math_cursor.h"
38
39 #include "support/filetools.h" // OnlyFilename()
40
41 #include <boost/bind.hpp>
42
43 #include <sys/time.h>
44 #include <unistd.h>
45
46 using lyx::support::MakeDisplayPath;
47 using lyx::support::OnlyFilename;
48
49 using std::endl;
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 = mathcursor;
108         bool const table =
109                 !getLyXFunc().getStatus(LFUN_LAYOUT_TABULAR).disabled();
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         string const & layout =
151                 bufferview_->getLyXText()->cursor.par()->layout()->name();
152
153         if (layout != current_layout) {
154                 toolbar_->setLayout(layout);
155                 current_layout = layout;
156         }
157 }
158
159
160 void LyXView::updateWindowTitle()
161 {
162         static string last_title = "LyX";
163         string maximize_title = "LyX";
164         string minimize_title = "LyX";
165
166         if (view()->available()) {
167                 string const cur_title = buffer()->fileName();
168                 if (!cur_title.empty()) {
169                         maximize_title += ": " + MakeDisplayPath(cur_title, 30);
170                         minimize_title = OnlyFilename(cur_title);
171                         if (!buffer()->isClean()) {
172                                 maximize_title += _(" (changed)");
173                                 minimize_title += '*';
174                         }
175                         if (buffer()->isReadonly())
176                                 maximize_title += _(" (read only)");
177                 }
178         }
179
180         if (maximize_title != last_title) {
181                 setWindowTitle(maximize_title, minimize_title);
182                 last_title = maximize_title;
183         }
184 }
185
186
187 void LyXView::dispatch(FuncRequest const & req)
188 {
189         // substitute the correct BufferView here
190         FuncRequest r = req;
191         r.setView(view().get());
192         getLyXFunc().dispatch(r);
193 }