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