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