]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetcollapsable.C
* src/LyXAction.C: mark goto-clear-bookmark as working without buffer
[lyx.git] / src / insets / insetcollapsable.C
index acfb6d28f80ec7ce0af22c9f96d0d774db3c3c60..aec1bf69ec0fdc5901a2008e87fd99723f979d08 100644 (file)
 #include "insetcollapsable.h"
 
 #include "buffer.h"
+#include "bufferparams.h"
 #include "BufferView.h"
 #include "cursor.h"
 #include "debug.h"
 #include "dispatchresult.h"
+#include "FloatList.h"
 #include "FuncStatus.h"
+#include "gettext.h"
 #include "LColor.h"
 #include "lyxlex.h"
 #include "funcrequest.h"
 #include "metricsinfo.h"
 #include "paragraph.h"
 
-#include "frontends/font_metrics.h"
+#include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
-#include "frontends/LyXView.h"
 
 
-using lyx::graphics::PreviewLoader;
+namespace lyx {
+
+using graphics::PreviewLoader;
 
 using std::endl;
 using std::string;
@@ -40,18 +44,42 @@ using std::min;
 using std::ostream;
 
 
+InsetCollapsable::CollapseStatus InsetCollapsable::status() const
+{
+       return (autoOpen_ && status_ != Inlined) ? Open : status_;
+}
+
+
 InsetCollapsable::InsetCollapsable
                (BufferParams const & bp, CollapseStatus status)
-       : InsetText(bp), label("Label"), status_(status), openinlined_(false)
+       : InsetText(bp), label(from_ascii("Label")), status_(status),
+         openinlined_(false), autoOpen_(false), mouse_hover_(false)
 {
        setAutoBreakRows(true);
        setDrawFrame(true);
        setFrameColor(LColor::collapsableframe);
-       setInsetName("Collapsable");
+       setInsetName(from_ascii("Collapsable"));
        setButtonLabel();
 }
 
 
+InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs):
+       InsetText(rhs),         
+       labelfont_(rhs.labelfont_),
+       button_dim(rhs.button_dim),
+       topx(rhs.topx),
+       topbaseline(rhs.topbaseline),
+       label(rhs.label),
+       status_(rhs.status_),
+       openinlined_(rhs.openinlined_),
+       autoOpen_(rhs.autoOpen_),
+       textdim_(rhs.textdim_),
+       // the sole purpose of this copy constructor
+       mouse_hover_(false)
+{
+}
+
+
 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
 {
        os << "status ";
@@ -115,22 +143,30 @@ void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
 Dimension InsetCollapsable::dimensionCollapsed() const
 {
        Dimension dim;
-       font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
+       theFontMetrics(labelfont_).buttonText(
+               label, dim.wid, dim.asc, dim.des);
        return dim;
 }
 
 
-void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
+bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
 {
+       autoOpen_ = mi.base.bv->cursor().isInside(this);
        mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
-       if (status_ == Inlined) {
+
+       if (status() == Inlined) {
                InsetText::metrics(mi, dim);
        } else {
                dim = dimensionCollapsed();
-               if (status_ == Open) {
+               if (status() == Open) {
                        InsetText::metrics(mi, textdim_);
-                       openinlined_ = (textdim_.wid + dim.wid <= mi.base.textwidth);
+                       // This expression should not contain mi.base.texwidth
+                       openinlined_ = !hasFixedWidth() 
+                               && textdim_.wid < 0.5 * mi.base.bv->workWidth();
                        if (openinlined_) {
+                               // Correct for button width, and re-fit
+                               mi.base.textwidth -= dim.wid;
+                               InsetText::metrics(mi, textdim_);
                                dim.wid += textdim_.wid;
                                dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
                                dim.asc = textdim_.asc;
@@ -144,14 +180,23 @@ void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
        dim.des += TEXT_TO_INSET_OFFSET;
        dim.wid += 2 * TEXT_TO_INSET_OFFSET;
        mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
+       bool const changed = dim_ != dim;
        dim_ = dim;
+       return changed;
+}
+
+
+bool InsetCollapsable::setMouseHover(bool mouse_hover)
+{
+       mouse_hover_ = mouse_hover;
+       return true;
 }
 
 
 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
 {
        const int xx = x + TEXT_TO_INSET_OFFSET;
-       if (status_ == Inlined) {
+       if (status() == Inlined) {
                InsetText::draw(pi, xx, y);
        } else {
                Dimension dimc = dimensionCollapsed();
@@ -161,8 +206,9 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
                button_dim.y1 = top;
                button_dim.y2 = top + dimc.height();
 
-               pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_);
-               if (status_ == Open) {
+               pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_, mouse_hover_);
+
+               if (status() == Open) {
                        int textx, texty;
                        if (openinlined_) {
                                textx = xx + dimc.width();
@@ -181,47 +227,44 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
 {
        x += TEXT_TO_INSET_OFFSET;
-       if (status_ == Open) {
+       if (status() == Open) {
                if (openinlined_)
                        x += dimensionCollapsed().wid;
                else
                        y += dimensionCollapsed().des + textdim_.asc;
        }
-       if (status_ != Collapsed)
+       if (status() != Collapsed)
                InsetText::drawSelection(pi, x, y);
 }
 
 
-void InsetCollapsable::getCursorPos
-       (CursorSlice const & sl, int & x, int & y) const
+void InsetCollapsable::cursorPos(BufferView const & bv, 
+               CursorSlice const & sl, bool boundary, int & x, int & y) const
 {
-       if (status_ == Collapsed) {
-               x = xo();
-               y = yo();
-               return;
-       }
+       BOOST_ASSERT(status() != Collapsed);
+
+       InsetText::cursorPos(bv, sl, boundary, x, y);
 
-       InsetText::getCursorPos(sl, x, y);
-       if (status_ == Open) {
+       if (status() == Open) {
                if (openinlined_)
                        x += dimensionCollapsed().wid;
                else
-                       y += dimensionCollapsed().height() - ascent() + TEXT_TO_INSET_OFFSET + textdim_.asc;
+                       y += dimensionCollapsed().height() - ascent()
+                               + TEXT_TO_INSET_OFFSET + textdim_.asc;
        }
-
        x += TEXT_TO_INSET_OFFSET;
 }
 
 
 InsetBase::EDITABLE InsetCollapsable::editable() const
 {
-       return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
+       return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
 }
 
 
 bool InsetCollapsable::descendable() const
 {
-       return status_ != Collapsed;
+       return status() != Collapsed;
 }
 
 
@@ -231,15 +274,15 @@ bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
 }
 
 
-string const InsetCollapsable::getNewLabel(string const & l) const
+docstring const InsetCollapsable::getNewLabel(docstring const & l) const
 {
-       string label;
+       docstring label;
        pos_type const max_length = 15;
        pos_type const p_siz = paragraphs().begin()->size();
        pos_type const n = min(max_length, p_siz);
        pos_type i = 0;
        pos_type j = 0;
-       for; i < n && j < p_siz; ++j) {
+       for (; i < n && j < p_siz; ++j) {
                if (paragraphs().begin()->isInset(j))
                        continue;
                label += paragraphs().begin()->getChar(j);
@@ -263,7 +306,7 @@ void InsetCollapsable::edit(LCursor & cur, bool left)
 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
 {
        //lyxerr << "InsetCollapsable: edit xy" << endl;
-       if (status_ == Collapsed)
+       if (status() == Collapsed || (button_dim.contains(x, y) && status() != Inlined))
                return this;
        cur.push(*this);
        return InsetText::editXY(cur, x, y);
@@ -277,12 +320,21 @@ void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
 
        switch (cmd.action) {
        case LFUN_MOUSE_PRESS:
-               if (status_ == Inlined)
+               if (cmd.button() == mouse_button::button1 && hitButton(cmd) && status() != Inlined) {
+                       // reset selection if necessary (see bug 3060)
+                       if (cur.selection())
+                               cur.bv().cursor().clearSelection();
+                       else
+                               cur.noUpdate();
+                       cur.dispatched();
+                       break;
+               }
+               if (status() == Inlined)
                        InsetText::doDispatch(cur, cmd);
-               else if (status_ == Open && !hitButton(cmd))
+               else if (status() == Open && !hitButton(cmd))
                        InsetText::doDispatch(cur, cmd);
                else
-                       cur.noUpdate();
+                       cur.undispatched();
                break;
 
        case LFUN_MOUSE_MOTION:
@@ -290,7 +342,7 @@ void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
        case LFUN_MOUSE_TRIPLE:
                if (status_ == Inlined)
                        InsetText::doDispatch(cur, cmd);
-               else if (status_ == Open && !hitButton(cmd))
+               else if (status() && !hitButton(cmd))
                        InsetText::doDispatch(cur, cmd);
                else
                        cur.undispatched();
@@ -298,45 +350,54 @@ void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
 
        case LFUN_MOUSE_RELEASE:
                if (cmd.button() == mouse_button::button3) {
+                       // Open the Inset configuration dialog
                        showInsetDialog(&cur.bv());
                        break;
                }
 
-               switch (status_) {
-
-               case Collapsed:
-                       //lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
-                       setStatus(cur, Open);
-                       edit(cur, true);
-                       cur.bv().cursor() = cur;
+               if (status() == Inlined) {
+                       // The mouse click has to be within the inset!
+                       InsetText::doDispatch(cur, cmd);
                        break;
+               }
 
-               case Open: {
-                       if (hitButton(cmd)) {
-                               //lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
+               if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
+                       // if we are selecting, we do not want to
+                       // toggle the inset.
+                       if (cur.selection())
+                               break;
+                       // Left button is clicked, the user asks to
+                       // toggle the inset visual state.
+                       cur.dispatched();
+                       cur.updateFlags(Update::Force | Update::FitCursor);
+                       if (status() == Collapsed) {
+                               setStatus(cur, Open);
+                               edit(cur, true);
+                       }
+                       else {
                                setStatus(cur, Collapsed);
-                               cur.bv().cursor() = cur;
-                       } else {
-                               //lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
-                               InsetText::doDispatch(cur, cmd);
                        }
+                       cur.bv().cursor() = cur;
                        break;
                }
 
-               case Inlined:
-                       //lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
+               // The mouse click is within the opened inset.
+               if (status() == Open)
                        InsetText::doDispatch(cur, cmd);
-                       break;
-               }
                break;
 
        case LFUN_INSET_TOGGLE:
-               if (cmd.argument == "open")
+               if (cmd.argument() == "open")
                        setStatus(cur, Open);
-               else if (cmd.argument == "close")
+               else if (cmd.argument() == "close")
                        setStatus(cur, Collapsed);
-               else if (cmd.argument == "toggle" || cmd.argument.empty())
-                       setStatus(cur, isOpen() ? Collapsed : Open);
+               else if (cmd.argument() == "toggle" || cmd.argument().empty())
+                       if (isOpen()) {
+                               setStatus(cur, Collapsed);
+                               cur.forwardPosNoDescend();
+                       }
+                       else
+                               setStatus(cur, Open);
                else // if assign or anything else
                        cur.undispatched();
                cur.dispatched();
@@ -355,8 +416,8 @@ bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
        switch (cmd.action) {
 
        case LFUN_INSET_TOGGLE:
-               if (cmd.argument == "open" || cmd.argument == "close" ||
-                   cmd.argument == "toggle")
+               if (cmd.argument() == "open" || cmd.argument() == "close" ||
+                   cmd.argument() == "toggle")
                        flag.enabled(true);
                else
                        flag.enabled(false);
@@ -368,18 +429,7 @@ bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
 }
 
 
-int InsetCollapsable::scroll(bool recursive) const
-{
-       int sx = UpdatableInset::scroll(false);
-
-       if (recursive)
-               sx += InsetText::scroll(false);
-
-       return sx;
-}
-
-
-void InsetCollapsable::setLabel(string const & l)
+void InsetCollapsable::setLabel(docstring const & l)
 {
        label = l;
 }
@@ -399,3 +449,13 @@ void InsetCollapsable::setLabelFont(LyXFont & font)
        labelfont_ = font;
 }
 
+docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp)
+{
+       FloatList const & floats = bp.getLyXTextClass().floats();
+       FloatList::const_iterator it = floats[type];
+       // FIXME UNICODE
+       return (it == floats.end()) ? from_ascii(type) : _(it->second.name());
+}
+
+
+} // namespace lyx