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