]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetCollapsable.cpp
* BufferView::buffer() returns a reference instead of a pointer.
[lyx.git] / src / insets / InsetCollapsable.cpp
index 7889d80b66d5af5a5362356d822c4c62d8d5fe6d..4108be23937c5ed427f89c46567370bda6a4666c 100644 (file)
@@ -60,20 +60,23 @@ InsetCollapsable::Geometry InsetCollapsable::geometry() const
                                return TopButton;
                } else
                        return ButtonOnly;
-               break;
+
        case Minimalistic:
                return NoButton;
-               break;
+
        case Conglomerate:
-               return ( status_ == Open ? SubLabel : Corners );
-               break;
+               return status_ == Open ? SubLabel : Corners;
        }
+
+       // dummy return value to shut down a warning,
+       // this is dead code.
+       return NoButton;
 }
 
 
 InsetCollapsable::InsetCollapsable
                (BufferParams const & bp, CollapseStatus status)
-       : InsetText(bp), label(from_ascii("Label")), status_(status),
+       : InsetText(bp), status_(status),
          openinlined_(false), autoOpen_(false), mouse_hover_(false)
 {
        setAutoBreakRows(true);
@@ -88,7 +91,6 @@ InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
                button_dim(rhs.button_dim),
                topx(rhs.topx),
                topbaseline(rhs.topbaseline),
-               label(rhs.label),
                layout_(rhs.layout_),
                status_(rhs.status_),
                openinlined_(rhs.openinlined_),
@@ -102,8 +104,7 @@ InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
 
 void  InsetCollapsable::setLayout(BufferParams const & bp)
 {
-       setLabelFont(getLayout(bp).labelfont);
-       setLabel(getLayout(bp).labelstring);
+       layout_ = getLayout(bp);
 }
 
 
@@ -165,7 +166,7 @@ Dimension InsetCollapsable::dimensionCollapsed() const
 {
        Dimension dim;
        theFontMetrics(layout_.labelfont).buttonText(
-               label, dim.wid, dim.asc, dim.des);
+               layout_.labelstring, dim.wid, dim.asc, dim.des);
        return dim;
 }
 
@@ -177,8 +178,29 @@ bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
 
        switch (decoration()) {
        case Minimalistic:
+               InsetText::metrics(mi, dim);
+               break;
        case Conglomerate:
                InsetText::metrics(mi, dim);
+               if (status() == Open) {
+                       // consider width of the inset label
+                       Font font(layout_.labelfont);
+                       font.realize(Font(Font::ALL_SANE));
+                       font.decSize();
+                       font.decSize();
+                       int w = 0;
+                       int a = 0;
+                       int d = 0;
+                       docstring s = layout_.labelstring;
+                       theFontMetrics(font).rectText(s, w, a, d);
+                       dim.wid = max(dim.wid, w);
+               }
+               if (status() == Open)
+                       dim.des += ascent();
+               else {
+                       dim.des -= 3;
+                       dim.asc -= 3;
+               }
                break;
        case Classic:
                dim = dimensionCollapsed();
@@ -225,43 +247,98 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
 {
        const int xx = x + TEXT_TO_INSET_OFFSET;
 
-       if (decoration() == Minimalistic)  {
-               InsetText::draw(pi, xx, y);
-       } else {
-               Dimension dimc = dimensionCollapsed();
-               int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
+       // Draw button first -- top, left or only
+       Dimension dimc = dimensionCollapsed();
+       int const top  = y - ascent() + TEXT_TO_INSET_OFFSET;
+       if (decoration() == Classic) {
                button_dim.x1 = xx + 0;
                button_dim.x2 = xx + dimc.width();
                button_dim.y1 = top;
                button_dim.y2 = top + dimc.height();
 
-               pi.pain.buttonText(xx, top + dimc.asc, label, layout_.labelfont, mouse_hover_);
+               pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
+       }
 
-               int textx, texty;
-               switch (geometry()) {
-               case LeftButton:
-                       textx = xx + dimc.width();
-                       texty = top + textdim_.asc;
-                       InsetText::draw(pi, textx, texty);
-                       break;
-               case TopButton:
-                       textx = xx;
-                       texty = top + dimc.height() + textdim_.asc;
-                       InsetText::draw(pi, textx, texty);
-                       break;
-               case ButtonOnly:
-                       break;
-               case NoButton:
-                       textx = xx;
-                       texty = top + textdim_.asc;
-                       InsetText::draw(pi, textx, texty);
-                       break;
-               case SubLabel:
-               case Corners:
-                       // FIXME add handling of SubLabel, Corners
-                       // still in CharStyle
-                       break;
+       int textx, texty;
+       switch (geometry()) {
+       case LeftButton:
+               textx = xx + dimc.width();
+               texty = top + textdim_.asc;
+               InsetText::draw(pi, textx, texty);
+               break;
+       case TopButton:
+               textx = xx;
+               texty = top + dimc.height() + textdim_.asc;
+               InsetText::draw(pi, textx, texty);
+               break;
+       case ButtonOnly:
+               break;
+       case NoButton:
+               textx = xx;
+               texty = y + textdim_.asc;
+               InsetText::draw(pi, textx, texty);
+               break;
+       case SubLabel:
+       case Corners:
+               textx = xx;
+               texty = y + textdim_.asc;
+               const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
+               InsetText::draw(pi, textx, texty);
+               const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
+
+               int desc = InsetText::descent();
+               if (status() == Open)
+                       desc -= ascent();
+               else
+                       desc -= 3;
+
+               pi.pain.line(x, y + desc - 4, x, y + desc, 
+                       layout_.labelfont.color());
+               if (internalStatus() == Open)
+                       pi.pain.line(x, y + desc, 
+                               x + dim_.wid - 3, y + desc,
+                               layout_.labelfont.color());
+               else {
+                       // Make status_ value visible:
+                       pi.pain.line(x, y + desc,
+                               x + 4, y + desc,
+                               layout_.labelfont.color());
+                       pi.pain.line(x + dim_.wid - 7, y + desc,
+                               x + dim_.wid -3, y + desc,
+                               layout_.labelfont.color());
+               }
+               pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
+                       layout_.labelfont.color());
+
+               // the label of the charstyle. Can be toggled.
+               if (status() == Open) {
+                       Font font(layout_.labelfont);
+                       font.realize(Font(Font::ALL_SANE));
+                       font.decSize();
+                       font.decSize();
+                       int w = 0;
+                       int a = 0;
+                       int d = 0;
+                       // FIXME UNICODE
+                       docstring s = layout_.labelstring;
+                       theFontMetrics(font).rectText(s, w, a, d);
+                       pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
+                               s, font, Color::none, Color::none);
+               }
+
+               // a visual cue when the cursor is inside the inset
+               Cursor & cur = pi.base.bv->cursor();
+               if (cur.isInside(this)) {
+                       y -= ascent();
+                       y += 3;
+                       pi.pain.line(x, y + 4, x, y, layout_.labelfont.color());
+                       pi.pain.line(x + 4, y, x, y, layout_.labelfont.color());
+                       pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
+                               layout_.labelfont.color());
+                       pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
+                               layout_.labelfont.color());
                }
+               break;
        }
        setPosCache(pi, x, y);
 }
