]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insettext.C
layout as string
[lyx.git] / src / insets / insettext.C
index e349b226383332c32ba2afa8b0432e9e1951a87e..f22c7cf84ed01192d1363747486308be32f285e3 100644 (file)
@@ -48,6 +48,7 @@
 #include "support/textutils.h"
 #include "support/LAssert.h"
 #include "support/lstrings.h"
+#include "support/lyxalgo.h" // lyx::count
 
 #include <fstream>
 #include <algorithm>
@@ -61,9 +62,9 @@ using std::min;
 using std::max;
 using std::make_pair;
 using std::vector;
+using std::pair;
 
 using lyx::pos_type;
-using lyx::layout_type;
 using lyx::textclass_type;
 
 extern unsigned char getCurrentTextClass(Buffer *);
@@ -349,15 +350,13 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
        int old_x = top_x;
        if (top_x != int(x)) {
                top_x = int(x);
+               topx_set = true;
                int nw = getMaxWidth(bv, this);
                if (nw > 0 && old_max_width != nw) {
                        need_update = INIT;
                        old_max_width = nw;
                        bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
-                       topx_set = true;
                        return;
-               } else {
-                       top_x = old_x;
                }
        }
 
@@ -369,15 +368,11 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
 
        // repaint the background if needed
        if (cleared && backgroundColor() != LColor::background) {
-               top_x = int(x);
                clearInset(bv, baseline, cleared);
-               top_x = old_x;
        }
 
        // no draw is necessary !!!
        if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
-               top_x = int(x);
-               topx_set = true;
                top_baseline = baseline;
                x += width(bv, f);
                if (need_update & CLEAR_FRAME)
@@ -391,15 +386,15 @@ void InsetText::draw(BufferView * bv, LyXFont const & f,
                x += static_cast<float>(scroll());
 
        // if top_x differs we did it already
-       if (!cleared && (top_x == int(x))
+       if (!cleared && (old_x == int(x))
            && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
-               ||(last_drawn_width != insetWidth))) {
+               ||(last_drawn_width != insetWidth)))
+       {
                // Condition necessary to eliminate bug 59 attachment 37
-               if (baseline > 0) clearInset(bv, baseline, cleared);
+               if (baseline > 0)
+                       clearInset(bv, baseline, cleared);
        }
 
-       top_x = int(x);
-       topx_set = true;
        if (cleared)
                frame_is_visible = false;
 
@@ -624,7 +619,7 @@ void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
        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);
        }
 }
@@ -673,7 +668,11 @@ void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
        showInsetCursor(bv);
        if (clear)
                lt = 0;
-       updateLocal(bv, CURSOR, false);
+       
+       int code = CURSOR;
+       if (drawFrame_ == LOCKED)
+               code = CURSOR|DRAW_FRAME;
+       updateLocal(bv, code, false);
 }
 
 
@@ -718,7 +717,10 @@ void InsetText::edit(BufferView * bv, bool front)
        showInsetCursor(bv);
        if (clear)
                lt = 0;
-       updateLocal(bv, CURSOR, false);
+       int code = CURSOR;
+       if (drawFrame_ == LOCKED)
+               code = CURSOR|DRAW_FRAME;
+       updateLocal(bv, code, false);
 }
 
 
@@ -746,11 +748,15 @@ 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
-       lt->setCursor(bv, par, 0);
+       if (par->size()) {
+               lt->setCursor(bv, par, 0);
+       } else if (par->next()) {
+               lt->setCursor(bv, par->next(), 0);
+       }
        if (clear)
                lt = 0;
        updateLocal(bv, code, false);
@@ -790,7 +796,7 @@ bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
                                }
                                if ((*it)->getInsetFromID(id)) {
                                        getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
-                                       lockInset(bv, static_cast<UpdatableInset *>(*it));
+                                       (*it)->edit(bv);
                                        return the_locking_inset->lockInsetInInset(bv, inset);
                                }
                        }
