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