On Fri, May 05, 2006 at 09:57:09AM +0200, Jean-Marc Lasgouttes wrote:
> >>>>> "Martin" == Martin Vermeer <[EMAIL PROTECTED]> writes:
> 
> Martin> Now I have implemented this "float type sensing" as well as
> Martin> getting the counter value. It all appears to work thanks to
> Martin> the existing infrastructure.
> 
> How does the counter works? I do not see how you do it outside of the
> updateLabels machinery. Also, where do you configure the way it looks?

This is the patch where the counter misbehaviour is fixed. I gave it
some testing and it appears to work OK. But as this is much more
intrusive (it actually replaces caption layout by caption inset!) it
will need more testing.

...and there is a lot more caption-layout special code that can go, if
we go for this solution.

- Martin

Index: text3.C
===================================================================
--- text3.C     (revision 13790)
+++ text3.C     (working copy)
@@ -1206,8 +1206,8 @@ void LyXText::dispatch(LCursor & cur, Fu
 #if 0
        case LFUN_INSET_LIST:
        case LFUN_INSET_THEOREM:
-       case LFUN_INSET_CAPTION:
 #endif
+       case LFUN_INSET_CAPTION:
        case LFUN_INSERT_NOTE:
        case LFUN_INSERT_CHARSTYLE:
        case LFUN_INSERT_BOX:
@@ -1238,9 +1238,8 @@ void LyXText::dispatch(LCursor & cur, Fu
        case LFUN_INSET_WRAP:
                doInsertInset(cur, this, cmd, true, true);
                cur.posRight();
-               // FIXME: the "Caption" name should not be hardcoded,
-               // but given by the float definition.
-               cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
+               cur.dispatch(FuncRequest(LFUN_INSET_CAPTION));
+               updateLabels(cur.buffer());
                break;
 
        case LFUN_INDEX_INSERT: {
Index: buffer_funcs.C
===================================================================
--- buffer_funcs.C      (revision 13790)
+++ buffer_funcs.C      (working copy)
@@ -38,6 +38,7 @@
 #include "frontends/Alert.h"
 
 #include "insets/insetbibitem.h"
+#include "insets/insetcaption.h"
 
 #include "support/filetools.h"
 #include "support/fs_extras.h"
@@ -473,7 +474,9 @@ void setLabel(Buffer const & buf, ParIte
                        par.bibitem()->setCounter(number);
                par.params().labelString(buf.B_(layout->labelstring()));
                // In biblio should't be following counters but...
-       } else if (layout->labeltype == LABEL_SENSITIVE) {
+       } else if (!par.insetlist.empty() 
+               && par.insetlist.begin()->inset->lyxCode() 
+                       == InsetBase::CAPTION_CODE) {
                // Search for the first float or wrap inset in the iterator
                string type;
                size_t i = it.depth();
@@ -487,20 +490,13 @@ void setLabel(Buffer const & buf, ParIte
                        }
                }
 
-               string s;
                if (!type.empty()) {
                        Floating const & fl = textclass.floats().getType(type);
-
                        counters.step(fl.type());
-
-                       // Doesn't work... yet.
-                       s = bformat(_("%1$s #:"), buf.B_(fl.name()));
-               } else {
-                       // par->SetLayout(0);
-                       s = buf.B_(layout->labelstring());
+                       InsetCaption * icap = static_cast<InsetCaption 
*>(par.insetlist.begin()->inset);
+                       icap->setCount(counters.value(fl.type()));
                }
-
-               par.params().labelString(s);
+               par.params().labelString(string());
        } else if (layout->labeltype == LABEL_NO_LABEL)
                par.params().labelString(string());
        else
Index: insets/insetcaption.C
===================================================================
--- insets/insetcaption.C       (revision 13797)
+++ insets/insetcaption.C       (working copy)
@@ -89,9 +89,40 @@ void InsetCaption::cursorPos
 }
 
 
+void InsetCaption::setLabel(LCursor & cur) const
+{
+       // 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>(counter_);
+
+               // Generate the label
+               label = bformat("%1$s %2$s:", _(s), num);
+       }
+}
+
+
 void InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
 {
        mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
+       LCursor cur = mi.base.bv->cursor();
+       setLabel(cur);
        labelwidth_ = font_metrics::width(label, mi.base.font);
        dim.wid = labelwidth_;
        Dimension textdim;
@@ -118,30 +149,7 @@ void InsetCaption::draw(PainterInfo & pi
        // See if we can find the name of the float this caption
        // belongs to.
        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()));
-
-               // Generate the label
-               label = bformat("%1$s %2$s:", _(s), num);
-       }
+       setLabel(cur);
 
        labelwidth_ = font_metrics::width(label, pi.base.font);
        pi.pain.text(x, y, label, pi.base.font);
Index: insets/insetcaption.h
===================================================================
--- insets/insetcaption.h       (revision 13797)
+++ insets/insetcaption.h       (working copy)
@@ -38,6 +38,8 @@ public:
        virtual void cursorPos 
                (CursorSlice const & sl, bool boundary, int & x, int & y) const;
        ///
+       bool descendable() const { return true; }
+       ///
        virtual void metrics(MetricsInfo & mi, Dimension & dim) const;
        ///
        virtual void draw(PainterInfo & pi, int x, int y) const;
@@ -54,14 +56,20 @@ public:
        ///
        int docbook(Buffer const & buf, std::ostream & os,
                    OutputParams const & runparams) const;
+       ///
+       void setCount(int c) { counter_ = c; }
 private:
        ///
+       void setLabel(LCursor & cur) const;
+       ///
        virtual std::auto_ptr<InsetBase> doClone() const;
        ///
        mutable std::string label;
        ///
        mutable int labelwidth_;
        ///
+       mutable int counter_;
+       ///
        LyXTextClass const & textclass_;
 };
 

Attachment: pgpabKthEDHIu.pgp
Description: PGP signature

Reply via email to