2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
16 #include "BufferView.h"
18 #include "lyxcursor.h"
20 #include "insets/inseterror.h"
21 #include "insets/insetinfo.h"
22 #include "insets/insetspecialchar.h"
24 #include "minibuffer.h"
25 #include "bufferlist.h"
26 #include "support/FileInfo.h"
27 #include "lyxscreen.h"
28 #include "support/filetools.h"
29 #include "lyx_gui_misc.h"
31 #include "BufferView_pimpl.h"
33 extern BufferList bufferlist;
39 // Inserts a file into current document
40 bool BufferView::insertLyXFile(string const & filen)
42 // Copyright CHT Software Service GmbH
45 // Insert a Lyxformat - file into current buffer
47 // Moved from lyx_cb.C (Lgb)
49 if (filen.empty()) return false;
51 string fname = MakeAbsPath(filen);
53 // check if file exist
57 WriteAlert(_("Error!"),
58 _("Specified file is unreadable: "),
59 MakeDisplayPath(fname, 50));
65 ifstream ifs(fname.c_str());
67 WriteAlert(_("Error!"),
68 _("Cannot open specified file: "),
69 MakeDisplayPath(fname, 50));
80 lyxerr.debug() << "Will insert file with header" << endl;
81 res = buffer()->readFile(lex, text->cursor.par);
83 lyxerr.debug() << "Will insert file without header" << endl;
84 res = buffer()->readLyXformat2(lex, text->cursor.par);
91 bool BufferView::removeAutoInsets()
93 LyXParagraph * par = buffer()->paragraph;
95 LyXCursor cursor = text->cursor;
96 LyXCursor tmpcursor = cursor;
97 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
98 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
102 if (par->AutoDeleteInsets()){
104 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
105 // this is possible now, since SetCursor takes
106 // care about footnotes
107 text->SetCursorIntern(par, 0);
108 text->RedoParagraphs(text->cursor,
109 text->cursor.par->Next());
115 // avoid forbidden cursor positions caused by error removing
116 if (cursor.pos > cursor.par->Last())
117 cursor.pos = cursor.par->Last();
118 text->SetCursorIntern(cursor.par, cursor.pos);
124 void BufferView::insertErrors(TeXErrors & terr)
126 // Save the cursor position
127 LyXCursor cursor = text->cursor;
129 // This is drastic, but it's the only fix, I could find. (Asger)
133 for (TeXErrors::Errors::const_iterator cit = terr.begin();
136 string desctext((*cit).error_desc);
137 string errortext((*cit).error_text);
138 string msgtxt = desctext + '\n' + errortext;
139 int errorrow = (*cit).error_in_line;
141 // Insert error string for row number
145 buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
147 LyXParagraph * texrowpar = 0;
150 texrowpar = text->FirstParagraph();
153 texrowpar = text->GetParFromID(tmpid);
159 InsetError * new_inset = new InsetError(msgtxt);
160 text->SetCursorIntern(texrowpar, tmppos);
161 text->InsertInset(new_inset);
164 // Restore the cursor position
165 text->SetCursorIntern(cursor.par, cursor.pos);
169 void BufferView::setCursorFromRow(int row)
174 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
176 LyXParagraph * texrowpar;
179 texrowpar = text->FirstParagraph();
182 texrowpar = text->GetParFromID(tmpid);
184 text->SetCursor(texrowpar, tmppos);
187 void BufferView::insertInset(Inset * inset, string const & lout,
190 // check for table/list in tables
191 if (no_table && text->cursor.par->table){
192 WriteAlert(_("Impossible Operation!"),
193 _("Cannot insert table/list in table."),
197 // not quite sure if we want this...
198 text->SetCursorParUndo();
204 text->BreakParagraph();
207 if (text->cursor.par->Last()) {
210 text->BreakParagraph();
214 pair<bool, LyXTextClass::size_type> lres =
215 textclasslist.NumberOfLayout(buffer()->params
217 LyXTextClass::size_type lay;
218 if (lres.first != false) {
222 // layout not fount using default "Standard" (0)
226 text->SetLayout(lay);
228 text->SetParagraph(0, 0,
230 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
236 text->current_font.setLatex(LyXFont::OFF);
239 text->InsertInset(inset);
241 // if we enter a text-inset the cursor should be to the left side
242 // of it! This couldn't happen before as Undo was not handled inside
243 // inset now after the Undo LyX tries to call inset->Edit(...) again
244 // and cannot do this as the cursor is behind the inset and GetInset
245 // does not return the inset!
246 if (inset->IsTextInset()) {
247 if (text->cursor.par->isRightToLeftPar())
255 text->UnFreezeUndo();
259 // Open and lock an updatable inset
260 void BufferView::open_new_inset(UpdatableInset * new_inset)
264 insertInset(new_inset);
267 new_inset->Edit(this, 0, 0, 0);
270 /* This is also a buffer property (ale) */
271 // Not so sure about that. a goto Label function can not be buffer local, just
272 // think how this will work in a multiwindo/buffer environment, all the
273 // cursors in all the views showing this buffer will move. (Lgb)
274 // OK, then no cursor action should be allowed in buffer. (ale)
275 bool BufferView::gotoLabel(string const & label)
278 LyXParagraph * par = buffer()->paragraph;
279 LyXParagraph::size_type pos;
283 while ((inset = par->ReturnNextInsetPointer(pos))){
284 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
285 if (label == inset->getLabel(i)) {
287 text->SetCursor(par, pos);
288 text->sel_cursor = text->cursor;
300 void BufferView::allFloats(char flag, char figmar)
302 if (!available()) return;
304 LyXCursor cursor = text->cursor;
306 if (!flag && cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
308 && cursor.par->footnotekind != LyXParagraph::FOOTNOTE
309 && cursor.par->footnotekind != LyXParagraph::MARGIN)
311 && cursor.par->footnotekind != LyXParagraph::FIG
312 && cursor.par->footnotekind != LyXParagraph::TAB
313 && cursor.par->footnotekind != LyXParagraph::WIDE_FIG
314 && cursor.par->footnotekind != LyXParagraph::WIDE_TAB
315 && cursor.par->footnotekind != LyXParagraph::ALGORITHM)))
320 LyXCursor tmpcursor = cursor;
321 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
322 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
324 LyXParagraph *par = buffer()->paragraph;
327 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
329 && par->footnotekind != LyXParagraph::FOOTNOTE
330 && par->footnotekind != LyXParagraph::MARGIN)
332 && par->footnotekind != LyXParagraph::FIG
333 && par->footnotekind != LyXParagraph::TAB
334 && par->footnotekind != LyXParagraph::WIDE_FIG
335 && par->footnotekind != LyXParagraph::WIDE_TAB
336 && par->footnotekind != LyXParagraph::ALGORITHM
341 && par->previous->footnoteflag !=
342 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
343 text->SetCursorIntern(par->previous,
345 text->OpenFootnote();
349 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
353 par->footnotekind != LyXParagraph::FOOTNOTE
355 par->footnotekind != LyXParagraph::MARGIN
360 par->footnotekind != LyXParagraph::FIG
362 par->footnotekind != LyXParagraph::TAB
364 par->footnotekind != LyXParagraph::WIDE_FIG
366 par->footnotekind != LyXParagraph::WIDE_TAB
368 par->footnotekind != LyXParagraph::ALGORITHM
372 text->SetCursorIntern(par, 0);
373 text->CloseFootnote();
379 text->SetCursorIntern(cursor.par, cursor.pos);
386 void BufferView::insertNote()
388 InsetInfo * new_inset = new InsetInfo();
389 insertInset(new_inset);
390 new_inset->Edit(this, 0, 0, 0);
394 void BufferView::openStuff()
397 owner()->getMiniBuffer()->Set(_("Open/Close..."));
408 void BufferView::toggleFloat()
411 owner()->getMiniBuffer()->Set(_("Open/Close..."));
415 text->ToggleFootnote();
421 void BufferView::menuUndo()
424 owner()->getMiniBuffer()->Set(_("Undo"));
428 if (!text->TextUndo())
429 owner()->getMiniBuffer()->Set(_("No further undo information"));
437 void BufferView::menuRedo()
439 if (the_locking_inset) {
440 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
445 owner()->getMiniBuffer()->Set(_("Redo"));
449 if (!text->TextRedo())
450 owner()->getMiniBuffer()->Set(_("No further redo information"));
458 void BufferView::hyphenationPoint()
463 InsetSpecialChar * new_inset =
464 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
465 insertInset(new_inset);
470 void BufferView::ldots()
475 InsetSpecialChar * new_inset =
476 new InsetSpecialChar(InsetSpecialChar::LDOTS);
477 insertInset(new_inset);
482 void BufferView::endOfSentenceDot()
487 InsetSpecialChar * new_inset =
488 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
489 insertInset(new_inset);
494 void BufferView::menuSeparator()
499 InsetSpecialChar * new_inset =
500 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
501 insertInset(new_inset);
506 void BufferView::newline()
511 text->InsertChar(LyXParagraph::META_NEWLINE);
517 void BufferView::protectedBlank()
522 InsetSpecialChar * new_inset =
523 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
524 insertInset(new_inset);
529 void BufferView::hfill()
534 text->InsertChar(LyXParagraph::META_HFILL);
539 void BufferView::copyEnvironment()
542 text->copyEnvironmentType();
543 // clear the selection, even if mark_set
545 text->ClearSelection();
547 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
552 void BufferView::pasteEnvironment()
555 text->pasteEnvironmentType();
556 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
562 void BufferView::copy()
565 text->CopySelection();
566 // clear the selection, even if mark_set
568 text->ClearSelection();
570 owner()->getMiniBuffer()->Set(_("Copy"));
574 void BufferView::cut()
579 text->CutSelection();
581 owner()->getMiniBuffer()->Set(_("Cut"));
586 void BufferView::paste()
588 if (!available()) return;
590 owner()->getMiniBuffer()->Set(_("Paste"));
592 // clear the selection
594 text->ClearSelection();
598 text->PasteSelection();
601 // clear the selection
603 text->ClearSelection();
608 void BufferView::gotoNote()
610 if (!available()) return;
617 if (!text->GotoNextNote()) {
619 || text->cursor.par != text->FirstParagraph()) {
621 text->cursor.par = text->FirstParagraph();
622 text->cursor.pos = 0;
623 if (!text->GotoNextNote()) {
625 owner()->getMiniBuffer()->Set(_("No more notes"));
629 owner()->getMiniBuffer()->Set(_("No more notes"));
634 text->sel_cursor = text->cursor;
638 void BufferView::insertCorrectQuote()
642 if (text->cursor.pos)
643 c = text->cursor.par->GetChar(text->cursor.pos - 1);
647 insertInset(new InsetQuotes(c, buffer()->params));
651 /* these functions are for the spellchecker */
652 char * BufferView::nextWord(float & value)
659 char * string = text->SelectNextWord(value);
665 void BufferView::selectLastWord()
667 if (!available()) return;
671 text->SelectSelectedWord();
672 toggleSelection(false);
677 void BufferView::endOfSpellCheck()
679 if (!available()) return;
683 text->SelectSelectedWord();
684 text->ClearSelection();
689 void BufferView::replaceWord(string const & replacestring)
691 if (!available()) return;
696 /* clear the selection (if there is any) */
697 toggleSelection(false);
700 /* clear the selection (if there is any) */
701 toggleSelection(false);
702 text->ReplaceSelectionWithString(replacestring.c_str());
704 text->SetSelectionOverString(replacestring.c_str());
706 // Go back so that replacement string is also spellchecked
707 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
708 text->CursorLeftIntern();
712 // End of spellchecker stuff
715 bool BufferView::lockInset(UpdatableInset * inset)
717 if (!the_locking_inset && inset) {
718 the_locking_inset = inset;
721 return the_locking_inset->LockInsetInInset(this, inset);
727 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
729 if (the_locking_inset && available()) {
730 LyXCursor cursor = text->cursor;
731 if ((cursor.pos - 1 >= 0) &&
732 (cursor.par->GetChar(cursor.pos-1) ==
733 LyXParagraph::META_INSET) &&
734 (cursor.par->GetInset(cursor.pos - 1) ==
735 the_locking_inset->GetLockingInset()))
736 text->SetCursor(cursor, cursor.par, cursor.pos-1);
737 y += cursor.y + the_locking_inset->InsetInInsetY();
738 pimpl_->screen->ShowManualCursor(x, y, asc, desc,
739 LyXScreen::BAR_SHAPE);
744 void BufferView::hideLockedInsetCursor()
746 if (the_locking_inset && available()) {
747 pimpl_->screen->HideCursor();
752 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
754 if (the_locking_inset && available()){
755 y += text->cursor.y + the_locking_inset->InsetInInsetY();
756 if (pimpl_->screen->FitManualCursor(x, y, asc, desc))
762 int BufferView::unlockInset(UpdatableInset * inset)
764 if (inset && the_locking_inset == inset) {
765 inset->InsetUnlock(this);
766 the_locking_inset = 0;
769 } else if (inset && the_locking_inset &&
770 the_locking_inset->UnlockInsetInInset(this, inset)) {
774 return bufferlist.unlockInset(inset);
778 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
780 if (!the_locking_inset)
781 return; // shouldn't happen
782 if (kind == Undo::EDIT) // in this case insets would not be stored!
786 ParFromPos(text->cursor.pos)->previous,
788 ParFromPos(text->cursor.pos)->next);
792 void BufferView::updateInset(Inset * inset, bool mark_dirty)
797 // first check for locking insets
798 if (the_locking_inset) {
799 if (the_locking_inset == inset) {
800 if (text->UpdateInset(inset)){
803 if (buffer()->isLyxClean())
804 owner()->getMiniBuffer()->
806 buffer()->markDirty();
811 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
812 if (text->UpdateInset(the_locking_inset)) {
815 if (buffer()->isLyxClean())
816 owner()->getMiniBuffer()->
818 buffer()->markDirty();
826 // then check the current buffer
830 if (text->UpdateInset(inset)){