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