]> git.lyx.org Git - lyx.git/blobdiff - src/insets/InsetCollapsible.cpp
Fix bug #12795
[lyx.git] / src / insets / InsetCollapsible.cpp
index 20e595ff32e22ed0371a58e913afc4969e75e9eb..a0a789cc6759416bd14f50b149928c8809870bf1 100644 (file)
@@ -26,7 +26,6 @@
 #include "InsetLayout.h"
 #include "Lexer.h"
 #include "MetricsInfo.h"
-#include "OutputParams.h"
 #include "TextClass.h"
 #include "TocBackend.h"
 
 
 #include "support/debug.h"
 #include "support/docstream.h"
+#include "support/FileName.h"
 #include "support/gettext.h"
 #include "support/lassert.h"
 #include "support/lstrings.h"
-#include "support/RefChanger.h"
+#include "support/Changer.h"
 #include "support/TempFile.h"
 
 using namespace std;
@@ -86,7 +86,7 @@ InsetCollapsible::~InsetCollapsible()
 
 InsetCollapsible::CollapseStatus InsetCollapsible::status(BufferView const & bv) const
 {
-       if (decoration() == InsetLayout::CONGLOMERATE)
+       if (decoration() == InsetDecoration::CONGLOMERATE)
                return status_;
        return view_[&bv].auto_open_ ? Open : status_;
 }
@@ -95,18 +95,21 @@ InsetCollapsible::CollapseStatus InsetCollapsible::status(BufferView const & bv)
 InsetCollapsible::Geometry InsetCollapsible::geometry(BufferView const & bv) const
 {
        switch (decoration()) {
-       case InsetLayout::CLASSIC:
+       case InsetDecoration::CLASSIC:
                if (status(bv) == Open)
                        return view_[&bv].openinlined_ ? LeftButton : TopButton;
                return ButtonOnly;
 
-       case InsetLayout::MINIMALISTIC:
-               return status(bv) == Open ? NoButton : ButtonOnly ;
+       case InsetDecoration::MINIMALISTIC: {
+               return status(bv) == Open ?
+                       (tempfile_ ? LeftButton : NoButton)
+                       : ButtonOnly;
+       }
 
-       case InsetLayout::CONGLOMERATE:
+       case InsetDecoration::CONGLOMERATE:
                return status(bv) == Open ? SubLabel : Corners ;
 
-       case InsetLayout::DEFAULT:
+       case InsetDecoration::DEFAULT:
                break; // this shouldn't happen
        }
 
@@ -185,8 +188,16 @@ Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
        Dimension dim;
        FontInfo labelfont(getLabelfont());
        labelfont.realize(sane_font);
+       int const offset = Inset::textOffset(&bv);
        theFontMetrics(labelfont).buttonText(
-               buttonLabel(bv), Inset::textOffset(&bv), dim.wid, dim.asc, dim.des);
+               buttonLabel(bv), offset, dim.wid, dim.asc, dim.des);
+       // remove spacing on the right for left buttons; we also do it for
+       // TopButton (although it is not useful per se), because
+       // openinlined_ is not always set properly at this point.
+       Geometry const geom = geometry(bv);
+       if (geom == LeftButton || geom == TopButton)
+               // this form makes a difference if offset is even
+               dim.wid -= offset - offset / 2;
        return dim;
 }
 
@@ -195,10 +206,6 @@ void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
 
-       FontInfo tmpfont = mi.base.font;
-       mi.base.font = getFont();
-       mi.base.font.realize(tmpfont);
-
        BufferView const & bv = *mi.base.bv;
 
        switch (geometry(bv)) {
@@ -220,6 +227,7 @@ void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
                int d = 0;
                theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
                dim.des += a + d;
+               dim.wid = max(dim.wid, w);
                break;
                }
        case TopButton:
@@ -236,8 +244,8 @@ void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
                        InsetText::metrics(mi, textdim);
                        view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
                        if (view_[&bv].openinlined_) {
-                               // Correct for button width.
-                               dim.wid += textdim.wid;
+                               // Correct for button width but remove spacing before frame
+                               dim.wid += textdim.wid - leftOffset(mi.base.bv) / 2;
                                dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
                                dim.asc = textdim.asc;
                        } else {
@@ -247,8 +255,6 @@ void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
                }
                break;
        }
-
-       mi.base.font = tmpfont;
 }
 
 
@@ -260,6 +266,18 @@ bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
 }
 
 
