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