]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
510e86d3b1d099b09ca4cd6112be63981a214d4b
[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 #include "metricsinfo.h"
41
42 #include "frontends/Alert.h"
43 #include "frontends/Clipboard.h"
44 #include "frontends/Dialogs.h"
45 #include "frontends/LyXView.h"
46
47 #include "insets/insetcommand.h" // ChangeRefs
48 #include "insets/insettext.h"
49
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)
65         : pimpl_(new Pimpl(*this, owner))
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 void BufferView::setBuffer(Buffer * b)
94 {
95         pimpl_->setBuffer(b);
96 }
97
98
99 string BufferView::firstLayout()
100 {
101         return pimpl_->firstLayout();
102 }
103
104
105 bool BufferView::loadLyXFile(string const & fn, bool tl)
106 {
107         return pimpl_->loadLyXFile(fn, tl);
108 }
109
110
111 void BufferView::reload()
112 {
113         string const fn = buffer()->fileName();
114         if (bufferlist.close(buffer(), false))
115                 loadLyXFile(fn);
116 }
117
118
119 void BufferView::resize()
120 {
121         if (pimpl_->buffer_)
122                 pimpl_->resizeCurrentBuffer();
123 }
124
125
126 bool BufferView::fitCursor()
127 {
128         return pimpl_->fitCursor();
129 }
130
131
132 bool BufferView::update(Update::flags flags)
133 {
134         return pimpl_->update(flags);
135 }
136
137
138 void BufferView::updateScrollbar()
139 {
140         pimpl_->updateScrollbar();
141 }
142
143
144 ScrollbarParameters const & BufferView::scrollbarParameters() const
145 {
146         return pimpl_->scrollbarParameters();
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_->width();
199 }
200
201
202 void BufferView::center()
203 {
204         pimpl_->center();
205 }
206
207
208 FuncStatus BufferView::getStatus(FuncRequest const & cmd)
209 {
210         return pimpl_->getStatus(cmd);
211 }
212
213
214 bool BufferView::dispatch(FuncRequest const & ev)
215 {
216         return pimpl_->dispatch(ev);
217 }
218
219
220 void BufferView::selectionRequested()
221 {
222         pimpl_->selectionRequested();
223 }
224
225
226 void BufferView::selectionLost()
227 {
228         pimpl_->selectionLost();
229 }
230
231
232 void BufferView::workAreaResize(int width, int height)
233 {
234         pimpl_->workAreaResize(width, height);
235 }
236
237
238 void BufferView::workAreaKeyPress(LyXKeySymPtr key, key_modifier::state state)
239 {
240         pimpl_->workAreaKeyPress(key, state);
241 }
242
243
244 bool BufferView::workAreaDispatch(FuncRequest const & ev)
245 {
246         return pimpl_->workAreaDispatch(ev);
247 }
248
249
250 void BufferView::scroll(int lines)
251 {
252         pimpl_->scroll(lines);
253 }
254
255
256 void BufferView::setCursorFromRow(int row)
257 {
258         int tmpid = -1;
259         int tmppos = -1;
260
261         buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
262
263         if (tmpid == -1)
264                 text()->setCursor(cursor(), 0, 0);
265         else
266                 text()->setCursor(cursor(), buffer()->getParFromID(tmpid).pit(), tmppos);
267 }
268
269
270 void BufferView::gotoLabel(string const & label)
271 {
272         for (InsetIterator it = inset_iterator_begin(buffer()->inset()); it; ++it) {
273                 vector<string> labels;
274                 it->getLabelList(*buffer(), labels);
275                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
276                         setCursor(it);
277                         update();
278                         return;
279                 }
280         }
281 }
282
283
284 LyXText * BufferView::getLyXText()
285 {
286         LyXText * text = cursor().innerText();
287         BOOST_ASSERT(text);
288         return text;
289 }
290
291
292 LyXText const * BufferView::getLyXText() const
293 {
294         LyXText const * text = cursor().innerText();
295         BOOST_ASSERT(text);
296         return text;
297 }
298
299
300 int BufferView::workHeight() const
301 {
302         return pimpl_->height();
303 }
304
305
306 LyXText * BufferView::text() const
307 {
308         return buffer() ? &buffer()->text() : 0;
309 }
310
311
312 void BufferView::setCursor(DocIterator const & dit)
313 {
314         size_t const n = dit.depth();
315         for (size_t i = 0; i < n; ++i)
316                 dit[i].inset().edit(cursor(), true);
317
318         cursor().setCursor(dit);
319         cursor().selection() = false;
320 }
321
322
323 void BufferView::mouseSetCursor(LCursor & cur)
324 {
325         BOOST_ASSERT(&cur.bv() == this);
326
327         // Has the cursor just left the inset?
328         if (&cursor().inset() != &cur.inset())
329                 cursor().inset().notifyCursorLeaves(cursor());
330
331         // do the dEPM magic if needed
332         if (cursor().inTexted())
333                 cursor().text()->deleteEmptyParagraphMechanism(cur, cursor());
334
335         cursor() = cur;
336         cursor().clearSelection();
337         cursor().setTargetX();
338         finishUndo();
339
340 }
341
342
343 void BufferView::putSelectionAt(DocIterator const & cur,
344                                 int length, bool backwards)
345 {
346         cursor().clearSelection();
347
348         setCursor(cur);
349
350         if (length) {
351                 if (backwards) {
352                         cursor().pos() += length;
353                         cursor().setSelection(cursor(), -length);
354                 } else
355                         cursor().setSelection(cursor(), length);
356         }
357 }
358
359
360 LCursor & BufferView::cursor()
361 {
362         return pimpl_->cursor_;
363 }
364
365
366 LCursor const & BufferView::cursor() const
367 {
368         return pimpl_->cursor_;
369 }
370
371
372 lyx::pit_type BufferView::anchor_ref() const
373 {
374         return pimpl_->anchor_ref_;
375 }
376
377
378 int BufferView::offset_ref() const
379 {
380         return pimpl_->offset_ref_;
381 }
382
383
384 ViewMetricsInfo const & BufferView::viewMetricsInfo()
385 {
386         return pimpl_->viewMetricsInfo();
387 }
388
389
390 void BufferView::updateMetrics(bool singlepar)
391 {
392         pimpl_->updateMetrics(singlepar);
393 }