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