]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettext.C
fix #832
[lyx.git] / src / insets / insettext.C
index 3ada4ab47cf517077e65a2457e3561f0a55e0447..51eac4683adb56bd16237e49dc98bce23eb5fcf2 100644 (file)
@@ -10,7 +10,6 @@
 
 #include <config.h>
 
-
 #include "insettext.h"
 
 #include "buffer.h"
@@ -38,6 +37,7 @@
 #include "sgml.h"
 #include "rowpainter.h"
 #include "insetnewline.h"
+#include "Lsstream.h"
 
 #include "frontends/Alert.h"
 #include "frontends/Dialogs.h"
@@ -71,6 +71,7 @@ using std::for_each;
 using lyx::pos_type;
 using lyx::textclass_type;
 
+
 // These functions should probably go into bufferview_funcs somehow (Jug)
 
 void InsetText::saveLyXTextState(LyXText * t) const
@@ -136,7 +137,7 @@ InsetText::InsetText(BufferParams const & bp)
        : UpdatableInset(), lt(0), in_update(false), do_resize(0),
          do_reinit(false)
 {
-       paragraphs.set(new Paragraph);
+       paragraphs.push_back(new Paragraph);
        paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
        if (bp.tracking_changes)
                paragraphs.begin()->trackChanges();
@@ -217,7 +218,7 @@ void InsetText::clear(bool just_mark_erased)
        LyXLayout_ptr old_layout = paragraphs.begin()->layout();
 
        paragraphs.clear();
-       paragraphs.set(new Paragraph);
+       paragraphs.push_back(new Paragraph);
        paragraphs.begin()->setInsetOwner(this);
        paragraphs.begin()->layout(old_layout);
 
@@ -598,109 +599,6 @@ string const InsetText::editMessage() const
 }
 
 
