]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
7b8a4cfa33a8e5cacf7ba0301b4e22ff404566e7
[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 "LaTeX.h"
16 #include "ParagraphParameters.h"
17 #include "WordLangTuple.h"
18 #include "buffer.h"
19 #include "bufferlist.h"
20 #include "debug.h"
21 #include "gettext.h"
22 #include "errorlist.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 #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/inseterror.h"
40 #include "insets/updatableinset.h"
41
42 #include "support/FileInfo.h"
43 #include "support/filetools.h"
44 #include "support/lyxfunctional.h" // equal_1st_in_pair
45 #include "support/types.h"
46 #include "support/lyxalgo.h" // lyx_count
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::toggleSelection(bool b)
226 {
227         pimpl_->toggleSelection(b);
228 }
229
230
231 void BufferView::toggleToggle()
232 {
233         pimpl_->toggleToggle();
234 }
235
236
237 void BufferView::center()
238 {
239         pimpl_->center();
240 }
241
242
243 string const BufferView::getClipboard() const
244 {
245         return pimpl_->workarea().getClipboard();
246 }
247
248
249 void BufferView::stuffClipboard(string const & stuff) const
250 {
251         pimpl_->stuffClipboard(stuff);
252 }
253
254
255 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
256                                   BufferView::UpdateCodes uc2)
257 {
258         return static_cast<BufferView::UpdateCodes>
259                 (static_cast<int>(uc1) | static_cast<int>(uc2));
260 }
261
262
263 bool BufferView::dispatch(FuncRequest const & ev)
264 {
265         return pimpl_->dispatch(ev);
266 }
267
268
269 void BufferView::scroll(int lines)
270 {
271         pimpl_->scroll(lines);
272 }
273
274
275 // Inserts a file into current document
276 bool BufferView::insertLyXFile(string const & filen)
277         //
278         // Copyright CHT Software Service GmbH
279         // Uwe C. Schroeder
280         //
281         // Insert a LyXformat - file into current buffer
282         //
283         // Moved from lyx_cb.C (Lgb)
284 {
285         if (filen.empty())
286                 return false;
287
288         string const fname = MakeAbsPath(filen);
289
290         // check if file exist
291         FileInfo const fi(fname);
292
293         if (!fi.readable()) {
294                 string const file = MakeDisplayPath(fname, 50);
295                 string const text =
296                         bformat(_("The specified document\n%1$s\ncould not be read."), file);
297                 Alert::error(_("Could not read document"), text);
298                 return false;
299         }
300
301         beforeChange(text);
302
303         ifstream ifs(fname.c_str());
304         if (!ifs) {
305                 string const file = MakeDisplayPath(fname, 50);
306                 string const text =
307                         bformat(_("Could not open the specified document %1$s\n"), file);
308                 Alert::error(_("Could not open file"), text);
309                 return false;
310         }
311
312         int const c = ifs.peek();
313
314         LyXLex lex(0, 0);
315         lex.setStream(ifs);
316
317         bool res = true;
318
319         if (c == '#') {
320                 // FIXME: huh ? No we won't !
321                 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
322                 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
323         } else {
324                 lyxerr[Debug::INFO] << "Will insert file without header"
325                                     << endl;
326                 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
327         }
328
329         resize();
330         return res;
331 }
332
333
334 void BufferView::resetErrorList()
335 {
336         pimpl_->errorlist_.clear();
337 }
338
339
340 void BufferView::setErrorList(ErrorList const & el)
341 {
342         pimpl_->errorlist_ = el;
343 }
344
345
346 void BufferView::addError(ErrorItem const & ei)
347 {
348         pimpl_->errorlist_.push_back(ei);
349
350 }
351
352
353 void BufferView::showErrorList(string const & action) const
354 {
355         if (getErrorList().size()) {
356                 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
357                 owner()->getDialogs().show("errorlist", title);
358         }
359 }
360
361
362 ErrorList const &
363 BufferView::getErrorList() const
364 {
365         return pimpl_->errorlist_;
366 }
367
368
369 void BufferView::setCursorFromRow(int row)
370 {
371         int tmpid = -1;
372         int tmppos = -1;
373
374         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
375
376         ParagraphList::iterator texrowpar;
377
378         if (tmpid == -1) {
379                 texrowpar = text->ownerParagraphs().begin();
380                 tmppos = 0;
381         } else {
382                 texrowpar = buffer()->getParFromID(tmpid).pit();
383         }
384         text->setCursor(texrowpar, tmppos);
385 }
386
387
388 bool BufferView::insertInset(Inset * inset, string const & lout)
389 {
390         return pimpl_->insertInset(inset, lout);
391 }
392
393
394 void BufferView::gotoLabel(string const & label)
395 {
396         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
397              it != buffer()->inset_iterator_end(); ++it) {
398                 vector<string> labels;
399                 it->getLabelList(labels);
400                 if (find(labels.begin(),labels.end(),label)
401                      != labels.end()) {
402                         beforeChange(text);
403                         text->setCursor(it.getPar(), it.getPos());
404                         text->selection.cursor = text->cursor;
405                         update(text, BufferView::SELECT);
406                         return;
407                 }
408         }
409 }
410
411
412 void BufferView::undo()
413 {
414         if (!available())
415                 return;
416
417         owner()->message(_("Undo"));
418         beforeChange(text);
419         update(text, BufferView::SELECT);
420         if (!textUndo(this))
421                 owner()->message(_("No further undo information"));
422         else
423                 update(text, BufferView::SELECT);
424         switchKeyMap();
425 }
426
427
428 void BufferView::redo()
429 {
430         if (!available())
431                 return;
432
433         owner()->message(_("Redo"));
434         beforeChange(text);
435         update(text, BufferView::SELECT);
436         if (!textRedo(this))
437                 owner()->message(_("No further redo information"));
438         else
439                 update(text, BufferView::SELECT);
440         switchKeyMap();
441 }
442
443
444 // these functions are for the spellchecker
445 WordLangTuple const BufferView::nextWord(float & value)
446 {
447         if (!available()) {
448                 value = 1;
449                 return WordLangTuple();
450         }
451
452         return text->selectNextWordToSpellcheck(value);
453 }
454
455
456 void BufferView::selectLastWord()
457 {
458         if (!available())
459                 return;
460
461         LyXCursor cur = text->selection.cursor;
462         beforeChange(text);
463         text->selection.cursor = cur;
464         text->selectSelectedWord();
465         toggleSelection(false);
466         update(text, BufferView::SELECT);
467 }
468
469
470 void BufferView::endOfSpellCheck()
471 {
472         if (!available()) return;
473
474         beforeChange(text);
475         text->selectSelectedWord();
476         text->clearSelection();
477         update(text, BufferView::SELECT);
478 }
479
480
481 void BufferView::replaceWord(string const & replacestring)
482 {
483         if (!available())
484                 return;
485
486         LyXText * tt = getLyXText();
487         update(tt, BufferView::SELECT);
488
489         // clear the selection (if there is any)
490         toggleSelection(false);
491         update(tt, BufferView::SELECT);
492
493         // clear the selection (if there is any)
494         toggleSelection(false);
495         tt->replaceSelectionWithString(replacestring);
496
497         tt->setSelectionRange(replacestring.length());
498
499         // Go back so that replacement string is also spellchecked
500         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
501                 tt->cursorLeft(this);
502         }
503         update(tt, BufferView::SELECT);
504
505         // FIXME: should be done through LFUN
506         buffer()->markDirty();
507         fitCursor();
508 }
509 // End of spellchecker stuff
510
511
512 bool BufferView::lockInset(UpdatableInset * inset)
513 {
514         if (!inset)
515                 return false;
516         // don't relock if we're already locked
517         if (theLockingInset() == inset)
518                 return true;
519         if (!theLockingInset()) {
520                 // first check if it's the inset under the cursor we want lock
521                 // should be most of the time
522                 if (text->cursor.pos() < text->cursor.par()->size()
523                     && text->cursor.par()->getChar(text->cursor.pos()) ==
524                     Paragraph::META_INSET) {
525                         Inset * in = text->cursor.par()->getInset(text->cursor.pos());
526                         if (inset == in) {
527                                 theLockingInset(inset);
528                                 return true;
529                         }
530                 }
531                 // Then do a deep look of the inset and lock the right one
532                 int const id = inset->id();
533                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
534                 ParagraphList::iterator pend = buffer()->paragraphs.end();
535                 for (; pit != pend; ++pit) {
536                         InsetList::iterator it = pit->insetlist.begin();
537                         InsetList::iterator end = pit->insetlist.end();
538                         for (; it != end; ++it) {
539                                 if (it->inset == inset) {
540                                         text->setCursorIntern(pit, it->pos);
541                                         theLockingInset(inset);
542                                         return true;
543                                 }
544                                 if (it->inset->getInsetFromID(id)) {
545                                         text->setCursorIntern(pit, it->pos);
546                                         FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
547                                         it->inset->localDispatch(cmd);
548                                         return theLockingInset()->lockInsetInInset(this, inset);
549                                 }
550                         }
551                 }
552                 return false;
553         }
554         return theLockingInset()->lockInsetInInset(this, inset);
555 }
556
557
558 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
559 {
560         if (theLockingInset() && available()) {
561                 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
562                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
563                         updateScrollbar();
564                         return true;
565                 }
566         }
567         return false;
568 }
569
570
571 void BufferView::hideCursor()
572 {
573         screen().hideCursor();
574 }
575
576
577 int BufferView::unlockInset(UpdatableInset * inset)
578 {
579         if (!inset)
580                 return 0;
581         if (inset && theLockingInset() == inset) {
582                 inset->insetUnlock(this);
583                 theLockingInset(0);
584                 // make sure we update the combo !
585                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
586                 // Tell the paragraph dialog that we changed paragraph
587                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
588                 finishUndo();
589                 return 0;
590         } else if (inset && theLockingInset() &&
591                    theLockingInset()->unlockInsetInInset(this, inset)) {
592                 // Tell the paragraph dialog that we changed paragraph
593                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
594                 // owner inset has updated the layout combo
595                 finishUndo();
596                 return 0;
597         }
598         return 1;
599 }
600
601
602 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
603 {
604         if (!theLockingInset())
605                 return; // shouldn't happen
606         if (kind == Undo::EDIT) // in this case insets would not be stored!
607                 kind = Undo::FINISH;
608         setUndo(this, kind, text->cursor.par());
609 }
610
611
612 void BufferView::updateInset(Inset * inset)
613 {
614         pimpl_->updateInset(inset);
615 }
616
617
618 bool BufferView::ChangeInsets(Inset::Code code,
619                               string const & from, string const & to)
620 {
621         bool need_update = false;
622         LyXCursor cursor = text->cursor;
623         LyXCursor tmpcursor = cursor;
624         cursor.par(tmpcursor.par());
625         cursor.pos(tmpcursor.pos());
626
627         ParIterator end = buffer()->par_iterator_end();
628         for (ParIterator it = buffer()->par_iterator_begin();
629              it != end; ++it) {
630                 bool changed_inset = false;
631                 for (InsetList::iterator it2 = it->insetlist.begin();
632                      it2 != it->insetlist.end(); ++it2) {
633                         if (it2->inset->lyxCode() == code) {
634                                 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
635                                 if (inset->getContents() == from) {
636                                         inset->setContents(to);
637                                         changed_inset = true;
638                                 }
639                         }
640                 }
641                 if (changed_inset) {
642                         need_update = true;
643
644                         // FIXME
645
646                         // The test it.size()==1 was needed to prevent crashes.
647                         // How to set the cursor corretly when it.size()>1 ??
648                         if (it.size() == 1) {
649                                 text->setCursorIntern(it.pit(), 0);
650                                 text->redoParagraphs(text->cursor,
651                                                      boost::next(text->cursor.par()));
652                                 text->fullRebreak();
653                         }
654                 }
655         }
656         text->setCursorIntern(cursor.par(), cursor.pos());
657         return need_update;
658 }
659
660
661 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
662 {
663         // Check if the label 'from' appears more than once
664         vector<string> labels;
665         buffer()->getLabelList(labels);
666
667         if (lyx::count(labels.begin(), labels.end(), from) > 1)
668                 return false;
669
670         return ChangeInsets(Inset::REF_CODE, from, to);
671 }
672
673
674 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
675 {
676         typedef pair<string, string> StringPair;
677
678         vector<StringPair> keys;
679         buffer()->fillWithBibKeys(keys);
680         if (count_if(keys.begin(), keys.end(),
681                      lyx::equal_1st_in_pair<StringPair>(from))
682             > 1)
683                 return false;
684
685         return ChangeInsets(Inset::CITE_CODE, from, to);
686 }
687
688
689 UpdatableInset * BufferView::theLockingInset() const
690 {
691         // If NULL is not allowed we should put an Assert here. (Lgb)
692         if (text)
693                 return text->the_locking_inset;
694         return 0;
695 }
696
697
698 void BufferView::theLockingInset(UpdatableInset * inset)
699 {
700         text->the_locking_inset = inset;
701 }
702
703
704 LyXText * BufferView::getLyXText() const
705 {
706         if (theLockingInset()) {
707                 LyXText * txt = theLockingInset()->getLyXText(this, true);
708                 if (txt)
709                         return txt;
710         }
711         return text;
712 }
713
714
715 LyXText * BufferView::getParentText(Inset * inset) const
716 {
717         if (inset->owner()) {
718                 LyXText * txt = inset->getLyXText(this);
719                 inset = inset->owner();
720                 while (inset && inset->getLyXText(this) == txt)
721                         inset = inset->owner();
722                 if (inset)
723                         return inset->getLyXText(this);
724         }
725         return text;
726 }
727
728
729 Language const * BufferView::getParentLanguage(Inset * inset) const
730 {
731         LyXText * text = getParentText(inset);
732         return text->cursor.par()->getFontSettings(buffer()->params,
733                                                    text->cursor.pos()).language();
734 }
735
736
737 Encoding const * BufferView::getEncoding() const
738 {
739         LyXText * t = getLyXText();
740         if (!t)
741                 return 0;
742
743         LyXCursor const & c = t->cursor;
744         LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
745                                               outerFont(c.par(), t->ownerParagraphs()));
746         return font.language()->encoding();
747 }
748
749
750 void BufferView::haveSelection(bool sel)
751 {
752         pimpl_->workarea().haveSelection(sel);
753 }
754
755
756 int BufferView::workHeight() const
757 {
758         return pimpl_->workarea().workHeight();
759 }