]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView2.C
Dekels tabular/textinset patches
[lyx.git] / src / BufferView2.C
index f241071ed2787093c4bee704cabafa728a8c3d97..5381ebdfab718c3c911cd7c5484dcdb44aabfe89 100644 (file)
@@ -39,6 +39,7 @@ using std::endl;
 using std::ifstream;
 using std::vector;
 using std::find;
+using std::count;
 
 // Inserts a file into current document
 bool BufferView::insertLyXFile(string const & filen)
@@ -52,10 +53,10 @@ bool BufferView::insertLyXFile(string const & filen)
 {
        if (filen.empty()) return false;
 
-       string fname = MakeAbsPath(filen);
+       string const fname = MakeAbsPath(filen);
 
        // check if file exist
-       FileInfo fi(fname);
+       FileInfo const fi(fname);
 
        if (!fi.readable()) {
                WriteAlert(_("Error!"),
@@ -74,7 +75,7 @@ bool BufferView::insertLyXFile(string const & filen)
                return false;
        }
        
-       char c = ifs.peek();
+       char const c = ifs.peek();
        
        LyXLex lex(0, 0);
        lex.setStream(ifs);
@@ -93,6 +94,7 @@ bool BufferView::insertLyXFile(string const & filen)
        return res;
 }
 
+
 bool BufferView::removeAutoInsets()
 {
        LyXParagraph * par = buffer()->paragraph;
@@ -144,17 +146,19 @@ void BufferView::insertErrors(TeXErrors & terr)
        for (TeXErrors::Errors::const_iterator cit = terr.begin();
             cit != terr.end();
             ++cit) {
-               string desctext((*cit).error_desc);
-               string errortext((*cit).error_text);
-               string msgtxt = desctext + '\n' + errortext;
-               int errorrow = (*cit).error_in_line;
+               string const desctext((*cit).error_desc);
+               string const errortext((*cit).error_text);
+               string const msgtxt = desctext + '\n' + errortext;
+               int const errorrow = (*cit).error_in_line;
 
                // Insert error string for row number
                int tmpid = -1; 
                int tmppos = -1;
 
-               buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
-
+               if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
+                       buffer()->texrow.increasePos(tmpid, tmppos);
+               }
+               
                LyXParagraph * texrowpar = 0;
 
                if (tmpid == -1) {
@@ -195,28 +199,18 @@ void BufferView::setCursorFromRow(int row)
        text->SetCursor(this, texrowpar, tmppos);
 }
 
+
 bool BufferView::insertInset(Inset * inset, string const & lout,
-                        bool no_table)
+                            bool /*no_table*/)
 {
        // if we are in a locking inset we should try to insert the
        // inset there otherwise this is a illegal function now
-       if (the_locking_inset) {
-               if (the_locking_inset->InsertInsetAllowed(inset) &&
-                   the_locking_inset->InsertInset(this, inset))
-                       return true;
+       if (theLockingInset()) {
+               if (theLockingInset()->InsertInsetAllowed(inset))
+                   return theLockingInset()->InsertInset(this, inset);
                return false;
        }
 
-#ifndef NEW_TABULAR
-       // check for table/list in tables
-       if (no_table && text->cursor.par()->table){
-               WriteAlert(_("Impossible Operation!"),
-                          _("Cannot insert table/list in table."),
-                          _("Sorry."));
-               return false;
-       }
-#endif
-
        // not quite sure if we want this...
        text->SetCursorParUndo(buffer());
        text->FreezeUndo();
@@ -260,19 +254,6 @@ bool BufferView::insertInset(Inset * inset, string const & lout,
        }
        
        text->InsertInset(this, inset);
-#if 1
-       // if we enter a text-inset the cursor should be to the left side
-       // of it! This couldn't happen before as Undo was not handled inside
-       // inset now after the Undo LyX tries to call inset->Edit(...) again
-       // and cannot do this as the cursor is behind the inset and GetInset
-       // does not return the inset!
-       if (inset->IsTextInset()) {
-               if (text->cursor.par()->isRightToLeftPar(buffer()->params))
-                       text->CursorRight(this);
-               else
-                       text->CursorLeft(this);
-       }
-#endif
        update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
 
        text->UnFreezeUndo();
@@ -281,15 +262,19 @@ bool BufferView::insertInset(Inset * inset, string const & lout,
 
 
 // Open and lock an updatable inset
-bool BufferView::open_new_inset(UpdatableInset * new_inset)
+bool BufferView::open_new_inset(UpdatableInset * new_inset, bool behind)
 {
        beforeChange();
        text->FinishUndo();
-       if (!insertInset(new_inset))
+       if (!insertInset(new_inset)) {
+               delete new_inset;
                return false;
-       text->CursorLeft(this);
-       update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
-       new_inset->Edit(this, 0, 0, 0);
+       }
+       if (behind) {
+               LyXFont & font = getLyXText()->real_current_font;
+               new_inset->Edit(this, new_inset->width(this, font), 0, 0);
+       } else
+               new_inset->Edit(this, 0, 0, 0);
        return true;
 }
 
@@ -304,7 +289,7 @@ bool BufferView::gotoLabel(string const & label)
        for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
             it != buffer()->inset_iterator_end(); ++it) {
                vector<string> labels = (*it)->getLabelList();
-               if ( find(labels.begin(),labels.end(),label)
+               if (find(labels.begin(),labels.end(),label)
                     != labels.end()) {
                        beforeChange();
                        text->SetCursor(this, it.getPar(), it.getPos());
@@ -402,8 +387,7 @@ void BufferView::allFloats(char flag, char figmar)
 
        text->SetCursorIntern(this, cursor.par(), cursor.pos());
        redraw();
-       fitCursor();
-       //updateScrollbar();
+       fitCursor(text);
 }
 #endif
 
@@ -463,7 +447,7 @@ void BufferView::menuUndo()
 
 void BufferView::menuRedo()
 {
-       if (the_locking_inset) {
+       if (theLockingInset()) {
                owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
                return;
        }    
@@ -676,16 +660,14 @@ void BufferView::insertCorrectQuote()
 
 
 /* these functions are for the spellchecker */ 
-char * BufferView::nextWord(float & value)
+string const BufferView::nextWord(float & value)
 {
        if (!available()) {
                value = 1;
-               return 0;
+               return string();
        }
 
-       char * string = text->SelectNextWord(this, value);
-
-       return string;
+       return text->SelectNextWord(this, value);
 }
 
   
@@ -726,13 +708,13 @@ void BufferView::replaceWord(string const & replacestring)
    
        /* clear the selection (if there is any) */ 
        toggleSelection(false);
-       text->ReplaceSelectionWithString(this, replacestring.c_str());
+       text->ReplaceSelectionWithString(this, replacestring);
    
-       text->SetSelectionOverString(this, replacestring.c_str());
+       text->SetSelectionOverString(this, replacestring);
 
        // Go back so that replacement string is also spellchecked
        for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
-               text->CursorLeftIntern(this);
+               text->CursorLeft(this);
        }
        update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
 }
@@ -741,47 +723,58 @@ void BufferView::replaceWord(string const & replacestring)
 
 bool BufferView::lockInset(UpdatableInset * inset)
 {
-       if (!the_locking_inset && inset) {
-               the_locking_inset = inset;
+       if (!theLockingInset() && inset) {
+               theLockingInset(inset);
                return true;
        } else if (inset) {
-           return the_locking_inset->LockInsetInInset(this, inset);
+           return theLockingInset()->LockInsetInInset(this, inset);
        }
        return false;
 }
 
 
-void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
+void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
 {
-       if (the_locking_inset && available()) {
+       if (theLockingInset() && available()) {
                LyXCursor cursor = text->cursor;
                if ((cursor.pos() - 1 >= 0) &&
                    (cursor.par()->GetChar(cursor.pos() - 1) ==
                     LyXParagraph::META_INSET) &&
                    (cursor.par()->GetInset(cursor.pos() - 1) ==
-                    the_locking_inset->GetLockingInset()))
+                    theLockingInset()->GetLockingInset()))
                        text->SetCursor(this, cursor,
                                        cursor.par(), cursor.pos() - 1);
-               y += cursor.y() + the_locking_inset->InsetInInsetY();
+               LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
+               LyXText * txt = getLyXText();
+               if (theLockingInset()->GetLockingInset()->LyxCode() ==
+                   Inset::TEXT_CODE &&
+                   (txt->real_current_font.language() !=
+                    buffer()->params.language
+                    || txt->real_current_font.isVisibleRightToLeft()
+                    != buffer()->params.language->RightToLeft()))
+                       shape = (txt->real_current_font.isVisibleRightToLeft())
+                               ? LyXScreen::REVERSED_L_SHAPE
+                               : LyXScreen::L_SHAPE;
+               y += cursor.y() + theLockingInset()->InsetInInsetY();
                pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
-                                                 LyXScreen::BAR_SHAPE);
+                                                 shape);
        }
 }
 
 
 void BufferView::hideLockedInsetCursor()
 {
-       if (the_locking_inset && available()) {
+       if (theLockingInset() && available()) {
                pimpl_->screen_->HideCursor();
        }
 }
 
 
-void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
+void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
 {
-       if (the_locking_inset && available()){
-               y += text->cursor.y() + the_locking_inset->InsetInInsetY();
-               if (pimpl_->screen_->FitManualCursor(text, x, y, asc, desc))
+       if (theLockingInset() && available()) {
+               y += text->cursor.y() + theLockingInset()->InsetInInsetY();
+               if (pimpl_->screen_->FitManualCursor(text, this, x, y, asc, desc))
                        updateScrollbar();
        }
 }
@@ -789,13 +782,13 @@ void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
 
 int BufferView::unlockInset(UpdatableInset * inset)
 {
-       if (inset && the_locking_inset == inset) {
+       if (inset && theLockingInset() == inset) {
                inset->InsetUnlock(this);
-               the_locking_inset = 0;
+               theLockingInset(0);
                text->FinishUndo();
                return 0;
-       } else if (inset && the_locking_inset &&
-                  the_locking_inset->UnlockInsetInInset(this, inset)) {
+       } else if (inset && theLockingInset() &&
+                  theLockingInset()->UnlockInsetInInset(this, inset)) {
                text->FinishUndo();
                return 0;
        }
@@ -805,7 +798,7 @@ int BufferView::unlockInset(UpdatableInset * inset)
 
 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
 {
-       if (!the_locking_inset)
+       if (!theLockingInset())
                return; // shouldn't happen
        if (kind == Undo::EDIT) // in this case insets would not be stored!
                kind = Undo::FINISH;
@@ -829,11 +822,11 @@ void BufferView::updateInset(Inset * inset, bool mark_dirty)
                return;
 
        // first check for locking insets
-       if (the_locking_inset) {
-               if (the_locking_inset == inset) {
-                       if (text->UpdateInset(this, inset)){
+       if (theLockingInset()) {
+               if (theLockingInset() == inset) {
+                       if (text->UpdateInset(this, inset)) {
                                update();
-                               if (mark_dirty){
+                               if (mark_dirty) {
                                        if (buffer()->isLyxClean())
                                                owner()->getMiniBuffer()->
                                                        setTimer(4);
@@ -842,8 +835,8 @@ void BufferView::updateInset(Inset * inset, bool mark_dirty)
                                updateScrollbar();
                                return;
                        }
-               } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
-                       if (text->UpdateInset(this, the_locking_inset)) {
+               } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
+                       if (text->UpdateInset(this, theLockingInset())) {
                                update();
                                if (mark_dirty){
                                        if (buffer()->isLyxClean())
@@ -861,7 +854,7 @@ void BufferView::updateInset(Inset * inset, bool mark_dirty)
        if (available()) {
                hideCursor();
                update(BufferView::UPDATE);
-               if (text->UpdateInset(this, inset)){
+               if (text->UpdateInset(this, inset)) {
                        if (mark_dirty)
                                update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
                        else 
@@ -871,6 +864,7 @@ void BufferView::updateInset(Inset * inset, bool mark_dirty)
        }
 }
 
+
 bool BufferView::ChangeRefs(string const & from, string const & to)
 {
        bool flag = false;
@@ -917,3 +911,61 @@ bool BufferView::ChangeRefs(string const & from, string const & to)
        text->SetCursorIntern(this, cursor.par(), cursor.pos());
        return flag;
 }
+
+
+bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
+{
+       // Check if the label 'from' appears more than once
+       vector<string> labels = buffer()->getLabelList();
+       if (count(labels.begin(), labels.end(), from) > 1)
+               return false;
+
+       return ChangeRefs(from, to);
+}
+
+
+UpdatableInset * BufferView::theLockingInset() const
+{
+       // If NULL is not allowed we should put an Assert here. (Lgb)
+       if (text)
+               return text->the_locking_inset;
+       return 0;
+}
+
+
+void BufferView::theLockingInset(UpdatableInset * inset)
+{
+    text->the_locking_inset = inset;
+}
+
+
+LyXText * BufferView::getLyXText() const
+{
+       if (theLockingInset()) {
+               LyXText * txt = theLockingInset()->getLyXText(this);
+               if (txt)
+                       return txt;
+       }
+       return text;
+}
+
+
+LyXText * BufferView::getParentText(Inset * inset) const
+{
+       if (inset->owner()) {
+               LyXText * txt = inset->getLyXText(this);
+               inset = inset->owner();
+               while (inset && inset->getLyXText(this) == txt)
+                       inset = inset->owner();
+               if (inset)
+                       return inset->getLyXText(this);
+       }
+       return text;
+}
+
+Language const * BufferView::getParentLanguage(Inset * inset) const
+{
+       LyXText * text = getParentText(inset);
+       return text->cursor.par()->GetFontSettings(buffer()->params,
+                                                  text->cursor.pos()).language();
+}