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