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