]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
move some selection related stuff over to textcursor.C
[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::showErrorList(string const & action) const
340 {
341         if (getErrorList().size()) {
342                 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
343                 owner()->getDialogs().show("errorlist", title);
344                 pimpl_->errorlist_.clear();
345         }
346 }
347
348
349 ErrorList const &
350 BufferView::getErrorList() const
351 {
352         return pimpl_->errorlist_;
353 }
354
355
356 void BufferView::setCursorFromRow(int row)
357 {
358         int tmpid = -1;
359         int tmppos = -1;
360
361         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
362
363         ParagraphList::iterator texrowpar;
364
365         if (tmpid == -1) {
366                 texrowpar = text->ownerParagraphs().begin();
367                 tmppos = 0;
368         } else {
369                 texrowpar = buffer()->getParFromID(tmpid).pit();
370         }
371         text->setCursor(texrowpar, tmppos);
372 }
373
374
375 bool BufferView::insertInset(Inset * inset, string const & lout)
376 {
377         return pimpl_->insertInset(inset, lout);
378 }
379
380
381 void BufferView::gotoLabel(string const & label)
382 {
383         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
384              it != buffer()->inset_iterator_end(); ++it) {
385                 vector<string> labels;
386                 it->getLabelList(labels);
387                 if (find(labels.begin(),labels.end(),label)
388                      != labels.end()) {
389                         beforeChange(text);
390                         text->setCursor(it.getPar(), it.getPos());
391                         text->selection.cursor = text->cursor;
392                         update(text, BufferView::SELECT);
393                         return;
394                 }
395         }
396 }
397
398
399 void BufferView::undo()
400 {
401         if (!available())
402                 return;
403
404         owner()->message(_("Undo"));
405         beforeChange(text);
406         update(text, BufferView::SELECT);
407         if (!textUndo(this))
408                 owner()->message(_("No further undo information"));
409         else
410                 update(text, BufferView::SELECT);
411         switchKeyMap();
412 }
413
414
415 void BufferView::redo()
416 {
417         if (!available())
418                 return;
419
420         owner()->message(_("Redo"));
421         beforeChange(text);
422         update(text, BufferView::SELECT);
423         if (!textRedo(this))
424                 owner()->message(_("No further redo information"));
425         else
426                 update(text, BufferView::SELECT);
427         switchKeyMap();
428 }
429
430
431 // these functions are for the spellchecker
432 WordLangTuple const BufferView::nextWord(float & value)
433 {
434         if (!available()) {
435                 value = 1;
436                 return WordLangTuple();
437         }
438
439         return text->selectNextWordToSpellcheck(value);
440 }
441
442
443 void BufferView::selectLastWord()
444 {
445         if (!available())
446                 return;
447
448         LyXCursor cur = text->selection.cursor;
449         beforeChange(text);
450         text->selection.cursor = cur;
451         text->selectSelectedWord();
452         toggleSelection(false);
453         update(text, BufferView::SELECT);
454 }
455
456
457 void BufferView::endOfSpellCheck()
458 {
459         if (!available()) return;
460
461         beforeChange(text);
462         text->selectSelectedWord();
463         text->clearSelection();
464         update(text, BufferView::SELECT);
465 }
466
467
468 void BufferView::replaceWord(string const & replacestring)
469 {
470         if (!available())
471                 return;
472
473         LyXText * tt = getLyXText();
474         update(tt, BufferView::SELECT);
475
476         // clear the selection (if there is any)
477         toggleSelection(false);
478         update(tt, BufferView::SELECT);
479
480         // clear the selection (if there is any)
481         toggleSelection(false);
482         tt->replaceSelectionWithString(replacestring);
483
484         tt->setSelectionRange(replacestring.length());
485
486         // Go back so that replacement string is also spellchecked
487         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
488                 tt->cursorLeft(this);
489         }
490         update(tt, BufferView::SELECT);
491
492         // FIXME: should be done through LFUN
493         buffer()->markDirty();
494         fitCursor();
495 }
496 // End of spellchecker stuff
497
498
499 bool BufferView::lockInset(UpdatableInset * inset)
500 {
501         if (!inset)
502                 return false;
503         // don't relock if we're already locked
504         if (theLockingInset() == inset)
505                 return true;
506         if (!theLockingInset()) {
507                 // first check if it's the inset under the cursor we want lock
508                 // should be most of the time
509                 if (text->cursor.pos() < text->cursor.par()->size()
510                     && text->cursor.par()->getChar(text->cursor.pos()) ==
511                     Paragraph::META_INSET) {
512                         Inset * in = text->cursor.par()->getInset(text->cursor.pos());
513                         if (inset == in) {
514                                 theLockingInset(inset);
515                                 return true;
516                         }
517                 }
518                 // Then do a deep look of the inset and lock the right one
519                 int const id = inset->id();
520                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
521                 ParagraphList::iterator pend = buffer()->paragraphs.end();
522                 for (; pit != pend; ++pit) {
523                         InsetList::iterator it = pit->insetlist.begin();
524                         InsetList::iterator end = pit->insetlist.end();
525                         for (; it != end; ++it) {
526                                 if (it->inset == inset) {
527                                         text->setCursorIntern(pit, it->pos);
528                                         theLockingInset(inset);
529                                         return true;
530                                 }
531                                 if (it->inset->getInsetFromID(id)) {
532                                         text->setCursorIntern(pit, it->pos);
533                                         FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
534                                         it->inset->localDispatch(cmd);
535                                         return theLockingInset()->lockInsetInInset(this, inset);
536                                 }
537                         }
538                 }
539                 return false;
540         }
541         return theLockingInset()->lockInsetInInset(this, inset);
542 }
543
544
545 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
546 {
547         if (theLockingInset() && available()) {
548                 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
549                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
550                         updateScrollbar();
551                         return true;
552                 }
553         }
554         return false;
555 }
556
557
558 void BufferView::hideCursor()
559 {
560         screen().hideCursor();
561 }
562
563
564 int BufferView::unlockInset(UpdatableInset * inset)
565 {
566         if (!inset)
567                 return 0;
568         if (inset && theLockingInset() == inset) {
569                 inset->insetUnlock(this);
570                 theLockingInset(0);
571                 // make sure we update the combo !
572                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
573                 // Tell the paragraph dialog that we changed paragraph
574                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
575                 finishUndo();
576                 return 0;
577         } else if (inset && theLockingInset() &&
578                    theLockingInset()->unlockInsetInInset(this, inset)) {
579                 // Tell the paragraph dialog that we changed paragraph
580                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
581                 // owner inset has updated the layout combo
582                 finishUndo();
583                 return 0;
584         }
585         return 1;
586 }
587
588
589 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
590 {
591         if (!theLockingInset())
592                 return; // shouldn't happen
593         if (kind == Undo::EDIT) // in this case insets would not be stored!
594                 kind = Undo::FINISH;
595         setUndo(this, kind, text->cursor.par());
596 }
597
598
599 void BufferView::updateInset(Inset * inset)
600 {
601         pimpl_->updateInset(inset);
602 }
603
604
605 bool BufferView::ChangeInsets(Inset::Code code,
606                               string const & from, string const & to)
607 {
608         bool need_update = false;
609         LyXCursor cursor = text->cursor;
610         LyXCursor tmpcursor = cursor;
611         cursor.par(tmpcursor.par());
612         cursor.pos(tmpcursor.pos());
613
614         ParIterator end = buffer()->par_iterator_end();
615         for (ParIterator it = buffer()->par_iterator_begin();
616              it != end; ++it) {
617                 bool changed_inset = false;
618                 for (InsetList::iterator it2 = it->insetlist.begin();
619                      it2 != it->insetlist.end(); ++it2) {
620                         if (it2->inset->lyxCode() == code) {
621                                 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
622                                 if (inset->getContents() == from) {
623                                         inset->setContents(to);
624                                         changed_inset = true;
625                                 }
626                         }
627                 }
628                 if (changed_inset) {
629                         need_update = true;
630
631                         // FIXME
632
633                         // The test it.size()==1 was needed to prevent crashes.
634                         // How to set the cursor corretly when it.size()>1 ??
635                         if (it.size() == 1) {
636                                 text->setCursorIntern(it.pit(), 0);
637                                 text->redoParagraphs(text->cursor,
638                                                      boost::next(text->cursor.par()));
639                                 text->partialRebreak();
640                         }
641                 }
642         }
643         text->setCursorIntern(cursor.par(), cursor.pos());
644         return need_update;
645 }
646
647
648 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
649 {
650         // Check if the label 'from' appears more than once
651         vector<string> labels;
652         buffer()->getLabelList(labels);
653
654         if (lyx::count(labels.begin(), labels.end(), from) > 1)
655                 return false;
656
657         return ChangeInsets(Inset::REF_CODE, from, to);
658 }
659
660
661 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
662 {
663         typedef pair<string, string> StringPair;
664
665         vector<StringPair> keys;
666         buffer()->fillWithBibKeys(keys);
667         if (count_if(keys.begin(), keys.end(),
668                      lyx::equal_1st_in_pair<StringPair>(from))
669             > 1)
670                 return false;
671
672         return ChangeInsets(Inset::CITE_CODE, from, to);
673 }
674
675
676 UpdatableInset * BufferView::theLockingInset() const
677 {
678         // If NULL is not allowed we should put an Assert here. (Lgb)
679         if (text)
680                 return text->the_locking_inset;
681         return 0;
682 }
683
684
685 void BufferView::theLockingInset(UpdatableInset * inset)
686 {
687         text->the_locking_inset = inset;
688 }
689
690
691 LyXText * BufferView::getLyXText() const
692 {
693         if (theLockingInset()) {
694                 LyXText * txt = theLockingInset()->getLyXText(this, true);
695                 if (txt)
696                         return txt;
697         }
698         return text;
699 }
700
701
702 LyXText * BufferView::getParentText(Inset * inset) const
703 {
704         if (inset->owner()) {
705                 LyXText * txt = inset->getLyXText(this);
706                 inset = inset->owner();
707                 while (inset && inset->getLyXText(this) == txt)
708                         inset = inset->owner();
709                 if (inset)
710                         return inset->getLyXText(this);
711         }
712         return text;
713 }
714
715
716 Language const * BufferView::getParentLanguage(Inset * inset) const
717 {
718         LyXText * text = getParentText(inset);
719         return text->cursor.par()->getFontSettings(buffer()->params,
720                                                    text->cursor.pos()).language();
721 }
722
723
724 Encoding const * BufferView::getEncoding() const
725 {
726         LyXText * t = getLyXText();
727         if (!t)
728                 return 0;
729
730         LyXCursor const & c = t->cursor;
731         LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
732                                               outerFont(c.par(), t->ownerParagraphs()));
733         return font.language()->encoding();
734 }
735
736
737 void BufferView::haveSelection(bool sel)
738 {
739         pimpl_->workarea().haveSelection(sel);
740 }
741
742
743 int BufferView::workHeight() const
744 {
745         return pimpl_->workarea().workHeight();
746 }