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