add/update constraint packing containers, test code

This commit is contained in:
Paul Davis 2020-06-10 14:10:26 -06:00
parent 606866ea00
commit a3039d3895
8 changed files with 1232 additions and 73 deletions

87
libs/canvas/canvas/cbox.h Normal file
View file

@ -0,0 +1,87 @@
/*
* Copyright (C) 2012 Carl Hetherington <carl@carlh.net>
* Copyright (C) 2016 Paul Davis <paul@linuxaudiosystems.com>
* Copyright (C) 2017 Robin Gareus <robin@gareus.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef __CANVAS_CBOX_H__
#define __CANVAS_CBOX_H__
#include <list>
#include "canvas/constraint_packer.h"
namespace ArdourCanvas
{
class Rectangle;
class BoxConstrainedItem;
class LIBCANVAS_API cBox : public ConstraintPacker
{
public:
cBox (Canvas *, Orientation);
cBox (Item *, Orientation);
void set_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);
/* aliases so that CSS box model terms work */
void set_border_width (double w) { set_outline_width (w); }
void set_border_color (Gtkmm2ext::Color c) { set_outline_color (c); }
void remove (Item*);
BoxConstrainedItem* pack_start (Item*, PackOptions primary_axis_packing = PackOptions (0), PackOptions secondary_axis_packing = PackOptions (PackExpand|PackFill));
BoxConstrainedItem* pack_end (Item*, PackOptions primary_axis_packing = PackOptions (0), PackOptions secondary_axis_packing = PackOptions (PackExpand|PackFill));
void set_collapse_on_hide (bool);
void set_homogenous (bool);
void preferred_size(Duple& minimum, Duple& natural) const;
void size_allocate (Rect const &);
ConstrainedItem* add_constrained (Item*);
protected:
Orientation orientation;
double _spacing;
double _top_padding;
double _bottom_padding;
double _left_padding;
double _right_padding;
double _top_margin;
double _bottom_margin;
double _left_margin;
double _right_margin;
void child_changed (bool bbox_changed);
private:
typedef std::list<BoxConstrainedItem*> Order;
Order order;
bool collapse_on_hide;
bool homogenous;
BoxConstrainedItem* pack (Item*, PackOptions primary_axis_packing, PackOptions secondary_axis_packing);
};
}
#endif /* __CANVAS_CBOX_H__ */

View file

@ -0,0 +1,119 @@
/*
* Copyright (C) 2020 Paul Davis <paul@linuxaudiosystems.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef __CANVAS_CONSTRAINED_ITEM_H__
#define __CANVAS_CONSTRAINED_ITEM_H__
#include "kiwi/kiwi.h"
#include "canvas/types.h"
#include "canvas/visibility.h"
namespace ArdourCanvas
{
class Item;
class ConstraintPacker;
class /* LIBCANVAS_API */ ConstrainedItem
{
public:
ConstrainedItem (Item& i);
virtual ~ConstrainedItem ();
Item& item() { return _item; }
kiwi::Variable& left () { return _left; }
kiwi::Variable& right () { return _right; }
kiwi::Variable& top () { return _top; }
kiwi::Variable& bottom () { return _bottom; }
kiwi::Variable& width () { return _width; }
kiwi::Variable& height () { return _height; }
void constrained (ConstraintPacker const & parent);
virtual bool involved (kiwi::Constraint const &) const;
std::vector<kiwi::Constraint> const & constraints() const { return _constraints; }
void add_constraint (kiwi::Constraint const & c) { _constraints.push_back (c); }
protected:
Item& _item;
std::vector<kiwi::Constraint> _constraints;
kiwi::Variable _left;
kiwi::Variable _right;
kiwi::Variable _top;
kiwi::Variable _bottom;
kiwi::Variable _width;
kiwi::Variable _height;
virtual void dump (std::ostream&);
};
class /* LIBCANVAS_API */ BoxConstrainedItem : public ConstrainedItem
{
public:
BoxConstrainedItem (Item& i, PackOptions primary_axis_opts, PackOptions secondary_axis_opts);
~BoxConstrainedItem ();
virtual bool involved (kiwi::Constraint const &) const;
kiwi::Variable& center_x () { return _center_x; }
kiwi::Variable& center_y () { return _center_y; }
kiwi::Variable& left_margin () { return _left_margin; }
kiwi::Variable& right_margin () { return _right_margin; }
kiwi::Variable& top_margin () { return _top_margin; }
kiwi::Variable& bottom_margin () { return _bottom_margin; }
/* Padding is not for use by items or anyone except the parent
* (constraint) container. It is used to space out items that are set
* to expand inside a container but not to "fill" (i.e. the extra space
* is assigned to padding around the item, not the item itself).
*/
kiwi::Variable& left_padding () { return _left_padding; }
kiwi::Variable& right_padding () { return _right_padding; }
kiwi::Variable& top_padding () { return _top_padding; }
kiwi::Variable& bottom_padding () { return _bottom_padding; }
PackOptions primary_axis_pack_options() const { return _primary_axis_pack_options; }
PackOptions secondary_axis_pack_options() const { return _secondary_axis_pack_options; }
void dump (std::ostream&);
private:
kiwi::Variable _center_x;
kiwi::Variable _center_y;
kiwi::Variable _left_margin;
kiwi::Variable _right_margin;
kiwi::Variable _top_margin;
kiwi::Variable _bottom_margin;
kiwi::Variable _left_padding;
kiwi::Variable _right_padding;
kiwi::Variable _top_padding;
kiwi::Variable _bottom_padding;
PackOptions _primary_axis_pack_options;
PackOptions _secondary_axis_pack_options;
};
}
#endif

View file

@ -19,7 +19,10 @@
#ifndef __CANVAS_CONSTRAINT_PACKER_H__
#define __CANVAS_CONSTRAINT_PACKER_H__
#include "canvas/item.h"
#include <list>
#include <map>
#include "canvas/container.h"
#include "kiwi/kiwi.h"
namespace ArdourCanvas
@ -28,40 +31,43 @@ namespace ArdourCanvas
class Rectangle;
class ConstrainedItem;
class LIBCANVAS_API ConstraintPacker : public Item
class LIBCANVAS_API ConstraintPacker : public Container
{
public:
ConstraintPacker (Canvas *);
ConstraintPacker (Item *);
void add (Item *);
void add_front (Item *);
void remove (Item *);
void constrain (kiwi::Constraint const &);
virtual ConstrainedItem* add_constrained (Item* item);
void solve ();
void apply ();
void apply (kiwi::Solver*);
void compute_bounding_box () const;
void render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const;
void preferred_size (Duple& mininum, Duple& natural) const;
void size_allocate (Rect const &);
protected:
void child_changed ();
private:
typedef std::map<Item*,ConstrainedItem*> ConstraintMap;
ConstraintMap constraint_map;
kiwi::Solver _solver;
kiwi::Variable width;
kiwi::Variable height;
Rectangle *self;
bool collapse_on_hide;
protected:
void child_changed (bool bbox_changed);
void reset_self ();
void reposition_children ();
typedef std::map<Item*,ConstrainedItem*> ConstrainedItemMap;
ConstrainedItemMap constrained_map;
typedef std::list<kiwi::Constraint> ConstraintList;
ConstraintList constraint_list;
bool in_alloc;
void add_constrained_internal (Item*, ConstrainedItem*);
void add_constraints (kiwi::Solver&, ConstrainedItem*) const;
};
}