]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
1771cbad8ec5e7cc5877998f98b68501ef864264
[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(buffer()->paragraphs());
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(0, 0);
301         else
302                 text()->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
303 }
304
305
306 void BufferView::gotoLabel(string const & label)
307 {
308         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
309              it != buffer()->inset_iterator_end(); ++it) {
310                 vector<string> labels;
311                 it->getLabelList(*buffer(), labels);
312                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
313                         cursor().clearSelection();
314                         text()->setCursor(
315                                 distance(text()->paragraphs().begin(), it.getPar()),
316                                 it.getPos());
317                         cursor().resetAnchor();
318                         update();
319                         return;
320                 }
321         }
322 }
323
324
325 void BufferView::undo()
326 {
327         if (!available())
328                 return;
329
330         owner()->message(_("Undo"));
331         cursor().clearSelection();
332         if (!textUndo(*this))
333                 owner()->message(_("No further undo information"));
334         update();
335         switchKeyMap();
336 }
337
338
339 void BufferView::redo()
340 {
341         if (!available())
342                 return;
343
344         owner()->message(_("Redo"));
345         cursor().clearSelection();
346         if (!textRedo(*this))
347                 owner()->message(_("No further redo information"));
348         update();
349         switchKeyMap();
350 }
351
352
353 void BufferView::replaceWord(string const & replacestring)
354 {
355         if (!available())
356                 return;
357
358         LyXText * t = getLyXText();
359
360         t->replaceSelectionWithString(replacestring);
361         t->setSelectionRange(replacestring.length());
362
363         // Go back so that replacement string is also spellchecked
364         for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
365                 t->cursorLeft(this);
366
367         // FIXME: should be done through LFUN
368         buffer()->markDirty();
369         update();
370 }
371
372
373 void BufferView::hideCursor()
374 {
375         screen().hideCursor();
376 }
377
378
379 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
380 {
381         // Check if the label 'from' appears more than once
382         vector<string> labels;
383         buffer()->getLabelList(labels);
384
385         if (lyx::count(labels.begin(), labels.end(), from) > 1)
386                 return false;
387
388         return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
389 }
390
391
392 LyXText * BufferView::getLyXText() const
393 {
394         return cursor().innerText();
395 }
396
397
398 Language const * BufferView::getParentLanguage(InsetOld * inset) const
399 {
400         Paragraph const & par = ownerPar(*buffer(), inset);
401         return par.getFontSettings(buffer()->params(),
402                                    par.getPositionOfInset(inset)).language();
403 }
404
405
406 Encoding const * BufferView::getEncoding() const
407 {
408         LyXText * t = getLyXText();
409         if (!t)
410                 return 0;
411         CursorSlice const & cur = cursor().innerTextSlice();
412         return t->getPar(cur.par())->getFont(
413                 buffer()->params(), cur.pos(),
414                 outerFont(t->getPar(cur.par()), t->paragraphs())
415         ).language()->encoding();
416 }
417
418
419 void BufferView::haveSelection(bool sel)
420 {
421         pimpl_->workarea().haveSelection(sel);
422 }
423
424
425 int BufferView::workHeight() const
426 {
427         return pimpl_->workarea().workHeight();
428 }
429
430
431 LyXText * BufferView::text() const
432 {
433         return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;
434 }
435
436
437 void BufferView::setCursor(ParIterator const & par,
438                            lyx::pos_type pos)
439 {
440         LCursor & cur = cursor();
441         cur.reset();
442         ParIterator::PosHolder const & positions = par.positions();
443         int const last = par.size() - 1;
444         for (int i = 0; i < last; ++i)
445                 (*positions[i].it)->inset->edit(cur, true);
446         cur.resetAnchor();
447         LyXText * lt = par.text(*buffer());
448         lt->setCursor(par.pit(), pos);
449 }
450
451
452 /*
453 if the fitCursor call refers to some point in never-explored-land, then we
454 don't have y information in insets there, then we cannot even do an update
455 to get it (because we need the y infomation for setting top_y first). So
456 this is solved in putSelectionAt with:
457
458 - setting top_y to the y of the outerPar (that has good info)
459 - calling update
460 - calling cursor().updatePos()
461 - then call fitCursor()
462
463 Ab.
464 */
465
466 void BufferView::putSelectionAt(PosIterator const & cur,
467                       int length, bool backwards)
468 {
469         ParIterator par(cur);
470
471         cursor().clearSelection();
472
473         LyXText * text = par.text(*buffer());
474         setCursor(par, cur.pos());
475         
476         // hack for the chicken and egg problem
477         if (par.inset())
478                 top_y(par.outerPar()->y);
479         update();
480         text->setCursor(cur.pit(), cur.pos());
481         cursor().updatePos();
482
483         if (length) {
484                 text->setSelectionRange(length);
485                 cursor().setSelection();
486                 if (backwards)
487                         swap(cursor().cursor_, cursor().anchor_);
488         }
489
490         fitCursor();
491         update();
492 }
493
494
495 LCursor & BufferView::cursor()
496 {
497         return pimpl_->cursor_;
498 }
499
500
501 LCursor const & BufferView::cursor() const
502 {
503         return pimpl_->cursor_;
504 }