Hello,
Here is a correctly working version of the Inset Caption feature. The
only problem is the wide property when writing in it.
You have to uncomment the "#define INSET_CAPTION" line in insetcaption.h
in order to try it (it is disabled by default).
I propose to put this in and to enable it only when the lyx2lyx support
is done and the (minor) wide frame problem is solved.
Objections?
Abdel.
Index: buffer_funcs.C
===================================================================
--- buffer_funcs.C (revision 16867)
+++ buffer_funcs.C (working copy)
@@ -38,6 +38,7 @@
#include "frontends/Alert.h"
#include "insets/insetbibitem.h"
+#include "insets/insetcaption.h"
#include "insets/insetinclude.h"
#include "support/filetools.h"
@@ -350,9 +351,50 @@
return true;
}
+void setCaptions(Paragraph & par, LyXTextClass const & textclass)
+{
+ Counters & counters = textclass.counters();
+ InsetList::iterator it = par.insetlist.begin();
+ InsetList::iterator end = par.insetlist.end();
+ for (; it != end; ++it) {
+ InsetBase & inset = *it->inset;
+ if (inset.lyxCode() != InsetBase::FLOAT_CODE
+ && inset.lyxCode() != InsetBase::WRAP_CODE)
+ continue;
+
+ docstring const & type = inset.getInsetName();
+ if (type.empty())
+ continue;
+
+ Floating const & fl =
textclass.floats().getType(to_ascii(type));
+ // FIXME UNICODE
+ docstring const counter = from_ascii(fl.type());
+ counters.step(counter);
+ int number = counters.value(counter);
+ docstring label = from_utf8(fl.name());
+ ParagraphList & pars = inset.getText(0)->paragraphs();
+ ParagraphList::iterator p = pars.begin();
+ for (; p != pars.end(); ++p) {
+ if (p->insetlist.empty())
+ continue;
+ setCaptions(*p, textclass);
+ InsetList::iterator it2 = p->insetlist.begin();
+ InsetList::iterator end2 = p->insetlist.end();
+ for (; it2 != end2; ++it2) {
+ InsetBase & icap = *it2->inset;
+ if (icap.lyxCode() != InsetBase::CAPTION_CODE)
+ continue;
+ static_cast<InsetCaption
&>(icap).setCount(number);
+ static_cast<InsetCaption
&>(icap).setLabel(label);
+ }
+ }
+ }
+}
+
// set the label of a paragraph. This includes the counters.
-void setLabel(Buffer const & buf, ParIterator & it, LyXTextClass const &
textclass)
+void setLabel(Buffer const & buf, ParIterator & it,
+ LyXTextClass const & textclass, bool do_caption = true)
{
Paragraph & par = *it;
LyXLayout_ptr const & layout = par.layout();
@@ -379,6 +421,12 @@
par.params().labelWidthString(docstring());
}
+#ifdef INSET_CAPTION
+ if (do_caption && !par.insetlist.empty()) {
+ setCaptions(par, textclass);
+ }
+#endif
+
// Optimisation: setLabel() can be called for each for each
// paragraph of the document. So we make the string static to
// avoid the repeated instanciation.
@@ -477,6 +525,8 @@
par.params().labelString(
par.translateIfPossible(layout->labelstring(),
buf.params()));
// In biblio shouldn't be following counters but...
+
+#ifndef INSET_CAPTION
} else if (layout->labeltype == LABEL_SENSITIVE) {
// Search for the first float or wrap inset in the iterator
size_t i = it.depth();
@@ -504,6 +554,7 @@
par.params().labelString(par.translateIfPossible(
layout->labelstring(), buf.params()));
}
+#endif // INSET_CAPTION
} else if (layout->labeltype == LABEL_NO_LABEL)
par.params().labelString(docstring());
@@ -533,7 +584,7 @@
case LABEL_CENTERED_TOP_ENVIRONMENT:
case LABEL_STATIC:
case LABEL_ITEMIZE:
- setLabel(buf, it, buf.params().getLyXTextClass());
+ setLabel(buf, it, buf.params().getLyXTextClass(), false);
return true;
case LABEL_SENSITIVE:
Index: insets/insetcaption.C
===================================================================
--- insets/insetcaption.C (revision 16867)
+++ insets/insetcaption.C (working copy)
@@ -90,41 +90,18 @@
}
-void InsetCaption::setLabel(LCursor & cur) const
+void InsetCaption::setLabel(docstring const & label)
{
- // 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 = to_utf8(in->getInsetName());
- break;
- }
- }
- Floating const & fl = textclass_.floats().getType(s);
- s = fl.name();
- docstring num;
- if (s.empty())
- s = "Senseless";
- else
- num = convert<docstring>(counter_);
-
- // Generate the label
- label = bformat(from_ascii("%1$s %2$s:"), _(s), num);
- }
+ label_ = _(to_ascii(label));
}
bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
{
mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
- LCursor cur = mi.base.bv->cursor();
- setLabel(cur);
- labelwidth_ = theFontMetrics(mi.base.font).width(label);
+ docstring const number = convert<docstring>(counter_);
+ full_label_ = bformat(from_ascii("%1$s %2$s:"), label_, number);
+ labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
dim.wid = labelwidth_;
Dimension textdim;
InsetText::metrics(mi, textdim);
@@ -149,11 +126,9 @@
// the text inset or the paragraph?
// We should also draw the float number (Lgb)
- // See if we can find the name of the float this caption
- // belongs to.
- LCursor cur = pi.base.bv->cursor();
- setLabel(cur);
- labelwidth_ = pi.pain.text(x, y, label, pi.base.font);
+ // Answer: the text inset (in buffer_funcs.C: setCaption).
+
+ labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font);
InsetText::draw(pi, x + labelwidth_, y);
setPosCache(pi, x, y);
}
Index: insets/insetcaption.h
===================================================================
--- insets/insetcaption.h (revision 16867)
+++ insets/insetcaption.h (working copy)
@@ -12,6 +12,8 @@
#ifndef INSETCAPTION_H
#define INSETCAPTION_H
+// In order to try the new caption inset uncomment the following line:
+//#define INSET_CAPTION
#include "insettext.h"
#include "lyxtextclass.h"
@@ -61,18 +63,21 @@
OutputParams const & runparams) const;
///
void setCount(int c) { counter_ = c; }
+ ///
+ void setLabel(docstring const & label);
+
private:
///
- void setLabel(LCursor & cur) const;
- ///
virtual std::auto_ptr<InsetBase> doClone() const;
///
- mutable docstring label;
+ mutable docstring full_label_;
///
mutable int labelwidth_;
///
- mutable int counter_;
+ docstring label_;
///
+ int counter_;
+ ///
LyXTextClass const & textclass_;
};
Index: text3.C
===================================================================
--- text3.C (revision 16867)
+++ text3.C (working copy)
@@ -51,6 +51,7 @@
#include "frontends/Clipboard.h"
#include "frontends/Selection.h"
+#include "insets/insetcaption.h" // Only for the INSET_CAPTION macro.
#include "insets/insetcommand.h"
#include "insets/insetfloatlist.h"
#include "insets/insetnewline.h"
@@ -1127,6 +1128,8 @@
#if 0
case LFUN_LIST_INSERT:
case LFUN_THEOREM_INSERT:
+#endif
+#ifdef INSET_CAPTION
case LFUN_CAPTION_INSERT:
#endif
case LFUN_NOTE_INSERT:
@@ -1159,9 +1162,14 @@
case LFUN_WRAP_INSERT:
doInsertInset(cur, this, cmd, true, true);
cur.posRight();
+#ifndef INSET_CAPTION
// FIXME: the "Caption" name should not be hardcoded,
// but given by the float definition.
cur.dispatch(FuncRequest(LFUN_LAYOUT, "Caption"));
+#else
+ cur.dispatch(FuncRequest(LFUN_CAPTION_INSERT));
+ updateLabels(cur.buffer());
+#endif
break;
case LFUN_INDEX_INSERT: