]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
minimal effort implementation of:
[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 "coordcache.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 #include "metricsinfo.h"
40
41 #include "frontends/Alert.h"
42 #include "frontends/Clipboard.h"
43 #include "frontends/Dialogs.h"
44 #include "frontends/LyXView.h"
45
46 #include "insets/insetcommand.h" // ChangeRefs
47 #include "insets/insettext.h"
48
49
50 using lyx::support::bformat;
51
52 using std::distance;
53 using std::find;
54 using std::string;
55 using std::vector;
56
57
58 extern BufferList bufferlist;
59
60
61 BufferView::BufferView(LyXView * owner)
62         : pimpl_(new Pimpl(*this, owner))
63 {}
64
65
66 BufferView::~BufferView()
67 {
68         delete pimpl_;
69 }
70
71
72 void BufferView::unsetXSel()
73 {
74         pimpl_->xsel_cache_.set = false;
75 }
76
77
78 Buffer * BufferView::buffer() const
79 {
80         return pimpl_->buffer_;
81 }
82
83
84 LyXView * BufferView::owner() const
85 {
86         return pimpl_->owner_;
87 }
88
89
90 void BufferView::setBuffer(Buffer * b)
91 {
92         pimpl_->setBuffer(b);
93 }
94
95
96 bool BufferView::loadLyXFile(string const & fn, bool tl)
97 {
98         return pimpl_->loadLyXFile(fn, tl);
99 }
100
101
102 void BufferView::reload()
103 {
104         string const fn = buffer()->fileName();
105         if (bufferlist.close(buffer(), false))
106                 loadLyXFile(fn);
107 }
108
109
110 void BufferView::resize()
111 {
112         if (pimpl_->buffer_)
113                 pimpl_->resizeCurrentBuffer();
114 }
115
116
117 bool BufferView::fitCursor()
118 {
119         return pimpl_->fitCursor();
120 }
121
122
123 bool BufferView::update(Update::flags flags)
124 {
125         return pimpl_->update(flags);
126 }
127
128
129 void BufferView::updateScrollbar()
130 {
131         pimpl_->updateScrollbar();
132 }
133
134
135 ScrollbarParameters const & BufferView::scrollbarParameters() const
136 {
137         return pimpl_->scrollbarParameters();
138 }
139
140
141 void BufferView::scrollDocView(int value)
142 {
143         pimpl_->scrollDocView(value);
144 }
145
146
147 void BufferView::setCursorFromScrollbar()
148 {
149         pimpl_->setCursorFromScrollbar();
150 }
151
152
153 bool BufferView::available() const
154 {
155         return pimpl_->available();
156 }
157
158
159 Change const BufferView::getCurrentChange()
160 {
161         return pimpl_->getCurrentChange();
162 }
163
164
165 void BufferView::savePosition(unsigned int i)
166 {
167         pimpl_->savePosition(i);
168 }
169
170
171 void BufferView::restorePosition(unsigned int i)
172 {
173         pimpl_->restorePosition(i);
174 }
175
176
177 bool BufferView::isSavedPosition(unsigned int i)
178 {
179         return pimpl_->isSavedPosition(i);
180 }
181
182 void BufferView::saveSavedPositions()
183 {
184         return pimpl_->saveSavedPositions();
185 }
186
187 void BufferView::switchKeyMap()
188 {
189         pimpl_->switchKeyMap();
190 }
191
192
193 int BufferView::workWidth() const
194 {
195         return pimpl_->width();
196 }
197
198
199 void BufferView::center()
200 {
201         pimpl_->center();
202 }
203
204
205 FuncStatus BufferView::getStatus(FuncRequest const & cmd)
206 {
207         return pimpl_->getStatus(cmd);
208 }
209
210
211 bool BufferView::dispatch(FuncRequest const & ev)
212 {
213         return pimpl_->dispatch(ev);
214 }
215
216
217 void BufferView::selectionRequested()
218 {
219         pimpl_->selectionRequested();
220 }
221
222
223 void BufferView::selectionLost()
224 {
225         pimpl_->selectionLost();
226 }
227
228
229 void BufferView::workAreaResize(int width, int height)
230 {
231         pimpl_->workAreaResize(width, height);
232 }
233
234
235 bool BufferView::workAreaDispatch(FuncRequest const & ev)
236 {
237         return pimpl_->workAreaDispatch(ev);
238 }
239
240
241 void BufferView::scroll(int lines)
242 {
243         pimpl_->scroll(lines);
244 }
245
246
247 void BufferView::setCursorFromRow(int row)
248 {
249         int tmpid = -1;
250         int tmppos = -1;
251
252         buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
253
254         if (tmpid == -1)
255                 text()->setCursor(cursor(), 0, 0);
256         else
257                 text()->setCursor(cursor(), buffer()->getParFromID(tmpid).pit(), tmppos);
258 }
259
260
261 void BufferView::gotoLabel(string const & label)
262 {
263         for (InsetIterator it = inset_iterator_begin(buffer()->inset()); it; ++it) {
264                 vector<string> labels;
265                 it->getLabelList(*buffer(), labels);
266                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
267                         setCursor(it);
268                         update();
269                         return;
270                 }
271         }
272 }
273
274
275 LyXText * BufferView::getLyXText()
276 {
277         LyXText * text = cursor().innerText();
278         BOOST_ASSERT(text);
279         return text;
280 }
281
282
283 LyXText const * BufferView::getLyXText() const
284 {
285         LyXText const * text = cursor().innerText();
286         BOOST_ASSERT(text);
287         return text;
288 }
289
290
291 int BufferView::workHeight() const
292 {
293         return pimpl_->height();
294 }
295
296
297 LyXText * BufferView::text() const
298 {
299         return buffer() ? &buffer()->text() : 0;
300 }
301
302
303 void BufferView::setCursor(DocIterator const & dit)
304 {
305         size_t const n = dit.depth();
306         for (size_t i = 0; i < n; ++i)
307                 dit[i].inset().edit(cursor(), true);
308
309         cursor().setCursor(dit);
310         cursor().selection() = false;
311 }
312
313
314 void BufferView::mouseSetCursor(LCursor & cur)
315 {
316         BOOST_ASSERT(&cur.bv() == this);
317
318         // Has the cursor just left the inset?
319         bool badcursor = false;
320         if (&cursor().inset() != &cur.inset())
321                 badcursor = cursor().inset().notifyCursorLeaves(cursor());
322
323         // do the dEPM magic if needed
324         // FIXME: move this to InsetText::notifyCursorLeaves?
325         if (!badcursor && cursor().inTexted())
326                 cursor().text()->deleteEmptyParagraphMechanism(cur, cursor());
327
328         cursor() = cur;
329         cursor().clearSelection();
330         cursor().setTargetX();
331         finishUndo();
332
333 }
334
335
336 void BufferView::putSelectionAt(DocIterator const & cur,
337                                 int length, bool backwards)
338 {
339         cursor().clearSelection();
340
341         setCursor(cur);
342
343         if (length) {
344                 if (backwards) {
345                         cursor().pos() += length;
346                         cursor().setSelection(cursor(), -length);
347                 } else
348                         cursor().setSelection(cursor(), length);
349         }
350 }
351
352
353 LCursor & BufferView::cursor()
354 {
355         return pimpl_->cursor_;
356 }
357
358
359 LCursor const & BufferView::cursor() const
360 {
361         return pimpl_->cursor_;
362 }
363
364
365 lyx::pit_type BufferView::anchor_ref() const
366 {
367         return pimpl_->anchor_ref_;
368 }
369
370
371 int BufferView::offset_ref() const
372 {
373         return pimpl_->offset_ref_;
374 }
375
376
377 ViewMetricsInfo const & BufferView::viewMetricsInfo()
378 {
379         return pimpl_->viewMetricsInfo();
380 }
381
382
383 void BufferView::updateMetrics(bool singlepar)
384 {
385         pimpl_->updateMetrics(singlepar);
386 }