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 tmpcursor = text->cursor;
100 // this has to be done before the delete
101 text->SetCursor(cursor, par, 0);
102 if (par->AutoDeleteInsets()){
104 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
105 text->RedoParagraphs(cursor,
112 // avoid forbidden cursor positions caused by error removing
113 if (tmpcursor.pos > tmpcursor.par->Last())
114 tmpcursor.pos = tmpcursor.par->Last();
115 text->SetCursorIntern(tmpcursor.par, tmpcursor.pos);
121 void BufferView::insertErrors(TeXErrors & terr)
123 // Save the cursor position
124 LyXCursor cursor = text->cursor;
126 // This is drastic, but it's the only fix, I could find. (Asger)
130 for (TeXErrors::Errors::const_iterator cit = terr.begin();
133 string desctext((*cit).error_desc);
134 string errortext((*cit).error_text);
135 string msgtxt = desctext + '\n' + errortext;
136 int errorrow = (*cit).error_in_line;
138 // Insert error string for row number
142 buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
144 LyXParagraph * texrowpar = 0;
147 texrowpar = text->FirstParagraph();
150 texrowpar = text->GetParFromID(tmpid);
156 InsetError * new_inset = new InsetError(msgtxt);
157 text->SetCursorIntern(texrowpar, tmppos);
158 text->InsertInset(new_inset);
161 // Restore the cursor position
162 text->SetCursorIntern(cursor.par, cursor.pos);
166 void BufferView::setCursorFromRow(int row)
171 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
173 LyXParagraph * texrowpar;
176 texrowpar = text->FirstParagraph();
179 texrowpar = text->GetParFromID(tmpid);
181 text->SetCursor(texrowpar, tmppos);
184 bool BufferView::insertInset(Inset * inset, string const & lout,
187 // if we are in a locking inset we should try to insert the
188 // inset there otherwise this is a illegal function now
189 if (the_locking_inset) {
190 if (the_locking_inset->InsertInsetAllowed(inset) &&
191 the_locking_inset->InsertInset(this, inset))
196 // check for table/list in tables
197 if (no_table && text->cursor.par->table){
198 WriteAlert(_("Impossible Operation!"),
199 _("Cannot insert table/list in table."),
204 // not quite sure if we want this...
205 text->SetCursorParUndo();
211 text->BreakParagraph();
214 if (text->cursor.par->Last()) {
217 text->BreakParagraph();
221 pair<bool, LyXTextClass::size_type> lres =
222 textclasslist.NumberOfLayout(buffer()->params
224 LyXTextClass::size_type lay;
225 if (lres.first != false) {
229 // layout not fount using default "Standard" (0)
233 text->SetLayout(lay);
235 text->SetParagraph(0, 0,
237 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
243 text->current_font.setLatex(LyXFont::OFF);
246 text->InsertInset(inset);
248 // if we enter a text-inset the cursor should be to the left side
249 // of it! This couldn't happen before as Undo was not handled inside
250 // inset now after the Undo LyX tries to call inset->Edit(...) again
251 // and cannot do this as the cursor is behind the inset and GetInset
252 // does not return the inset!
253 if (inset->IsTextInset()) {
254 if (text->cursor.par->isRightToLeftPar())
262 text->UnFreezeUndo();
267 // Open and lock an updatable inset
268 void BufferView::open_new_inset(UpdatableInset * new_inset)
272 insertInset(new_inset);
275 new_inset->Edit(this, 0, 0, 0);
278 /* This is also a buffer property (ale) */
279 // Not so sure about that. a goto Label function can not be buffer local, just
280 // think how this will work in a multiwindo/buffer environment, all the
281 // cursors in all the views showing this buffer will move. (Lgb)
282 // OK, then no cursor action should be allowed in buffer. (ale)
283 bool BufferView::gotoLabel(string const & label)
286 LyXParagraph * par = buffer()->paragraph;
287 LyXParagraph::size_type pos;
291 while ((inset = par->ReturnNextInsetPointer(pos))){
292 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
293 if (label == inset->getLabel(i)) {
295 text->SetCursor(par, pos);
296 text->sel_cursor = text->cursor;
308 void BufferView::allFloats(char flag, char figmar)
310 if (!available()) return;
312 LyXCursor cursor = text->cursor;
314 if (!flag && cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
316 && cursor.par->footnotekind != LyXParagraph::FOOTNOTE
317 && cursor.par->footnotekind != LyXParagraph::MARGIN)
319 && cursor.par->footnotekind != LyXParagraph::FIG
320 && cursor.par->footnotekind != LyXParagraph::TAB
321 && cursor.par->footnotekind != LyXParagraph::WIDE_FIG
322 && cursor.par->footnotekind != LyXParagraph::WIDE_TAB
323 && cursor.par->footnotekind != LyXParagraph::ALGORITHM)))
328 LyXCursor tmpcursor = cursor;
329 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
330 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
332 LyXParagraph *par = buffer()->paragraph;
335 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
337 && par->footnotekind != LyXParagraph::FOOTNOTE
338 && par->footnotekind != LyXParagraph::MARGIN)
340 && par->footnotekind != LyXParagraph::FIG
341 && par->footnotekind != LyXParagraph::TAB
342 && par->footnotekind != LyXParagraph::WIDE_FIG
343 && par->footnotekind != LyXParagraph::WIDE_TAB
344 && par->footnotekind != LyXParagraph::ALGORITHM
349 && par->previous->footnoteflag !=
350 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
351 text->SetCursorIntern(par->previous,
353 text->OpenFootnote();
357 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
361 par->footnotekind != LyXParagraph::FOOTNOTE
363 par->footnotekind != LyXParagraph::MARGIN
368 par->footnotekind != LyXParagraph::FIG
370 par->footnotekind != LyXParagraph::TAB
372 par->footnotekind != LyXParagraph::WIDE_FIG
374 par->footnotekind != LyXParagraph::WIDE_TAB
376 par->footnotekind != LyXParagraph::ALGORITHM
380 text->SetCursorIntern(par, 0);
381 text->CloseFootnote();
387 text->SetCursorIntern(cursor.par, cursor.pos);
394 void BufferView::insertNote()
396 InsetInfo * new_inset = new InsetInfo();
397 insertInset(new_inset);
398 new_inset->Edit(this, 0, 0, 0);
402 void BufferView::openStuff()
405 owner()->getMiniBuffer()->Set(_("Open/Close..."));
416 void BufferView::toggleFloat()
419 owner()->getMiniBuffer()->Set(_("Open/Close..."));
423 text->ToggleFootnote();
429 void BufferView::menuUndo()
432 owner()->getMiniBuffer()->Set(_("Undo"));
436 if (!text->TextUndo())
437 owner()->getMiniBuffer()->Set(_("No further undo information"));
445 void BufferView::menuRedo()
447 if (the_locking_inset) {
448 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
453 owner()->getMiniBuffer()->Set(_("Redo"));
457 if (!text->TextRedo())
458 owner()->getMiniBuffer()->Set(_("No further redo information"));
466 void BufferView::hyphenationPoint()
471 InsetSpecialChar * new_inset =
472 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
473 insertInset(new_inset);
478 void BufferView::ldots()
483 InsetSpecialChar * new_inset =
484 new InsetSpecialChar(InsetSpecialChar::LDOTS);
485 insertInset(new_inset);
490 void BufferView::endOfSentenceDot()
495 InsetSpecialChar * new_inset =
496 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
497 insertInset(new_inset);
502 void BufferView::menuSeparator()
507 InsetSpecialChar * new_inset =
508 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
509 insertInset(new_inset);
514 void BufferView::newline()
519 text->InsertChar(LyXParagraph::META_NEWLINE);
525 void BufferView::protectedBlank()
530 InsetSpecialChar * new_inset =
531 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
532 insertInset(new_inset);
537 void BufferView::hfill()
542 text->InsertChar(LyXParagraph::META_HFILL);
547 void BufferView::copyEnvironment()
550 text->copyEnvironmentType();
551 // clear the selection, even if mark_set
553 text->ClearSelection();
555 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
560 void BufferView::pasteEnvironment()
563 text->pasteEnvironmentType();
564 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
570 void BufferView::copy()
573 text->CopySelection();
574 // clear the selection, even if mark_set
576 text->ClearSelection();
578 owner()->getMiniBuffer()->Set(_("Copy"));
582 void BufferView::cut()
587 text->CutSelection();
589 owner()->getMiniBuffer()->Set(_("Cut"));
594 void BufferView::paste()
596 if (!available()) return;
598 owner()->getMiniBuffer()->Set(_("Paste"));
600 // clear the selection
602 text->ClearSelection();
606 text->PasteSelection();
609 // clear the selection
611 text->ClearSelection();
616 void BufferView::gotoNote()
618 if (!available()) return;
625 if (!text->GotoNextNote()) {
627 || text->cursor.par != text->FirstParagraph()) {
629 text->cursor.par = text->FirstParagraph();
630 text->cursor.pos = 0;
631 if (!text->GotoNextNote()) {
633 owner()->getMiniBuffer()->Set(_("No more notes"));
637 owner()->getMiniBuffer()->Set(_("No more notes"));
642 text->sel_cursor = text->cursor;
646 void BufferView::insertCorrectQuote()
650 if (text->cursor.pos)
651 c = text->cursor.par->GetChar(text->cursor.pos - 1);
655 insertInset(new InsetQuotes(c, buffer()->params));
659 /* these functions are for the spellchecker */
660 char * BufferView::nextWord(float & value)
667 char * string = text->SelectNextWord(value);
673 void BufferView::selectLastWord()
675 if (!available()) return;
679 text->SelectSelectedWord();
680 toggleSelection(false);
685 void BufferView::endOfSpellCheck()
687 if (!available()) return;
691 text->SelectSelectedWord();
692 text->ClearSelection();
697 void BufferView::replaceWord(string const & replacestring)
699 if (!available()) return;
704 /* clear the selection (if there is any) */
705 toggleSelection(false);
708 /* clear the selection (if there is any) */
709 toggleSelection(false);
710 text->ReplaceSelectionWithString(replacestring.c_str());
712 text->SetSelectionOverString(replacestring.c_str());
714 // Go back so that replacement string is also spellchecked
715 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
716 text->CursorLeftIntern();
720 // End of spellchecker stuff
723 bool BufferView::lockInset(UpdatableInset * inset)
725 if (!the_locking_inset && inset) {
726 the_locking_inset = inset;
729 return the_locking_inset->LockInsetInInset(this, inset);
735 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
737 if (the_locking_inset && available()) {
738 LyXCursor cursor = text->cursor;
739 if ((cursor.pos - 1 >= 0) &&
740 (cursor.par->GetChar(cursor.pos-1) ==
741 LyXParagraph::META_INSET) &&
742 (cursor.par->GetInset(cursor.pos - 1) ==
743 the_locking_inset->GetLockingInset()))
744 text->SetCursor(cursor, cursor.par, cursor.pos-1);
745 y += cursor.y + the_locking_inset->InsetInInsetY();
746 pimpl_->screen->ShowManualCursor(x, y, asc, desc,
747 LyXScreen::BAR_SHAPE);
752 void BufferView::hideLockedInsetCursor()
754 if (the_locking_inset && available()) {
755 pimpl_->screen->HideCursor();
760 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
762 if (the_locking_inset && available()){
763 y += text->cursor.y + the_locking_inset->InsetInInsetY();
764 if (pimpl_->screen->FitManualCursor(x, y, asc, desc))
770 int BufferView::unlockInset(UpdatableInset * inset)
772 if (inset && the_locking_inset == inset) {
773 inset->InsetUnlock(this);
774 the_locking_inset = 0;
777 } else if (inset && the_locking_inset &&
778 the_locking_inset->UnlockInsetInInset(this, inset)) {
782 return bufferlist.unlockInset(inset);
786 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
788 if (!the_locking_inset)
789 return; // shouldn't happen
790 if (kind == Undo::EDIT) // in this case insets would not be stored!
794 ParFromPos(text->cursor.pos)->previous,
796 ParFromPos(text->cursor.pos)->next);
800 void BufferView::updateInset(Inset * inset, bool mark_dirty)
805 // first check for locking insets
806 if (the_locking_inset) {
807 if (the_locking_inset == inset) {
808 if (text->UpdateInset(inset)){
811 if (buffer()->isLyxClean())
812 owner()->getMiniBuffer()->
814 buffer()->markDirty();
819 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
820 if (text->UpdateInset(the_locking_inset)) {
823 if (buffer()->isLyxClean())
824 owner()->getMiniBuffer()->
826 buffer()->markDirty();
834 // then check the current buffer
838 if (text->UpdateInset(inset)){