]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
573da75b7a14cc6ecbf575a810a59c445e8a433d
[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 "debug.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "iterators.h"
27 #include "language.h"
28 #include "lyxlayout.h"
29 #include "lyxtext.h"
30 #include "lyxtextclass.h"
31 #include "paragraph.h"
32 #include "paragraph_funcs.h"
33 #include "PosIterator.h"
34 #include "texrow.h"
35 #include "undo.h"
36 #include "WordLangTuple.h"
37
38 #include "frontends/Alert.h"
39 #include "frontends/Dialogs.h"
40 #include "frontends/LyXView.h"
41 #include "frontends/screen.h"
42 #include "frontends/WorkArea.h"
43
44 #include "insets/insetcommand.h" // ChangeRefs
45 #include "insets/updatableinset.h"
46 #include "insets/insettext.h"
47
48 #include "support/filetools.h"
49 #include "support/lyxalgo.h" // lyx_count
50
51 using lyx::support::bformat;
52 using lyx::support::MakeAbsPath;
53
54 using std::distance;
55 using std::find;
56 using std::string;
57 using std::swap;
58 using std::vector;
59
60
61 extern BufferList bufferlist;
62
63
64 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
65                        int width, int height)
66         : pimpl_(new Pimpl(*this, owner, xpos, ypos, width, height))
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 LyXScreen & BufferView::screen() const
89 {
90         return pimpl_->screen();
91 }
92
93
94 LyXView * BufferView::owner() const
95 {
96         return pimpl_->owner_;
97 }
98
99
100 Painter & BufferView::painter() const
101 {
102         return pimpl_->painter();
103 }
104
105
106 void BufferView::setBuffer(Buffer * b)
107 {
108         pimpl_->setBuffer(b);
109 }
110
111
112 void BufferView::newFile(string const & fn, string const & tn, bool named)
113 {
114         pimpl_->newFile(fn, tn, named);
115 }
116
117
118 bool BufferView::loadLyXFile(string const & fn, bool tl)
119 {
120         return pimpl_->loadLyXFile(fn, tl);
121 }
122
123
124 void BufferView::reload()
125 {
126         string const fn = buffer()->fileName();
127         if (bufferlist.close(buffer(), false))
128                 loadLyXFile(fn);
129 }
130
131
132 void BufferView::resize()
133 {
134         if (pimpl_->buffer_)
135                 pimpl_->resizeCurrentBuffer();
136 }
137
138
139 bool BufferView::fitCursor()
140 {
141         return pimpl_->fitCursor();
142 }
143
144
145 void BufferView::update()
146 {
147         pimpl_->update();
148 }
149
150
151 void BufferView::updateScrollbar()
152 {
153         pimpl_->updateScrollbar();
154 }
155
156
157 void BufferView::scrollDocView(int value)
158 {
159         pimpl_->scrollDocView(value);
160 }
161
162
163 void BufferView::redoCurrentBuffer()
164 {
165         pimpl_->redoCurrentBuffer();
166 }
167
168
169 bool BufferView::available() const
170 {
171         return pimpl_->available();
172 }
173
174
175 Change const BufferView::getCurrentChange()
176 {
177         return pimpl_->getCurrentChange();
178 }
179
180
181 void BufferView::savePosition(unsigned int i)
182 {
183         pimpl_->savePosition(i);
184 }
185
186
187 void BufferView::restorePosition(unsigned int i)
188 {
189         pimpl_->restorePosition(i);
190 }
191
192
193 bool BufferView::isSavedPosition(unsigned int i)
194 {
195         return pimpl_->isSavedPosition(i);
196 }
197
198
199 void BufferView::switchKeyMap()
200 {
201         pimpl_->switchKeyMap();
202 }
203
204
205 int BufferView::workWidth() const
206 {
207         return pimpl_->workarea().workWidth();
208 }
209
210
211 void BufferView::center()
212 {
213         pimpl_->center();
214 }
215
216
217 int BufferView::top_y() const
218 {
219         return pimpl_->top_y();
220 }
221
222
223 void BufferView::top_y(int y)
224 {
225         pimpl_->top_y(y);
226 }
227
228
229 string const BufferView::getClipboard() const
230 {
231         return pimpl_->workarea().getClipboard();
232 }
233
234
235 void BufferView::stuffClipboard(string const & stuff) const
236 {
237         pimpl_->stuffClipboard(stuff);
238 }
239
240
241 bool BufferView::dispatch(FuncRequest const & ev)
242 {
243         return pimpl_->dispatch(ev);
244 }
245
246
247 void BufferView::scroll(int lines)
248 {
249         pimpl_->scroll(lines);
250 }
251
252
253 // Inserts a file into current document
254 bool BufferView::insertLyXFile(string const & filen)
255         //
256         // Copyright CHT Software Service GmbH
257         // Uwe C. Schroeder
258         //
259         // Insert a LyXformat - file into current buffer
260         //
261         // Moved from lyx_cb.C (Lgb)
262 {
263         BOOST_ASSERT(!filen.empty());
264
265         string const fname = MakeAbsPath(filen);
266
267         cursor().clearSelection();
268         text()->breakParagraph(cursor());
269
270         bool res = buffer()->readFile(fname, text()->cursorPar());
271         resize();
272         return res;
273 }
274
275
276 void BufferView::showErrorList(string const & action) const
277 {
278         if (getErrorList().size()) {
279                 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
280                         action, buffer()->fileName());
281                 owner()->getDialogs().show("errorlist", title);
282                 pimpl_->errorlist_.clear();
283         }
284 }
285
286
287 ErrorList const & BufferView::getErrorList() const
288 {
289         return pimpl_->errorlist_;
290 }
291
292
293 void BufferView::setCursorFromRow(int row)
294 {
295         int tmpid = -1;
296         int tmppos = -1;
297
298         buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
299
300         if (tmpid == -1)
301                 text()->setCursor(cursor(), 0, 0);
302         else
303                 text()->setCursor(cursor(),
304                         text()->parOffset(buffer()->getParFromID(tmpid).pit()),
305                         tmppos);
306 }
307
308
309 void BufferView::gotoLabel(string const & label)
310 {
311         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
312              it != buffer()->inset_iterator_end(); ++it) {
313                 vector<string> labels;
314                 it->getLabelList(*buffer(), labels);
315                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
316                         cursor().clearSelection();
317                         text()->setCursor(cursor(), 
318                                 distance(text()->paragraphs().begin(), it.getPar()),
319                                 it.getPos());
320                         cursor().resetAnchor();
321                         update();
322                         return;
323                 }
324         }
325 }
326
327
328 void BufferView::hideCursor()
329 {
330         screen().hideCursor();
331 }
332
333
334 LyXText * BufferView::getLyXText() const
335 {
336         LyXText * text = cursor().innerText();
337         BOOST_ASSERT(text);
338         return text;
339 }
340
341
342 Language const * BufferView::getParentLanguage(InsetOld * inset) const
343 {
344         Paragraph const & par = ownerPar(*buffer(), inset);
345         return par.getFontSettings(buffer()->params(),
346                                    par.getPositionOfInset(inset)).language();
347 }
348
349
350 void BufferView::haveSelection(bool sel)
351 {
352         pimpl_->workarea().haveSelection(sel);
353 }
354
355
356 int BufferView::workHeight() const
357 {
358         return pimpl_->workarea().workHeight();
359 }
360
361
362 LyXText * BufferView::text() const
363 {
364         return buffer() ? &buffer()->text() : 0;
365 }
366
367
368 void BufferView::setCursor(ParIterator const & par, lyx::pos_type pos)
369 {
370         LCursor & cur = cursor();
371         cur.reset();
372         cur.push(buffer()->inset());
373         ParIterator::PosHolder const & positions = par.positions();
374         int const last = par.size() - 1;
375         for (int i = 0; i < last; ++i)
376                 (*positions[i].it)->inset->edit(cur, true);
377         cur.resetAnchor();
378         LyXText * text = par.text(*buffer());
379         text->setCursor(cur, text->parOffset(par.pit()), pos);
380 }
381
382
383 /*
384 if the fitCursor call refers to some point in never-explored-land, then we
385 don't have y information in insets there, then we cannot even do an update
386 to get it (because we need the y infomation for setting top_y first). So
387 this is solved in putSelectionAt with:
388
389 - setting top_y to the y of the outerPar (that has good info)
390 - calling update
391 - calling cursor().updatePos()
392 - then call fitCursor()
393
394 Ab.
395 */
396
397 void BufferView::putSelectionAt(PosIterator const & cur,
398                       int length, bool backwards)
399 {
400         ParIterator par(cur);
401
402         cursor().clearSelection();
403
404         LyXText * text = par.text(*buffer());
405         setCursor(par, cur.pos());
406         
407         // hack for the chicken and egg problem
408         if (par.inset())
409                 top_y(par.outerPar()->y);
410         update();
411         text->setCursor(cursor(), text->parOffset(cur.pit()), cur.pos());
412         cursor().updatePos();
413
414         if (length) {
415                 text->setSelectionRange(cursor(), length);
416                 cursor().setSelection();
417                 if (backwards) {
418 #if 0
419                         swap(cursor().cursor_, cursor().anchor_);
420 #else
421                         DocumentIterator it = cursor();
422                         cursor().setCursor(cursor().anchor_, false);
423                         cursor().anchor_ = it;
424 #endif
425                 }
426         }
427
428         fitCursor();
429         update();
430 }
431
432
433 LCursor & BufferView::cursor()
434 {
435         return pimpl_->cursor_;
436 }
437
438
439 LCursor const & BufferView::cursor() const
440 {
441         return pimpl_->cursor_;
442 }