-void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
-{
-       UpdatableInset::edit(bv, x, y, button);
-
-       if (!bv->lockInset(this)) {
-               lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
-               return;
-       }
-       locked = true;
-       the_locking_inset = 0;
-       inset_pos = inset_x = inset_y = 0;
-       inset_boundary = false;
-       inset_par = 0;
-       old_par = 0;
-       int tmp_y = (y < 0) ? 0 : y;
-       bool clear = false;
-       if (!lt) {
-               lt = getLyXText(bv);
-               clear = true;
-       }
-       // we put here -1 and not button as now the button in the
-       // edit call should not be needed we will fix this in 1.3.x
-       // cycle hopefully (Jug 20020509)
-       // FIXME: GUII I've changed this to none: probably WRONG
-       if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
-               lt->setCursorFromCoordinates(x - drawTextXOffset,
-                                           y + insetAscent);
-               lt->cursor.x_fix(lt->cursor.x());
-       }
-       lt->clearSelection();
-       finishUndo();
-       // If the inset is empty set the language of the current font to the
-       // language to the surronding text (if different).
-       if (paragraphs.begin()->empty() &&
-           boost::next(paragraphs.begin()) == paragraphs.end()&&
-               bv->getParentLanguage(this) != lt->current_font.language())
-       {
-               LyXFont font(LyXFont::ALL_IGNORE);
-               font.setLanguage(bv->getParentLanguage(this));
-               setFont(bv, font, false);
-       }
-       if (clear)
-               lt = 0;
-
-       int code = CURSOR;
-       if (drawFrame_ == LOCKED)
-               code = CURSOR|DRAW_FRAME;
-       updateLocal(bv, code, false);
-
-       // Tell the paragraph dialog that we've entered an insettext.
-       bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
-}
-
-
-void InsetText::edit(BufferView * bv, bool front)
-{
-       UpdatableInset::edit(bv, front);
-
-       if (!bv->lockInset(this)) {
-               lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
-               return;
-       }
-       locked = true;
-       the_locking_inset = 0;
-       inset_pos = inset_x = inset_y = 0;
-       inset_boundary = false;
-       inset_par = 0;
-       old_par = 0;
-       bool clear = false;
-       if (!lt) {
-               lt = getLyXText(bv);
-               clear = true;
-       }
-       if (front)
-               lt->setCursor(&*(paragraphs.begin()), 0);
-       else {
-               ParagraphList::iterator it = paragraphs.begin();
-               ParagraphList::iterator end = paragraphs.end();
-               while (boost::next(it) != end)
-                       ++it;
-//             int const pos = (p->size() ? p->size()-1 : p->size());
-               lt->setCursor(&*it, it->size());
-       }
-       lt->clearSelection();
-       finishUndo();
-       // If the inset is empty set the language of the current font to the
-       // language to the surronding text (if different).
-       if (paragraphs.begin()->empty() &&
-           boost::next(paragraphs.begin()) == paragraphs.end() &&
-               bv->getParentLanguage(this) != lt->current_font.language()) {
-               LyXFont font(LyXFont::ALL_IGNORE);
-               font.setLanguage(bv->getParentLanguage(this));
-               setFont(bv, font, false);
-       }
-       if (clear)
-               lt = 0;
-       int code = CURSOR;
-       if (drawFrame_ == LOCKED)
-               code = CURSOR|DRAW_FRAME;
-       updateLocal(bv, code, false);
-}
-
-
 void InsetText::insetUnlock(BufferView * bv)
 {
        if (the_locking_inset) {
@@ -727,9 +625,9 @@ void InsetText::insetUnlock(BufferView * bv)
                bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
        // hack for deleteEmptyParMech
        if (!paragraphs.begin()->empty()) {
-               lt->setCursor(&*(paragraphs.begin()), 0);
+               lt->setCursor(paragraphs.begin(), 0);
        } else if (boost::next(paragraphs.begin()) != paragraphs.end()) {
-               lt->setCursor(&*boost::next(paragraphs.begin()), 0);
+               lt->setCursor(boost::next(paragraphs.begin()), 0);
        }
        if (clear)
                lt = 0;
@@ -755,7 +653,7 @@ void InsetText::lockInset(BufferView * bv)
                lt = getLyXText(bv);
                clear = true;
        }
-       lt->setCursor(&*(paragraphs.begin()), 0);
+       lt->setCursor(paragraphs.begin(), 0);
        lt->clearSelection();
        finishUndo();
        // If the inset is empty set the language of the current font to the
@@ -806,13 +704,13 @@ bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
                                pit->insetlist.end();
                        for (; it != end; ++it) {
                                if (it.getInset() == inset) {
-                                       getLyXText(bv)->setCursorIntern(&*pit, it.getPos());
+                                       getLyXText(bv)->setCursorIntern(pit, it.getPos());
                                        lockInset(bv, inset);
                                        return true;
                                }
                                if (it.getInset()->getInsetFromID(id)) {
-                                       getLyXText(bv)->setCursorIntern(&*pit, it.getPos());
-                                       it.getInset()->edit(bv);
+                                       getLyXText(bv)->setCursorIntern(pit, it.getPos());
+                                       it.getInset()->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
                                        return the_locking_inset->lockInsetInInset(bv, inset);
                                }
                        }
@@ -1090,18 +988,91 @@ void InsetText::lfunMouseMotion(FuncRequest const & cmd)
 }
 
 
-Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
+Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
 {
-       BufferView * bv = ev.view();
-       switch (ev.action) {
+       BufferView * bv = cmd.view();
+
+       if (cmd.action == LFUN_INSET_EDIT) {
+               UpdatableInset::localDispatch(cmd);
+
+               if (!bv->lockInset(this)) {
+                       lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
+                       return DISPATCHED;
+               }
+
+               locked = true;
+               the_locking_inset = 0;
+               inset_pos = inset_x = inset_y = 0;
+               inset_boundary = false;
+               inset_par = 0;
+               old_par = 0;
+
+               bool clear = false;
+               if (!lt) {
+                       lt = getLyXText(bv);
+                       clear = true;
+               }
+
+               if (cmd.argument.size()) {
+                       if (cmd.argument == "left")
+                               lt->setCursor(paragraphs.begin(), 0);
+                       else {
+                               ParagraphList::iterator it = paragraphs.begin();
+                               ParagraphList::iterator end = paragraphs.end();
+                               while (boost::next(it) != end)
+                                       ++it;
+               //              int const pos = (p->size() ? p->size()-1 : p->size());
+                               lt->setCursor(it, it->size());
+                       }
+               } else {
+                       int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
+                       // we put here -1 and not button as now the button in the
+                       // edit call should not be needed we will fix this in 1.3.x
+                       // cycle hopefully (Jug 20020509)
+                       // FIXME: GUII I've changed this to none: probably WRONG
+                       if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
+                               lt->setCursorFromCoordinates(cmd.x - drawTextXOffset,
+                                                                       cmd.y + insetAscent);
+                               lt->cursor.x_fix(lt->cursor.x());
+                       }
+               }
+
+               lt->clearSelection();
+               finishUndo();
+
+               // If the inset is empty set the language of the current font to the
+               // language to the surronding text (if different).
+               if (paragraphs.begin()->empty() &&
+                               boost::next(paragraphs.begin()) == paragraphs.end()&&
+                       bv->getParentLanguage(this) != lt->current_font.language())
+               {
+                       LyXFont font(LyXFont::ALL_IGNORE);
+                       font.setLanguage(bv->getParentLanguage(this));
+                       setFont(bv, font, false);
+               }
+
+               if (clear)
+                       lt = 0;
+               int code = CURSOR;
+               if (drawFrame_ == LOCKED)
+                       code = CURSOR | DRAW_FRAME;
+
+               updateLocal(bv, code, false);
+               // Tell the paragraph dialog that we've entered an insettext.
+               bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
+               return DISPATCHED;
+       }
+
+
+       switch (cmd.action) {
                case LFUN_MOUSE_PRESS:
-                       lfunMousePress(ev);
+                       lfunMousePress(cmd);
                        return DISPATCHED;
                case LFUN_MOUSE_MOTION:
-                       lfunMouseMotion(ev);
+                       lfunMouseMotion(cmd);
                        return DISPATCHED;
                case LFUN_MOUSE_RELEASE:
-                       return lfunMouseRelease(ev) ? DISPATCHED : UNDISPATCHED;
+                       return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
                default:
                        break;
        }
@@ -1109,16 +1080,16 @@ Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
        bool was_empty = (paragraphs.begin()->empty() &&
                          boost::next(paragraphs.begin()) == paragraphs.end());
        no_selection = false;
-       RESULT result = UpdatableInset::localDispatch(ev);
+       RESULT result = UpdatableInset::localDispatch(cmd);
        if (result != UNDISPATCHED)
                return DISPATCHED;
 
        result = DISPATCHED;
-       if (ev.action < 0 && ev.argument.empty())
+       if (cmd.action < 0 && cmd.argument.empty())
                return FINISHED;
 
        if (the_locking_inset) {
-               result = the_locking_inset->localDispatch(ev);
+               result = the_locking_inset->localDispatch(cmd);
                if (result == DISPATCHED_NOUPDATE)
                        return result;
                else if (result == DISPATCHED) {
@@ -1167,7 +1138,7 @@ Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
        if (lt->selection.set())
                cursor_update = SELECTION;
 
-       switch (ev.action) {
+       switch (cmd.action) {
 
        // Normal chars
        case LFUN_SELFINSERT:
@@ -1175,7 +1146,7 @@ Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
 //         setErrorMessage(N_("Document is read only"));
                        break;
                }
-               if (!ev.argument.empty()) {
+               if (!cmd.argument.empty()) {
                        /* Automatically delete the currently selected
                         * text and replace it with what is being
                         * typed in now. Depends on lyxrc settings
@@ -1193,9 +1164,9 @@ Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
                                }
                        }
                        lt->clearSelection();
-                       for (string::size_type i = 0; i < ev.argument.length(); ++i) {
+                       for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
                                bv->owner()->getIntl().getTransManager().
-                                       TranslateAndInsert(ev.argument[i], lt);
+                                       TranslateAndInsert(cmd.argument[i], lt);
                        }
                }
                lt->selection.cursor = lt->cursor;
@@ -1289,7 +1260,7 @@ Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
 
                if (clip.empty())
                        break;
-               if (ev.argument == "paragraph") {
+               if (cmd.argument == "paragraph") {
                        lt->insertStringAsParagraphs(clip);
                } else {
                        lt->insertStringAsLines(clip);
@@ -1362,7 +1333,7 @@ Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
                        // and current buffer's textclass (number). */
                        LyXTextClass const & tclass =
                                bv->buffer()->params.getLyXTextClass();
-                       string layout = ev.argument;
+                       string layout = cmd.argument;
                        bool hasLayout = tclass.hasLayout(layout);
 
                        // If the entry is obsolete, use the new one instead.
@@ -1375,7 +1346,7 @@ Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
 
                        // see if we found the layout number:
                        if (!hasLayout) {
-                               FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
+                               FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
                                bv->owner()->dispatch(lf);
                                break;
                        }
@@ -1405,7 +1376,7 @@ Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
                        cur_value = pit->params().spacing().getValue();
                }
 
-               istringstream istr(ev.argument.c_str());
+               istringstream istr(STRCONV(cmd.argument));
                string tmp;
                istr >> tmp;
                Spacing::Space new_spacing = cur_spacing;
@@ -1430,7 +1401,7 @@ Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
                        new_spacing = Spacing::Default;
                } else {
                        lyxerr << _("Unknown spacing argument: ")
-                                  << ev.argument << endl;
+                                  << cmd.argument << endl;
                }
                if (cur_spacing != new_spacing || cur_value != new_value) {
                        pit->params().spacing(Spacing(new_spacing, new_value));
@@ -1452,7 +1423,7 @@ Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
        case LFUN_ENDBUF:
        case LFUN_BEGINNINGBUF:
                updwhat = cursor_update;
-               if (!bv->dispatch(ev))
+               if (!bv->dispatch(cmd))
                        result = UNDISPATCHED;
                break;
 
@@ -1469,7 +1440,7 @@ Inset::RESULT InsetText::localDispatch(FuncRequest const & ev)
                // fallthrough
 
        default:
-               if (!bv->dispatch(ev))
+               if (!bv->dispatch(cmd))
                        result = UNDISPATCHED;
                break;
        }
@@ -1929,7 +1900,8 @@ bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
                        static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
                if (!isHighlyEditableInset(inset))
                        return false;
-               inset->edit(bv, front);
+               FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
+               inset->localDispatch(cmd);
                if (!the_locking_inset)
                        return false;
                updateLocal(bv, CURSOR, false);
@@ -1961,7 +1933,8 @@ bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
                        y = insetDescent;
                inset_x = cix(bv) - top_x + drawTextXOffset;
                inset_y = ciy(bv) + drawTextYOffset;
-               inset->edit(bv, x - inset_x, y - inset_y, button);
+               FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
+               inset->localDispatch(cmd);
                if (!the_locking_inset)
                        return false;
                updateLocal(bv, CURSOR, false);
@@ -2070,7 +2043,7 @@ int InsetText::cx(BufferView * bv) const
        LyXText * llt = getLyXText(bv);
        int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
        if (the_locking_inset) {
-               LyXFont font = llt->getFont(bv->buffer(), &*llt->cursor.par(),
+               LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
                                            llt->cursor.pos());
                if (font.isVisibleRightToLeft())
                        x -= the_locking_inset->width(bv, font);
@@ -2085,7 +2058,7 @@ int InsetText::cix(BufferView * bv) const
        LyXText * llt = getLyXText(bv);
        int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
        if (the_locking_inset) {
-               LyXFont font = llt->getFont(bv->buffer(), &*llt->cursor.par(),
+               LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
                                            llt->cursor.pos());
                if (font.isVisibleRightToLeft())
                        x -= the_locking_inset->width(bv, font);
@@ -2139,7 +2112,7 @@ LyXText * InsetText::getLyXText(BufferView const * lbv,
                if (recursive && the_locking_inset)
                        return the_locking_inset->getLyXText(lbv, true);
                LyXText * lt = cached_text.get();
-               lyx::Assert(lt && lt->rows().begin()->par() == &*(paragraphs.begin()));
+               lyx::Assert(lt && lt->rows().begin()->par() == paragraphs.begin());
                return lt;
        }
        // Super UGLY! (Lgb)
@@ -2591,7 +2564,7 @@ bool InsetText::searchBackward(BufferView * bv, string const & str,
                while (boost::next(pit) != pend)
                        ++pit;
 
-               lt->setCursor(&*pit, pit->size());
+               lt->setCursor(pit, pit->size());
        }
        lyxfind::SearchResult result =
                lyxfind::LyXFind(bv, lt, str, false, cs, mw);
@@ -2632,12 +2605,12 @@ void InsetText::collapseParagraphs(BufferView * bv)
                }
                if (llt->selection.set()) {
                        if (llt->selection.start.par() == boost::next(paragraphs.begin())) {
-                               llt->selection.start.par(&*(paragraphs.begin()));
+                               llt->selection.start.par(paragraphs.begin());
                                llt->selection.start.pos(
                                        llt->selection.start.pos() + paragraphs.begin()->size());
                        }
                        if (llt->selection.end.par() == boost::next(paragraphs.begin())) {
-                               llt->selection.end.par(&*(paragraphs.begin()));
+                               llt->selection.end.par(paragraphs.begin());
                                llt->selection.end.pos(
                                        llt->selection.end.pos() + paragraphs.begin()->size());
                        }