]> git.lyx.org Git - features.git/blob - src/frontends/LyXView.C
e5be0f283340567610781dca89977498fe256c4e
[features.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 "debug.h"
18 #include "intl.h"
19 #include "lyxrc.h"
20 #include "lyxtext.h"
21 #include "buffer.h"
22 #include "MenuBackend.h"
23 #include "gettext.h"
24 #include "lyxfunc.h"
25 #include "lyx_cb.h"
26 #include "BufferView.h"
27 #include "bufferview_funcs.h"
28 #include "lyxtextclasslist.h"
29
30 #include "Dialogs.h"
31 #include "Toolbar.h"
32 #include "Timeout.h"
33 #include "Menubar.h"
34 #include "controllers/ControlCommandBuffer.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         controlcommand_.reset(new ControlCommandBuffer(*getLyXFunc()));
60 }
61
62
63 LyXView::~LyXView()
64 {
65 }
66
67
68 void LyXView::init()
69 {
70         updateLayoutChoice();
71         updateMenubar();
72
73         // Start autosave timer
74         if (lyxrc.autosave) {
75                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
76                 autosave_timeout_->start();
77         }
78
79         intl_->InitKeyMapper(lyxrc.use_kbmap);
80 }
81
82
83 Buffer * LyXView::buffer() const
84 {
85         return bufferview_->buffer();
86 }
87
88
89 BufferView * LyXView::view() const
90 {
91         return bufferview_.get();
92 }
93
94
95 Toolbar * LyXView::getToolbar() const
96 {
97         return toolbar_.get();
98 }
99
100
101 void LyXView::setLayout(string const & layout)
102 {
103         toolbar_->setLayout(layout);
104 }
105
106
107 void LyXView::updateToolbar()
108 {
109         toolbar_->update();
110 }
111
112
113 LyXFunc * LyXView::getLyXFunc() const
114 {
115         return lyxfunc_.get();
116 }
117
118
119 Menubar * LyXView::getMenubar() const
120 {
121         return menubar_.get();
122 }
123
124
125 void LyXView::updateMenubar()
126 {
127         if (!view()->buffer() && menubackend.hasMenu("main_nobuffer")) {
128                 menubar_->set("main_nobuffer");
129         } else {
130                 menubar_->set("main");
131         }
132
133         menubar_->update();
134 }
135
136
137 Intl * LyXView::getIntl() const
138 {
139         return intl_.get();
140 }
141
142
143 void LyXView::autoSave()
144 {
145         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
146
147         if (view()->available()) {
148                 ::AutoSave(view());
149         }
150 }
151
152
153 void LyXView::resetAutosaveTimer()
154 {
155         if (lyxrc.autosave)
156                 autosave_timeout_->restart();
157 }
158
159
160 void LyXView::updateLayoutChoice()
161 {
162         // don't show any layouts without a buffer
163         if (!view()->buffer()) {
164                 toolbar_->clearLayoutList();
165                 return;
166         }
167
168         // update the layout display
169         if (toolbar_->updateLayoutList(buffer()->params.textclass)) {
170                 current_layout = textclasslist[buffer()->params.textclass].defaultLayoutName();
171         }
172
173         string const & layout =
174                 bufferview_->getLyXText()->cursor.par()->layout()->name();
175
176         if (layout != current_layout) {
177                 toolbar_->setLayout(layout);
178                 current_layout = layout;
179         }
180 }
181
182
183 void LyXView::updateWindowTitle()
184 {
185         static string last_title = "LyX";
186         string title = "LyX";
187         string icon_title = "LyX";
188
189         if (view()->available()) {
190                 string const cur_title = buffer()->fileName();
191                 if (!cur_title.empty()) {
192                         title += ": " + MakeDisplayPath(cur_title, 30);
193                         if (!buffer()->isLyxClean())
194                                 title += _(" (changed)");
195                         if (buffer()->isReadonly())
196                                 title += _(" (read only)");
197                         // Show only the filename if it's available
198                         icon_title = OnlyFilename(cur_title);
199                 }
200         }
201
202         if (title != last_title) {
203                 setWindowTitle(title, icon_title);
204                 last_title = title;
205         }
206 }