13
0

clear up some CSS padding/margin confusion, and add row/col spacing

This commit is contained in:
Paul Davis 2017-01-30 17:11:22 +01:00
parent 46041c0afa
commit 91908dde7e
2 changed files with 50 additions and 35 deletions

View File

@ -37,7 +37,9 @@ public:
Grid (Item *);
Grid (Item *, Duple const & position);
void set_spacing (double s);
void set_row_spacing (double s);
void set_col_spacing (double s);
void set_padding (double top, double right = -1.0, double bottom = -1.0, double left = -1.0);
void set_margin (double top, double right = -1.0, double bottom = -1.0, double left = -1.0);
@ -54,7 +56,8 @@ public:
void render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const;
protected:
double spacing;
double row_spacing;
double col_spacing;
double top_padding, right_padding, bottom_padding, left_padding;
double top_margin, right_margin, bottom_margin, left_margin;
@ -63,11 +66,11 @@ public:
typedef std::map<Item*,Duple> CoordsByItem;
CoordsByItem coords_by_item;
Rectangle *self;
Rectangle *bg;
bool collapse_on_hide;
bool homogenous;
void reset_self ();
void reset_bg ();
void reposition_children ();
};

View File

@ -30,38 +30,44 @@ using std::endl;
Grid::Grid (Canvas* canvas)
: Item (canvas)
, spacing (0)
, row_spacing (0)
, col_spacing (0)
, top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
, top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
, homogenous (false)
{
self = new Rectangle (this);
self->set_outline (false);
self->set_fill (false);
bg = new Rectangle (this);
bg->set_outline (false);
bg->set_fill (false);
bg->hide ();
}
Grid::Grid (Item* parent)
: Item (parent)
, spacing (0)
, row_spacing (0)
, col_spacing (0)
, top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
, top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
, homogenous (false)
{
self = new Rectangle (this);
self->set_outline (false);
self->set_fill (false);
bg = new Rectangle (this);
bg->set_outline (false);
bg->set_fill (false);
bg->hide ();
}
Grid::Grid (Item* parent, Duple const & p)
: Item (parent, p)
, spacing (0)
, row_spacing (0)
, col_spacing (0)
, top_padding (0), right_padding (0), bottom_padding (0), left_padding (0)
, top_margin (0), right_margin (0), bottom_margin (0), left_margin (0)
, homogenous (true)
{
self = new Rectangle (this);
self->set_outline (false);
self->set_fill (false);
bg = new Rectangle (this);
bg->set_outline (false);
bg->set_fill (false);
bg->hide ();
}
void
@ -91,19 +97,25 @@ Grid::compute_bounding_box () const
if (_bounding_box) {
Rect r = _bounding_box;
_bounding_box = r.expand (outline_width() + top_margin,
outline_width() + right_margin,
outline_width() + bottom_margin,
outline_width() + left_margin);
_bounding_box = r.expand (outline_width() + top_margin + top_padding,
outline_width() + right_margin + right_padding,
outline_width() + bottom_margin + bottom_padding,
outline_width() + left_margin + left_padding);
}
_bounding_box_dirty = false;
}
void
Grid::set_spacing (double s)
Grid::set_row_spacing (double s)
{
spacing = s;
row_spacing = s;
}
void
Grid::set_col_spacing (double s)
{
col_spacing = s;
}
void
@ -147,14 +159,14 @@ Grid::set_margin (double t, double r, double b, double l)
}
void
Grid::reset_self ()
Grid::reset_bg ()
{
if (_bounding_box_dirty) {
compute_bounding_box ();
}
if (!_bounding_box) {
self->hide ();
bg->hide ();
return;
}
@ -162,7 +174,7 @@ Grid::reset_self ()
/* XXX need to shrink by margin */
self->set (r);
bg->set (r);
}
void
@ -199,7 +211,7 @@ Grid::reposition_children ()
if (homogenous) {
for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
if (*i == self) {
if (*i == bg) {
continue;
}
@ -216,8 +228,8 @@ Grid::reposition_children ()
cerr << "Uniform size will be " << uniform_size << endl;
for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
if (*i == self) {
/* self-rect is not a normal child */
if (*i == bg) {
/* bg rect is not a normal child */
continue;
}
(*i)->size_allocate (uniform_size);
@ -231,8 +243,8 @@ Grid::reposition_children ()
} else {
for (std::list<Item*>::iterator i = _items.begin(); i != _items.end(); ++i) {
if (*i == self) {
/* self-rect is not a normal child */
if (*i == bg) {
/* bg rect is not a normal child */
continue;
}
@ -255,7 +267,7 @@ Grid::reposition_children ()
* column.
*/
double current_top_edge = top_margin;
double current_top_edge = top_margin + top_padding;
for (uint32_t n = 0; n < max_row; ++n) {
if (row_dimens[n]) {
@ -263,11 +275,11 @@ Grid::reposition_children ()
const double h = row_dimens[n]; /* save height */
row_dimens[n] = current_top_edge;
cerr << "row[" << n << "] @ " << row_dimens[n] << endl;
current_top_edge = current_top_edge + h + top_padding + bottom_padding;
current_top_edge = current_top_edge + h + row_spacing;
}
}
double current_right_edge = left_margin;
double current_right_edge = left_margin + left_padding;
for (uint32_t n = 0; n < max_col; ++n) {
if (col_dimens[n]) {
@ -275,7 +287,7 @@ Grid::reposition_children ()
const double w = col_dimens[n]; /* save width of this column */
col_dimens[n] = current_right_edge;
cerr << "col[" << n << "] @ " << col_dimens[n] << endl;
current_right_edge = current_right_edge + w + left_padding + right_padding;
current_right_edge = current_right_edge + w + col_spacing;
}
}
@ -297,7 +309,7 @@ Grid::reposition_children ()
}
_bounding_box_dirty = true;
reset_self ();
reset_bg ();
}
void