]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
1bcac4dd0f6e3a33d7786dd9f998347a8b0b2ae4
[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 "funcrequest.h"
26 #include "lyx_cb.h"
27 #include "BufferView.h"
28 #include "bufferview_funcs.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         : controlcommand_(new ControlCommandBuffer(getLyXFunc())),
50           intl_(new Intl),
51           autosave_timeout_(new Timeout(5000)),
52           lyxfunc_(new LyXFunc(this)),
53           dialogs_(new Dialogs(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         toolbar_->update();
101 }
102
103
104 void LyXView::updateMenubar()
105 {
106         menubar_->update();
107 }
108
109
110 void LyXView::autoSave()
111 {
112         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
113
114         if (view()->available()) {
115                 ::AutoSave(view().get());
116         }
117 }
118
119
120 void LyXView::resetAutosaveTimer()
121 {
122         if (lyxrc.autosave)
123                 autosave_timeout_->restart();
124 }
125
126
127 void LyXView::updateLayoutChoice()
128 {
129         // don't show any layouts without a buffer
130         if (!view()->buffer()) {
131                 toolbar_->clearLayoutList();
132                 return;
133         }
134
135         // update the layout display
136         if (toolbar_->updateLayoutList(buffer()->params.textclass)) {
137                 current_layout = buffer()->params.getLyXTextClass().defaultLayoutName();
138         }
139
140         string const & layout =
141                 bufferview_->getLyXText()->cursor.par()->layout()->name();
142
143         if (layout != current_layout) {
144                 toolbar_->setLayout(layout);
145                 current_layout = layout;
146         }
147 }
148
149
150 void LyXView::updateWindowTitle()
151 {
152         static string last_title = "LyX";
153         string title = "LyX";
154         string icon_title = "LyX";
155
156         if (view()->available()) {
157                 string const cur_title = buffer()->fileName();
158                 if (!cur_title.empty()) {
159                         title += ": " + MakeDisplayPath(cur_title, 30);
160                         if (!buffer()->isClean())
161                                 title += _(" (changed)");
162                         if (buffer()->isReadonly())
163                                 title += _(" (read only)");
164                         // Show only the filename if it's available
165                         icon_title = OnlyFilename(cur_title);
166                 }
167         }
168
169         if (title != last_title) {
170                 setWindowTitle(title, icon_title);
171                 last_title = title;
172         }
173 }
174
175
176 void LyXView::dispatch(FuncRequest const & req)
177 {
178         // substitute the correct BufferView here
179         FuncRequest r = req;
180         r.setView(view().get());
181         getLyXFunc().dispatch(r); 
182 }