]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
d52233651d9ef724f31f30ba5b828a06051c37c8
[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/Clipboard.h"
43 #include "frontends/Dialogs.h"
44 #include "frontends/LyXView.h"
45 #include "frontends/Gui.h"
46 #include "frontends/WorkArea.h"
47
48 #include "insets/insetcommand.h" // ChangeRefs
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::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 LyXView * BufferView::owner() const
88 {
89         return pimpl_->owner_;
90 }
91
92
93 lyx::frontend::Painter & BufferView::painter() const
94 {
95         return pimpl_->painter();
96 }
97
98
99 void BufferView::setBuffer(Buffer * b)
100 {
101         pimpl_->setBuffer(b);
102 }
103
104
105 void BufferView::newFile(string const & fn, string const & tn, bool named)
106 {
107         pimpl_->newFile(fn, tn, named);
108 }
109
110
111 bool BufferView::loadLyXFile(string const & fn, bool tl)
112 {
113         return pimpl_->loadLyXFile(fn, tl);
114 }
115
116
117 void BufferView::reload()
118 {
119         string const fn = buffer()->fileName();
120         if (bufferlist.close(buffer(), false))
121                 loadLyXFile(fn);
122 }
123
124
125 void BufferView::resize()
126 {
127         if (pimpl_->buffer_)
128                 pimpl_->resizeCurrentBuffer();
129 }
130
131
132 bool BufferView::fitCursor()
133 {
134         return pimpl_->fitCursor();
135 }
136
137
138 void BufferView::update(Update::flags flags)
139 {
140         pimpl_->update(flags);
141 }
142
143
144 void BufferView::updateScrollbar()
145 {
146         pimpl_->updateScrollbar();
147 }
148
149
150 void BufferView::scrollDocView(int value)
151 {
152         pimpl_->scrollDocView(value);
153 }
154
155
156 bool BufferView::available() const
157 {
158         return pimpl_->available();
159 }
160
161
162 Change const BufferView::getCurrentChange()
163 {
164         return pimpl_->getCurrentChange();
165 }
166
167
168 void BufferView::savePosition(unsigned int i)
169 {
170         pimpl_->savePosition(i);
171 }
172
173
174 void BufferView::restorePosition(unsigned int i)
175 {
176         pimpl_->restorePosition(i);
177 }
178
179
180 bool BufferView::isSavedPosition(unsigned int i)
181 {
182         return pimpl_->isSavedPosition(i);
183 }
184
185 void BufferView::saveSavedPositions()
186 {
187         return pimpl_->saveSavedPositions();
188 }
189
190 void BufferView::switchKeyMap()
191 {
192         pimpl_->switchKeyMap();
193 }
194
195
196 int BufferView::workWidth() const
197 {
198         return pimpl_->workarea().width();
199 }
200
201
202 void BufferView::center()
203 {
204         pimpl_->center();
205 }
206
207
208 string const BufferView::getClipboard() const
209 {
210         return pimpl_->clipboard().get();
211 }
212
213
214 void BufferView::stuffClipboard(string const & stuff) const
215 {
216         pimpl_->stuffClipboard(stuff);
217 }
218
219
220 FuncStatus BufferView::getStatus(FuncRequest const & cmd)
221 {
222         return pimpl_->getStatus(cmd);
223 }
224
225
226 bool BufferView::dispatch(FuncRequest const & ev)
227 {
228         return pimpl_->dispatch(ev);
229 }
230
231
232 void BufferView::selectionRequested()
233 {
234         pimpl_->selectionRequested();
235 }
236
237
238 void BufferView::selectionLost()
239 {
240         pimpl_->selectionLost();
241 }
242
243
244 void BufferView::workAreaResize()
245 {
246         pimpl_->workAreaResize();
247 }
248
249
250 void BufferView::workAreaKeyPress(LyXKeySymPtr key, key_modifier::state state)
251 {
252         pimpl_->workAreaKeyPress(key, state);
253 }
254
255
256 bool BufferView::workAreaDispatch(FuncRequest const & ev)
257 {
258         return pimpl_->workAreaDispatch(ev);
259 }
260
261
262 void BufferView::scroll(int lines)
263 {
264         pimpl_->scroll(lines);
265 }
266
267
268 void BufferView::showErrorList(string const & action) const
269 {
270         if (getErrorList().size()) {
271                 string const title = bformat(_("%1$s Errors (%2$s)"),
272                         action, buffer()->fileName());
273                 owner()->getDialogs().show("errorlist", title);
274                 pimpl_->errorlist_.clear();
275         }
276 }
277
278
279 ErrorList const & BufferView::getErrorList() const
280 {
281         return pimpl_->errorlist_;
282 }
283
284
285 void BufferView::setCursorFromRow(int row)
286 {
287         int tmpid = -1;
288         int tmppos = -1;
289
290         buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
291
292         if (tmpid == -1)
293                 text()->setCursor(cursor(), 0, 0);
294         else
295                 text()->setCursor(cursor(), buffer()->getParFromID(tmpid).pit(), tmppos);
296 }
297
298
299 void BufferView::gotoLabel(string const & label)
300 {
301         for (InsetIterator it = inset_iterator_begin(buffer()->inset()); it; ++it) {
302                 vector<string> labels;
303                 it->getLabelList(*buffer(), labels);
304                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
305                         setCursor(it);
306                         update();
307                         return;
308                 }
309         }
310 }
311
312
313 void BufferView::hideCursor()
314 {
315         pimpl_->gui().guiCursor().hide();
316 }
317
318 LyXText * BufferView::getLyXText()
319 {
320         LyXText * text = cursor().innerText();
321         BOOST_ASSERT(text);
322         return text;
323 }
324
325
326 LyXText const * BufferView::getLyXText() const
327 {
328         LyXText const * text = cursor().innerText();
329         BOOST_ASSERT(text);
330         return text;
331 }
332
333
334 void BufferView::haveSelection(bool sel)
335 {
336         pimpl_->clipboard().haveSelection(sel);
337 }
338
339
340 int BufferView::workHeight() const
341 {
342         return pimpl_->workarea().height();
343 }
344
345
346 LyXText * BufferView::text() const
347 {
348         return buffer() ? &buffer()->text() : 0;
349 }
350
351
352 void BufferView::setCursor(DocIterator const & dit)
353 {
354         size_t const n = dit.depth();
355         for (size_t i = 0; i < n; ++i)
356                 dit[i].inset().edit(cursor(), true);
357
358         cursor().setCursor(dit);
359         cursor().selection() = false;
360 }
361
362
363 void BufferView::mouseSetCursor(LCursor & cur)
364 {
365         BOOST_ASSERT(&cur.bv() == this);
366
367         // Has the cursor just left the inset?
368         if (&cursor().inset() != &cur.inset())
369                 cursor().inset().notifyCursorLeaves(cursor());
370
371         // do the dEPM magic if needed
372         if (cursor().inTexted())
373                 cursor().text()->deleteEmptyParagraphMechanism(cur, cursor());
374
375         cursor() = cur;
376         cursor().clearSelection();
377         cursor().setTargetX();
378         finishUndo();
379
380 }
381
382
383 void BufferView::putSelectionAt(DocIterator const & cur,
384                                 int length, bool backwards)
385 {
386         cursor().clearSelection();
387
388         setCursor(cur);
389
390         if (length) {
391                 if (backwards) {
392                         cursor().pos() += length;
393                         cursor().setSelection(cursor(), -length);
394                 } else
395                         cursor().setSelection(cursor(), length);
396         }
397 }
398
399
400 bool const BufferView::repaintAll() const
401 {
402         return pimpl_->repaintAll();
403 }
404
405
406 void const BufferView::repaintAll(bool r) const
407 {
408         pimpl_->repaintAll(r);
409 }
410
411
412 LCursor & BufferView::cursor()
413 {
414         return pimpl_->cursor_;
415 }
416
417
418 LCursor const & BufferView::cursor() const
419 {
420         return pimpl_->cursor_;
421 }
422
423
424 lyx::pit_type BufferView::anchor_ref() const
425 {
426         return pimpl_->anchor_ref_;
427 }
428
429
430 int BufferView::offset_ref() const
431 {
432         return pimpl_->offset_ref_;
433 }