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