+ColorCode InsetCollapsible::backgroundColor(PainterInfo const &) const
+{
+       return getLayout().bgcolor();
+}
+
+
+ColorCode InsetCollapsible::labelColor() const
+{
+       return getLayout().labelfont().color();
+}
+
+
 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
 {
        BufferView const & bv = *pi.base.bv;
@@ -304,7 +322,9 @@ void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
        case LeftButton:
        case TopButton: {
                if (g == LeftButton) {
-                       textx = x + dimc.width();
+                       // correct for spacing added before the frame in
+                       // InsetText::draw. We want the button to touch the frame.
+                       textx = x + dimc.width() - leftOffset(pi.base.bv) / 2;
                        texty = baseline;
                } else {
                        textx = x;
@@ -313,8 +333,8 @@ void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
                // Do not draw the cue for INSERTED -- it is already in the button and
                // that's enough.
                Changer cdummy = (pi.change.type == Change::INSERTED)
-                       ? make_change(pi.change, Change())
-                       : Changer();
+                       ? changeVar(pi.change, Change())
+                       : noChange();
                InsetText::draw(pi, textx, texty);
                break;
        }
@@ -332,7 +352,7 @@ void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
                // We will take care of the frame and the change tracking cue
                // ourselves, below.
                {
-                       Changer cdummy = make_change(pi.change, Change());
+                       Changer cdummy = changeVar(pi.change, Change());
                        const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
                        InsetText::draw(pi, textx, texty);
                        const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
@@ -407,7 +427,7 @@ void InsetCollapsible::cursorPos(BufferView const & bv,
 
        switch (geometry(bv)) {
        case LeftButton:
-               x += dimensionCollapsed(bv).wid;
+               x += dimensionCollapsed(bv).wid - leftOffset(&bv) / 2;
                break;
        case TopButton: {
                y += dimensionCollapsed(bv).des + textdim.asc;
@@ -431,8 +451,8 @@ bool InsetCollapsible::editable() const
                return false;
        
        switch (decoration()) {
-       case InsetLayout::CLASSIC:
-       case InsetLayout::MINIMALISTIC:
+       case InsetDecoration::CLASSIC:
+       case InsetDecoration::MINIMALISTIC:
                return status_ == Open;
        default:
                return true;
@@ -648,11 +668,13 @@ bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
                return true;
 
        case LFUN_INSET_EDIT:
-               flag.setEnabled(getLayout().editExternally() && tempfile_ == 0);
+               flag.setEnabled(!buffer().hasReadonlyFlag() &&
+                       getLayout().editExternally() && tempfile_ == nullptr);
                return true;
 
        case LFUN_INSET_END_EDIT:
-               flag.setEnabled(getLayout().editExternally() && tempfile_ != 0);
+               flag.setEnabled(!buffer().hasReadonlyFlag() &&
+                       getLayout().editExternally() && tempfile_ != nullptr);
                return true;
 
        default:
@@ -677,6 +699,8 @@ docstring InsetCollapsible::getLabel() const
 
 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
 {
+       // U+1F512 LOCK
+       docstring const locked = tempfile_ ? docstring(1, 0x1F512) : docstring();
        // indicate changed content in label (#8645)
        // ✎ U+270E LOWER RIGHT PENCIL
        docstring const indicator = (isChanged() && geometry(bv) == ButtonOnly)
@@ -684,8 +708,8 @@ docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
        InsetLayout const & il = getLayout();
        docstring const label = getLabel();
        if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
-               return indicator + label;
-       return indicator + getNewLabel(label);
+               return locked + indicator + label;
+       return locked + indicator + getNewLabel(label);
 }
 
 
@@ -698,10 +722,10 @@ void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
 }
 
 
-InsetLayout::InsetDecoration InsetCollapsible::decoration() const
+InsetDecoration InsetCollapsible::decoration() const
 {
-       InsetLayout::InsetDecoration const dec = getLayout().decoration();
-       return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
+       InsetDecoration const dec = getLayout().decoration();
+       return dec == InsetDecoration::DEFAULT ? InsetDecoration::CLASSIC : dec;
 }
 
 
@@ -710,7 +734,7 @@ string InsetCollapsible::contextMenu(BufferView const & bv, int x,
 {
        string context_menu = contextMenuName();
        string const it_context_menu = InsetText::contextMenuName();
-       if (decoration() == InsetLayout::CONGLOMERATE)
+       if (decoration() == InsetDecoration::CONGLOMERATE)
                return context_menu + ";" + it_context_menu;
 
        string const ic_context_menu = InsetCollapsible::contextMenuName();
@@ -730,7 +754,7 @@ string InsetCollapsible::contextMenu(BufferView const & bv, int x,
 
 string InsetCollapsible::contextMenuName() const
 {
-       if (decoration() == InsetLayout::CONGLOMERATE)
+       if (decoration() == InsetDecoration::CONGLOMERATE)
                return "context-conglomerate";
        else
                return "context-collapsible";