]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
d8c4ee1539caf2adc2ae77c6d6e51f02c1f14e25
[lyx.git] / src / frontends / LyXView.C
1 /**
2  * \file LyXView.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Lars Gullik Bjornes
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #ifdef __GNUG__
13 #pragma implementation
14 #endif
15
16 #include "LyXView.h"
17 #include "minibuffer.h"
18 #include "debug.h"
19 #include "intl.h"
20 #include "lyxrc.h"
21 #include "lyxtext.h"
22 #include "buffer.h"
23 #include "MenuBackend.h"
24 #include "gettext.h"
25 #include "lyxfunc.h"
26 #include "lyx_cb.h"
27 #include "BufferView.h"
28 #include "bufferview_funcs.h"
29 #include "lyxtextclasslist.h"
30
31 #include "frontends/Dialogs.h"
32 #include "frontends/Toolbar.h"
33 #include "frontends/Timeout.h"
34 #include "frontends/Menubar.h"
35
36 #include "support/filetools.h" // OnlyFilename()
37
38 #include <boost/bind.hpp>
39
40 #include <sys/time.h>
41 #include <unistd.h>
42
43 using std::endl;
44
45 string current_layout;
46
47
48 LyXView::LyXView()
49 {
50         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
51
52         lyxfunc_.reset(new LyXFunc(this));
53         intl_.reset(new Intl);
54
55         // Give the timeout some default sensible value.
56         autosave_timeout_.reset(new Timeout(5000));
57
58         dialogs_.reset(new Dialogs(this));
59 }
60
61
62 LyXView::~LyXView()
63 {
64 }
65
66
67 void LyXView::init()
68 {
69         // Set the textclass choice
70         invalidateLayoutChoice();
71         updateLayoutChoice();
72         updateMenubar();
73         
74         // Start autosave timer
75         if (lyxrc.autosave) {
76                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
77                 autosave_timeout_->start();
78         }
79
80         intl_->InitKeyMapper(lyxrc.use_kbmap);
81 }
82
83
84 Buffer * LyXView::buffer() const
85 {
86         return bufferview_->buffer();
87 }
88
89
90 BufferView * LyXView::view() const
91 {
92         return bufferview_.get();
93 }
94
95
96 Toolbar * LyXView::getToolbar() const
97 {
98         return toolbar_.get();
99 }
100
101
102 void LyXView::setLayout(string const & layout)
103 {
104         toolbar_->setLayout(layout);
105 }
106
107
108 void LyXView::updateToolbar()
109 {
110         toolbar_->update();
111 }
112
113
114 LyXFunc * LyXView::getLyXFunc() const
115 {
116         return lyxfunc_.get();
117 }
118
119
120 MiniBuffer * LyXView::getMiniBuffer() const
121 {
122         return minibuffer_.get();
123 }
124
125
126 void LyXView::message(string const & str)
127 {
128         minibuffer_->message(str);
129 }
130
131
132 void LyXView::messagePush(string const & str)
133 {
134         minibuffer_->messagePush(str);
135 }
136
137
138 void LyXView::messagePop()
139 {
140         minibuffer_->messagePop();
141 }
142
143
144 Menubar * LyXView::getMenubar() const
145 {
146         return menubar_.get();
147 }
148
149
150 void LyXView::updateMenubar()
151 {
152         if (!view()->buffer() && menubackend.hasMenu("main_nobuffer")) {
153                 menubar_->set("main_nobuffer");
154         } else {
155                 menubar_->set("main");
156         }
157
158         menubar_->update();
159 }
160
161
162 Intl * LyXView::getIntl() const
163 {
164         return intl_.get();
165 }
166
167
168 void LyXView::autoSave()
169 {
170         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
171
172         if (view()->available()) {
173                 ::AutoSave(view());
174         }
175 }
176
177
178 void LyXView::resetAutosaveTimer()
179 {
180         if (lyxrc.autosave)
181                 autosave_timeout_->restart();
182 }
183
184
185 void LyXView::invalidateLayoutChoice()
186 {
187         last_textclass_ = -1;
188 }
189
190
191 void LyXView::updateLayoutChoice()
192 {
193         // don't show any layouts without a buffer
194         if (!view()->buffer()) {
195                 toolbar_->clearLayoutList();
196                 return;
197         }
198
199         // update the layout display
200         if (last_textclass_ != int(buffer()->params.textclass)) {
201                 toolbar_->updateLayoutList(true);
202                 last_textclass_ = int(buffer()->params.textclass);
203                 current_layout = textclasslist[last_textclass_].defaultLayoutName();
204         } else {
205                 toolbar_->updateLayoutList(false);
206         }
207
208         string const & layout =
209                 bufferview_->getLyXText()->cursor.par()->layout();
210
211         if (layout != current_layout) {
212                 toolbar_->setLayout(layout);
213                 current_layout = layout;
214         }
215 }
216
217
218 void LyXView::updateWindowTitle()
219 {
220         static string last_title = "LyX";
221         string title = "LyX";
222         string icon_title = "LyX";
223
224         if (view()->available()) {
225                 string const cur_title = buffer()->fileName();
226                 if (!cur_title.empty()) {
227                         title += ": " + MakeDisplayPath(cur_title, 30);
228                         if (!buffer()->isLyxClean())
229                                 title += _(" (changed)");
230                         if (buffer()->isReadonly())
231                                 title += _(" (read only)");
232                         // Show only the filename if it's available
233                         icon_title = OnlyFilename(cur_title);
234                 }
235         }
236
237         if (title != last_title) {
238                 setWindowTitle(title, icon_title);
239                 last_title = title;
240         }
241 }
242
243
244 void LyXView::showState()
245 {
246         message(currentState(view()));
247 }