]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettext.C
Some more fixes to selection, fitCursor calls for InsetText/Tabular.
[lyx.git] / src / insets / insettext.C
index 55f7759411c548b57b6a430dab227d735dfa3c11..3ceb4f48812088d2d5af000f6cfb8fbe54e070fe 100644 (file)
@@ -1,6 +1,6 @@
 /* This file is part of
  * ======================================================
- * 
+ *
  *           LyX, The Document Processor
  *
  *           Copyright 1998-2001 The LyX Team.
@@ -36,7 +36,6 @@
 #include "intl.h"
 #include "trans_mgr.h"
 #include "lyxscreen.h"
-#include "WorkArea.h"
 #include "gettext.h"
 #include "lyxfunc.h"
 #include "ParagraphParameters.h"
@@ -44,6 +43,7 @@
 #include "lyxfind.h"
 
 #include "frontends/Alert.h"
+#include "frontends/Dialogs.h"
 
 #include "support/textutils.h"
 #include "support/LAssert.h"
@@ -65,7 +65,6 @@ using std::vector;
 using std::pair;
 
 using lyx::pos_type;
-using lyx::layout_type;
 using lyx::textclass_type;
 
 extern unsigned char getCurrentTextClass(Buffer *);
@@ -84,7 +83,7 @@ void InsetText::saveLyXTextState(LyXText * t) const
                        break;
                p = p->next();
        }
-       
+
        if (p && t->cursor.pos() <= p->size()) {
                sstate.lpar = t->cursor.par();
                sstate.pos = t->cursor.pos();
@@ -112,10 +111,10 @@ void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
                t->selection.mark(sstate.mark_set);
                if (sstate.selection) {
                        t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
-                                    true, sstate.selstartboundary);
+                                    true, sstate.selstartboundary);
                        t->selection.cursor = t->cursor;
                        t->setCursor(bv, sstate.selendpar, sstate.selendpos,
-                                    true, sstate.selendboundary);
+                                    true, sstate.selendboundary);
                        t->setSelection(bv);
                        t->setCursor(bv, sstate.lpar, sstate.pos);
                } else {
@@ -136,11 +135,13 @@ InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
 }
 
 
-InsetText::InsetText()
+InsetText::InsetText(BufferParams const & bp)
        : UpdatableInset(), lt(0), in_update(false), do_resize(0),
          do_reinit(false)
 {
        par = new Paragraph;
+       par->layout(textclasslist[bp.textclass].defaultLayoutName());
+
        init();
 }
 
@@ -186,7 +187,7 @@ void InsetText::init(InsetText const * ins, bool same_id)
        insetDescent = 0;
        insetWidth = 0;
        old_max_width = 0;
-       no_selection = false;
+       no_selection = true;
        need_update = FULL;
        drawTextXOffset = 0;
        drawTextYOffset = 0;
@@ -206,7 +207,7 @@ InsetText::~InsetText()
        cached_bview = 0;
 
        // NOTE
-       
+
        while (par) {
                Paragraph * tmp = par->next();
                delete par;
@@ -217,12 +218,18 @@ InsetText::~InsetText()
 
 void InsetText::clear()
 {
+       // This is a gross hack...
+       string old_layout = par->layout();
+
        while (par) {
                Paragraph * tmp = par->next();
                delete par;
                par = tmp;
        }
        par = new Paragraph;
+       par->setInsetOwner(this);
+       par->layout(old_layout);
+
        reinitLyXText();
        need_update = INIT;
 }
@@ -230,7 +237,7 @@ void InsetText::clear()
 
 Inset * InsetText::clone(Buffer const &, bool same_id) const
 {
-       return  new InsetText(*this, same_id);
+       return new InsetText(*this, same_id);
 }
 
 
@@ -252,11 +259,11 @@ void InsetText::read(Buffer const * buf, LyXLex & lex)
        string token;
        int pos = 0;
        Paragraph * return_par = 0;
-       Paragraph::depth_type depth = 0; 
+       Paragraph::depth_type depth = 0;
        LyXFont font(LyXFont::ALL_INHERIT);
 
        clear();
-       
+
        while (lex.isOK()) {
                lex.nextToken();
                token = lex.getString();
@@ -268,7 +275,7 @@ void InsetText::read(Buffer const * buf, LyXLex & lex)
 #endif
                        break;
                }
-               
+
                if (const_cast<Buffer*>(buf)->
                        parseSingleLyXformat2Token(lex, par, return_par,
                                                   token, pos, depth, font)) {
@@ -284,7 +291,7 @@ void InsetText::read(Buffer const * buf, LyXLex & lex)
                return_par->setInsetOwner(this);
                return_par = return_par->next();
        }
-       
+
        if (token != "\\end_inset") {
                lex.printError("Missing \\end_inset at this point. "
                                           "Read: `$$Token'");
@@ -329,7 +336,7 @@ int InsetText::textWidth(BufferView * bv, bool fordraw) const
        }
        if (fordraw) {
                return max(w - (2 * TEXT_TO_INSET_OFFSET),
-                          (int)getLyXText(bv)->width);
+                          (int)getLyXText(bv)->width);
        } else if (w < 0) {
            return -1;
        }
@@ -338,13 +345,13 @@ int InsetText::textWidth(BufferView * bv, bool fordraw) const
 
 
 void InsetText::draw(BufferView * bv, LyXFont const & f,
-                     int baseline, float & x, bool cleared) const
+                    int baseline, float & x, bool cleared) const
 {
        if (nodraw())
                return;
 
        Painter & pain = bv->painter();
-       
+
        // this is the first thing we have to ask because if the x pos
        // changed we have to do a complete rebreak of the text as we
        // may have few space to draw in. Well we should check on this too
@@ -362,7 +369,7 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
        }
 
        // call these methods so that insetWidth, insetAscent and
-       // insetDescent have the right values. 
+       // insetDescent have the right values.
        width(bv, f);
        ascent(bv, f);
        descent(bv, f);
@@ -409,7 +416,7 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
        top_y = baseline - insetAscent;
 
        if (last_drawn_width != insetWidth) {
-               if (!cleared) 
+               if (!cleared)
                        clearInset(bv, baseline, cleared);
                need_update |= FULL;
                last_drawn_width = insetWidth;
@@ -423,7 +430,7 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
        if (!cleared && (need_update == CURSOR)
            && !getLyXText(bv)->selection.set()) {
                drawFrame(pain, cleared);
-               x += insetWidth; 
+               x += insetWidth;
                need_update = NONE;
                return;
        }
@@ -446,13 +453,13 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
        }
        if (y_offset < 0)
                y_offset = y;
-       lt->first = first;
+       lt->first_y = first;
        if (cleared || (need_update&(INIT|FULL))) {
                int yf = y_offset;
                y = 0;
                while ((row != 0) && (yf < ph)) {
                        lt->getVisibleRow(bv, y+y_offset, int(x), row,
-                                               y+first, cleared);
+                                               y+first, cleared);
                        y += row->height();
                        yf += row->height();
                        row = row->next();
@@ -479,15 +486,17 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
 
        lt->refresh_y = 0;
        lt->status(bv, LyXText::UNCHANGED);
-       if ((need_update != CURSOR_PAR) &&
-           ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked))) {
+       if ((drawFrame_ == ALWAYS) ||
+               ((cleared || (need_update != CURSOR_PAR)) &&
+                (drawFrame_ == LOCKED) && locked))
+       {
                drawFrame(pain, cleared);
        } else if (need_update & CLEAR_FRAME) {
                clearFrame(pain, cleared);
        }
-       
+
        x += insetWidth - TEXT_TO_INSET_OFFSET;
-       
+
        if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
                need_update |= FULL;
        } else if (need_update != INIT) {
@@ -507,7 +516,7 @@ void InsetText::drawFrame(Painter & pain, bool cleared) const
                frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
                frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
                pain.rectangle(frame_x, frame_y, frame_w, frame_h,
-                              frame_color);
+                              frame_color);
                frame_is_visible = true;
        }
 }
@@ -518,7 +527,7 @@ void InsetText::clearFrame(Painter & pain, bool cleared) const
        if (frame_is_visible) {
                if (!cleared) {
                        pain.rectangle(frame_x, frame_y, frame_w, frame_h,
-                                      backgroundColor());
+                                      backgroundColor());
                }
                frame_is_visible = false;
        }
@@ -544,6 +553,10 @@ void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
                in_update = false;
                return;
        }
+
+       if (!autoBreakRows && par->next())
+               collapseParagraphs(bv->buffer()->params);
+
        if (the_locking_inset) {
                inset_x = cx(bv) - top_x + drawTextXOffset;
                inset_y = cy(bv) + drawTextYOffset;
@@ -572,7 +585,7 @@ void InsetText::setUpdateStatus(BufferView * bv, int what) const
 {
        // this does nothing dangerous so use only a localized buffer
        LyXText * llt = getLyXText(bv);
-       
+
        need_update |= what;
        // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
        // if we don't break row so that we only have one row to update!
@@ -605,22 +618,25 @@ void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
        }
        lt->fullRebreak(bv);
        setUpdateStatus(bv, what);
-       bool flag = (((need_update != CURSOR) && (need_update != NONE)) ||
-                    (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
+       bool flag = mark_dirty ||
+               (((need_update != CURSOR) && (need_update != NONE)) ||
+                (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
+       if (!lt->selection.set())
+               lt->selection.cursor = lt->cursor;
        if (clear)
                lt = 0;
+       if (locked && (need_update & CURSOR) && bv->fitCursor())
+               need_update |= FULL;
        if (flag)
                bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
-       else
-               bv->fitCursor();
-       
+
        if (need_update == CURSOR)
                need_update = NONE;
        bv->owner()->showState();
        bv->owner()->updateMenubar();
        bv->owner()->updateToolbar();
        if (old_par != cpar(bv)) {
-               bv->owner()->setLayout(cpar(bv)->getLayout());
+               bv->owner()->setLayout(cpar(bv)->layout());
                old_par = cpar(bv);
        }
 }
@@ -635,7 +651,7 @@ string const InsetText::editMessage() const
 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
 {
        UpdatableInset::edit(bv, x, y, button);
-       
+
        if (!bv->lockInset(this)) {
                lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
                return;
@@ -654,7 +670,7 @@ void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
        }
        if (!checkAndActivateInset(bv, x, tmp_y, button))
                lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
-                                           y + insetAscent);
+                                           y + insetAscent);
        lt->clearSelection();
        finishUndo();
        // If the inset is empty set the language of the current font to the
@@ -666,21 +682,24 @@ void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
                font.setLanguage(bv->getParentLanguage(this));
                setFont(bv, font, false);
        }
-       showInsetCursor(bv);
        if (clear)
                lt = 0;
-       
+
        int code = CURSOR;
        if (drawFrame_ == LOCKED)
                code = CURSOR|DRAW_FRAME;
        updateLocal(bv, code, false);
+       showInsetCursor(bv);
+
+       // Tell the paragraph dialog that we've entered an insettext.
+       bv->owner()->getDialogs()->updateParagraph();
 }
 
 
 void InsetText::edit(BufferView * bv, bool front)
 {
        UpdatableInset::edit(bv, front);
-       
+
        if (!bv->lockInset(this)) {
                lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
                return;
@@ -715,13 +734,13 @@ void InsetText::edit(BufferView * bv, bool front)
                font.setLanguage(bv->getParentLanguage(this));
                setFont(bv, font, false);
        }
-       showInsetCursor(bv);
        if (clear)
                lt = 0;
        int code = CURSOR;
        if (drawFrame_ == LOCKED)
                code = CURSOR|DRAW_FRAME;
        updateLocal(bv, code, false);
+       showInsetCursor(bv);
 }
 
 
@@ -732,12 +751,12 @@ void InsetText::insetUnlock(BufferView * bv)
                the_locking_inset = 0;
        }
        hideInsetCursor(bv);
-       no_selection = false;
+       no_selection = true;
        locked = false;
        int code;
        if (drawFrame_ == LOCKED)
                code = CURSOR|CLEAR_FRAME;
-       else 
+       else
                code = CURSOR;
        bool clear = false;
        if (!lt) {
@@ -749,9 +768,9 @@ void InsetText::insetUnlock(BufferView * bv)
                code = FULL;
        } else if (owner()) {
                bv->owner()->setLayout(owner()->getLyXText(bv)
-                                      ->cursor.par()->getLayout());
+                                      ->cursor.par()->layout());
        } else
-               bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
+               bv->owner()->setLayout(bv->text->cursor.par()->layout());
        // hack for deleteEmptyParMech
        if (par->size()) {
                lt->setCursor(bv, par, 0);
@@ -827,7 +846,7 @@ bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
 
 
 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
-                                   bool lr)
+                                  bool lr)
 {
        if (!the_locking_inset)
                return false;
@@ -859,20 +878,27 @@ bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
                lt = getLyXText(bv);
                clear = true;
        }
-       if (!the_locking_inset) {
-               bool found = lt->updateInset(bv, inset);
-               if (clear)
-                       lt = 0;
+       if (inset->owner() != this) {
+               int ustat = CURSOR_PAR;
+               bool found = false;
+               UpdatableInset * tl_inset = the_locking_inset;
+               if (tl_inset)
+                       found = tl_inset->updateInsetInInset(bv, inset);
+               if (!found) {
+                       tl_inset = static_cast<UpdatableInset *>(inset);
+                       while(tl_inset->owner() && tl_inset->owner() != this)
+                               tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
+                       if (!tl_inset->owner())
+                               return false;
+                       found = tl_inset->updateInsetInInset(bv, inset);
+                       ustat = FULL;
+               }
                if (found)
-                       setUpdateStatus(bv, NONE);
-               return found;
-       }
-       if (the_locking_inset != inset) {
-               bool found = the_locking_inset->updateInsetInInset(bv, inset);
+                       lt->updateInset(bv, tl_inset);
                if (clear)
                        lt = 0;
                if (found)
-                       setUpdateStatus(bv, CURSOR_PAR);
+                       setUpdateStatus(bv, ustat);
                return found;
        }
        bool found = lt->updateInset(bv, inset);
@@ -880,7 +906,9 @@ bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
                lt = 0;
        if (found) {
                setUpdateStatus(bv, CURSOR_PAR);
-               if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
+               if (the_locking_inset &&
+                   cpar(bv) == inset_par && cpos(bv) == inset_pos)
+               {
                        inset_x = cx(bv) - top_x + drawTextXOffset;
                        inset_y = cy(bv) + drawTextYOffset;
                }
@@ -898,17 +926,16 @@ void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
        mouse_y = y;
 
        int tmp_x = x - drawTextXOffset;
-       int tmp_y = y + insetAscent - getLyXText(bv)->first;
+       int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
        Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
 
        hideInsetCursor(bv);
        if (the_locking_inset) {
                if (the_locking_inset == inset) {
                        the_locking_inset->insetButtonPress(bv,
-                                                           x - inset_x,
-                                                           y - inset_y,
-                                                           button);
-                       no_selection = false;
+                                                           x - inset_x,
+                                                           y - inset_y,
+                                                           button);
                        return;
                } else if (inset) {
                        // otherwise unlock the_locking_inset and lock the new inset
@@ -917,17 +944,19 @@ void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
                        inset_y = cy(bv) + drawTextYOffset;
                        the_locking_inset = 0;
                        inset->insetButtonPress(bv, x - inset_x,
-                                               y - inset_y, button);
-                       inset->edit(bv, x - inset_x, y - inset_y, button);
+                                               y - inset_y, button);
+//                     inset->edit(bv, x - inset_x, y - inset_y, button);
                        if (the_locking_inset)
                                updateLocal(bv, CURSOR, false);
-                       no_selection = false;
                        return;
                }
                // otherwise only unlock the_locking_inset
                the_locking_inset->insetUnlock(bv);
                the_locking_inset = 0;
        }
+       if (!inset)
+               no_selection = false;
+
        if (bv->theLockingInset()) {
                if (isHighlyEditableInset(inset)) {
                        UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
@@ -938,11 +967,10 @@ void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
                        inset_boundary = cboundary(bv);
                        the_locking_inset = uinset;
                        uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
-                                                button);
+                                                button);
                        uinset->edit(bv, x - inset_x, y - inset_y, 0);
                        if (the_locking_inset)
                                updateLocal(bv, CURSOR, false);
-                       no_selection = false;
                        return;
                }
        }
@@ -957,9 +985,10 @@ void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
                        lt = getLyXText(bv);
                        clear = true;
                }
+               int old_first_y = lt->first_y;
 
                lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
-                                            y + insetAscent);
+                                            y + insetAscent);
                // set the selection cursor!
                lt->selection.cursor = lt->cursor;
                lt->cursor.x_fix(lt->cursor.x());
@@ -975,7 +1004,10 @@ void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
                                lt = 0;
                        updateLocal(bv, CURSOR, false);
                }
-               bv->owner()->setLayout(cpar(bv)->getLayout());
+               bv->owner()->setLayout(cpar(bv)->layout());
+               // we moved the view we cannot do mouse selection in this case!
+               if (getLyXText(bv)->first_y != old_first_y)
+                       no_selection = true;
                old_par = cpar(bv);
                // Insert primary selection with middle mouse
                // if there is a local selection in the current buffer,
@@ -985,38 +1017,38 @@ void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
                                localDispatch(bv, LFUN_PASTE, "");
                        else
                                localDispatch(bv, LFUN_PASTESELECTION,
-                                             "paragraph");
+                                             "paragraph");
                }
        } else {
                getLyXText(bv)->clearSelection();
        }
        showInsetCursor(bv);
-       no_selection = false;
 }
 
 
 bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
 {
+       no_selection = true;
        if (the_locking_inset) {
                return the_locking_inset->insetButtonRelease(bv,
-                                                            x - inset_x, y - inset_y,
-                                                            button);
+                                                            x - inset_x, y - inset_y,
+                                                            button);
        }
        int tmp_x = x - drawTextXOffset;
-       int tmp_y = y + insetAscent - getLyXText(bv)->first;
+       int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
        Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
        bool ret = false;
        if (inset) {
                if (isHighlyEditableInset(inset)) {
                        ret = inset->insetButtonRelease(bv, x - inset_x,
-                                                       y - inset_y, button);
+                                                       y - inset_y, button);
                } else {
                        inset_x = cx(bv) - top_x + drawTextXOffset;
                        inset_y = cy(bv) + drawTextYOffset;
                        ret = inset->insetButtonRelease(bv, x - inset_x,
-                                                       y - inset_y, button);
+                                                       y - inset_y, button);
                        inset->edit(bv, x - inset_x,
-                                   y - inset_y, button);
+                                   y - inset_y, button);
                }
                updateLocal(bv, CURSOR_PAR, false);
        }
@@ -1026,13 +1058,15 @@ bool InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
 
 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
 {
-       if (no_selection || ((mouse_x == x) && (mouse_y == y)))
-               return;
        if (the_locking_inset) {
                the_locking_inset->insetMotionNotify(bv, x - inset_x,
-                                                    y - inset_y,state);
+                                                    y - inset_y,state);
                return;
        }
+
+       if (no_selection || ((mouse_x == x) && (mouse_y == y)))
+               return;
+
        bool clear = false;
        if (!lt) {
                lt = getLyXText(bv);
@@ -1069,7 +1103,7 @@ void InsetText::insetKeyPress(XKeyEvent * xke)
 
 UpdatableInset::RESULT
 InsetText::localDispatch(BufferView * bv,
-                         kb_action action, string const & arg)
+                        kb_action action, string const & arg)
 {
        bool was_empty = par->size() == 0 && !par->next();
        no_selection = false;
@@ -1116,6 +1150,7 @@ InsetText::localDispatch(BufferView * bv,
 #ifdef WITH_WARNINGS
 #warning I changed this to always return Dispatched maybe it is wrong (20011001 Jug)
 #endif
+                       updateLocal(bv, CURSOR, false);
                        return result;
                }
        }
@@ -1142,7 +1177,7 @@ InsetText::localDispatch(BufferView * bv,
                         * true (on). */
 
                        setUndo(bv, Undo::INSERT,
-                               lt->cursor.par(), lt->cursor.par()->next());
+                               lt->cursor.par(), lt->cursor.par()->next());
                        bv->setState();
                        if (lyxrc.auto_region_delete) {
                                if (lt->selection.set()) {
@@ -1215,7 +1250,7 @@ InsetText::localDispatch(BufferView * bv,
                break;
        case LFUN_BACKSPACE: {
                setUndo(bv, Undo::DELETE,
-                       lt->cursor.par(), lt->cursor.par()->next());
+                       lt->cursor.par(), lt->cursor.par()->next());
                if (lt->selection.set())
                        lt->cutSelection(bv);
                else
@@ -1224,10 +1259,10 @@ InsetText::localDispatch(BufferView * bv,
                updflag = true;
        }
        break;
-       
+
        case LFUN_DELETE: {
                setUndo(bv, Undo::DELETE,
-                       lt->cursor.par(), lt->cursor.par()->next());
+                       lt->cursor.par(), lt->cursor.par()->next());
                if (lt->selection.set()) {
                        lt->cutSelection(bv);
                } else {
@@ -1237,10 +1272,10 @@ InsetText::localDispatch(BufferView * bv,
                updflag = true;
        }
        break;
-       
+
        case LFUN_CUT: {
                setUndo(bv, Undo::DELETE,
-                       lt->cursor.par(), lt->cursor.par()->next());
+                       lt->cursor.par(), lt->cursor.par()->next());
                lt->cutSelection(bv);
                updwhat = CURSOR_PAR;
                updflag = true;
@@ -1255,7 +1290,7 @@ InsetText::localDispatch(BufferView * bv,
        case LFUN_PASTESELECTION:
        {
                string const clip(bv->getClipboard());
-       
+
                if (clip.empty())
                        break;
                if (arg == "paragraph") {
@@ -1278,7 +1313,7 @@ InsetText::localDispatch(BufferView * bv,
                        }
                }
                setUndo(bv, Undo::INSERT,
-                       lt->cursor.par(), lt->cursor.par()->next());
+                       lt->cursor.par(), lt->cursor.par()->next());
                lt->pasteSelection(bv);
                updwhat = CURSOR_PAR;
                updflag = true;
@@ -1291,7 +1326,7 @@ InsetText::localDispatch(BufferView * bv,
                        break;
                }
                lt->breakParagraph(bv, 0);
-               updwhat = FULL;
+               updwhat = CURSOR | FULL;
                updflag = true;
                break;
        case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
@@ -1300,7 +1335,7 @@ InsetText::localDispatch(BufferView * bv,
                        break;
                }
                lt->breakParagraph(bv, 1);
-               updwhat = FULL;
+               updwhat = CURSOR | FULL;
                updflag = true;
                break;
 
@@ -1310,9 +1345,9 @@ InsetText::localDispatch(BufferView * bv,
                        break;
                }
                setUndo(bv, Undo::INSERT,
-                       lt->cursor.par(), lt->cursor.par()->next());
+                       lt->cursor.par(), lt->cursor.par()->next());
                lt->insertChar(bv, Paragraph::META_NEWLINE);
-               updwhat = CURSOR_PAR;
+               updwhat = CURSOR | CURSOR_PAR;
                updflag = true;
        }
        break;
@@ -1320,39 +1355,40 @@ InsetText::localDispatch(BufferView * bv,
        case LFUN_LAYOUT:
                // do not set layouts on non breakable textinsets
                if (autoBreakRows) {
-                       layout_type cur_layout = cpar(bv)->layout;
-         
+                       string cur_layout = cpar(bv)->layout();
+
                        // Derive layout number from given argument (string)
-                       // and current buffer's textclass (number). */    
+                       // and current buffer's textclass (number). */
                        textclass_type tclass = bv->buffer()->params.textclass;
-                       pair<bool, layout_type> layout = 
-                               textclasslist.NumberOfLayout(tclass, arg);
+                       string layout = arg;
+                       bool hasLayout = textclasslist[tclass].hasLayout(layout);
 
                        // If the entry is obsolete, use the new one instead.
-                       if (layout.first) {
-                               string obs = textclasslist.Style(tclass,layout.second).
+                       if (hasLayout) {
+                               string const & obs =
+                                       textclasslist[tclass][layout].
                                        obsoleted_by();
-                               if (!obs.empty()) 
-                                       layout = textclasslist.NumberOfLayout(tclass, obs);
+                               if (!obs.empty())
+                                       layout = obs;
                        }
 
                        // see if we found the layout number:
-                       if (!layout.first) {
+                       if (!hasLayout) {
                                string const msg = string(N_("Layout ")) + arg + N_(" not known");
                                bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
                                break;
                        }
 
-                       if (cur_layout != layout.second) {
-                               cur_layout = layout.second;
-                               lt->setLayout(bv, layout.second);
-                               bv->owner()->setLayout(cpar(bv)->getLayout());
+                       if (cur_layout != layout) {
+                               cur_layout = layout;
+                               lt->setLayout(bv, layout);
+                               bv->owner()->setLayout(cpar(bv)->layout());
                                updwhat = CURSOR_PAR;
                                updflag = true;
                        }
                } else {
                        // reset the layout box
-                       bv->owner()->setLayout(cpar(bv)->getLayout());
+                       bv->owner()->setLayout(cpar(bv)->layout());
                }
                break;
        case LFUN_PARAGRAPH_SPACING:
@@ -1366,7 +1402,7 @@ InsetText::localDispatch(BufferView * bv,
                if (cur_spacing == Spacing::Other) {
                        cur_value = par->params().spacing().getValue();
                }
-                               
+
                istringstream istr(arg.c_str());
                string tmp;
                istr >> tmp;
@@ -1401,7 +1437,7 @@ InsetText::localDispatch(BufferView * bv,
                }
        }
        break;
-       
+
        default:
                if (!bv->Dispatch(action, arg))
                        result = UNDISPATCHED;
@@ -1420,10 +1456,11 @@ InsetText::localDispatch(BufferView * bv,
                setFont(bv, font, false);
        }
 
-       if (result < FINISHED) {
-               showInsetCursor(bv);
-       } else
+       if (result >= FINISHED)
                bv->unlockInset(this);
+
+       if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
+               result = DISPATCHED;
        return result;
 }
 
@@ -1440,7 +1477,7 @@ int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
 {
        Paragraph * p = par;
        unsigned int lines = 0;
-       
+
        while (p) {
                string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
                lines += lyx::count(tmp.begin(), tmp.end(), '\n');
@@ -1458,10 +1495,10 @@ int InsetText::docbook(Buffer const * buf, ostream & os) const
 
        vector<string> environment_stack(10);
        vector<string> environment_inner(10);
-       
+
        int const command_depth = 0;
        string item_name;
-       
+
        Paragraph::depth_type depth = 0; // paragraph depth
 
        while (p) {
@@ -1469,8 +1506,7 @@ int InsetText::docbook(Buffer const * buf, ostream & os) const
                int desc_on = 0; // description mode
 
                LyXLayout const & style =
-                       textclasslist.Style(buf->params.textclass,
-                                           p->layout);
+                       textclasslist[buf->params.textclass][p->layout()];
 
                // environment tag closing
                for (; depth > p->params().depth(); --depth) {
@@ -1500,13 +1536,13 @@ int InsetText::docbook(Buffer const * buf, ostream & os) const
                                                          depth + command_depth,
                                                          environment_inner[depth]);
                        }
-                       
+
                        buf->sgmlCloseTag(os, depth + command_depth,
                                          environment_stack[depth]);
-                       
+
                        environment_stack[depth].erase();
                        environment_inner[depth].erase();
-                }
+               }
 
                // Write opening SGML tags.
                switch (style.latextype) {
@@ -1549,7 +1585,7 @@ int InsetText::docbook(Buffer const * buf, ostream & os) const
                                                                  environment_inner[depth]);
                                }
                        }
-                       
+
                        if (style.latextype == LATEX_ENVIRONMENT) {
                                if (!style.latexparam().empty()) {
                                        if (style.latexparam() == "CDATA")
@@ -1624,16 +1660,16 @@ int InsetText::docbook(Buffer const * buf, ostream & os) const
                                item_name = "listitem";
                                buf->sgmlCloseTag(os, command_depth + depth,
                                                  item_name);
-                               if (environment_inner[depth] == "varlistentry")
+                              if (environment_inner[depth] == "varlistentry")
                                       buf->sgmlCloseTag(os, depth + command_depth,
                                                         environment_inner[depth]);
                        }
-                       
+
                        buf->sgmlCloseTag(os, depth + command_depth,
                                          environment_stack[depth]);
                }
        }
-       
+
        return lines;
 }
 
@@ -1650,8 +1686,7 @@ void InsetText::validate(LaTeXFeatures & features) const
 
 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
 {
-       if (textclasslist.Style(buf->params.textclass,
-                               p->getLayout()).labeltype != LABEL_MANUAL)
+       if (textclasslist[buf->params.textclass][p->layout()].labeltype != LABEL_MANUAL)
                return 0;
        else
                return p->beginningOfMainBody();
@@ -1690,7 +1725,7 @@ void InsetText::toggleInsetCursor(BufferView * bv)
 
        int const asc = lyxfont::maxAscent(font);
        int const desc = lyxfont::maxDescent(font);
-  
+
        if (isCursorVisible())
                bv->hideLockedInsetCursor();
        else
@@ -1708,7 +1743,7 @@ void InsetText::showInsetCursor(BufferView * bv, bool show)
        if (!isCursorVisible()) {
                LyXFont const font =
                        getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
-       
+
                int const asc = lyxfont::maxAscent(font);
                int const desc = lyxfont::maxDescent(font);
 
@@ -1739,11 +1774,12 @@ void InsetText::fitInsetCursor(BufferView * bv) const
        }
        LyXFont const font =
                getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
-       
+
        int const asc = lyxfont::maxAscent(font);
        int const desc = lyxfont::maxDescent(font);
 
-       bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
+       if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
+               need_update |= FULL;
 }
 
 
@@ -1768,8 +1804,8 @@ InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
 
 
 UpdatableInset::RESULT
-InsetText::moveRightIntern(BufferView * bv, bool behind, 
-                           bool activate_inset, bool selecting)
+InsetText::moveRightIntern(BufferView * bv, bool behind,
+                          bool activate_inset, bool selecting)
 {
        if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
                return FINISHED_RIGHT;
@@ -1784,7 +1820,7 @@ InsetText::moveRightIntern(BufferView * bv, bool behind,
 
 UpdatableInset::RESULT
 InsetText::moveLeftIntern(BufferView * bv, bool behind,
-                          bool activate_inset, bool selecting)
+                         bool activate_inset, bool selecting)
 {
        if (!cpar(bv)->previous() && (cpos(bv) <= 0))
                return FINISHED;
@@ -1887,7 +1923,7 @@ bool InsetText::showInsetDialog(BufferView * bv) const
 }
 
 
-vector<string> const InsetText::getLabelList() const 
+vector<string> const InsetText::getLabelList() const
 {
        vector<string> label_list;
 
@@ -1906,7 +1942,7 @@ vector<string> const InsetText::getLabelList() const
 
 
 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
-                        bool selectall)
+                       bool selectall)
 {
        if (the_locking_inset) {
                the_locking_inset->setFont(bv, font, toggleall, selectall);
@@ -2039,12 +2075,12 @@ void InsetText::setParagraphData(Paragraph * p, bool same_id)
 }
 
 
-void InsetText::setText(string const & data)
+void InsetText::setText(string const & data, LyXFont const & font)
 {
        clear();
-       LyXFont font(LyXFont::ALL_SANE);
        for (unsigned int i=0; i < data.length(); ++i)
                par->insertChar(i, data[i], font);
+       reinitLyXText();
 }
 
 
@@ -2086,7 +2122,7 @@ int InsetText::cx(BufferView * bv) const
        int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
        if (the_locking_inset) {
                LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
-                                           llt->cursor.pos());
+                                           llt->cursor.pos());
                if (font.isVisibleRightToLeft())
                        x -= the_locking_inset->width(bv, font);
        }
@@ -2126,17 +2162,18 @@ Row * InsetText::crow(BufferView * bv) const
 
 
 LyXText * InsetText::getLyXText(BufferView const * lbv,
-                                bool const recursive) const
+                               bool const recursive) const
 {
-       if (!recursive && (cached_bview == lbv)) {
+       if (cached_bview == lbv) {
+               if (recursive && the_locking_inset)
+                       return the_locking_inset->getLyXText(lbv, true);
                LyXText * lt = cached_text.get();
                lyx::Assert(lt && lt->firstRow()->par() == par);
                return lt;
        }
-       
        // Super UGLY! (Lgb)
        BufferView * bv = const_cast<BufferView *>(lbv);
-       
+
        cached_bview = bv;
        Cache::iterator it = cache.find(bv);
 
@@ -2181,7 +2218,7 @@ LyXText * InsetText::getLyXText(BufferView const * lbv,
        restoreLyXTextState(bv, cached_text.get());
 
        cache.insert(make_pair(bv, cached_text));
-       
+
        if (the_locking_inset && recursive) {
                return the_locking_inset->getLyXText(bv);
        }
@@ -2194,7 +2231,7 @@ void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
        cached_bview = 0;
 
        Cache::iterator it = cache.find(bv);
-       
+
        if (it == cache.end()) {
                return;
        }
@@ -2253,11 +2290,11 @@ void InsetText::resizeLyXText(BufferView * bv, bool force) const
        }
 
        if (bv->screen()) {
-               t->first = bv->screen()->topCursorVisible(t);
+               t->first_y = bv->screen()->topCursorVisible(t);
        }
        if (!owner()) {
                updateLocal(bv, FULL, false);
-               // this will scroll the screen such that the cursor becomes visible 
+               // this will scroll the screen such that the cursor becomes visible
                bv->updateScrollbar();
        } else {
                need_update |= FULL;
@@ -2293,11 +2330,11 @@ void InsetText::reinitLyXText() const
                        inset_y = cy(bv) + drawTextYOffset;
                }
                if (bv->screen()) {
-                       t->first = bv->screen()->topCursorVisible(t);
+                       t->first_y = bv->screen()->topCursorVisible(t);
                }
                if (!owner()) {
                        updateLocal(bv, FULL, false);
-                       // this will scroll the screen such that the cursor becomes visible 
+                       // this will scroll the screen such that the cursor becomes visible
                        bv->updateScrollbar();
                } else {
                        need_update = FULL;
@@ -2309,7 +2346,7 @@ void InsetText::reinitLyXText() const
 void InsetText::removeNewlines()
 {
        bool changed = false;
-       
+
        for (Paragraph * p = par; p; p = p->next()) {
                for (int i = 0; i < p->size(); ++i) {
                        if (p->getChar(i) == Paragraph::META_NEWLINE) {
@@ -2369,7 +2406,7 @@ void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
        int w = insetWidth;
        int h = insetAscent + insetDescent;
        int ty = baseline - insetAscent;
-       
+
        if (ty < 0) {
                h += ty;
                ty = 0;
@@ -2552,7 +2589,7 @@ void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
        }
        if (y_offset < 0)
                y_offset = y;
-       
+
        if (need_update & SELECTION)
                need_update = NONE;
        bv->screen()->toggleSelection(lt, bv, kill_selection, y_offset, x);
@@ -2562,7 +2599,7 @@ void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
 
 
 bool InsetText::searchForward(BufferView * bv, string const & str,
-                              bool cs, bool mw)
+                             bool cs, bool mw)
 {
        if (the_locking_inset) {
                if (the_locking_inset->searchForward(bv, str, cs, mw))
@@ -2600,7 +2637,7 @@ bool InsetText::searchForward(BufferView * bv, string const & str,
 }
 
 bool InsetText::searchBackward(BufferView * bv, string const & str,
-                               bool cs, bool mw)
+                              bool cs, bool mw)
 {
        if (the_locking_inset)
                if (the_locking_inset->searchBackward(bv, str, cs, mw))
@@ -2625,8 +2662,11 @@ bool InsetText::checkInsertChar(LyXFont & font)
 void InsetText::collapseParagraphs(BufferParams const & bparams) const
 {
        while(par->next()) {
-               if (!par->isSeparator(par->size()-1))
-                       par->insertChar(par->size()-1, ' ');
+               if (par->size() && par->next()->size() &&
+                       !par->isSeparator(par->size()-1))
+               {
+                       par->insertChar(par->size(), ' ');
+               }
                par->pasteParagraph(bparams);
        }
        reinitLyXText();
@@ -2639,3 +2679,37 @@ void InsetText::getDrawFont(LyXFont & font) const
                return;
        owner()->getDrawFont(font);
 }
+
+
+void InsetText::appendParagraphs(BufferParams const & bparams,
+                                Paragraph * newpar)
+{
+       Paragraph * buf;
+       Paragraph * tmpbuf = newpar;
+       Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
+
+       while (tmpbuf->next()) {
+               tmpbuf = tmpbuf->next();
+               lastbuffer->next(new Paragraph(*tmpbuf, false));
+               lastbuffer->next()->previous(lastbuffer);
+               lastbuffer = lastbuffer->next();
+       }
+       lastbuffer = par;
+       while (lastbuffer->next())
+               lastbuffer = lastbuffer->next();
+       if (newpar->size() && lastbuffer->size() &&
+               !lastbuffer->isSeparator(lastbuffer->size()-1))
+       {
+               lastbuffer->insertChar(lastbuffer->size(), ' ');
+       }
+
+       // make the buf exactly the same layout than our last paragraph
+       buf->makeSameLayout(lastbuffer);
+
+       // paste it!
+       lastbuffer->next(buf);
+       buf->previous(lastbuffer);
+       lastbuffer->pasteParagraph(bparams);
+
+       reinitLyXText();
+}