]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
More alert fixes
[lyx.git] / src / BufferView.C
1 /**
2  * \file BufferView.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author unknown
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #include "BufferView.h"
13 #include "BufferView_pimpl.h"
14
15 #include "BufferView_pimpl.h"
16 #include "LaTeX.h"
17 #include "ParagraphParameters.h"
18 #include "WordLangTuple.h"
19 #include "buffer.h"
20 #include "bufferlist.h"
21 #include "debug.h"
22 #include "gettext.h"
23 #include "iterators.h"
24 #include "language.h"
25 #include "lyxcursor.h"
26 #include "lyxlex.h"
27 #include "lyxtext.h"
28 #include "undo_funcs.h"
29 #include "changes.h"
30
31 #include "frontends/Alert.h"
32 #include "frontends/Dialogs.h"
33 #include "frontends/LyXView.h"
34 #include "frontends/WorkArea.h"
35 #include "frontends/screen.h"
36
37 #include "insets/insetcommand.h" // ChangeRefs
38 #include "insets/inseterror.h"
39 #include "insets/updatableinset.h"
40
41 #include "support/FileInfo.h"
42 #include "support/filetools.h"
43 #include "support/lyxfunctional.h" // equal_1st_in_pair
44 #include "support/types.h"
45 #include "support/lyxalgo.h" // lyx_count
46 #include "BoostFormat.h"
47
48 #include <fstream>
49
50 extern BufferList bufferlist;
51
52 using lyx::pos_type;
53
54 using std::pair;
55 using std::endl;
56 using std::ifstream;
57 using std::vector;
58 using std::find;
59 using std::count_if;
60
61
62 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
63                        int width, int height)
64         : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
65 {
66         text = 0;
67 }
68
69
70 BufferView::~BufferView()
71 {
72         delete text;
73         delete pimpl_;
74 }
75
76
77 Buffer * BufferView::buffer() const
78 {
79         return pimpl_->buffer_;
80 }
81
82
83 LyXScreen & BufferView::screen() const
84 {
85         return pimpl_->screen();
86 }
87
88
89 LyXView * BufferView::owner() const
90 {
91         return pimpl_->owner_;
92 }
93
94
95 Painter & BufferView::painter() const
96 {
97         return pimpl_->painter();
98 }
99
100
101 void BufferView::buffer(Buffer * b)
102 {
103         pimpl_->buffer(b);
104 }
105
106
107 void BufferView::reload()
108 {
109         string const fn = buffer()->fileName();
110         if (bufferlist.close(buffer(), false))
111                 buffer(bufferlist.loadLyXFile(fn));
112 }
113
114
115 void BufferView::resize()
116 {
117         if (pimpl_->buffer_) {
118                 pimpl_->resizeCurrentBuffer();
119         }
120 }
121
122
123 void BufferView::repaint()
124 {
125         pimpl_->repaint();
126 }
127
128
129 bool BufferView::fitCursor()
130 {
131         return pimpl_->fitCursor();
132 }
133
134
135 void BufferView::update()
136 {
137         pimpl_->update();
138 }
139
140
141 void BufferView::updateScrollbar()
142 {
143         pimpl_->updateScrollbar();
144 }
145
146
147 void BufferView::scrollDocView(int value)
148 {
149         pimpl_->scrollDocView(value);
150 }
151
152
153 void BufferView::redoCurrentBuffer()
154 {
155         pimpl_->redoCurrentBuffer();
156 }
157
158
159 bool BufferView::available() const
160 {
161         return pimpl_->available();
162 }
163
164
165 Change const BufferView::getCurrentChange()
166 {
167         return pimpl_->getCurrentChange();
168 }
169
170
171 void BufferView::beforeChange(LyXText * text)
172 {
173         pimpl_->beforeChange(text);
174 }
175
176
177 void BufferView::savePosition(unsigned int i)
178 {
179         pimpl_->savePosition(i);
180 }
181
182
183 void BufferView::restorePosition(unsigned int i)
184 {
185         pimpl_->restorePosition(i);
186 }
187
188
189 bool BufferView::isSavedPosition(unsigned int i)
190 {
191         return pimpl_->isSavedPosition(i);
192 }
193
194
195 void BufferView::update(LyXText * text, UpdateCodes f)
196 {
197         pimpl_->update(text, f);
198 }
199
200
201 void BufferView::update(UpdateCodes f)
202 {
203         pimpl_->update(f);
204 }
205
206
207 void BufferView::switchKeyMap()
208 {
209         pimpl_->switchKeyMap();
210 }
211
212
213 void BufferView::insetUnlock()
214 {
215         pimpl_->insetUnlock();
216 }
217
218
219 int BufferView::workWidth() const
220 {
221         return pimpl_->workarea().workWidth();
222 }
223
224
225 void BufferView::showCursor()
226 {
227         pimpl_->showCursor();
228 }
229
230
231 void BufferView::hideCursor()
232 {
233         pimpl_->hideCursor();
234 }
235
236
237 void BufferView::toggleSelection(bool b)
238 {
239         pimpl_->toggleSelection(b);
240 }
241
242
243 void BufferView::toggleToggle()
244 {
245         pimpl_->toggleToggle();
246 }
247
248
249 void BufferView::center()
250 {
251         pimpl_->center();
252 }
253
254
255 string const BufferView::getClipboard() const
256 {
257         return pimpl_->workarea().getClipboard();
258 }
259
260
261 void BufferView::stuffClipboard(string const & stuff) const
262 {
263         pimpl_->stuffClipboard(stuff);
264 }
265
266
267 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
268                                   BufferView::UpdateCodes uc2)
269 {
270         return static_cast<BufferView::UpdateCodes>
271                 (static_cast<int>(uc1) | static_cast<int>(uc2));
272 }
273
274
275 bool BufferView::dispatch(FuncRequest const & ev)
276 {
277         return pimpl_->dispatch(ev);
278 }
279
280
281 void BufferView::scroll(int lines)
282 {
283         pimpl_->scroll(lines);
284 }
285
286
287 // Inserts a file into current document
288 bool BufferView::insertLyXFile(string const & filen)
289         //
290         // Copyright CHT Software Service GmbH
291         // Uwe C. Schroeder
292         //
293         // Insert a LyXformat - file into current buffer
294         //
295         // Moved from lyx_cb.C (Lgb)
296 {
297         if (filen.empty())
298                 return false;
299
300         string const fname = MakeAbsPath(filen);
301
302         // check if file exist
303         FileInfo const fi(fname);
304
305         if (!fi.readable()) {
306                 string const file = MakeDisplayPath(fname, 50);
307 #if USE_BOOST_FORMAT
308                 boost::format fmt(_("The specified document\n%1$s\ncould not be read."));
309                 fmt % file;
310                 string text = fmt.str();
311 #else
312                 string text = _("The specified document\n");
313                 text += file + _(" could not be read.");
314 #endif
315                 Alert::error(_("Could not read document"), text);
316                 return false;
317         }
318
319         beforeChange(text);
320
321         ifstream ifs(fname.c_str());
322         if (!ifs) {
323                 string const error = strerror(errno);
324                 string const file = MakeDisplayPath(fname, 50);
325 #if USE_BOOST_FORMAT
326                 boost::format fmt(_("Could not open the specified document\n%1$s\ndue to the error: %2$s"));
327                 fmt % file;
328                 fmt % error;
329                 string text = fmt.str();
330 #else
331                 string text = _("Could not open the specified document\n");
332                 text += file + _(" due to the error: ");
333                 text += error;
334 #endif
335                 Alert::error(_("Could not open file"), text);
336                 return false;
337         }
338
339         int const c = ifs.peek();
340
341         LyXLex lex(0, 0);
342         lex.setStream(ifs);
343
344         bool res = true;
345
346         if (c == '#') {
347                 // FIXME: huh ? No we won't !
348                 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
349                 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
350         } else {
351                 lyxerr[Debug::INFO] << "Will insert file without header"
352                                     << endl;
353                 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
354         }
355
356         resize();
357         return res;
358 }
359
360
361 bool BufferView::removeAutoInsets()
362 {
363         // keep track of which pos and par the cursor was on
364         Paragraph * cursor_par = text->cursor.par();
365         Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
366         Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
367         pos_type cursor_pos = text->cursor.pos();
368
369         bool found = false;
370
371         // Trap the deletion of the paragraph the cursor is in.
372         // Iterate until we find a paragraph that won't be immediately deleted.
373         // In reality this should mean we only execute the body of the while
374         // loop once at most.  However for safety we iterate rather than just
375         // make this an if () conditional.
376         while ((cursor_par_prev || cursor_par_next)
377                && text->setCursor(
378                                   cursor_par_prev ? cursor_par_prev : cursor_par_next,
379                                   0)) {
380                 // We just removed cursor_par so have to fix the "cursor"
381                 if (cursor_par_prev) {
382                         // '.' = cursor_par
383                         //  a -> a.
384                         // .
385                         cursor_par = cursor_par_prev;
386                         cursor_pos = cursor_par->size();
387                 } else {
388                         // .  -> .a
389                         //  a
390                         cursor_par = cursor_par_next;
391                         cursor_pos = 0;
392                 }
393                 cursor_par_prev = cursor_par->previous();
394                 cursor_par_next = cursor_par->next();
395         }
396
397         // Iterate through the paragraphs removing autoDelete insets as we go.
398         // If the paragraph ends up empty after all the autoDelete insets are
399         // removed that paragraph will be removed by the next setCursor() call.
400         ParIterator it = buffer()->par_iterator_begin();
401         ParIterator end = buffer()->par_iterator_end();
402         for (; it != end; ++it) {
403                 Paragraph * par = *it;
404                 Paragraph * par_prev = par ? par->previous() : 0;
405                 bool removed = false;
406
407                 if (text->setCursor(par, 0)
408                     && cursor_par == par_prev) {
409                         // The previous setCursor line was deleted and that
410                         // was the cursor_par line.  This can only happen if an
411                         // error box was the sole item on cursor_par.
412                         // It is possible for cursor_par_prev to be stray if
413                         // the line it pointed to only had a error box on it
414                         // so we have to set it to a known correct value.
415                         // This is often the same value it already had.
416                         cursor_par_prev = par->previous();
417                         if (cursor_par_prev) {
418                                 // '|' = par, '.' = cursor_par, 'E' = error box
419                                 // First step below may occur before while{}
420                                 //  a    |a      a     a     a.
421                                 //  E -> .E -> |.E -> .  -> |b
422                                 // .      b      b    |b
423                                 //  b
424                                 cursor_par = cursor_par_prev;
425                                 cursor_pos = cursor_par_prev->size();
426                                 cursor_par_prev = cursor_par->previous();
427                                 // cursor_par_next remains the same
428                         } else if (cursor_par_next) {
429                                 // First step below may occur before while{}
430                                 // .
431                                 //  E -> |.E -> |.  -> . -> .|a
432                                 //  a      a      a    |a
433                                 cursor_par = cursor_par_next;
434                                 cursor_pos = 0;
435                                 // cursor_par_prev remains unset
436                                 cursor_par_next = cursor_par->next();
437                         } else {
438                                 // I can't find a way to trigger this
439                                 // so it should be unreachable code
440                                 // unless the buffer is corrupted.
441                                 lyxerr << "BufferView::removeAutoInsets() is bad\n";
442                         }
443                 }
444
445                 InsetList::iterator pit = par->insetlist.begin();
446                 InsetList::iterator pend = par->insetlist.end();
447
448                 while (pit != pend) {
449                         if (pit.getInset()->autoDelete()) {
450                                 removed = true;
451                                 pos_type const pos = pit.getPos();
452
453                                 par->erase(pos);
454                                 // We just invalidated par's inset iterators so
455                                 // we get the next valid iterator position
456                                 pit = par->insetlist.insetIterator(pos);
457                                 // and ensure we have a valid end iterator.
458                                 pend = par->insetlist.end();
459
460                                 if (cursor_par == par) {
461                                         // update the saved cursor position
462                                         if (cursor_pos > pos)
463                                                 --cursor_pos;
464                                 }
465                         } else {
466                                 ++pit;
467                         }
468                 }
469                 if (removed) {
470                         found = true;
471                         text->redoParagraph();
472                 }
473         }
474
475         // It is possible that the last line is empty if it was cursor_par
476         // and/or only had an error inset on it.  So we set the cursor to the
477         // start of the doc to force its removal and ensure a valid saved cursor
478         if (text->setCursor(text->ownerParagraph(), 0)
479             && 0 == cursor_par_next) {
480                 cursor_par = cursor_par_prev;
481                 cursor_pos = cursor_par->size();
482         } else if (cursor_pos > cursor_par->size()) {
483                 // Some C-Enter lines were removed by the setCursor call which
484                 // then invalidated cursor_pos. It could still be "wrong" because
485                 // the cursor may appear to have jumped but since we collapsed
486                 // some C-Enter lines this should be a reasonable compromise.
487                 cursor_pos = cursor_par->size();
488         }
489
490         // restore the original cursor in its corrected location.
491         text->setCursorIntern(cursor_par, cursor_pos);
492
493         return found;
494 }
495
496
497 void BufferView::insertErrors(TeXErrors & terr)
498 {
499         // Save the cursor position
500         LyXCursor cursor = text->cursor;
501
502         TeXErrors::Errors::const_iterator cit = terr.begin();
503         TeXErrors::Errors::const_iterator end = terr.end();
504         for (; cit != end; ++cit) {
505                 string const desctext(cit->error_desc);
506                 string const errortext(cit->error_text);
507                 string const msgtxt = desctext + '\n' + errortext;
508                 int const errorrow = cit->error_in_line;
509
510                 // Insert error string for row number
511                 int tmpid = -1;
512                 int tmppos = -1;
513
514                 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
515                         buffer()->texrow.increasePos(tmpid, tmppos);
516                 }
517
518                 Paragraph * texrowpar = 0;
519
520                 if (tmpid == -1) {
521                         texrowpar = text->ownerParagraph();
522                         tmppos = 0;
523                 } else {
524                         texrowpar = buffer()->getParFromID(tmpid);
525                 }
526
527                 if (texrowpar == 0)
528                         continue;
529
530                 freezeUndo();
531                 InsetError * new_inset = new InsetError(msgtxt);
532                 text->setCursorIntern(texrowpar, tmppos);
533                 text->insertInset(new_inset);
534                 text->fullRebreak();
535                 unFreezeUndo();
536         }
537         // Restore the cursor position
538         text->setCursorIntern(cursor.par(), cursor.pos());
539 }
540
541
542 void BufferView::setCursorFromRow(int row)
543 {
544         int tmpid = -1;
545         int tmppos = -1;
546
547         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
548
549         Paragraph * texrowpar;
550
551         if (tmpid == -1) {
552                 texrowpar = text->ownerParagraph();
553                 tmppos = 0;
554         } else {
555                 texrowpar = buffer()->getParFromID(tmpid);
556         }
557         text->setCursor(texrowpar, tmppos);
558 }
559
560
561 bool BufferView::insertInset(Inset * inset, string const & lout)
562 {
563         return pimpl_->insertInset(inset, lout);
564 }
565
566
567 // This is also a buffer property (ale)
568 // Not so sure about that. a goto Label function can not be buffer local, just
569 // think how this will work in a multiwindow/buffer environment, all the
570 // cursors in all the views showing this buffer will move. (Lgb)
571 // OK, then no cursor action should be allowed in buffer. (ale)
572 bool BufferView::gotoLabel(string const & label)
573 {
574         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
575              it != buffer()->inset_iterator_end(); ++it) {
576                 vector<string> labels = it->getLabelList();
577                 if (find(labels.begin(),labels.end(),label)
578                      != labels.end()) {
579                         beforeChange(text);
580                         text->setCursor(it.getPar(), it.getPos());
581                         text->selection.cursor = text->cursor;
582                         update(text, BufferView::SELECT);
583                         return true;
584                 }
585         }
586         return false;
587 }
588
589
590 void BufferView::undo()
591 {
592         if (!available())
593                 return;
594
595         owner()->message(_("Undo"));
596         hideCursor();
597         beforeChange(text);
598         update(text, BufferView::SELECT);
599         if (!textUndo(this))
600                 owner()->message(_("No further undo information"));
601         else
602                 update(text, BufferView::SELECT);
603         switchKeyMap();
604 }
605
606
607 void BufferView::redo()
608 {
609         if (!available())
610                 return;
611
612         owner()->message(_("Redo"));
613         hideCursor();
614         beforeChange(text);
615         update(text, BufferView::SELECT);
616         if (!textRedo(this))
617                 owner()->message(_("No further redo information"));
618         else
619                 update(text, BufferView::SELECT);
620         switchKeyMap();
621 }
622
623
624 void BufferView::copyEnvironment()
625 {
626         if (available()) {
627                 text->copyEnvironmentType();
628                 owner()->message(_("Paragraph environment type copied"));
629         }
630 }
631
632
633 void BufferView::pasteEnvironment()
634 {
635         if (available()) {
636                 text->pasteEnvironmentType();
637                 owner()->message(_("Paragraph environment type set"));
638                 update(text, BufferView::SELECT);
639         }
640 }
641
642
643 // these functions are for the spellchecker
644 WordLangTuple const BufferView::nextWord(float & value)
645 {
646         if (!available()) {
647                 value = 1;
648                 return WordLangTuple();
649         }
650
651         return text->selectNextWordToSpellcheck(value);
652 }
653
654
655 void BufferView::selectLastWord()
656 {
657         if (!available())
658                 return;
659
660         LyXCursor cur = text->selection.cursor;
661         hideCursor();
662         beforeChange(text);
663         text->selection.cursor = cur;
664         text->selectSelectedWord();
665         toggleSelection(false);
666         update(text, BufferView::SELECT);
667 }
668
669
670 void BufferView::endOfSpellCheck()
671 {
672         if (!available()) return;
673
674         hideCursor();
675         beforeChange(text);
676         text->selectSelectedWord();
677         text->clearSelection();
678         update(text, BufferView::SELECT);
679 }
680
681
682 void BufferView::replaceWord(string const & replacestring)
683 {
684         if (!available())
685                 return;
686
687         LyXText * tt = getLyXText();
688         hideCursor();
689         update(tt, BufferView::SELECT);
690
691         // clear the selection (if there is any)
692         toggleSelection(false);
693         update(tt, BufferView::SELECT);
694
695         // clear the selection (if there is any)
696         toggleSelection(false);
697         tt->replaceSelectionWithString(replacestring);
698
699         tt->setSelectionRange(replacestring.length());
700
701         // Go back so that replacement string is also spellchecked
702         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
703                 tt->cursorLeft(this);
704         }
705         update(tt, BufferView::SELECT);
706
707         // FIXME: should be done through LFUN
708         buffer()->markDirty();
709         fitCursor();
710 }
711 // End of spellchecker stuff
712
713
714 bool BufferView::lockInset(UpdatableInset * inset)
715 {
716         if (!inset)
717                 return false;
718         // don't relock if we're already locked
719         if (theLockingInset() == inset)
720                 return true;
721         if (!theLockingInset()) {
722                 // first check if it's the inset under the cursor we want lock
723                 // should be most of the time
724                 char const c = text->cursor.par()->getChar(text->cursor.pos());
725                 if (c == Paragraph::META_INSET) {
726                         Inset * in = text->cursor.par()->getInset(text->cursor.pos());
727                         if (inset == in) {
728                                 theLockingInset(inset);
729                                 return true;
730                         }
731                 }
732                 // Then do a deep look of the inset and lock the right one
733                 int const id = inset->id();
734                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
735                 ParagraphList::iterator pend = buffer()->paragraphs.end();
736                 for (; pit != pend; ++pit) {
737                         InsetList::iterator it = pit->insetlist.begin();
738                         InsetList::iterator end = pit->insetlist.end();
739                         for (; it != end; ++it) {
740                                 if (it.getInset() == inset) {
741                                         text->setCursorIntern(&*pit, it.getPos());
742                                         theLockingInset(inset);
743                                         return true;
744                                 }
745                                 if (it.getInset()->getInsetFromID(id)) {
746                                         text->setCursorIntern(&*pit, it.getPos());
747                                         it.getInset()->edit(this);
748                                         return theLockingInset()->lockInsetInInset(this, inset);
749                                 }
750                         }
751                 }
752                 return false;
753         }
754         return theLockingInset()->lockInsetInInset(this, inset);
755 }
756
757
758 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
759 {
760         if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
761                 LyXCursor cursor = text->cursor;
762                 Inset * locking_inset = theLockingInset()->getLockingInset();
763
764                 if ((cursor.pos() - 1 >= 0) &&
765                     cursor.par()->isInset(cursor.pos() - 1) &&
766                     (cursor.par()->getInset(cursor.pos() - 1) ==
767                      locking_inset))
768                         text->setCursor(cursor,
769                                         cursor.par(), cursor.pos() - 1);
770                 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
771                 LyXText * txt = getLyXText();
772                 if (locking_inset->isTextInset() &&
773                     locking_inset->lyxCode() != Inset::ERT_CODE &&
774                     (txt->real_current_font.language() !=
775                      buffer()->params.language
776                      || txt->real_current_font.isVisibleRightToLeft()
777                      != buffer()->params.language->RightToLeft()))
778                         shape = (txt->real_current_font.isVisibleRightToLeft())
779                                 ? LyXScreen::REVERSED_L_SHAPE
780                                 : LyXScreen::L_SHAPE;
781                 y += cursor.iy() + theLockingInset()->insetInInsetY();
782                 screen().showManualCursor(text, x, y, asc, desc,
783                                                   shape);
784         }
785 }
786
787
788 void BufferView::hideLockedInsetCursor()
789 {
790         if (theLockingInset() && available()) {
791                 screen().hideCursor();
792         }
793 }
794
795
796 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
797 {
798         if (theLockingInset() && available()) {
799                 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
800                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
801                         updateScrollbar();
802                         return true;
803                 }
804         }
805         return false;
806 }
807
808
809 int BufferView::unlockInset(UpdatableInset * inset)
810 {
811         if (!inset)
812                 return 0;
813         if (inset && theLockingInset() == inset) {
814                 inset->insetUnlock(this);
815                 theLockingInset(0);
816                 // make sure we update the combo !
817                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
818                 // Tell the paragraph dialog that we changed paragraph
819                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
820                 finishUndo();
821                 return 0;
822         } else if (inset && theLockingInset() &&
823                    theLockingInset()->unlockInsetInInset(this, inset)) {
824                 // Tell the paragraph dialog that we changed paragraph
825                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
826                 // owner inset has updated the layout combo
827                 finishUndo();
828                 return 0;
829         }
830         return 1;
831 }
832
833
834 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
835 {
836         if (!theLockingInset())
837                 return; // shouldn't happen
838         if (kind == Undo::EDIT) // in this case insets would not be stored!
839                 kind = Undo::FINISH;
840         setUndo(this, kind,
841                 text->cursor.par(),
842                 text->cursor.par()->next());
843 }
844
845
846 void BufferView::updateInset(Inset * inset)
847 {
848         pimpl_->updateInset(inset);
849 }
850
851
852 bool BufferView::ChangeInsets(Inset::Code code,
853                               string const & from, string const & to)
854 {
855         bool need_update = false;
856         LyXCursor cursor = text->cursor;
857         LyXCursor tmpcursor = cursor;
858         cursor.par(tmpcursor.par());
859         cursor.pos(tmpcursor.pos());
860
861         ParIterator end = buffer()->par_iterator_end();
862         for (ParIterator it = buffer()->par_iterator_begin();
863              it != end; ++it) {
864                 Paragraph * par = *it;
865                 bool changed_inset = false;
866                 for (InsetList::iterator it2 = par->insetlist.begin();
867                      it2 != par->insetlist.end(); ++it2) {
868                         if (it2.getInset()->lyxCode() == code) {
869                                 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
870                                 if (inset->getContents() == from) {
871                                         inset->setContents(to);
872                                         changed_inset = true;
873                                 }
874                         }
875                 }
876                 if (changed_inset) {
877                         need_update = true;
878
879                         // FIXME
880
881                         // The test it.size()==1 was needed to prevent crashes.
882                         // How to set the cursor corretly when it.size()>1 ??
883                         if (it.size() == 1) {
884                                 text->setCursorIntern(par, 0);
885                                 text->redoParagraphs(text->cursor,
886                                                      text->cursor.par()->next());
887                                 text->fullRebreak();
888                         }
889                 }
890         }
891         text->setCursorIntern(cursor.par(), cursor.pos());
892         return need_update;
893 }
894
895
896 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
897 {
898         // Check if the label 'from' appears more than once
899         vector<string> labels = buffer()->getLabelList();
900
901         if (lyx::count(labels.begin(), labels.end(), from) > 1)
902                 return false;
903
904         return ChangeInsets(Inset::REF_CODE, from, to);
905 }
906
907
908 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
909 {
910         typedef pair<string, string> StringPair;
911
912         vector<StringPair> keys;
913         buffer()->fillWithBibKeys(keys);
914         if (count_if(keys.begin(), keys.end(),
915                      lyx::equal_1st_in_pair<StringPair>(from))
916             > 1)
917                 return false;
918
919         return ChangeInsets(Inset::CITE_CODE, from, to);
920 }
921
922
923 UpdatableInset * BufferView::theLockingInset() const
924 {
925         // If NULL is not allowed we should put an Assert here. (Lgb)
926         if (text)
927                 return text->the_locking_inset;
928         return 0;
929 }
930
931
932 void BufferView::theLockingInset(UpdatableInset * inset)
933 {
934         text->the_locking_inset = inset;
935 }
936
937
938 LyXText * BufferView::getLyXText() const
939 {
940         if (theLockingInset()) {
941                 LyXText * txt = theLockingInset()->getLyXText(this, true);
942                 if (txt)
943                         return txt;
944         }
945         return text;
946 }
947
948
949 LyXText * BufferView::getParentText(Inset * inset) const
950 {
951         if (inset->owner()) {
952                 LyXText * txt = inset->getLyXText(this);
953                 inset = inset->owner();
954                 while (inset && inset->getLyXText(this) == txt)
955                         inset = inset->owner();
956                 if (inset)
957                         return inset->getLyXText(this);
958         }
959         return text;
960 }
961
962
963 Language const * BufferView::getParentLanguage(Inset * inset) const
964 {
965         LyXText * text = getParentText(inset);
966         return text->cursor.par()->getFontSettings(buffer()->params,
967                                                    text->cursor.pos()).language();
968 }
969
970
971 Encoding const * BufferView::getEncoding() const
972 {
973         LyXText * t = getLyXText();
974         if (!t)
975                 return 0;
976
977         LyXCursor const & c= t->cursor;
978         LyXFont const font = c.par()->getFont(buffer()->params, c.pos());
979         return font.language()->encoding();
980 }
981
982
983 void BufferView::haveSelection(bool sel)
984 {
985         pimpl_->workarea().haveSelection(sel);
986 }
987
988
989 int BufferView::workHeight() const
990 {
991         return pimpl_->workarea().workHeight();
992 }