Cleanup the TEXT_TO_INSET_OFFSET mess. This correction is done now once in InsetText. Various other cleanup resulting from this change. In particular, pave the way to the removal of Dimension Inset::dim_ member by registering the inset dimension in RowPainter::paintInset() instead of InsetXXX::draw().

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20326 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2007-09-17 21:45:14 +00:00
parent f69e03ae89
commit 74acf6b528
8 changed files with 49 additions and 62 deletions

View File

@ -57,7 +57,7 @@ public:
int parPosition(pit_type pit) const; int parPosition(pit_type pit) const;
/// ///
Dimension const & dimension() const; Dimension const & dimension() const { return dim_; }
Point const & origin() const { return origin_; } Point const & origin() const { return origin_; }

View File

@ -79,8 +79,8 @@ void Painter::buttonText(int x, int y, docstring const & str,
FontMetrics const & fm = theFontMetrics(font); FontMetrics const & fm = theFontMetrics(font);
fm.buttonText(str, width, ascent, descent); fm.buttonText(str, width, ascent, descent);
button(x, y - ascent, width, descent + ascent, mouseHover); button(x + 1, y - ascent, width - 2, descent + ascent, mouseHover);
text(x + 3, y - 1, str, font); text(x + 4, y - 1, str, font);
} }

View File

@ -177,11 +177,9 @@ bool InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
MetricsInfo mi = m; MetricsInfo mi = m;
// first round in order to know the minimum size. // first round in order to know the minimum size.
InsetCollapsable::metrics(mi, dim); InsetCollapsable::metrics(mi, dim);
TextMetrics & tm = mi.base.bv->textMetrics(&text_);
if (hasFixedWidth()) if (hasFixedWidth())
mi.base.textwidth = mi.base.textwidth = std::max(dim.width(),
std::max(tm.width() + 2 * border_ + (int) (2.5 * TEXT_TO_INSET_OFFSET), params_.width.inPixels(m.base.textwidth));
params_.width.inPixels(m.base.textwidth));
InsetCollapsable::metrics(mi, dim); InsetCollapsable::metrics(mi, dim);
bool const changed = dim_ != dim; bool const changed = dim_ != dim;
dim_ = dim; dim_ = dim;

View File

@ -127,14 +127,11 @@ void InsetCaption::addToToc(TocList & toclist, Buffer const & buf, ParConstItera
bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
{ {
int const width_offset = TEXT_TO_INSET_OFFSET / 2;
labelwidth_ = theFontMetrics(mi.base.font).width(full_label_); labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
// add some space to separate the label from the inset text // add some space to separate the label from the inset text
labelwidth_ += 2 * TEXT_TO_INSET_OFFSET; labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
dim.wid = labelwidth_; dim.wid = labelwidth_;
Dimension textdim; Dimension textdim;
dim.wid += width_offset;
// Correct for button and label width // Correct for button and label width
mi.base.textwidth -= dim.wid; mi.base.textwidth -= dim.wid;
InsetText::metrics(mi, textdim); InsetText::metrics(mi, textdim);
@ -142,8 +139,6 @@ bool InsetCaption::metrics(MetricsInfo & mi, Dimension & dim) const
dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des); dim.des = std::max(dim.des - textdim.asc + dim.asc, textdim.des);
dim.asc = textdim.asc; dim.asc = textdim.asc;
dim.wid += textdim.wid; dim.wid += textdim.wid;
dim.asc += TEXT_TO_INSET_OFFSET;
dim.des += TEXT_TO_INSET_OFFSET;
bool const changed = dim_ != dim; bool const changed = dim_ != dim;
dim_ = dim; dim_ = dim;
return changed; return changed;
@ -160,11 +155,8 @@ void InsetCaption::draw(PainterInfo & pi, int x, int y) const
// Answer: the text inset (in buffer_funcs.cpp: setCaption). // Answer: the text inset (in buffer_funcs.cpp: setCaption).
labelwidth_ = pi.pain.text(x, y, full_label_, pi.base.font); pi.pain.text(x, y, full_label_, pi.base.font);
// add some space to separate the label from the inset text
labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
InsetText::draw(pi, x + labelwidth_, y); InsetText::draw(pi, x + labelwidth_, y);
setPosCache(pi, x, y);
} }

View File

