]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
f49bdeeed1b542862f4c9b9dd170c07704233c97
[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 "BufferView.h"
22 #include "bufferview_funcs.h"
23 #include "debug.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "intl.h"
27 #include "lyx_cb.h"
28 #include "lyxfunc.h"
29 #include "lyxrc.h"
30 #include "lyxtext.h"
31 #include "MenuBackend.h"
32
33 #include "controllers/ControlCommandBuffer.h"
34
35 #include "mathed/math_cursor.h"
36
37 #include "support/filetools.h" // OnlyFilename()
38
39 #include <boost/bind.hpp>
40
41 #include <sys/time.h>
42 #include <unistd.h>
43
44 using namespace lyx::support;
45 using std::endl;
46
47
48 string current_layout;
49
50
51 LyXView::LyXView()
52         : intl_(new Intl),
53           autosave_timeout_(new Timeout(5000)),
54           lyxfunc_(new LyXFunc(this)),
55           dialogs_(new Dialogs(*this)),
56           controlcommand_(new ControlCommandBuffer(*this))
57 {
58         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
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 void LyXView::setLayout(string const & layout)
96 {
97         toolbar_->setLayout(layout);
98 }
99
100
101 void LyXView::updateToolbar()
102 {
103         bool const math = mathcursor;
104         bool const table =
105                 !getLyXFunc().getStatus(LFUN_LAYOUT_TABULAR).disabled();
106         toolbar_->update(math, table);
107 }
108
109
110 void LyXView::updateMenubar()
111 {
112         menubar_->update();
113 }
114
115
116 void LyXView::autoSave()
117 {
118         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
119
120         if (view()->available()) {
121                 ::AutoSave(view().get());
122         }
123 }
124
125
126 void LyXView::resetAutosaveTimer()
127 {
128         if (lyxrc.autosave)
129                 autosave_timeout_->restart();
130 }
131
132
133 void LyXView::updateLayoutChoice()
134 {
135         // don't show any layouts without a buffer
136         if (!view()->buffer()) {
137                 toolbar_->clearLayoutList();
138                 return;
139         }
140
141         // update the layout display
142         if (toolbar_->updateLayoutList(buffer()->params.textclass)) {
143                 current_layout = buffer()->params.getLyXTextClass().defaultLayoutName();
144         }
145
146         string const & layout =
147                 bufferview_->getLyXText()->cursor.par()->layout()->name();
148
149         if (layout != current_layout) {
150                 toolbar_->setLayout(layout);
151                 current_layout = layout;
152         }
153 }
154
155
156 void LyXView::updateWindowTitle()
157 {
158         static string last_title = "LyX";
159         string maximize_title = "LyX";
160         string minimize_title = "LyX";
161
162         if (view()->available()) {
163                 string const cur_title = buffer()->fileName();
164                 if (!cur_title.empty()) {
165                         maximize_title += ": " + MakeDisplayPath(cur_title, 30);
166                         minimize_title = OnlyFilename(cur_title);
167                         if (!buffer()->isClean()) {
168                                 maximize_title += _(" (changed)");
169                                 minimize_title += '*';
170                         }
171                         if (buffer()->isReadonly())
172                                 maximize_title += _(" (read only)");
173                 }
174         }
175
176         if (maximize_title != last_title) {
177                 setWindowTitle(maximize_title, minimize_title);
178                 last_title = maximize_title;
179         }
180 }
181
182
183 void LyXView::dispatch(FuncRequest const & req)
184 {
185         // substitute the correct BufferView here
186         FuncRequest r = req;
187         r.setView(view().get());
188         getLyXFunc().dispatch(r);
189 }