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