@ -36,8 +36,9 @@
namespace lyx { namespace lyx {
using std::endl; using std::endl;
using std::string; using std::max;
using std::ostream; using std::ostream;
using std::string;
InsetCollapsable::CollapseStatus InsetCollapsable::status() const InsetCollapsable::CollapseStatus InsetCollapsable::status() const
@ -171,10 +172,7 @@ Dimension InsetCollapsable::dimensionCollapsed() const
bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
{ {
using std::max;
autoOpen_ = mi.base.bv->cursor().isInside(this); autoOpen_ = mi.base.bv->cursor().isInside(this);
mi.base.textwidth -= int(1.5 * TEXT_TO_INSET_OFFSET);
switch (geometry()) { switch (geometry()) {
case NoButton: case NoButton:
@ -224,10 +222,7 @@ bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
} }
break; break;
} }
dim.asc += TEXT_TO_INSET_OFFSET;
dim.des += TEXT_TO_INSET_OFFSET;
dim.wid += int(1.5 * TEXT_TO_INSET_OFFSET);
mi.base.textwidth += int(1.5 * TEXT_TO_INSET_OFFSET);
bool const changed = dim_ != dim; bool const changed = dim_ != dim;
dim_ = dim; dim_ = dim;
return changed; return changed;
@ -246,7 +241,6 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
autoOpen_ = pi.base.bv->cursor().isInside(this); autoOpen_ = pi.base.bv->cursor().isInside(this);
int const old_color = pi.background_color; int const old_color = pi.background_color;
pi.background_color = backgroundColor(); pi.background_color = backgroundColor();
int const xx = x + TEXT_TO_INSET_OFFSET;
// Draw button first -- top, left or only // Draw button first -- top, left or only
Dimension dimc = dimensionCollapsed(); Dimension dimc = dimensionCollapsed();
@ -254,12 +248,12 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
if (geometry() == TopButton || if (geometry() == TopButton ||
geometry() == LeftButton || geometry() == LeftButton ||
geometry() == ButtonOnly) { geometry() == ButtonOnly) {
button_dim.x1 = xx + 0; button_dim.x1 = x + 0;
button_dim.x2 = xx + dimc.width(); button_dim.x2 = x + dimc.width();
button_dim.y1 = y - dimc.asc; button_dim.y1 = y - dimc.asc;
button_dim.y2 = y + dimc.des; button_dim.y2 = y + dimc.des;
pi.pain.buttonText(xx, y, layout_.labelstring, layout_.labelfont, mouse_hover_); pi.pain.buttonText(x, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
} else { } else {
button_dim.x1 = 0; button_dim.x1 = 0;
button_dim.y1 = 0; button_dim.y1 = 0;
@ -267,29 +261,30 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
button_dim.y2 = 0; button_dim.y2 = 0;
} }
Dimension const textdim = InsetText::dimension(*pi.base.bv);
int const baseline = y; int const baseline = y;
int textx, texty; int textx, texty;
switch (geometry()) { switch (geometry()) {
case LeftButton: case LeftButton:
textx = xx + dimc.width(); textx = x + dimc.width();
texty = baseline; texty = baseline;
InsetText::draw(pi, textx, texty); InsetText::draw(pi, textx, texty);
break; break;
case TopButton: case TopButton:
textx = xx; textx = x;
texty = baseline + dimc.height(); texty = baseline + dimc.des + textdim.asc;
InsetText::draw(pi, textx, texty); InsetText::draw(pi, textx, texty);
break; break;
case ButtonOnly: case ButtonOnly:
break; break;
case NoButton: case NoButton:
textx = xx; textx = x;
texty = baseline; texty = baseline;
InsetText::draw(pi, textx, texty); InsetText::draw(pi, textx, texty);
break; break;
case SubLabel: case SubLabel:
case Corners: case Corners:
textx = xx; textx = x;
texty = baseline; texty = baseline;
const_cast<InsetCollapsable *>(this)->setDrawFrame(false); const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
InsetText::draw(pi, textx, texty); InsetText::draw(pi, textx, texty);
@ -301,9 +296,8 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
else else
desc -= 3; desc -= 3;
const int xx1 = xx + border_ - 1; const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
const int xx2 = x + dim_.wid - border_ const int xx2 = x + dim_.wid - 2 * TEXT_TO_INSET_OFFSET - 1;
- TEXT_TO_INSET_OFFSET + 1;
pi.pain.line(xx1, y + desc - 4, pi.pain.line(xx1, y + desc - 4,
xx1, y + desc, xx1, y + desc,
layout_.labelfont.color()); layout_.labelfont.color());
@ -353,14 +347,12 @@ void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
} }
break; break;
} }
setPosCache(pi, x, y);
pi.background_color = old_color; pi.background_color = old_color;
} }
void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
{ {
x += TEXT_TO_INSET_OFFSET;
switch (geometry()) { switch (geometry()) {
case LeftButton: case LeftButton:
x += dimensionCollapsed().wid; x += dimensionCollapsed().wid;
@ -387,15 +379,14 @@ void InsetCollapsable::cursorPos(BufferView const & bv,
BOOST_ASSERT(geometry() != ButtonOnly); BOOST_ASSERT(geometry() != ButtonOnly);
InsetText::cursorPos(bv, sl, boundary, x, y); InsetText::cursorPos(bv, sl, boundary, x, y);
Dimension const textdim = InsetText::dimension(bv);
switch (geometry()) { switch (geometry()) {
case LeftButton: case LeftButton:
x += dimensionCollapsed().wid; x += dimensionCollapsed().wid;
break; break;
case TopButton: { case TopButton: {
TextMetrics const & tm = bv.textMetrics(&text_); y += dimensionCollapsed().des + textdim.asc;
y += dimensionCollapsed().height() - ascent()
+ TEXT_TO_INSET_OFFSET + tm.ascent();
break; break;
} }
case NoButton: case NoButton:
@ -407,7 +398,6 @@ void InsetCollapsable::cursorPos(BufferView const & bv,
// Cannot get here // Cannot get here
break; break;
} }
x += TEXT_TO_INSET_OFFSET;
} }

View File

@ -74,9 +74,6 @@ using std::ostream;
using std::vector; using std::vector;
int InsetText::border_ = 2;
InsetText::InsetText(BufferParams const & bp) InsetText::InsetText(BufferParams const & bp)
: drawFrame_(false), frame_color_(Color::insetframe) : drawFrame_(false), frame_color_(Color::insetframe)
{ {
@ -128,6 +125,17 @@ Inset * InsetText::clone() const
} }
Dimension const InsetText::dimension(BufferView const & bv) const
{
TextMetrics const & tm = bv.textMetrics(&text_);
Dimension dim = tm.dimension();
dim.wid += 2 * TEXT_TO_INSET_OFFSET;
dim.des += TEXT_TO_INSET_OFFSET;
dim.asc += TEXT_TO_INSET_OFFSET;
return dim;
}
void InsetText::write(Buffer const & buf, ostream & os) const void InsetText::write(Buffer const & buf, ostream & os) const
{ {
os << "Text\n"; os << "Text\n";
@ -166,12 +174,12 @@ bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
// Hand font through to contained lyxtext: // Hand font through to contained lyxtext:
tm.font_ = mi.base.font; tm.font_ = mi.base.font;
mi.base.textwidth -= 2 * border_; mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
tm.metrics(mi, dim); tm.metrics(mi, dim);
mi.base.textwidth += 2 * border_; mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
dim.asc += border_; dim.asc += TEXT_TO_INSET_OFFSET;
dim.des += border_; dim.des += TEXT_TO_INSET_OFFSET;
dim.wid += 2 * border_; dim.wid += 2 * TEXT_TO_INSET_OFFSET;
bool const changed = dim_ != dim; bool const changed = dim_ != dim;
dim_ = dim; dim_ = dim;
return changed; return changed;
@ -180,22 +188,19 @@ bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
void InsetText::draw(PainterInfo & pi, int x, int y) const void InsetText::draw(PainterInfo & pi, int x, int y) const
{ {
// update our idea of where we are
setPosCache(pi, x, y);
TextMetrics & tm = pi.base.bv->textMetrics(&text_); TextMetrics & tm = pi.base.bv->textMetrics(&text_);
if (drawFrame_ || pi.full_repaint) { if (drawFrame_ || pi.full_repaint) {
int const w = hasFixedWidth() ? int const w = hasFixedWidth() ?
tm.maxWidth() : tm.width() + 2 * border_; tm.maxWidth() : tm.width() + 2 * TEXT_TO_INSET_OFFSET;
int const a = border_; int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
int const h = a + tm.height() + border_; int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
if (pi.full_repaint) if (pi.full_repaint)
pi.pain.fillRectangle(x, y - a - tm.ascent(), w, h, backgroundColor()); pi.pain.fillRectangle(x, yframe, w, h, backgroundColor());
if (drawFrame_) if (drawFrame_)
pi.pain.rectangle(x, y - a - tm.ascent(), w, h, frameColor()); pi.pain.rectangle(x, yframe, w, h, frameColor());
} }
tm.draw(pi, x + border_, y); tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
} }
@ -314,7 +319,7 @@ void InsetText::validate(LaTeXFeatures & features) const
void InsetText::cursorPos(BufferView const & bv, void InsetText::cursorPos(BufferView const & bv,
CursorSlice const & sl, bool boundary, int & x, int & y) const CursorSlice const & sl, bool boundary, int & x, int & y) const
{ {
x = bv.textMetrics(&text_).cursorX(sl, boundary) + border_; x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
y = bv.textMetrics(&text_).cursorY(sl, boundary); y = bv.textMetrics(&text_).cursorY(sl, boundary);
} }

View File

@ -44,6 +44,9 @@ public:
/// ///
InsetText(); InsetText();
///
Dimension const dimension(BufferView const &) const;
/// empty inset to empty par /// empty inset to empty par
void clear(); void clear();
/// ///
@ -157,8 +160,6 @@ private:
public: public:
/// ///
mutable Text text_; mutable Text text_;
///
static int border_;
}; };
} // namespace lyx } // namespace lyx

View File

@ -133,6 +133,7 @@ void RowPainter::paintInset(Inset const * inset, pos_type const pos)
font; font;
pi_.ltr_pos = (bidi_.level(pos) % 2 == 0); pi_.ltr_pos = (bidi_.level(pos) % 2 == 0);
pi_.erased_ = erased_ || par_.isDeleted(pos); pi_.erased_ = erased_ || par_.isDeleted(pos);
pi_.base.bv->coordCache().insets().add(inset, int(x_), yo_);
// insets are painted completely. Recursive // insets are painted completely. Recursive
inset->drawSelection(pi_, int(x_), yo_); inset->drawSelection(pi_, int(x_), yo_);
inset->draw(pi_, int(x_), yo_); inset->draw(pi_, int(x_), yo_);