]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
6ef914396950ab781e053468174b146debc3c911
[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 Braustein
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 #include "BufferView_pimpl.h"
19
20 #include "WordLangTuple.h"
21 #include "buffer.h"
22 #include "bufferlist.h"
23 #include "gettext.h"
24 #include "funcrequest.h"
25 #include "iterators.h"
26 #include "language.h"
27 #include "lyxtext.h"
28 #include "undo_funcs.h"
29 #include "paragraph.h"
30 #include "paragraph_funcs.h"
31
32 #include "frontends/Alert.h"
33 #include "frontends/Dialogs.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/WorkArea.h"
36 #include "frontends/screen.h"
37
38 #include "insets/insetcommand.h" // ChangeRefs
39 #include "insets/updatableinset.h"
40
41 #include "support/filetools.h"
42 #include "support/lyxalgo.h" // lyx_count
43 #include "support/LAssert.h"
44
45
46 extern BufferList bufferlist;
47
48 using lyx::pos_type;
49 using namespace lyx::support;
50
51 using std::pair;
52 using std::endl;
53 using std::ifstream;
54 using std::vector;
55 using std::find;
56 using std::count_if;
57
58
59 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
60                        int width, int height)
61         : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
62 {
63         text = 0;
64 }
65
66
67 BufferView::~BufferView()
68 {
69         delete text;
70         delete pimpl_;
71 }
72
73
74 Buffer * BufferView::buffer() const
75 {
76         return pimpl_->buffer_;
77 }
78
79
80 LyXScreen & BufferView::screen() const
81 {
82         return pimpl_->screen();
83 }
84
85
86 LyXView * BufferView::owner() const
87 {
88         return pimpl_->owner_;
89 }
90
91
92 Painter & BufferView::painter() const
93 {
94         return pimpl_->painter();
95 }
96
97
98 void BufferView::buffer(Buffer * b)
99 {
100         pimpl_->buffer(b);
101 }
102
103
104 bool BufferView::newFile(string const & fn, string const & tn, bool named)
105 {
106         return pimpl_->newFile(fn, tn, named);
107 }
108
109
110 bool BufferView::loadLyXFile(string const & fn, bool tl)
111 {
112         return pimpl_->loadLyXFile(fn, tl);
113 }
114
115
116 void BufferView::reload()
117 {
118         string const fn = buffer()->fileName();
119         if (bufferlist.close(buffer(), false))
120                 loadLyXFile(fn);
121 }
122
123
124 void BufferView::resize()
125 {
126         if (pimpl_->buffer_)
127                 pimpl_->resizeCurrentBuffer();
128 }
129
130
131 bool BufferView::fitCursor()
132 {
133         return pimpl_->fitCursor();
134 }
135
136
137 void BufferView::update()
138 {
139         pimpl_->update();
140 }
141
142
143 void BufferView::updateScrollbar()
144 {
145         pimpl_->updateScrollbar();
146 }
147
148
149 void BufferView::scrollDocView(int value)
150 {
151         pimpl_->scrollDocView(value);
152 }
153
154
155 void BufferView::redoCurrentBuffer()
156 {
157         pimpl_->redoCurrentBuffer();
158 }
159
160
161 bool BufferView::available() const
162 {
163         return pimpl_->available();
164 }
165
166
167 Change const BufferView::getCurrentChange()
168 {
169         return pimpl_->getCurrentChange();
170 }
171
172
173 void BufferView::beforeChange(LyXText * text)
174 {
175         pimpl_->beforeChange(text);
176 }
177
178
179 void BufferView::savePosition(unsigned int i)
180 {
181         pimpl_->savePosition(i);
182 }
183
184
185 void BufferView::restorePosition(unsigned int i)
186 {
187         pimpl_->restorePosition(i);
188 }
189
190
191 bool BufferView::isSavedPosition(unsigned int i)
192 {
193         return pimpl_->isSavedPosition(i);
194 }
195
196
197 void BufferView::switchKeyMap()
198 {
199         pimpl_->switchKeyMap();
200 }
201
202
203 void BufferView::insetUnlock()
204 {
205         pimpl_->insetUnlock();
206 }
207
208
209 int BufferView::workWidth() const
210 {
211         return pimpl_->workarea().workWidth();
212 }
213
214
215 void BufferView::center()
216 {
217         pimpl_->center();
218 }
219
220
221 int BufferView::top_y() const
222 {
223         return pimpl_->top_y();
224 }
225
226
227 void BufferView::top_y(int y)
228 {
229         pimpl_->top_y(y);
230 }
231
232
233 string const BufferView::getClipboard() const
234 {
235         return pimpl_->workarea().getClipboard();
236 }
237
238
239 void BufferView::stuffClipboard(string const & stuff) const
240 {
241         pimpl_->stuffClipboard(stuff);
242 }
243
244
245 bool BufferView::dispatch(FuncRequest const & ev)
246 {
247         return pimpl_->dispatch(ev);
248 }
249
250
251 void BufferView::scroll(int lines)
252 {
253         pimpl_->scroll(lines);
254 }
255
256
257 // Inserts a file into current document
258 bool BufferView::insertLyXFile(string const & filen)
259         //
260         // Copyright CHT Software Service GmbH
261         // Uwe C. Schroeder
262         //
263         // Insert a LyXformat - file into current buffer
264         //
265         // Moved from lyx_cb.C (Lgb)
266 {
267         Assert(!filen.empty());
268
269         string const fname = MakeAbsPath(filen);
270
271         beforeChange(text);
272
273         text->breakParagraph(buffer()->paragraphs);
274
275         bool res = buffer()->readFile(fname, text->cursor.par());
276
277         resize();
278         return res;
279 }
280
281
282 void BufferView::showErrorList(string const & action) const
283 {
284         if (getErrorList().size()) {
285                 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
286                 owner()->getDialogs().show("errorlist", title);
287                 pimpl_->errorlist_.clear();
288         }
289 }
290
291
292 ErrorList const &
293 BufferView::getErrorList() const
294 {
295         return pimpl_->errorlist_;
296 }
297
298
299 void BufferView::setCursorFromRow(int row)
300 {
301         int tmpid = -1;
302         int tmppos = -1;
303
304         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
305
306         ParagraphList::iterator texrowpar;
307
308         if (tmpid == -1) {
309                 texrowpar = text->ownerParagraphs().begin();
310                 tmppos = 0;
311         } else {
312                 texrowpar = buffer()->getParFromID(tmpid).pit();
313         }
314         text->setCursor(texrowpar, tmppos);
315 }
316
317
318 bool BufferView::insertInset(InsetOld * inset, string const & lout)
319 {
320         return pimpl_->insertInset(inset, lout);
321 }
322
323
324 void BufferView::gotoLabel(string const & label)
325 {
326         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
327              it != buffer()->inset_iterator_end(); ++it) {
328                 vector<string> labels;
329                 it->getLabelList(labels);
330                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
331                         beforeChange(text);
332                         text->setCursor(it.getPar(), it.getPos());
333                         text->selection.cursor = text->cursor;
334                         update();
335                         return;
336                 }
337         }
338 }
339
340
341 void BufferView::undo()
342 {
343         if (!available())
344                 return;
345
346         owner()->message(_("Undo"));
347         beforeChange(text);
348         if (!textUndo(this))
349                 owner()->message(_("No further undo information"));
350         update();
351         switchKeyMap();
352 }
353
354
355 void BufferView::redo()
356 {
357         if (!available())
358                 return;
359
360         owner()->message(_("Redo"));
361         beforeChange(text);
362         if (!textRedo(this))
363                 owner()->message(_("No further redo information"));
364         update();
365         switchKeyMap();
366 }
367
368
369 // these functions are for the spellchecker
370 WordLangTuple const BufferView::nextWord(float & value)
371 {
372         if (!available()) {
373                 value = 1;
374                 return WordLangTuple();
375         }
376
377         return text->selectNextWordToSpellcheck(value);
378 }
379
380
381 void BufferView::selectLastWord()
382 {
383         if (!available())
384                 return;
385
386         LyXCursor cur = text->selection.cursor;
387         beforeChange(text);
388         text->selection.cursor = cur;
389         text->selectSelectedWord();
390         update();
391 }
392
393
394 void BufferView::endOfSpellCheck()
395 {
396         if (!available()) return;
397
398         beforeChange(text);
399         text->selectSelectedWord();
400         text->clearSelection();
401         update();
402 }
403
404
405 void BufferView::replaceWord(string const & replacestring)
406 {
407         if (!available())
408                 return;
409
410         LyXText * tt = getLyXText();
411
412         tt->replaceSelectionWithString(replacestring);
413         tt->setSelectionRange(replacestring.length());
414
415         // Go back so that replacement string is also spellchecked
416         for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
417                 tt->cursorLeft(this);
418
419         // FIXME: should be done through LFUN
420         buffer()->markDirty();
421         update();
422 }
423
424
425 bool BufferView::lockInset(UpdatableInset * inset)
426 {
427         if (!inset)
428                 return false;
429         // don't relock if we're already locked
430         if (theLockingInset() == inset)
431                 return true;
432         if (!theLockingInset()) {
433                 // first check if it's the inset under the cursor we want lock
434                 // should be most of the time
435                 if (text->cursor.pos() < text->cursor.par()->size()
436                     && text->cursor.par()->getChar(text->cursor.pos()) ==
437                     Paragraph::META_INSET) {
438                         InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
439                         if (inset == in) {
440                                 theLockingInset(inset);
441                                 return true;
442                         }
443                 }
444                 // Then do a deep look of the inset and lock the right one
445                 int const id = inset->id();
446                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
447                 ParagraphList::iterator pend = buffer()->paragraphs.end();
448                 for (; pit != pend; ++pit) {
449                         InsetList::iterator it = pit->insetlist.begin();
450                         InsetList::iterator end = pit->insetlist.end();
451                         for (; it != end; ++it) {
452                                 if (it->inset == inset) {
453                                         text->setCursorIntern(pit, it->pos);
454                                         theLockingInset(inset);
455                                         return true;
456                                 }
457                                 if (it->inset->getInsetFromID(id)) {
458                                         text->setCursorIntern(pit, it->pos);
459                                         FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
460                                         it->inset->localDispatch(cmd);
461                                         return theLockingInset()->lockInsetInInset(this, inset);
462                                 }
463                         }
464                 }
465                 return false;
466         }
467         return theLockingInset()->lockInsetInInset(this, inset);
468 }
469
470
471 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
472 {
473         if (theLockingInset() && available()) {
474                 y += text->cursor.y() + theLockingInset()->insetInInsetY();
475                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
476                         updateScrollbar();
477                         return true;
478                 }
479         }
480         return false;
481 }
482
483
484 void BufferView::hideCursor()
485 {
486         screen().hideCursor();
487 }
488
489
490 int BufferView::unlockInset(UpdatableInset * inset)
491 {
492         if (!inset)
493                 return 0;
494         if (inset && theLockingInset() == inset) {
495                 inset->insetUnlock(this);
496                 theLockingInset(0);
497                 // make sure we update the combo !
498                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
499                 // Tell the paragraph dialog that we changed paragraph
500                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
501                 finishUndo();
502                 return 0;
503         } else if (inset && theLockingInset() &&
504                    theLockingInset()->unlockInsetInInset(this, inset)) {
505                 // Tell the paragraph dialog that we changed paragraph
506                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
507                 // owner inset has updated the layout combo
508                 finishUndo();
509                 return 0;
510         }
511         return 1;
512 }
513
514
515 void BufferView::updateInset(InsetOld const * inset)
516 {
517         pimpl_->updateInset(inset);
518 }
519
520
521 bool BufferView::ChangeInsets(InsetOld::Code code,
522                               string const & from, string const & to)
523 {
524         bool need_update = false;
525         LyXCursor cursor = text->cursor;
526         LyXCursor tmpcursor = cursor;
527         cursor.par(tmpcursor.par());
528         cursor.pos(tmpcursor.pos());
529
530         ParIterator end = buffer()->par_iterator_end();
531         for (ParIterator it = buffer()->par_iterator_begin();
532              it != end; ++it) {
533                 bool changed_inset = false;
534                 for (InsetList::iterator it2 = it->insetlist.begin();
535                      it2 != it->insetlist.end(); ++it2) {
536                         if (it2->inset->lyxCode() == code) {
537                                 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
538                                 if (inset->getContents() == from) {
539                                         inset->setContents(to);
540                                         changed_inset = true;
541                                 }
542                         }
543                 }
544                 if (changed_inset) {
545                         need_update = true;
546
547                         // FIXME
548
549                         // The test it.size()==1 was needed to prevent crashes.
550                         // How to set the cursor corretly when it.size()>1 ??
551                         if (it.size() == 1) {
552                                 text->setCursorIntern(it.pit(), 0);
553                                 text->redoParagraph(text->cursor.par());
554                         }
555                 }
556         }
557         text->setCursorIntern(cursor.par(), cursor.pos());
558         return need_update;
559 }
560
561
562 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
563 {
564         // Check if the label 'from' appears more than once
565         vector<string> labels;
566         buffer()->getLabelList(labels);
567
568         if (lyx::count(labels.begin(), labels.end(), from) > 1)
569                 return false;
570
571         return ChangeInsets(InsetOld::REF_CODE, from, to);
572 }
573
574
575 UpdatableInset * BufferView::theLockingInset() const
576 {
577         // If NULL is not allowed we should put an Assert here. (Lgb)
578         if (text)
579                 return text->the_locking_inset;
580         return 0;
581 }
582
583
584 void BufferView::theLockingInset(UpdatableInset * inset)
585 {
586         text->the_locking_inset = inset;
587 }
588
589
590 LyXText * BufferView::getLyXText() const
591 {
592         if (theLockingInset()) {
593                 LyXText * txt = theLockingInset()->getLyXText(this, true);
594                 if (txt)
595                         return txt;
596         }
597         return text;
598 }
599
600
601 Language const * BufferView::getParentLanguage(InsetOld * inset) const
602 {
603         Paragraph const & par = ownerPar(*buffer(), inset);
604         return par.getFontSettings(buffer()->params,
605                                    par.getPositionOfInset(inset)).language();
606 }
607
608
609 Encoding const * BufferView::getEncoding() const
610 {
611         LyXText * t = getLyXText();
612         if (!t)
613                 return 0;
614
615         LyXCursor const & c = t->cursor;
616         LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
617                                               outerFont(c.par(), t->ownerParagraphs()));
618         return font.language()->encoding();
619 }
620
621
622 void BufferView::haveSelection(bool sel)
623 {
624         pimpl_->workarea().haveSelection(sel);
625 }
626
627
628 int BufferView::workHeight() const
629 {
630         return pimpl_->workarea().workHeight();
631 }