]> git.lyx.org Git - lyx.git/blobdiff - src/BufferView_pimpl.C
Fixed cut&paste bugs and added freespacing for ERT Insets.
[lyx.git] / src / BufferView_pimpl.C
index 4cef48be677264074bc672c45050216313caa1e5..b7d44616bdab029e05c7dbc106a962d827b7b1ae 100644 (file)
@@ -14,6 +14,7 @@
 #include "lyxscreen.h"
 #include "lyxtext.h"
 #include "lyxrow.h"
+#include "paragraph.h"
 #include "LyXView.h"
 #include "commandtags.h"
 #include "lyxfunc.h"
@@ -28,6 +29,7 @@
 #include "support/LAssert.h"
 #include "support/lstrings.h"
 #include "frontends/Dialogs.h"
+#include "frontends/Alert.h"
 #include "insets/insetbib.h"
 #include "insets/insettext.h"
 /// added for Dispatch functions
@@ -66,8 +68,7 @@
 #include "undo_funcs.h"
 #include "mathed/formulabase.h"
 
-extern LyXTextClass::size_type current_layout;
-extern int greek_kb_flag;
+extern lyx::layout_type current_layout;
 
 using std::vector;
 using std::find_if;
@@ -77,6 +78,7 @@ using std::endl;
 using std::make_pair;
 using std::min;
 using SigC::slot;
+using lyx::pos_type;
 
 /* the selection possible is needed, that only motion events are 
  * used, where the bottom press event was on the drawing area too */
@@ -85,7 +87,6 @@ bool selection_possible = false;
 extern BufferList bufferlist;
 extern char ascii_type;
 
-extern bool math_insert_greek(BufferView *, char);
 extern void sigchldchecker(pid_t pid, int * status);
 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
 
@@ -295,9 +296,9 @@ int BufferView::Pimpl::resizeCurrentBuffer()
        Paragraph * selendpar = 0;
        UpdatableInset * the_locking_inset = 0;
        
-       Paragraph::size_type pos = 0;
-       Paragraph::size_type selstartpos = 0;
-       Paragraph::size_type selendpos = 0;
+       pos_type pos = 0;
+       pos_type selstartpos = 0;
+       pos_type selendpos = 0;
        bool selection = false;
        bool mark_set  = false;
 
@@ -368,10 +369,7 @@ int BufferView::Pimpl::resizeCurrentBuffer()
 
        /// clear the "Formatting Document" message 
        owner_->message("");
-       /// get rid of the splash screen if it's not gone already
-       owner_->getDialogs()->destroySplash();
+
        return 0;
 }
 
@@ -569,7 +567,7 @@ void BufferView::Pimpl::workAreaMotionNotify(int x, int y, unsigned int state)
 
 // Single-click on work area
 void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
