From 69429f22f503824bda7b4a57d7ec8af67541f921 Mon Sep 17 00:00:00 2001 From: Martin Vermeer Date: Fri, 5 May 2006 05:51:51 +0000 Subject: [PATCH] Restore the caption inset to functionality on-screen * insetcaption.[Ch] (InsetCaption::draw): draw label with surrounding-float-sensitive name and true counter number cursorPos, metrics, edit, editXY: add git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@13797 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/insets/insetcaption.C | 101 +++++++++++++++++++++++++++----------- src/insets/insetcaption.h | 18 +++++++ 2 files changed, 90 insertions(+), 29 deletions(-) diff --git a/src/insets/insetcaption.C b/src/insets/insetcaption.C index a3f37890ea..b205fbe123 100644 --- a/src/insets/insetcaption.C +++ b/src/insets/insetcaption.C @@ -16,6 +16,8 @@ #include "buffer.h" #include "bufferparams.h" +#include "counters.h" +#include "cursor.h" #include "BufferView.h" #include "Floating.h" #include "FloatList.h" @@ -28,6 +30,7 @@ #include "frontends/Painter.h" #include "support/lstrings.h" +#include "support/convert.h" #include @@ -42,7 +45,7 @@ using std::ostringstream; InsetCaption::InsetCaption(BufferParams const & bp) - : InsetText(bp) + : InsetText(bp), textclass_(bp.getLyXTextClass()) { setAutoBreakRows(true); setDrawFrame(true); @@ -78,6 +81,32 @@ string const InsetCaption::editMessage() const } +void InsetCaption::cursorPos + (CursorSlice const & sl, bool boundary, int & x, int & y) const +{ + InsetText::cursorPos(sl, boundary, x, y); + x += labelwidth_; +} + + +void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const +{ + mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET; + labelwidth_ = font_metrics::width(label, mi.base.font); + dim.wid = labelwidth_; + Dimension textdim; + InsetText::metrics(mi, textdim); + dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des); + dim.asc = textdim.asc; + dim.wid += textdim.wid; + dim.asc += TEXT_TO_INSET_OFFSET; + dim.des += TEXT_TO_INSET_OFFSET; + dim.wid += 2 * TEXT_TO_INSET_OFFSET; + mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET; + dim_ = dim; +} + + void InsetCaption::draw(PainterInfo & pi, int x, int y) const { // We must draw the label, we should get the label string @@ -88,36 +117,50 @@ void InsetCaption::draw(PainterInfo & pi, int x, int y) const // See if we can find the name of the float this caption // belongs to. -#if 0 - InsetBase * i1 = owner(); - InsetBase * i2 = i1 ? i1->owner() : 0; - string type; - if (i2->lyxCode() == FLOAT_CODE) -#ifdef WITH_WARNINGS -#warning Now, what happens for i2 == 0? -#endif - type = static_cast(i2)->params().type; - else if (i2->lyxCode() == WRAP_CODE) - type = static_cast(i2)->params().type; - else - BOOST_ASSERT(false); - - FloatList const & floats = - pi.base.bv->buffer()->params().getLyXTextClass().floats(); - string const fl = i2 ? floats.getType(type).name() : N_("Float"); -#else - string type = "float"; - string const fl = N_("Float"); -#endif - - // Discover the number... - string const num = "#"; + LCursor cur = pi.base.bv->cursor(); + // Set caption label _only_ if the cursor is in _this_ float: + if (cur.top().text() == &text_) { + string s; + size_t i = cur.depth(); + while (i > 0) { + --i; + InsetBase * const in = &cur[i].inset(); + if (in->lyxCode() == InsetBase::FLOAT_CODE + || in->lyxCode() == InsetBase::WRAP_CODE) { + s = in->getInsetName(); + break; + } + } + Floating const & fl = textclass_.floats().getType(s); + s = fl.name(); + string num; + if (s.empty()) + s = "Senseless"; + else + num = convert(textclass_.counters().value(fl.type())); + + // Generate the label + label = bformat("%1$s %2$s:", _(s), num); + } - // Generate the label - string const label = bformat("%1$s %2$s:", _(fl), num); - int const w = font_metrics::width(label, pi.base.font); + labelwidth_ = font_metrics::width(label, pi.base.font); pi.pain.text(x, y, label, pi.base.font); - InsetText::draw(pi, x + w, y); + InsetText::draw(pi, x + labelwidth_, y); + setPosCache(pi, x, y); +} + + +void InsetCaption::edit(LCursor & cur, bool left) +{ + cur.push(*this); + InsetText::edit(cur, left); +} + + +InsetBase * InsetCaption::editXY(LCursor & cur, int x, int y) +{ + cur.push(*this); + return InsetText::editXY(cur, x, y); } diff --git a/src/insets/insetcaption.h b/src/insets/insetcaption.h index c356bc7713..8fd7a28972 100644 --- a/src/insets/insetcaption.h +++ b/src/insets/insetcaption.h @@ -14,6 +14,7 @@ #include "insettext.h" +#include "lyxtextclass.h" /** A caption inset */ @@ -28,12 +29,23 @@ public: /// virtual bool display() const; /// + virtual bool neverIndent() const { return true; } + /// virtual InsetBase::Code lyxCode() const; /// virtual std::string const editMessage() const; /// + virtual void cursorPos + (CursorSlice const & sl, bool boundary, int & x, int & y) const; + /// + virtual void metrics(MetricsInfo & mi, Dimension & dim) const; + /// virtual void draw(PainterInfo & pi, int x, int y) const; /// + virtual void edit(LCursor & cur, bool left); + /// + virtual InsetBase * editXY(LCursor & cur, int x, int y); + /// virtual int latex(Buffer const & buf, std::ostream & os, OutputParams const &) const; /// @@ -45,6 +57,12 @@ public: private: /// virtual std::auto_ptr doClone() const; + /// + mutable std::string label; + /// + mutable int labelwidth_; + /// + LyXTextClass const & textclass_; }; -- 2.39.2