]> git.lyx.org Git - lyx.git/blob - src/frontends/LyXView.C
disable open dialogs if applying them is not allowed
[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 "Toolbars.h"
18 #include "Menubar.h"
19
20 #include "buffer.h"
21 #include "bufferparams.h"
22 #include "BufferView.h"
23 #include "bufferview_funcs.h"
24 #include "cursor.h"
25 #include "debug.h"
26 #include "funcrequest.h"
27 #include "gettext.h"
28 #include "intl.h"
29 #include "lyx_cb.h"
30 #include "lyxfunc.h"
31 #include "lyxrc.h"
32 #include "lyxtext.h"
33 #include "MenuBackend.h"
34 #include "paragraph.h"
35
36 #include "controllers/ControlCommandBuffer.h"
37
38 #include "support/filetools.h" // OnlyFilename()
39
40 #include <boost/bind.hpp>
41
42 #include <sys/time.h>
43 #ifdef HAVE_UNISTD_H
44 # include <unistd.h>
45 #endif
46
47 using lyx::support::MakeDisplayPath;
48 using lyx::support::OnlyFilename;
49
50 using std::endl;
51 using std::string;
52
53 using lyx::frontend::ControlCommandBuffer;
54
55 string current_layout;
56
57
58 LyXView::LyXView()
59         : toolbars_(new Toolbars(*this)),
60           intl_(new Intl),
61           autosave_timeout_(new Timeout(5000)),
62           lyxfunc_(new LyXFunc(this)),
63           dialogs_(new Dialogs(*this)),
64           controlcommand_(new ControlCommandBuffer(*this))
65 {
66         lyxerr[Debug::INIT] << "Initializing LyXFunc" << endl;
67 }
68
69
70 LyXView::~LyXView()
71 {
72 }
73
74
75 void LyXView::init()
76 {
77         updateLayoutChoice();
78         updateMenubar();
79
80         // Start autosave timer
81         if (lyxrc.autosave) {
82                 autosave_timeout_->timeout.connect(boost::bind(&LyXView::autoSave, this));
83                 autosave_timeout_->setTimeout(lyxrc.autosave * 1000);
84                 autosave_timeout_->start();
85         }
86
87         intl_->InitKeyMapper(lyxrc.use_kbmap);
88 }
89
90
91 Buffer * LyXView::buffer() const
92 {
93         return bufferview_->buffer();
94 }
95
96
97 boost::shared_ptr<BufferView> const & LyXView::view() const
98 {
99         return bufferview_;
100 }
101
102
103 void LyXView::setLayout(string const & layout)
104 {
105         toolbars_->setLayout(layout);
106 }
107
108
109 void LyXView::updateToolbars()
110 {
111         bool const math = bufferview_->cursor().inMathed();
112         bool const table =
113                 getLyXFunc().getStatus(FuncRequest(LFUN_LAYOUT_TABULAR)).enabled();
114         toolbars_->update(math, table);
115         // update redaonly status of open dialogs. This could also be in
116         // updateMenubar(), but since updateToolbars() and updateMenubar()
117         // are always called together it is only here.
118         getDialogs().checkStatus();
119 }
120
121
122 void LyXView::updateMenubar()
123 {
124         menubar_->update();
125 }
126
127
128 void LyXView::autoSave()
129 {
130         lyxerr[Debug::INFO] << "Running autoSave()" << endl;
131
132         if (view()->available()) {
133                 ::AutoSave(view().get());
134         }
135 }
136
137
138 void LyXView::resetAutosaveTimer()
139 {
140         if (lyxrc.autosave)
141                 autosave_timeout_->restart();
142 }
143
144
145 void LyXView::updateLayoutChoice()
146 {
147         // Don't show any layouts without a buffer
148         if (!view()->buffer()) {
149                 toolbars_->clearLayoutList();
150                 return;
151         }
152
153         // Update the layout display
154         if (toolbars_->updateLayoutList(buffer()->params().textclass)) {
155                 current_layout = buffer()->params().getLyXTextClass().defaultLayoutName();
156         }
157
158         if (bufferview_->cursor().inMathed())
159                 return;
160
161         string const & layout =
162                 bufferview_->cursor().paragraph().layout()->name();
163
164         if (layout != current_layout) {
165                 toolbars_->setLayout(layout);
166                 current_layout = layout;
167         }
168 }
169
170
171 void LyXView::updateWindowTitle()
172 {
173         static string last_title = "LyX";
174         string maximize_title = "LyX";
175         string minimize_title = "LyX";
176
177         if (view()->available()) {
178                 string const cur_title = buffer()->fileName();
179                 if (!cur_title.empty()) {
180                         maximize_title += ": " + MakeDisplayPath(cur_title, 30);
181                         minimize_title = OnlyFilename(cur_title);
182                         if (!buffer()->isClean()) {
183                                 maximize_title += _(" (changed)");
184                                 minimize_title += '*';
185                         }
186                         if (buffer()->isReadonly())
187                                 maximize_title += _(" (read only)");
188                 }
189         }
190
191         if (maximize_title != last_title) {
192                 setWindowTitle(maximize_title, minimize_title);
193                 last_title = maximize_title;
194         }
195 }
196
197
198 void LyXView::dispatch(FuncRequest const & cmd)
199 {
200         getLyXFunc().dispatch(cmd);
201 }
202
203
204 Buffer const * const LyXView::updateInset(InsetBase const * inset) const
205 {
206         Buffer const * buffer_ptr = 0;
207         if (inset) {
208                 buffer_ptr = bufferview_->buffer();
209                 bufferview_->update();
210         }
211         return buffer_ptr;
212 }