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;
///
Dimension const & dimension() const;
Dimension const & dimension() const { return dim_; }
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);
fm.buttonText(str, width, ascent, descent);
button(x, y - ascent, width, descent + ascent, mouseHover);
text(x + 3, y - 1, str, font);
button(x + 1, y - ascent, width - 2, descent + ascent, mouseHover);
text(x + 4, y - 1, str, font);
}

View File

@ -177,11 +177,9 @@ bool InsetBox::metrics(MetricsInfo & m, Dimension & dim) const
MetricsInfo mi = m;
// first round in order to know the minimum size.
InsetCollapsable::metrics(mi, dim);
TextMetrics & tm = mi.base.bv->textMetrics(&text_);
if (hasFixedWidth())
mi.base.textwidth =
std::max(tm.width() + 2 * border_ + (int) (2.5 * TEXT_TO_INSET_OFFSET),
params_.width.inPixels(m.base.textwidth));
mi.base.textwidth = std::max(dim.width(),
params_.width.inPixels(m.base.textwidth));
InsetCollapsable::metrics(mi, dim);
bool const changed = 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
{
int const width_offset = TEXT_TO_INSET_OFFSET / 2;
labelwidth_ = theFontMetrics(mi.base.font).width(full_label_);
// add some space to separate the label from the inset text
labelwidth_ += 2 * TEXT_TO_INSET_OFFSET;
dim.wid = labelwidth_;
Dimension textdim;
dim.wid += width_offset;
// Correct for button and label width
mi.base.textwidth -= dim.wid;
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.asc = textdim.asc;
dim.wid += textdim.wid;
dim.asc += TEXT_TO_INSET_OFFSET;
dim.des += TEXT_TO_INSET_OFFSET;
bool const changed = dim_ != dim;
dim_ = dim;
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).
labelwidth_ = 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;
pi.pain.text(x, y, full_label_, pi.base.font);
InsetText::draw(pi, x + labelwidth_, y);
setPosCache(pi, x, y);
}

View File

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

View File

@ -74,9 +74,6 @@ using std::ostream;
using std::vector;
int InsetText::border_ = 2;
InsetText::InsetText(BufferParams const & bp)
: 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
{
os << "Text\n";
@ -166,12 +174,12 @@ bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
// Hand font through to contained lyxtext:
tm.font_ = mi.base.font;
mi.base.textwidth -= 2 * border_;
mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
tm.metrics(mi, dim);
mi.base.textwidth += 2 * border_;
dim.asc += border_;
dim.des += border_;
dim.wid += 2 * border_;
mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
dim.asc += TEXT_TO_INSET_OFFSET;
dim.des += TEXT_TO_INSET_OFFSET;
dim.wid += 2 * TEXT_TO_INSET_OFFSET;
bool const changed = dim_ != dim;
dim_ = dim;
return changed;
@ -180,22 +188,19 @@ bool InsetText::metrics(MetricsInfo & mi, Dimension & dim) 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_);
if (drawFrame_ || pi.full_repaint) {
int const w = hasFixedWidth() ?
tm.maxWidth() : tm.width() + 2 * border_;
int const a = border_;
int const h = a + tm.height() + border_;
tm.maxWidth() : tm.width() + 2 * TEXT_TO_INSET_OFFSET;
int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
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_)
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,
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);
}

View File

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

View File

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