]> git.lyx.org Git - lyx.git/blobdiff - src/text2.C
Make lyx2lyx output the new external inset format.
[lyx.git] / src / text2.C
index 2580ec7c6b1e5168c7b80ad0b48864bb786527fe..afa44ca91d7b7ba26e7b8d62a5e2e3b8c789a77f 100644 (file)
 
 #include "lyxtext.h"
 #include "LString.h"
+#include "Lsstream.h"
 #include "paragraph.h"
+#include "funcrequest.h"
 #include "frontends/LyXView.h"
 #include "undo_funcs.h"
 #include "buffer.h"
 #include "bufferparams.h"
+#include "errorlist.h"
 #include "gettext.h"
 #include "BufferView.h"
 #include "CutAndPaste.h"
@@ -33,6 +36,7 @@
 #include "paragraph_funcs.h"
 
 #include "insets/insetbibitem.h"
+#include "insets/insetenv.h"
 #include "insets/insetfloat.h"
 #include "insets/insetwrap.h"
 
@@ -40,7 +44,6 @@
 #include "support/textutils.h"
 #include "support/lstrings.h"
 
-#include "support/BoostFormat.h"
 #include <boost/tuple/tuple.hpp>
 
 using std::vector;
@@ -327,7 +330,7 @@ void LyXText::toggleInset()
                // No, try to see if we are inside a collapsable inset
                if (inset_owner && inset_owner->owner()
                    && inset_owner->owner()->isOpen()) {
-                       bv()->unlockInset(static_cast<UpdatableInset *>(inset_owner->owner()));
+                       bv()->unlockInset(inset_owner->owner());
                        inset_owner->owner()->close(bv());
                        bv()->getLyXText()->cursorRight(bv());
                }
@@ -390,7 +393,7 @@ LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
                ++endpit;
        }
 
-       setUndo(bv(), Undo::EDIT, sstart_cur.par(), undoendpit);
+       setUndo(bv(), Undo::EDIT, sstart_cur.par(), boost::prior(undoendpit));
 
        // ok we have a selection. This is always between sstart_cur
        // and sel_end cursor