@@ -418,9 +495,19 @@ void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_MOUSE_RELEASE:
                if (cmd.button() == mouse_button::button3) {
-                       // Open the Inset configuration dialog
-                       showInsetDialog(&cur.bv());
-                       break;
+                       if (decoration() == Conglomerate) {
+
+                               if (internalStatus() == Open)
+                                       setStatus(cur, Collapsed);
+                               else
+                                       setStatus(cur, Open);
+                               break;
+                       } else {
+                               // Open the Inset 
+                               // configuration dialog
+                               showInsetDialog(&cur.bv());
+                               break;
+                       }
                }
 
                if (decoration() == Minimalistic) {
@@ -461,11 +548,11 @@ void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
                else if (cmd.argument() == "close")
                        setStatus(cur, Collapsed);
                else if (cmd.argument() == "toggle" || cmd.argument().empty())
-                       if (isOpen()) {
+                       if (internalStatus() == Open) {
                                setStatus(cur, Collapsed);
-                               cur.top().forwardPos();
-                       }
-                       else
+                               if (geometry() == ButtonOnly)
+                                       cur.top().forwardPos();
+                       else
                                setStatus(cur, Open);
                else // if assign or anything else
                        cur.undispatched();
@@ -500,7 +587,7 @@ bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
 
 void InsetCollapsable::setLabel(docstring const & l)
 {
-       label = l;
+       layout_.labelstring = l;
 }
 
 
@@ -513,7 +600,7 @@ void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
        // Because the collapse status is part of the inset and thus an
        // integral part of the Buffer contents a changed status must be
        // signaled to all views of current buffer.
-       cur.bv().buffer()->changed();
+       cur.bv().buffer().changed();
 }
 
 
@@ -531,4 +618,37 @@ docstring InsetCollapsable::floatName(string const & type, BufferParams const &
 }
 
 
+
+int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
+                         OutputParams const & runparams) const
+{
+       // This implements the standard way of handling the LaTeX output of
+       // a collapsable inset, either a command or an environment. Standard 
+       // collapsable insets should not redefine this, non-standard ones may
+       // call this.
+       if (!layout_.latexname.empty()) {
+               if (layout_.latextype == "command") {
+                       // FIXME UNICODE
+                       os << '\\' << from_utf8(layout_.latexname);
+                       if (!layout_.latexparam.empty())
+                               os << from_utf8(layout_.latexparam);
+                       os << '{';
+               } else if (layout_.latextype == "environment") {
+                       os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
+                       if (!layout_.latexparam.empty())
+                               os << from_utf8(layout_.latexparam);
+               }
+       }
+       int i = InsetText::latex(buf, os, runparams);
+       if (!layout_.latexname.empty())
+               if (layout_.latextype == "command") {
+                       os << "}";
+               } else if (layout_.latextype == "environment") {
+                       os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
+                       i += 4;
+               }
+       return i;
+}
+
+
 } // namespace lyx