]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
fix crash when collapsing ert with cursor inside
[lyx.git] / src / BufferView.C
1 /**
2  * \file BufferView.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author André Pönitz
10  * \author Jürgen Vigna
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "BufferView.h"
18
19 #include "buffer.h"
20 #include "bufferlist.h"
21 #include "bufferparams.h"
22 #include "BufferView_pimpl.h"
23 #include "CutAndPaste.h"
24 #include "coordcache.h"
25 #include "debug.h"
26 #include "funcrequest.h"
27 #include "FuncStatus.h"
28 #include "gettext.h"
29 #include "insetiterator.h"
30 #include "language.h"
31 #include "lyxlayout.h"
32 #include "lyxtext.h"
33 #include "lyxtextclass.h"
34 #include "paragraph.h"
35 #include "paragraph_funcs.h"
36 #include "pariterator.h"
37 #include "texrow.h"
38 #include "undo.h"
39 #include "WordLangTuple.h"
40
41 #include "frontends/Alert.h"
42 #include "frontends/Dialogs.h"
43 #include "frontends/LyXView.h"
44 #include "frontends/screen.h"
45 #include "frontends/WorkArea.h"
46
47 #include "insets/insetcommand.h" // ChangeRefs
48 #include "insets/updatableinset.h"
49 #include "insets/insettext.h"
50
51 using lyx::support::bformat;
52
53 using lyx::cap::setSelectionRange;
54
55 using std::distance;
56 using std::find;
57 using std::string;
58 using std::swap;
59 using std::vector;
60
61
62 extern BufferList bufferlist;
63
64
65 BufferView::BufferView(LyXView * owner, int width, int height)
66         : pimpl_(new Pimpl(*this, owner, width, height))
67 {}
68
69
70 BufferView::~BufferView()
71 {
72         delete pimpl_;
73 }
74
75
76 void BufferView::unsetXSel()
77 {
78         pimpl_->xsel_cache_.set = false;
79 }
80
81
82 Buffer * BufferView::buffer() const
83 {
84         return pimpl_->buffer_;
85 }
86
87
88 LyXScreen & BufferView::screen() const
89 {
90         return pimpl_->screen();
91 }
92
93
94 LyXView * BufferView::owner() const
95 {
96         return pimpl_->owner_;
97 }
98
99
100 Painter & BufferView::painter() const
101 {
102         return pimpl_->painter();
103 }
104
105
106 void BufferView::setBuffer(Buffer * b)
107 {
108         pimpl_->setBuffer(b);
109 }
110
111
112 void BufferView::newFile(string const & fn, string const & tn, bool named)
113 {
114         pimpl_->newFile(fn, tn, named);
115 }
116
117
118 bool BufferView::loadLyXFile(string const & fn, bool tl)
119 {
120         return pimpl_->loadLyXFile(fn, tl);
121 }
122
123
124 void BufferView::reload()
125 {
126         string const fn = buffer()->fileName();
127         if (bufferlist.close(buffer(), false))
128                 loadLyXFile(fn);
129 }
130
131
132 void BufferView::resize()
133 {
134         if (pimpl_->buffer_)
135                 pimpl_->resizeCurrentBuffer();
136 }
137
138
139 bool BufferView::fitCursor()
140 {
141         return pimpl_->fitCursor();
142 }
143
144
145 void BufferView::update(bool fitcursor, bool forceupdate)
146 {
147         pimpl_->update(fitcursor, forceupdate);
148 }
149
150
151 void BufferView::updateScrollbar()
152 {
153         pimpl_->updateScrollbar();
154 }
155
156
157 void BufferView::scrollDocView(int value)
158 {
159         pimpl_->scrollDocView(value);
160 }
161
162
163 bool BufferView::available() const
164 {
165         return pimpl_->available();
166 }
167
168
169 Change const BufferView::getCurrentChange()
170 {
171         return pimpl_->getCurrentChange();
172 }
173
174
175 void BufferView::savePosition(unsigned int i)
176 {
177         pimpl_->savePosition(i);
178 }
179
180
181 void BufferView::restorePosition(unsigned int i)
182 {
183         pimpl_->restorePosition(i);
184 }
185
186
187 bool BufferView::isSavedPosition(unsigned int i)
188 {
189         return pimpl_->isSavedPosition(i);
190 }
191
192
193 void BufferView::switchKeyMap()
194 {
195         pimpl_->switchKeyMap();
196 }
197
198
199 int BufferView::workWidth() const
200 {
201         return pimpl_->workarea().workWidth();
202 }
203
204
205 void BufferView::center()
206 {
207         pimpl_->center();
208 }
209
210
211 string const BufferView::getClipboard() const
212 {
213         return pimpl_->workarea().getClipboard();
214 }
215
216
217 void BufferView::stuffClipboard(string const & stuff) const
218 {
219         pimpl_->stuffClipboard(stuff);
220 }
221
222
223 FuncStatus BufferView::getStatus(FuncRequest const & cmd)
224 {
225         return pimpl_->getStatus(cmd);
226 }
227
228
229 bool BufferView::dispatch(FuncRequest const & ev)
230 {
231         return pimpl_->dispatch(ev);
232 }
233
234
235 void BufferView::scroll(int lines)
236 {
237         pimpl_->scroll(lines);
238 }
239
240
241 void BufferView::showErrorList(string const & action) const
242 {
243         if (getErrorList().size()) {
244                 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
245                         action, buffer()->fileName());
246                 owner()->getDialogs().show("errorlist", title);
247                 pimpl_->errorlist_.clear();
248         }
249 }
250
251
252 ErrorList const & BufferView::getErrorList() const
253 {
254         return pimpl_->errorlist_;
255 }
256
257
258 void BufferView::setCursorFromRow(int row)
259 {
260         int tmpid = -1;
261         int tmppos = -1;
262
263         buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
264
265         if (tmpid == -1)
266                 text()->setCursor(cursor(), 0, 0);
267         else
268                 text()->setCursor(cursor(), buffer()->getParFromID(tmpid).pit(), tmppos);
269 }
270
271
272 void BufferView::gotoLabel(string const & label)
273 {
274         for (InsetIterator it = inset_iterator_begin(buffer()->inset()); it; ++it) {
275                 vector<string> labels;
276                 it->getLabelList(*buffer(), labels);
277                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
278                         setCursor(it);
279                         update();
280                         return;
281                 }
282         }
283 }
284
285
286 void BufferView::hideCursor()
287 {
288         screen().hideCursor();
289 }
290
291 LyXText * BufferView::getLyXText()
292 {
293         LyXText * text = cursor().innerText();
294         BOOST_ASSERT(text);
295         return text;
296 }
297
298
299 LyXText const * BufferView::getLyXText() const
300 {
301         LyXText const * text = cursor().innerText();
302         BOOST_ASSERT(text);
303         return text;
304 }
305
306
307 void BufferView::haveSelection(bool sel)
308 {
309         pimpl_->workarea().haveSelection(sel);
310 }
311
312
313 int BufferView::workHeight() const
314 {
315         return pimpl_->workarea().workHeight();
316 }
317
318
319 LyXText * BufferView::text() const
320 {
321         return buffer() ? &buffer()->text() : 0;
322 }
323
324
325 void BufferView::setCursor(DocIterator const & dit)
326 {
327         size_t const n = dit.depth();
328         for (size_t i = 0; i < n; ++i)
329                 dit[i].inset().edit(cursor(), true);
330
331         cursor().setCursor(dit);
332         cursor().selection() = false;
333 }
334
335
336 void BufferView::putSelectionAt(DocIterator const & cur,
337                                 int length, bool backwards)
338 {
339         cursor().clearSelection();
340
341         setCursor(cur);
342
343         if (length) {
344                 if (backwards) {
345                         cursor().pos() += length;
346                         cursor().setSelection(cursor(), -length);
347                 } else
348                         cursor().setSelection(cursor(), length);
349         }
350 }
351
352
353 LCursor & BufferView::cursor()
354 {
355         return pimpl_->cursor_;
356 }
357
358
359 LCursor const & BufferView::cursor() const
360 {
361         return pimpl_->cursor_;
362 }
363
364
365 lyx::pit_type BufferView::anchor_ref() const
366 {
367         return pimpl_->anchor_ref_;
368 }
369
370
371 int BufferView::offset_ref() const
372 {
373         return pimpl_->offset_ref_;
374 }