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