X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2Finsetcollapsable.C;h=aec1bf69ec0fdc5901a2008e87fd99723f979d08;hb=e28331ed63062dea10d0a21b9ec12034b4b17b9a;hp=16b0d68f75a24c847657ab0123893762a3bb14f8;hpb=28ad9602557a145bfd88c79dddecdc34c95c698c;p=lyx.git diff --git a/src/insets/insetcollapsable.C b/src/insets/insetcollapsable.C index 16b0d68f75..aec1bf69ec 100644 --- a/src/insets/insetcollapsable.C +++ b/src/insets/insetcollapsable.C @@ -32,8 +32,10 @@ #include "frontends/FontMetrics.h" #include "frontends/Painter.h" -using lyx::docstring; -using lyx::graphics::PreviewLoader; + +namespace lyx { + +using graphics::PreviewLoader; using std::endl; using std::string; @@ -50,17 +52,34 @@ InsetCollapsable::CollapseStatus InsetCollapsable::status() const InsetCollapsable::InsetCollapsable (BufferParams const & bp, CollapseStatus status) - : InsetText(bp), label(lyx::from_ascii("Label")), status_(status), - openinlined_(false), autoOpen_(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 "; @@ -124,14 +143,13 @@ void InsetCollapsable::read(Buffer const & buf, LyXLex & lex) Dimension InsetCollapsable::dimensionCollapsed() const { Dimension dim; - docstring dlab(label.begin(), label.end()); theFontMetrics(labelfont_).buttonText( - dlab, dim.wid, dim.asc, dim.des); + 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; @@ -143,7 +161,8 @@ void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const if (status() == Open) { InsetText::metrics(mi, textdim_); // This expression should not contain mi.base.texwidth - openinlined_ = textdim_.wid < 0.5 * mi.base.bv->workWidth(); + openinlined_ = !hasFixedWidth() + && textdim_.wid < 0.5 * mi.base.bv->workWidth(); if (openinlined_) { // Correct for button width, and re-fit mi.base.textwidth -= dim.wid; @@ -161,7 +180,16 @@ 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; } @@ -178,8 +206,7 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const button_dim.y1 = top; button_dim.y2 = top + dimc.height(); - docstring dlab(label.begin(), label.end()); - pi.pain.buttonText(xx, top + dimc.asc, dlab, labelfont_); + pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_, mouse_hover_); if (status() == Open) { int textx, texty; @@ -211,12 +238,12 @@ void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const } -void InsetCollapsable::cursorPos - (CursorSlice const & sl, bool boundary, int & x, int & y) const +void InsetCollapsable::cursorPos(BufferView const & bv, + CursorSlice const & sl, bool boundary, int & x, int & y) const { BOOST_ASSERT(status() != Collapsed); - InsetText::cursorPos(sl, boundary, x, y); + InsetText::cursorPos(bv, sl, boundary, x, y); if (status() == Open) { if (openinlined_) @@ -279,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); @@ -293,6 +320,15 @@ void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd) switch (cmd.action) { case LFUN_MOUSE_PRESS: + 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)) @@ -314,36 +350,40 @@ 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: @@ -414,5 +454,8 @@ docstring InsetCollapsable::floatName(string const & type, BufferParams const & FloatList const & floats = bp.getLyXTextClass().floats(); FloatList::const_iterator it = floats[type]; // FIXME UNICODE - return (it == floats.end()) ? lyx::from_ascii(type) : _(it->second.name()); + return (it == floats.end()) ? from_ascii(type) : _(it->second.name()); } + + +} // namespace lyx