]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
bd4324cb77146165d03f940d04de44a01e0ef9ce
[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 FuncStatus BufferView::getStatus(FuncRequest const & cmd)
216 {
217         return pimpl_->getStatus(cmd);
218 }
219
220
221 bool BufferView::dispatch(FuncRequest const & ev)
222 {
223         return pimpl_->dispatch(ev);
224 }
225
226
227 void BufferView::selectionRequested()
228 {
229         pimpl_->selectionRequested();
230 }
231
232
233 void BufferView::selectionLost()
234 {
235         pimpl_->selectionLost();
236 }
237
238
239 void BufferView::workAreaResize(int width, int height)
240 {
241         pimpl_->workAreaResize(width, height);
242 }
243
244
245 void BufferView::workAreaKeyPress(LyXKeySymPtr key, key_modifier::state state)
246 {
247         pimpl_->workAreaKeyPress(key, state);
248 }
249
250
251 bool BufferView::workAreaDispatch(FuncRequest const & ev)
252 {
253         return pimpl_->workAreaDispatch(ev);
254 }
255
256
257 void BufferView::scroll(int lines)
258 {
259         pimpl_->scroll(lines);
260 }
261
262
263 void BufferView::showErrorList(string const & action) const
264 {
265         if (getErrorList().size()) {
266                 string const title = bformat(_("%1$s Errors (%2$s)"),
267                         action, buffer()->fileName());
268                 owner()->getDialogs().show("errorlist", title);
269                 pimpl_->errorlist_.clear();
270         }
271 }
272
273
274 ErrorList const & BufferView::getErrorList() const
275 {
276         return pimpl_->errorlist_;
277 }
278
279
280 void BufferView::setCursorFromRow(int row)
281 {
282         int tmpid = -1;
283         int tmppos = -1;
284
285         buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
286
287         if (tmpid == -1)
288                 text()->setCursor(cursor(), 0, 0);
289         else
290                 text()->setCursor(cursor(), buffer()->getParFromID(tmpid).pit(), tmppos);
291 }
292
293
294 void BufferView::gotoLabel(string const & label)
295 {
296         for (InsetIterator it = inset_iterator_begin(buffer()->inset()); it; ++it) {
297                 vector<string> labels;
298                 it->getLabelList(*buffer(), labels);
299                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
300                         setCursor(it);
301                         update();
302                         return;
303                 }
304         }
305 }
306
307
308 void BufferView::hideCursor()
309 {
310         pimpl_->gui().guiCursor().hide();
311 }
312
313 LyXText * BufferView::getLyXText()
314 {
315         LyXText * text = cursor().innerText();
316         BOOST_ASSERT(text);
317         return text;
318 }
319
320
321 LyXText const * BufferView::getLyXText() const
322 {
323         LyXText const * text = cursor().innerText();
324         BOOST_ASSERT(text);
325         return text;
326 }
327
328
329 int BufferView::workHeight() const
330 {
331         return pimpl_->height();
332 }
333
334
335 LyXText * BufferView::text() const
336 {
337         return buffer() ? &buffer()->text() : 0;
338 }
339
340
341 void BufferView::setCursor(DocIterator const & dit)
342 {
343         size_t const n = dit.depth();
344         for (size_t i = 0; i < n; ++i)
345                 dit[i].inset().edit(cursor(), true);
346
347         cursor().setCursor(dit);
348         cursor().selection() = false;
349 }
350
351
352 void BufferView::mouseSetCursor(LCursor & cur)
353 {
354         BOOST_ASSERT(&cur.bv() == this);
355
356         // Has the cursor just left the inset?
357         if (&cursor().inset() != &cur.inset())
358                 cursor().inset().notifyCursorLeaves(cursor());
359
360         // do the dEPM magic if needed
361         if (cursor().inTexted())
362                 cursor().text()->deleteEmptyParagraphMechanism(cur, cursor());
363
364         cursor() = cur;
365         cursor().clearSelection();
366         cursor().setTargetX();
367         finishUndo();
368
369 }
370
371
372 void BufferView::putSelectionAt(DocIterator const & cur,
373                                 int length, bool backwards)
374 {
375         cursor().clearSelection();
376
377         setCursor(cur);
378
379         if (length) {
380                 if (backwards) {
381                         cursor().pos() += length;
382                         cursor().setSelection(cursor(), -length);
383                 } else
384                         cursor().setSelection(cursor(), length);
385         }
386 }
387
388
389 LCursor & BufferView::cursor()
390 {
391         return pimpl_->cursor_;
392 }
393
394
395 LCursor const & BufferView::cursor() const
396 {
397         return pimpl_->cursor_;
398 }
399
400
401 lyx::pit_type BufferView::anchor_ref() const
402 {
403         return pimpl_->anchor_ref_;
404 }
405
406
407 int BufferView::offset_ref() const
408 {
409         return pimpl_->offset_ref_;
410 }