Fix and improves labels and references in mathed.

* InsetMathHull:
- label_: now is a vector of InsetLabel instead of docstring
- addToToc() and updateLabels() update the existing labels.
- nonum_ : use bool instead of int



git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@23435 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2008-03-04 12:22:48 +00:00
parent 81a42b82f4
commit de5fb7caf9
2 changed files with 97 additions and 35 deletions

View File

@ -142,7 +142,7 @@ docstring hullName(HullType type)
InsetMathHull::InsetMathHull() InsetMathHull::InsetMathHull()
: InsetMathGrid(1, 1), type_(hullNone), nonum_(1), label_(1), : InsetMathGrid(1, 1), type_(hullNone), nonum_(1, false), label_(1, 0),
preview_(new RenderPreview(this)) preview_(new RenderPreview(this))
{ {
//lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl; //lyxerr << "sizeof InsetMath: " << sizeof(InsetMath) << endl;
@ -155,7 +155,7 @@ InsetMathHull::InsetMathHull()
InsetMathHull::InsetMathHull(HullType type) InsetMathHull::InsetMathHull(HullType type)
: InsetMathGrid(getCols(type), 1), type_(type), nonum_(1), label_(1), : InsetMathGrid(getCols(type), 1), type_(type), nonum_(1, false), label_(1, 0),
preview_(new RenderPreview(this)) preview_(new RenderPreview(this))
{ {
initMath(); initMath();
@ -164,14 +164,18 @@ InsetMathHull::InsetMathHull(HullType type)
InsetMathHull::InsetMathHull(InsetMathHull const & other) InsetMathHull::InsetMathHull(InsetMathHull const & other)
: InsetMathGrid(other), {
type_(other.type_), nonum_(other.nonum_), label_(other.label_), operator=(other);
preview_(new RenderPreview(*other.preview_, this)) }
{}
InsetMathHull::~InsetMathHull() InsetMathHull::~InsetMathHull()
{} {
for (size_t i = 0; i < label_.size(); ++i) {
if (label_[i])
delete label_[i];
}
}
Inset * InsetMathHull::clone() const Inset * InsetMathHull::clone() const
@ -184,16 +188,49 @@ InsetMathHull & InsetMathHull::operator=(InsetMathHull const & other)
{ {
if (this == &other) if (this == &other)
return *this; return *this;
*static_cast<InsetMathGrid*>(this) = InsetMathGrid(other); InsetMathGrid::operator=(other);
type_ = other.type_; type_ = other.type_;
nonum_ = other.nonum_; nonum_ = other.nonum_;
for (size_t i = 0; i < label_.size(); ++i) {
if (label_[i])
delete label_[i];
}
label_ = other.label_; label_ = other.label_;
for (size_t i = 0; i != label_.size(); ++i) {
if (label_[i])
label_[i] = new InsetLabel(*label_[i]);
}
preview_.reset(new RenderPreview(*other.preview_, this)); preview_.reset(new RenderPreview(*other.preview_, this));
return *this; return *this;
} }
void InsetMathHull::setBuffer(Buffer & buffer)
{
buffer_ = &buffer;
for (size_t i = 0; i != label_.size(); ++i) {
if (label_[i])
label_[i]->setBuffer(buffer);
}
}
void InsetMathHull::updateLabels(ParIterator const & it)
{
if (!buffer_) {
//FIXME: buffer_ should be set at creation for this inset! Problem is
// This inset is created at too many places (see Parser::parse1() in
// MathParser.cpp).
return;
}
for (size_t i = 0; i != label_.size(); ++i) {
if (label_[i])
label_[i]->updateLabels(it);
}
}
void InsetMathHull::addToToc(ParConstIterator const & pit) const void InsetMathHull::addToToc(ParConstIterator const & pit) const
{ {
if (!buffer_) { if (!buffer_) {
@ -203,13 +240,19 @@ void InsetMathHull::addToToc(ParConstIterator const & pit) const
return; return;
} }
vector<docstring> labels; // FIXME: it would be way better to directly use InsetLabel instead of this
getLabelList(labels); // label list. But it should be possible to copy&paste the code in
if (labels.empty()) // InsetLabel::addToToc() anyway.
return;
Toc & toc = buffer().tocBackend().toc("equation"); Toc & toc = buffer().tocBackend().toc("equation");
toc.push_back(TocItem(pit, 0, labels[0]));
for (row_type row = 0; row != nrows(); ++row) {
if (nonum_[row])
continue;
if (label_[row])
label_[row]->addToToc(pit);
toc.push_back(TocItem(pit, 0, nicelabel(row)));
}
} }
@ -436,22 +479,34 @@ bool InsetMathHull::notifyCursorLeaves(Cursor const & /*old*/, Cursor & cur)
docstring InsetMathHull::label(row_type row) const docstring InsetMathHull::label(row_type row) const
{ {
BOOST_ASSERT(row < nrows()); BOOST_ASSERT(row < nrows());
return label_[row]; if (InsetLabel * il = label_[row])
return il->screenLabel();
return docstring();
} }
void InsetMathHull::label(row_type row, docstring const & label) void InsetMathHull::label(row_type row, docstring const & label)
{ {
//lyxerr << "setting label '" << label << "' for row " << row << endl; //lyxerr << "setting label '" << label << "' for row " << row << endl;
label_[row] = label; if (label_[row]) {
label_[row]->updateCommand(label);
return;
}
InsetCommandParams p(LABEL_CODE);
p["name"] = label;
label_[row] = new InsetLabel(p);
if (buffer_)
label_[row]->setBuffer(buffer());
} }
void InsetMathHull::numbered(row_type row, bool num) void InsetMathHull::numbered(row_type row, bool num)
{ {
nonum_[row] = !num; nonum_[row] = !num;
if (nonum_[row]) if (nonum_[row] && label_[row]) {
label_[row].clear(); delete label_[row];
label_[row] = 0;
}
} }
@ -483,8 +538,8 @@ Inset::DisplayType InsetMathHull::display() const
void InsetMathHull::getLabelList(vector<docstring> & labels) const void InsetMathHull::getLabelList(vector<docstring> & labels) const
{ {
for (row_type row = 0; row < nrows(); ++row) for (row_type row = 0; row < nrows(); ++row)
if (!label_[row].empty() && nonum_[row] != 1) if (label_[row] && !nonum_[row])
labels.push_back(label_[row]); labels.push_back(label_[row]->screenLabel());
} }
@ -632,7 +687,7 @@ void InsetMathHull::addRow(row_type row)
if (!rowChangeOK()) if (!rowChangeOK())
return; return;
nonum_.insert(nonum_.begin() + row + 1, !numberedType()); nonum_.insert(nonum_.begin() + row + 1, !numberedType());
label_.insert(label_.begin() + row + 1, docstring()); label_.insert(label_.begin() + row + 1, 0);
InsetMathGrid::addRow(row); InsetMathGrid::addRow(row);
} }
@ -659,6 +714,8 @@ void InsetMathHull::delRow(row_type row)
if (row == nrows() + 1) if (row == nrows() + 1)
row--; row--;
nonum_.erase(nonum_.begin() + row); nonum_.erase(nonum_.begin() + row);
if (label_[row])
delete label_[row];
label_.erase(label_.begin() + row); label_.erase(label_.begin() + row);
} }
@ -683,9 +740,9 @@ docstring InsetMathHull::nicelabel(row_type row) const
{ {
if (nonum_[row]) if (nonum_[row])
return docstring(); return docstring();
if (label_[row].empty()) if (!label_[row])
return from_ascii("(#)"); return from_ascii("(#)");
return '(' + label_[row] + ')'; return '(' + label_[row]->screenLabel() + ')';
} }
@ -832,23 +889,23 @@ void InsetMathHull::mutate(HullType newtype)
else if (type_ == hullEqnArray) { else if (type_ == hullEqnArray) {
if (newtype < type_) { if (newtype < type_) {
// set correct (no)numbering // set correct (no)numbering
bool allnonum = true; nonum_[0] = true;
for (row_type row = 0; row < nrows(); ++row) for (row_type row = 0; row < nrows(); ++row) {
if (!nonum_[row]) if (!nonum_[row]) {
allnonum = false; nonum_[0] = false;
break;
}
}
// set first non-empty label // set first non-empty label
docstring label;
for (row_type row = 0; row < nrows(); ++row) { for (row_type row = 0; row < nrows(); ++row) {
if (!label_[row].empty()) { if (label_[row]) {
label = label_[row]; label_[0] = label_[row];
break; break;
} }
} }
glueall(); glueall();
nonum_[0] = allnonum;
label_[0] = label;
mutate(newtype); mutate(newtype);
} else { // align & Co. } else { // align & Co.
changeCols(2); changeCols(2);
@ -921,8 +978,8 @@ docstring InsetMathHull::eolString(row_type row, bool emptyline, bool fragile) c
{ {
docstring res; docstring res;
if (numberedType()) { if (numberedType()) {
if (!label_[row].empty() && !nonum_[row]) if (label_[row] && !nonum_[row])
res += "\\label{" + label_[row] + '}'; res += "\\label{" + label_[row]->screenLabel() + '}';
if (nonum_[row] && (type_ != hullMultline)) if (nonum_[row] && (type_ != hullMultline))
res += "\\nonumber "; res += "\\nonumber ";
} }

View File

@ -19,6 +19,7 @@
namespace lyx { namespace lyx {
class InsetLabel;
class ParConstIterator; class ParConstIterator;
class RenderPreview; class RenderPreview;
@ -33,6 +34,10 @@ public:
/// ///
~InsetMathHull(); ~InsetMathHull();
/// ///
void setBuffer(Buffer &);
///
void updateLabels(ParIterator const &);
///
void addToToc(ParConstIterator const &) const; void addToToc(ParConstIterator const &) const;
/// ///
InsetMathHull & operator=(InsetMathHull const &); InsetMathHull & operator=(InsetMathHull const &);
@ -180,9 +185,9 @@ private:
/// "none", "simple", "display", "eqnarray",... /// "none", "simple", "display", "eqnarray",...
HullType type_; HullType type_;
/// ///
std::vector<int> nonum_; std::vector<bool> nonum_;
/// ///
std::vector<docstring> label_; std::vector<InsetLabel *> label_;
/// ///
boost::scoped_ptr<RenderPreview> preview_; boost::scoped_ptr<RenderPreview> preview_;
/// ///