@@ -898,9 +904,9 @@ void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
        if (the_locking_inset) {
                if (the_locking_inset == inset) {
                        the_locking_inset->insetButtonPress(bv,
-                                                           x - inset_x,
-                                                           y - inset_y,
-                                                           button);
+                                                           x - inset_x,
+                                                           y - inset_y,
+                                                           button);
                        no_selection = false;
                        return;
                } else if (inset) {
@@ -910,7 +916,7 @@ 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);
+                                               y - inset_y, button);
                        inset->edit(bv, x - inset_x, y - inset_y, button);
                        if (the_locking_inset)
                                updateLocal(bv, CURSOR, false);
@@ -968,7 +974,7 @@ 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());
                old_par = cpar(bv);
                // Insert primary selection with middle mouse
                // if there is a local selection in the current buffer,
@@ -1084,7 +1090,7 @@ InsetText::localDispatch(BufferView * bv,
                        updateLocal(bv, CURSOR_PAR, false);
                        return result;
                } else if (result >= FINISHED) {
-                       switch(result) {
+                       switch (result) {
                        case FINISHED_RIGHT:
                                moveRightIntern(bv, false, false);
                                result = DISPATCHED;
@@ -1313,39 +1319,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). */    
                        textclass_type tclass = bv->buffer()->params.textclass;
-                       std::pair <bool, layout_type> layout = 
-                               textclasslist.NumberOfLayout(tclass, arg);
+                       string layout = lowercase(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);
+                                       layout = lowercase(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:
@@ -1436,7 +1443,7 @@ int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
        
        while (p) {
                string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
-               lines += countChar(tmp, '\n');
+               lines += lyx::count(tmp.begin(), tmp.end(), '\n');
                os << tmp;
                p = p->next();
        }
@@ -1462,8 +1469,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) {
@@ -1522,7 +1528,7 @@ int InsetText::docbook(Buffer const * buf, ostream & os) const
                        }
 
                        if (environment_stack[depth] != style.latexname()) {
-                               if(environment_stack.size() == depth + 1) {
+                               if (environment_stack.size() == depth + 1) {
                                        environment_stack.push_back("!-- --");
                                        environment_inner.push_back("!-- --");
                                }
@@ -1545,7 +1551,7 @@ int InsetText::docbook(Buffer const * buf, ostream & os) const
                        
                        if (style.latextype == LATEX_ENVIRONMENT) {
                                if (!style.latexparam().empty()) {
-                                       if(style.latexparam() == "CDATA")
+                                       if (style.latexparam() == "CDATA")
                                                os << "<![CDATA[";
                                        else
                                                buf->sgmlOpenTag(os, depth + command_depth,
@@ -1589,7 +1595,7 @@ int InsetText::docbook(Buffer const * buf, ostream & os) const
                switch (style.latextype) {
                case LATEX_ENVIRONMENT:
                        if (!style.latexparam().empty()) {
-                               if(style.latexparam() == "CDATA")
+                               if (style.latexparam() == "CDATA")
                                        os << "]]>";
                                else
                                        buf->sgmlCloseTag(os, depth + command_depth,
@@ -1643,8 +1649,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();
@@ -1880,16 +1885,16 @@ bool InsetText::showInsetDialog(BufferView * bv) const
 }
 
 
-std::vector<string> const InsetText::getLabelList() const 
+vector<string> const InsetText::getLabelList() const 
 {
-       std::vector<string> label_list;
+       vector<string> label_list;
 
        Paragraph * tpar = par;
        while (tpar) {
                Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
                Paragraph::inset_iterator end = tpar->inset_iterator_end();
                for (; beg != end; ++beg) {
-                       std::vector<string> const l = (*beg)->getLabelList();
+                       vector<string> const l = (*beg)->getLabelList();
                        label_list.insert(label_list.end(), l.begin(), l.end());
                }
                tpar = tpar->next();
@@ -2124,7 +2129,7 @@ LyXText * InsetText::getLyXText(BufferView const * lbv,
        if (!recursive && (cached_bview == lbv)) {
                LyXText * lt = cached_text.get();
                lyx::Assert(lt && lt->firstRow()->par() == par);
-               return cached_text.get();
+               return lt;
        }
        
        // Super UGLY! (Lgb)
@@ -2624,3 +2629,11 @@ void InsetText::collapseParagraphs(BufferParams const & bparams) const
        }
        reinitLyXText();
 }
+
+
+void InsetText::getDrawFont(LyXFont & font) const
+{
+       if (!owner())
+               return;
+       owner()->getDrawFont(font);
+}