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