@@ -424,22 +427,41 @@ LyXText::setLayout(LyXCursor & cur, LyXCursor & sstart_cur,
 // set layout over selection and make a total rebreak of those paragraphs
 void LyXText::setLayout(string const & layout)
 {
-       LyXCursor tmpcursor = cursor;  /* store the current cursor  */
+       LyXCursor tmpcursor = cursor;  // store the current cursor
 
        // if there is no selection just set the layout
-       // of the current paragraph  */
+       // of the current paragraph
        if (!selection.set()) {
                selection.start = cursor;  // dummy selection
                selection.end = cursor;
        }
+
+       // special handling of new environment insets
+       BufferParams const & params = bv()->buffer()->params;
+       LyXLayout_ptr const & lyxlayout = params.getLyXTextClass()[layout];
+       if (lyxlayout->is_environment) {
+               // move everything in a new environment inset
+               lyxerr << "setting layout " << layout << endl;
+               bv()->owner()->dispatch(FuncRequest(LFUN_HOME));
+               bv()->owner()->dispatch(FuncRequest(LFUN_ENDSEL));
+               bv()->owner()->dispatch(FuncRequest(LFUN_CUT));
+               Inset * inset = new InsetEnvironment(params, layout);
+               if (bv()->insertInset(inset)) {
+                       //inset->edit(bv());
+                       //bv()->owner()->dispatch(FuncRequest(LFUN_PASTE));
+               }
+               else
+                       delete inset;
+               return;
+       }
+
        ParagraphList::iterator endpit = setLayout(cursor, selection.start,
                                                   selection.end, layout);
        redoParagraphs(selection.start, endpit);
 
        // we have to reset the selection, because the
        // geometry could have changed
-       setCursor(selection.start.par(),
-                 selection.start.pos(), false);
+       setCursor(selection.start.par(), selection.start.pos(), false);
        selection.cursor = cursor;
        setCursor(selection.end.par(), selection.end.pos(), false);
        updateCounters();
@@ -464,7 +486,7 @@ bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
        ParagraphList::iterator pastend = boost::next(end);
 
        if (!test_only)
-               setUndo(bv(), Undo::EDIT, start, pastend);
+               setUndo(bv(), Undo::EDIT, start, end);
 
        bool changed = false;
 
@@ -566,8 +588,7 @@ void LyXText::setFont(LyXFont const & font, bool toggleall)
        // ok we have a selection. This is always between sel_start_cursor
        // and sel_end cursor
 
-       setUndo(bv(), Undo::EDIT,
-               selection.start.par(), boost::next(selection.end.par()));
+       setUndo(bv(), Undo::EDIT, selection.start.par(), selection.end.par());
        freezeUndo();
        cursor = selection.start;
        while (cursor.par() != selection.end.par() ||
@@ -637,7 +658,7 @@ void LyXText::redoDrawingOfParagraph(LyXCursor const & cur)
 }
 
 
-// deletes and inserts again all paragaphs between the cursor
+// deletes and inserts again all paragraphs between the cursor
 // and the specified par
 // This function is needed after SetLayout and SetFont etc.
 void LyXText::redoParagraphs(LyXCursor const & cur,
@@ -651,7 +672,7 @@ void LyXText::redoParagraphs(LyXCursor const & cur,
        if (tmprit == rows().begin()) {
                // A trick/hack for UNDO.
                // This is needed because in an UNDO/REDO we could have
-               // changed the ownerParagrah() so the paragraph inside
+               // changed the ownerParagraph() so the paragraph inside
                // the row is NOT my really first par anymore.
                // Got it Lars ;) (Jug 20011206)
                first_phys_pit = ownerParagraphs().begin();
@@ -688,10 +709,10 @@ void LyXText::redoParagraphs(LyXCursor const & cur,
        }
        if (prevrit != rows().end()) {
                setHeightOfRow(prevrit);
-               const_cast<LyXText *>(this)->postPaint(y - prevrit->height());
+               postPaint(y - prevrit->height());
        } else {
                setHeightOfRow(rows().begin());
-               const_cast<LyXText *>(this)->postPaint(0);
+               postPaint(0);
        }
        if (tmprit != rows().end())
                setHeightOfRow(tmprit);
@@ -862,12 +883,9 @@ void LyXText::cursorTop()
 
 void LyXText::cursorBottom()
 {
-#warning FIXME
-       // This is how it should be:
-       // ParagraphList::iterator lastpit = boost::prior(ownerParagraphs().end());
-       ParagraphList::iterator lastpit = &ownerParagraphs().back();
-       int pos = lastpit->size();
-       setCursor(lastpit, pos);
+       ParagraphList::iterator lastpit =
+               boost::prior(ownerParagraphs().end());
+       setCursor(lastpit, lastpit->size());
 }
 
 
@@ -969,7 +987,8 @@ void LyXText::setParagraph(bool line_top, bool line_bottom,
                ++endpit;
        }
 
-       setUndo(bv(), Undo::EDIT, selection.start.par(), undoendpit);
+       setUndo(bv(), Undo::EDIT, selection.start.par(),
+               boost::prior(undoendpit));
 
 
        ParagraphList::iterator tmppit = selection.end.par();
@@ -1169,7 +1188,7 @@ void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
                                        isOK = true;
                                        break;
                                } else {
-                                       tmppit = in->parOwner();
+                                       tmppit = std::find(ownerParagraphs().begin(), ownerParagraphs().end(), *in->parOwner());
                                }
                        }
 
@@ -1188,17 +1207,7 @@ void LyXText::setCounter(Buffer const * buf, ParagraphList::iterator pit)
                                textclass.counters().step(fl.type());
 
                                // Doesn't work... yet.
-#if USE_BOOST_FORMAT
-                               s = boost::io::str(boost::format(_("%1$s #:")) % buf->B_(fl.name()));
-                               // s << boost::format(_("%1$s %1$d:")
-                               //        % fl.name()
-                               //        % buf->counters().value(fl.name());
-#else
-                               ostringstream o;
-                               //o << fl.name() << ' ' << buf->counters().value(fl.name()) << ":";
-                               o << buf->B_(fl.name()) << " #:";
-                               s = STRCONV(o.str());
-#endif
+                               s = bformat(_("%1$s #:"), buf->B_(fl.name()));
                        } else {
                                // par->SetLayout(0);
                                // s = layout->labelstring;
@@ -1240,7 +1249,7 @@ void LyXText::updateCounters()
 
                string const oldLabel = pit->params().labelString();
 
-               int maxdepth = 0;
+               size_t maxdepth = 0;
                if (pit != ownerParagraphs().begin())
                        maxdepth = boost::prior(pit)->getMaxDepthAfter();
 
@@ -1264,8 +1273,7 @@ void LyXText::insertInset(Inset * inset)
 {
        if (!cursor.par()->insetAllowed(inset->lyxCode()))
                return;
-       setUndo(bv(), Undo::FINISH, cursor.par(),
-               boost::next(cursor.par()));
+       setUndo(bv(), Undo::FINISH, cursor.par());
        freezeUndo();
        cursor.par()->insertInset(cursor.pos(), inset);
        // Just to rebreak and refresh correctly.
@@ -1317,7 +1325,8 @@ void LyXText::cutSelection(bool doclear, bool realcut)
                ++endpit;
        }
 
-       setUndo(bv(), Undo::DELETE, selection.start.par(), undoendpit);
+       setUndo(bv(), Undo::DELETE, selection.start.par(),
+               boost::prior(undoendpit));
 
 
        endpit = selection.end.par();
@@ -1389,16 +1398,20 @@ void LyXText::pasteSelection()
        if (!CutAndPaste::checkPastePossible())
                return;
 
-       setUndo(bv(), Undo::INSERT,
-               cursor.par(), boost::next(cursor.par()));
+       setUndo(bv(), Undo::INSERT, cursor.par());
 
        ParagraphList::iterator endpit;
        PitPosPair ppp;
 
+       ErrorList el;
+
        boost::tie(ppp, endpit) =
                CutAndPaste::pasteSelection(ownerParagraphs(),
                                            cursor.par(), cursor.pos(),
-                                           bv()->buffer()->params.textclass);
+                                           bv()->buffer()->params.textclass,
+                                           el);
+       bv()->setErrorList(el);
+       bv()->showErrorList(_("Paste"));
 
        redoParagraphs(cursor, endpit);
 
@@ -1759,7 +1772,7 @@ float LyXText::getCursorX(RowList::iterator rit,
 void LyXText::setCursorIntern(ParagraphList::iterator pit,
                              pos_type pos, bool setfont, bool boundary)
 {
-       InsetText * it = static_cast<InsetText *>(pit->inInset());
+       UpdatableInset * it = pit->inInset();
        if (it) {
                if (it != inset_owner) {
                        lyxerr[Debug::INSETS] << "InsetText   is " << it
@@ -2055,7 +2068,8 @@ void LyXText::cursorUp(bool selecting)
                y -= topy;
                Inset * inset_hit = checkInsetHit(x, y1);
                if (inset_hit && isHighlyEditableInset(inset_hit)) {
-                       inset_hit->edit(bv(), x, y - (y2 - y1), mouse_button::none);
+                       inset_hit->localDispatch(
+                               FuncRequest(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none));
                }
        }
 #else
@@ -2079,7 +2093,8 @@ void LyXText::cursorDown(bool selecting)
                y -= topy;
                Inset * inset_hit = checkInsetHit(x, y1);
                if (inset_hit && isHighlyEditableInset(inset_hit)) {
-                       inset_hit->edit(bv(), x, y - (y2 - y1), mouse_button::none);
+                       FuncRequest cmd(bv(), LFUN_INSET_EDIT, x, y - (y2 - y1), mouse_button::none);
+                       inset_hit->localDispatch(cmd);
                }
        }
 #else
@@ -2245,7 +2260,8 @@ bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
                                ++endpit;
                        }
 
-                       setUndo(bv(), Undo::DELETE, old_cursor.par(), endpit);
+                       setUndo(bv(), Undo::DELETE, old_cursor.par(),
+                               boost::prior(endpit));
                        cursor = tmpcursor;
 
                        // delete old row
@@ -2276,7 +2292,7 @@ bool LyXText::deleteEmptyParagraphMechanism(LyXCursor const & old_cursor)
                                ++endpit;
                        }
 
-                       setUndo(bv(), Undo::DELETE, old_cursor.par(), endpit);
+                       setUndo(bv(), Undo::DELETE, old_cursor.par(), boost::prior(endpit));
                        cursor = tmpcursor;
 
                        // delete old row