]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
db4d1f2c75ba32d379622f431fa33fa4c445ed54
[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 "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
29 #include "Dialogs.h"
30 #include "Toolbar.h"
31 #include "Timeout.h"
32 #include "Menubar.h"
33 #include "controllers/ControlCommandBuffer.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 std::endl;
43
44 string current_layout;
45
46
47 LyXView::LyXView()
48 {
49         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
50
51         lyxfunc_.reset(new LyXFunc(this));
52         intl_.reset(new Intl);
53
54         // Give the timeout some default sensible value.
55         autosave_timeout_.reset(new Timeout(5000));
56
57         dialogs_.reset(new Dialogs(this));
58         controlcommand_.reset(new ControlCommandBuffer(*getLyXFunc()));
59 }
60
61
62 LyXView::~LyXView()
63 {
64 }
65
66
67 void LyXView::init()
68 {
69         updateLayoutChoice();
70         updateMenubar();
71
72         // Start autosave timer
73         if (lyxrc.autosave) {
74                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
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 boost::shared_ptr<BufferView> const & LyXView::view() const
90 {
91         return bufferview_;
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         menubar_->update();
128 }
129
130
131 Intl * LyXView::getIntl() const
132 {
133         return intl_.get();
134 }
135
136
137 void LyXView::autoSave()
138 {
139         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
140
141         if (view()->available()) {
142                 ::AutoSave(view().get());
143         }
144 }
145
146
147 void LyXView::resetAutosaveTimer()
148 {
149         if (lyxrc.autosave)
150                 autosave_timeout_->restart();
151 }
152
153
154 void LyXView::updateLayoutChoice()
155 {
156         // don't show any layouts without a buffer
157         if (!view()->buffer()) {
158                 toolbar_->clearLayoutList();
159                 return;
160         }
161
162         // update the layout display
163         if (toolbar_->updateLayoutList(buffer()->params.textclass)) {
164                 current_layout = buffer()->params.getLyXTextClass().defaultLayoutName();
165         }
166
167         string const & layout =
168                 bufferview_->getLyXText()->cursor.par()->layout()->name();
169
170         if (layout != current_layout) {
171                 toolbar_->setLayout(layout);
172                 current_layout = layout;
173         }
174 }
175
176
177 void LyXView::updateWindowTitle()
178 {
179         static string last_title = "LyX";
180         string title = "LyX";
181         string icon_title = "LyX";
182
183         if (view()->available()) {
184                 string const cur_title = buffer()->fileName();
185                 if (!cur_title.empty()) {
186                         title += ": " + MakeDisplayPath(cur_title, 30);
187                         if (!buffer()->isLyxClean())
188                                 title += _(" (changed)");
189                         if (buffer()->isReadonly())
190                                 title += _(" (read only)");
191                         // Show only the filename if it's available
192                         icon_title = OnlyFilename(cur_title);
193                 }
194         }
195
196         if (title != last_title) {
197                 setWindowTitle(title, icon_title);
198                 last_title = title;
199         }
200 }