-                                           unsigned int button)
+                                            unsigned int button)
 {
        if (!buffer_ || !screen_.get()) return;
 
@@ -594,9 +592,7 @@ void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
                   otherwise give the event to the inset */
                if (inset_hit == bv_->theLockingInset()) {
                        bv_->theLockingInset()->
-                               insetButtonPress(bv_,
-                                                xpos, ypos,
-                                                button);
+                               insetButtonPress(bv_,xpos, ypos,button);
                        return;
                } else {
                        bv_->unlockInset(bv_->theLockingInset());
@@ -629,8 +625,7 @@ void BufferView::Pimpl::workAreaButtonPress(int xpos, int ypos,
        updateScrollbar();
        
        // Single left click in math inset?
-       if ((inset_hit != 0) &&
-           (inset_hit->editable()==Inset::HIGHLY_EDITABLE)) {
+       if (isHighlyEditableInset(inset_hit)) {
                // Highly editable inset, like math
                UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
                selection_possible = false;
@@ -703,7 +698,7 @@ void BufferView::Pimpl::doubleClick(int /*x*/, int /*y*/, unsigned int button)
 void BufferView::Pimpl::tripleClick(int /*x*/, int /*y*/, unsigned int button)
 {
        // select a line
-       if (buffer_)
+       if (!buffer_)
                return;
 
        LyXText * text = bv_->getLyXText();
@@ -799,7 +794,7 @@ void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
 
                owner_->message(inset_hit->editMessage());
 
-               if (inset_hit->editable()==Inset::HIGHLY_EDITABLE) {
+               if (isHighlyEditableInset(inset_hit)) {
                        // Highly editable inset, like math
                        UpdatableInset *inset = (UpdatableInset *)inset_hit;
                        inset->insetButtonRelease(bv_, x, y, button);
@@ -810,6 +805,9 @@ void BufferView::Pimpl::workAreaButtonRelease(int x, int y,
                return;
        }
 
+#ifdef WITH_WARNINGS
+#warning variable c is set but never used. What is it good for?? (JMarc)
+#endif
        // check whether we want to open a float
        if (bv_->text) {
                bool hit = false;
@@ -860,9 +858,8 @@ Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
 
 
        if (cursor.pos() < cursor.par()->size()
-           && cursor.par()->getChar(cursor.pos()) == Paragraph::META_INSET
-           && cursor.par()->getInset(cursor.pos())
-           && cursor.par()->getInset(cursor.pos())->editable()) {
+           && cursor.par()->isInset(cursor.pos())
+           && isEditableInset(cursor.par()->getInset(cursor.pos()))) {
 
                // Check whether the inset really was hit
                Inset * tmpinset = cursor.par()->getInset(cursor.pos());
@@ -886,12 +883,11 @@ Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y,
        }
 
        if ((cursor.pos() - 1 >= 0) &&
-           (cursor.par()->getChar(cursor.pos()-1) == Paragraph::META_INSET) &&
-           (cursor.par()->getInset(cursor.pos() - 1)) &&
-           (cursor.par()->getInset(cursor.pos() - 1)->editable())) {
+           cursor.par()->isInset(cursor.pos() - 1) &&
+           isEditableInset(cursor.par()->getInset(cursor.pos() - 1))) {
                Inset * tmpinset = cursor.par()->getInset(cursor.pos()-1);
                LyXFont font = text->getFont(buffer_, cursor.par(),
-                                                 cursor.pos()-1);
+                                                 cursor.pos() - 1);
                int const width = tmpinset->width(bv_, font);
                int const inset_x = font.isVisibleRightToLeft()
                        ? cursor.x() : cursor.x() - width;
@@ -972,16 +968,22 @@ void BufferView::Pimpl::update()
        {
                LyXText::text_status st = bv_->text->status();
                screen_->update(bv_->text, bv_);
+               bool fitc = false;
                while(bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
                        if (bv_->text->fullRebreak(bv_)) {
                                st = LyXText::NEED_MORE_REFRESH;
                                bv_->text->setCursor(bv_, bv_->text->cursor.par(),
                                                                         bv_->text->cursor.pos());
-                               fitCursor();
+                               fitc = true;
                        }
                        bv_->text->status(bv_, st);
                        screen_->update(bv_->text, bv_);
                }
+               // do this here instead of in the screen::update because of
+               // the above loop!
+               bv_->text->status(bv_, LyXText::UNCHANGED);
+               if (fitc)
+                       fitCursor();
        }
 }
 
@@ -1035,7 +1037,7 @@ void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
 
        if (text->inset_owner) {
                text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
-           updateInset(text->inset_owner, true);
+           updateInset(text->inset_owner, false);
        } else {
            update();
        }
@@ -1211,12 +1213,7 @@ void BufferView::Pimpl::setState()
                return;
 
        LyXText * text = bv_->getLyXText();
-       if (text->real_current_font.isRightToLeft()
-#ifndef NO_LATEX
-           &&
-           text->real_current_font.latex() != LyXFont::ON
-#endif
-               ) {
+       if (text->real_current_font.isRightToLeft()) {
                if (owner_->getIntl()->keymap == Intl::PRIMARY)
                        owner_->getIntl()->KeyMapSec();
        } else {
@@ -1598,14 +1595,6 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                owner_->getDialogs()->setUserFreeFont();
                break;
 
-#ifndef NO_LATEX
-       case LFUN_TEX:
-               Tex(bv_);
-               setState();
-               owner_->showState();
-               break;
-#endif
-               
        case LFUN_FILE_INSERT:
        {
                MenuInsertLyXFile(argument);
@@ -1668,58 +1657,58 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
        break;
 
        case LFUN_LANGUAGE:
-               Lang(bv_, argument);
+               lang(bv_, argument);
                setState();
                owner_->showState();
                break;
 
        case LFUN_EMPH:
-               Emph(bv_);
+               emph(bv_);
                owner_->showState();
                break;
 
        case LFUN_BOLD:
-               Bold(bv_);
+               bold(bv_);
                owner_->showState();
                break;
                
        case LFUN_NOUN:
-               Noun(bv_);
+               noun(bv_);
                owner_->showState();
                break;
                
        case LFUN_CODE:
-               Code(bv_);
+               code(bv_);
                owner_->showState();
                break;
                
        case LFUN_SANS:
-               Sans(bv_);
+               sans(bv_);
                owner_->showState();
                break;
                
        case LFUN_ROMAN:
-               Roman(bv_);
+               roman(bv_);
                owner_->showState();
                break;
                
        case LFUN_DEFAULT:
-               StyleReset(bv_);
+               styleReset(bv_);
                owner_->showState();
                break;
                
        case LFUN_UNDERLINE:
-               Underline(bv_);
+               underline(bv_);
                owner_->showState();
                break;
                
        case LFUN_FONT_SIZE:
-               FontSize(bv_, argument);
+               fontSize(bv_, argument);
                owner_->showState();
                break;
                
        case LFUN_FONT_STATE:
-               owner_->getLyXFunc()->setMessage(CurrentState(bv_));
+               owner_->getLyXFunc()->setMessage(currentState(bv_));
                break;
                
        case LFUN_UPCASE_WORD:
@@ -1788,7 +1777,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
        case LFUN_INSERT_LABEL:
                MenuInsertLabel(bv_, argument);
                break;
-               
+
        case LFUN_REF_INSERT:
                if (argument.empty()) {
                        InsetCommandParams p("ref");
@@ -1828,7 +1817,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                if (!label.empty()) {
                        //bv_->savePosition(0);
                        if (!bv_->gotoLabel(label))
-                               WriteAlert(_("Error"), 
+                               Alert::alert(_("Error"), 
                                           _("Couldn't find this label"), 
                                           _("in current document."));
                }
@@ -1847,10 +1836,8 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                if (is_rtl)
                        lt->cursorLeft(bv_, false);
                if (lt->cursor.pos() < lt->cursor.par()->size()
-                   && lt->cursor.par()->getChar(lt->cursor.pos())
-                   == Paragraph::META_INSET
-                   && lt->cursor.par()->getInset(lt->cursor.pos())
-                   && lt->cursor.par()->getInset(lt->cursor.pos())->editable() == Inset::HIGHLY_EDITABLE){
+                   && lt->cursor.par()->isInset(lt->cursor.pos())
+                   && isHighlyEditableInset(lt->cursor.par()->getInset(lt->cursor.pos()))) {
                        Inset * tmpinset = lt->cursor.par()->getInset(lt->cursor.pos());
                        owner_->getLyXFunc()->setMessage(tmpinset->editMessage());
                        if (is_rtl)
@@ -1881,12 +1868,8 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                        lt->cursorLeft(bv_, false);
                if ((is_rtl || cur != lt->cursor) && // only if really moved!
                    lt->cursor.pos() < lt->cursor.par()->size() &&
-                   (lt->cursor.par()->getChar(lt->cursor.pos()) ==
-                    Paragraph::META_INSET) &&
-                   lt->cursor.par()->getInset(lt->cursor.pos()) &&
-                   (lt->cursor.par()->getInset(lt->cursor.pos())->editable()
-                    == Inset::HIGHLY_EDITABLE))
-               {
+                   lt->cursor.par()->isInset(lt->cursor.pos()) &&
+                   isHighlyEditableInset(lt->cursor.par()->getInset(lt->cursor.pos()))) {
                        Inset * tmpinset = lt->cursor.par()->getInset(lt->cursor.pos());
                        owner_->getLyXFunc()->setMessage(tmpinset->editMessage());
                        if (is_rtl)
@@ -2362,7 +2345,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                        // just comment out the line below...
                        showCursor();
                } else {
-                       bv_->cut();
+                       bv_->cut(false);
                }
                moveCursorUpdate(false);
                owner_->showState();
@@ -2393,6 +2376,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                                                 cursor.par()->params().pagebreakBottom(),
                                                 VSpace(VSpace::NONE), 
                                                 cursor.par()->params().spaceBottom(),
+                                                cursor.par()->params().spacing(), 
                                                 cursor.par()->params().align(), 
                                                 cursor.par()->params().labelWidthString(), 0);
                                        lt->cursorLeft(bv_);
@@ -2418,7 +2402,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                                       | BufferView::CHANGE);
                        }
                } else {
-                       bv_->cut();
+                       bv_->cut(false);
                }
        }
        break;
@@ -2505,7 +2489,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                                showCursor();
                        }
                } else {
-                       bv_->cut();
+                       bv_->cut(false);
                }
                owner_->showState();
                setState();
@@ -2530,6 +2514,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                                         cursor.par()->params().pagebreakTop(), 
                                         cursor.par()->params().pagebreakBottom(),
                                         VSpace(VSpace::NONE), cursor.par()->params().spaceBottom(),
+                                        cursor.par()->params().spacing(), 
                                         cursor.par()->params().align(), 
                                         cursor.par()->params().labelWidthString(), 0);
                                update(lt,
@@ -2545,7 +2530,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                                       | BufferView::CHANGE);
                        }
                } else
-                       bv_->cut();
+                       bv_->cut(false);
        }
        break;
 
@@ -2600,6 +2585,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                                         cursor.par()->params().pagebreakTop(), 
                                         cursor.par()->params().pagebreakBottom(),
                                         VSpace(VSpace::DEFSKIP), cursor.par()->params().spaceBottom(),
+                                        cursor.par()->params().spacing(), 
                                         cursor.par()->params().align(), 
                                         cursor.par()->params().labelWidthString(), 1);
                                //update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
@@ -2744,7 +2730,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                if (floatList.typeExist(argument)) {
                        insertAndEditInset(new InsetFloat(argument));
                } else {
-                       lyxerr << "Non-existant float type: "
+                       lyxerr << "Non-existent float type: "
                               << argument << endl;
                }
                break;
@@ -2760,7 +2746,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                        else
                                delete new_inset;
                } else {
-                       lyxerr << "Non-existant float type: "
+                       lyxerr << "Non-existent float type: "
                               << argument << endl;
                }
                
@@ -2815,7 +2801,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
 
        case LFUN_CHARATCURSOR:
        {
-               Paragraph::size_type pos = bv_->getLyXText()->cursor.pos();
+               pos_type pos = bv_->getLyXText()->cursor.pos();
                if (pos < bv_->getLyXText()->cursor.par()->size())
                        owner_->getLyXFunc()->setMessage(
                                tostr(bv_->getLyXText()->cursor.par()->getChar(pos)));
@@ -2859,18 +2845,6 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
        }
        break;
 
-       case LFUN_GETLATEX:
-       {
-#ifndef NO_LATEX
-               LyXFont & font = bv_->getLyXText()->current_font;
-                if (font.latex() == LyXFont::ON)
-                       owner_->getLyXFunc()->setMessage("L");
-                else
-#endif
-                       owner_->getLyXFunc()->setMessage("0");
-       }
-       break;
-
        // --- accented characters ---------------------------
                
        case LFUN_UMLAUT:
@@ -2932,6 +2906,10 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                mathDispatchMathMode(bv_, argument);
                break;
                
+       case LFUN_GREEK:                 // Insert a single greek letter
+               mathDispatchGreek(bv_, argument);
+               break;
+               
        case LFUN_CITATION_INSERT:
        {
                InsetCommandParams p;
@@ -2951,9 +2929,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                // The argument can be up to two tokens separated 
                // by a space. The first one is the bibstyle.
                string const db       = token(argument, ' ', 0);
-               string bibstyle = token(argument, ' ', 1);
-               if (bibstyle.empty())
-                       bibstyle = "plain";
+               string const bibstyle = token(argument, ' ', 1);
 
                InsetCommandParams p( "BibTeX", db, bibstyle );
                InsetBibtex * inset = new InsetBibtex(p);
@@ -3002,10 +2978,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                InsetCommandParams p("index");
                if (argument.empty()) {
                        string const idxstring(bv_->getLyXText()->getStringToIndex(bv_));
-                       if (!idxstring.empty()) 
-                               p.setContents(idxstring);
-                       else
-                               break;
+                       p.setContents(idxstring);
                } else {
                        p.setContents(argument);
                }
@@ -3094,13 +3067,14 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                string arg = argument;
 
                if (arg.empty()) {
-                       arg = bv_->getLyXText()->selectionAsString(buffer_);
+                       arg = bv_->getLyXText()->selectionAsString(buffer_,
+                                                                  false);
  
                        // FIXME
                        if (arg.size() > 100 || arg.empty()) {
                                // Get word or selection
                                bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
-                               arg = bv_->getLyXText()->selectionAsString(buffer_);
+                               arg = bv_->getLyXText()->selectionAsString(buffer_, false);
                                // FIXME: where is getLyXText()->unselect(bv_) ?
                        }
                }
@@ -3123,7 +3097,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                
                if (lyxrc.auto_region_delete) {
                        if (lt->selection.set()) {
-                               lt->cutSelection(bv_, false);
+                               lt->cutSelection(bv_, false, false);
                                bv_->update(lt,
                                            BufferView::SELECT
                                            | BufferView::FITCUR
@@ -3137,11 +3111,7 @@ bool BufferView::Pimpl::Dispatch(kb_action action, string const & argument)
                string::const_iterator cit = argument.begin();
                string::const_iterator end = argument.end();
                for (; cit != end; ++cit) {
-                       if (greek_kb_flag) {
-                               if (!math_insert_greek(bv_, *cit))
-                                       owner_->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
-                       } else
-                               owner_->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
+                       owner_->getIntl()->getTrans().TranslateAndInsert(*cit, lt);
                }
                
                bv_->update(lt,
@@ -3266,22 +3236,34 @@ void BufferView::Pimpl::specialChar(InsetSpecialChar::Kind kind)
                InsetSpecialChar * new_inset = 
                        new InsetSpecialChar(kind);
                insertInset(new_inset);
+               // Ok, what happens here if we are unable to insert
+               // the inset? Leak it?
        }
 }
 
 
 void BufferView::Pimpl::smartQuote()
 {
+       LyXText const * lt = bv_->getLyXText();
+       Paragraph const * par = lt->cursor.par();
+       pos_type pos = lt->cursor.pos();
        char c;
-       LyXText * lt = bv_->getLyXText();
 
-       if (lt->cursor.pos())
-               c = lt->cursor.par()->getChar(lt->cursor.pos() - 1);
-       else 
+       if (!pos
+           || (par->isInset(pos - 1)
+               && par->getInset(pos - 1)->isSpace()))
                c = ' ';
+       else
+               c = par->getChar(pos - 1);
+               
 
        hideCursor();
-       if (!insertInset(new InsetQuotes(c, bv_->buffer()->params)))
+
+       LyXLayout const & style = textclasslist.Style(
+               bv_->buffer()->params.textclass, par->getLayout());
+       
+       if (style.pass_thru ||
+               (!insertInset(new InsetQuotes(c, bv_->buffer()->params))))
                Dispatch(LFUN_SELFINSERT, "\"");
 }
 
@@ -3355,13 +3337,11 @@ bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
                bv_->text->setParagraph(bv_, 0, 0,
                                   0, 0,
                                   VSpace(VSpace::NONE), VSpace(VSpace::NONE),
+                                  Spacing(),
                                   LYX_ALIGN_LAYOUT, 
                                   string(),
                                   0);
                update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
-#ifndef NO_LATEX
-               bv_->text->current_font.setLatex(LyXFont::OFF);
-#endif
        }
        
        bv_->text->insertInset(bv_, inset);
@@ -3429,16 +3409,19 @@ void BufferView::Pimpl::gotoInset(vector<Inset::Code> const & codes,
        beforeChange(bv_->text);
        update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
        
+       LyXCursor const & cursor = bv_->text->cursor;
        string contents;
        if (same_content &&
-           bv_->text->cursor.par()->getChar(bv_->text->cursor.pos()) == Paragraph::META_INSET) {
-               Inset const * inset = bv_->text->cursor.par()->getInset(bv_->text->cursor.pos());
+           cursor.par()->isInset(cursor.pos())) {
+               Inset const * inset = cursor.par()->getInset(cursor.pos());
                if (find(codes.begin(), codes.end(), inset->lyxCode())
                    != codes.end())
                        contents =
                                static_cast<InsetCommand const *>(inset)->getContents();
        }
        
        if (!bv_->text->gotoNextInset(bv_, codes, contents)) {
                if (bv_->text->cursor.pos() 
                    || bv_->text->cursor.par() != bv_->text->firstParagraph()) {