On Thu, May 04, 2006 at 11:41:55AM +0200, Georg Baum wrote:
> Martin Vermeer wrote:
> 
> > On Thu, 2006-05-04 at 11:24 +0200, Georg Baum wrote:
> >> Why is the mutable label needed?
> > 
> > Because it complains otherwise:
> 
> OK, I did not really ask what I wanted to know. Second try: Why do you need
> to set the label in draw()? Why not set it in read() and whenever the
> content is changed? I am not a big fan of mutable variables as you can see,
> since they defeat somehow the const concept.

Yes, I see your point, but in this case I see no way to avoid making the
label mutable, especially as we want to generate it on the fly depending
which float it is inside of. Placing it somewhere else than in draw()
will not change that.

Now I have implemented this "float type sensing" as well as getting the
counter value. It all appears to work thanks to the existing
infrastructure. 

If you want to test this, go into text3.C line 1209 (for me) and move
LFUN_INSET_CAPTION outside the comment block. Then the minibuffer
command caption-insert will work.

I propose to just check this in (cannot harm) and take it from there.

- Martin

Index: insetcaption.C
===================================================================
--- insetcaption.C      (revision 13790)
+++ insetcaption.C      (working copy)
@@ -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 <sstream>
 
@@ -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()
 }
 
 
+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
 
        // 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<InsetFloat *>(i2)->params().type;
-       else if (i2->lyxCode() == WRAP_CODE)
-               type = static_cast<InsetWrap *>(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
+       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<string>(textclass_.counters().value(fl.type()));
 
-       // Discover the number...
-       string const num = "#";
+               // 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);
 }
 
 
Index: insetcaption.h
===================================================================
--- insetcaption.h      (revision 13790)
+++ insetcaption.h      (working copy)
@@ -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<InsetBase> doClone() const;
+       ///
+       mutable std::string label;
+       ///
+       mutable int labelwidth_;
+       ///
+       LyXTextClass const & textclass_;
 };
 
 

Attachment: pgp9u7GZ07JuZ.pgp
Description: PGP signature

Reply via email to