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