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