2019-06-26 03:35:10 +00:00
|
|
|
/* Constraints/Simple
|
2020-09-13 18:59:33 +00:00
|
|
|
* #Keywords: GtkLayoutManager
|
2019-06-13 18:33:36 +00:00
|
|
|
*
|
|
|
|
* GtkConstraintLayout provides a layout manager that uses relations
|
|
|
|
* between widgets (also known as "constraints") to compute the position
|
|
|
|
* and size of each child.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
G_DECLARE_FINAL_TYPE (SimpleGrid, simple_grid, SIMPLE, GRID, GtkWidget)
|
|
|
|
|
|
|
|
struct _SimpleGrid
|
|
|
|
{
|
|
|
|
GtkWidget parent_instance;
|
|
|
|
|
|
|
|
GtkWidget *button1, *button2;
|
|
|
|
GtkWidget *button3;
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (SimpleGrid, simple_grid, GTK_TYPE_WIDGET)
|
|
|
|
|
|
|
|
static void
|
2020-05-05 20:57:28 +00:00
|
|
|
simple_grid_dispose (GObject *object)
|
2019-06-13 18:33:36 +00:00
|
|
|
{
|
2020-05-05 20:57:28 +00:00
|
|
|
SimpleGrid *self = SIMPLE_GRID (object);
|
2019-06-13 18:33:36 +00:00
|
|
|
|
2020-05-09 14:33:02 +00:00
|
|
|
g_clear_pointer (&self->button1, gtk_widget_unparent);
|
|
|
|
g_clear_pointer (&self->button2, gtk_widget_unparent);
|
|
|
|
g_clear_pointer (&self->button3, gtk_widget_unparent);
|
2019-06-13 18:33:36 +00:00
|
|
|
|
2020-05-05 20:57:28 +00:00
|
|
|
G_OBJECT_CLASS (simple_grid_parent_class)->dispose (object);
|
2019-06-13 18:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
simple_grid_class_init (SimpleGridClass *klass)
|
|
|
|
{
|
2020-05-05 20:57:28 +00:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
2019-06-13 18:33:36 +00:00
|
|
|
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
|
|
|
|
|
2020-05-05 20:57:28 +00:00
|
|
|
object_class->dispose = simple_grid_dispose;
|
2019-06-13 18:33:36 +00:00
|
|
|
|
|
|
|
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_CONSTRAINT_LAYOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Layout:
|
|
|
|
*
|
2019-06-29 14:15:42 +00:00
|
|
|
* +-------------------------------------+
|
|
|
|
* | +-----------++-------++-----------+ |
|
|
|
|
* | | Child 1 || Space || Child 2 | |
|
|
|
|
* | +-----------++-------++-----------+ |
|
|
|
|
* | +---------------------------------+ |
|
|
|
|
* | | Child 3 | |
|
|
|
|
* | +---------------------------------+ |
|
|
|
|
* +-------------------------------------+
|
2019-06-13 18:33:36 +00:00
|
|
|
*
|
|
|
|
* Constraints:
|
|
|
|
*
|
|
|
|
* super.start = child1.start - 8
|
|
|
|
* child1.width = child2.width
|
2019-06-29 14:15:42 +00:00
|
|
|
* child1.end = space.start
|
|
|
|
* space.end = child2.start
|
2019-06-13 18:33:36 +00:00
|
|
|
* child2.end = super.end - 8
|
|
|
|
* super.start = child3.start - 8
|
|
|
|
* child3.end = super.end - 8
|
|
|
|
* super.top = child1.top - 8
|
|
|
|
* super.top = child2.top - 8
|
|
|
|
* child1.bottom = child3.top - 12
|
|
|
|
* child2.bottom = child3.top - 12
|
|
|
|
* child3.height = child1.height
|
|
|
|
* child3.height = child2.height
|
|
|
|
* child3.bottom = super.bottom - 8
|
|
|
|
*
|
2019-06-29 14:15:42 +00:00
|
|
|
* To add some flexibility, we make the space
|
|
|
|
* stretchable:
|
|
|
|
*
|
|
|
|
* space.width >= 10
|
|
|
|
* space.width = 100
|
|
|
|
* space.width <= 200
|
2019-06-13 18:33:36 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
build_constraints (SimpleGrid *self,
|
|
|
|
GtkConstraintLayout *manager)
|
|
|
|
{
|
2019-06-25 23:38:32 +00:00
|
|
|
GtkConstraintGuide *guide;
|
|
|
|
|
2019-06-27 22:06:53 +00:00
|
|
|
guide = gtk_constraint_guide_new ();
|
2019-06-28 22:54:28 +00:00
|
|
|
gtk_constraint_guide_set_name (guide, "space");
|
2019-06-27 22:06:53 +00:00
|
|
|
gtk_constraint_guide_set_min_size (guide, 10, 10);
|
|
|
|
gtk_constraint_guide_set_nat_size (guide, 100, 10);
|
|
|
|
gtk_constraint_guide_set_max_size (guide, 200, 20);
|
2019-06-29 14:18:23 +00:00
|
|
|
gtk_constraint_guide_set_strength (guide, GTK_CONSTRAINT_STRENGTH_STRONG);
|
2019-06-25 23:38:32 +00:00
|
|
|
gtk_constraint_layout_add_guide (manager, guide);
|
|
|
|
|
2019-06-26 02:35:15 +00:00
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new_constant (GTK_CONSTRAINT_TARGET (self->button1),
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_WIDTH,
|
|
|
|
GTK_CONSTRAINT_RELATION_LE,
|
|
|
|
200.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
2019-06-13 18:33:36 +00:00
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (NULL,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_START,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
self->button1,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_START,
|
|
|
|
1.0,
|
|
|
|
-8.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (self->button1,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_WIDTH,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
self->button2,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_WIDTH,
|
|
|
|
1.0,
|
|
|
|
0.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (self->button1,
|
2019-06-25 23:38:32 +00:00
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_END,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
guide,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_START,
|
|
|
|
1.0,
|
|
|
|
0.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (guide,
|
2019-06-13 18:33:36 +00:00
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_END,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
self->button2,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_START,
|
|
|
|
1.0,
|
2019-06-25 23:38:32 +00:00
|
|
|
0.0,
|
2019-06-13 18:33:36 +00:00
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (self->button2,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_END,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
NULL,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_END,
|
|
|
|
1.0,
|
|
|
|
-8.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (NULL,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_START,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
self->button3,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_START,
|
|
|
|
1.0,
|
|
|
|
-8.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (self->button3,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_END,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
NULL,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_END,
|
|
|
|
1.0,
|
|
|
|
-8.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (NULL,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_TOP,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
self->button1,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_TOP,
|
|
|
|
1.0,
|
|
|
|
-8.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (NULL,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_TOP,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
self->button2,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_TOP,
|
|
|
|
1.0,
|
|
|
|
-8.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (self->button1,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_BOTTOM,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
self->button3,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_TOP,
|
|
|
|
1.0,
|
|
|
|
-12.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (self->button2,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_BOTTOM,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
self->button3,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_TOP,
|
|
|
|
1.0,
|
|
|
|
-12.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (self->button3,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_HEIGHT,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
self->button1,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_HEIGHT,
|
|
|
|
1.0,
|
|
|
|
0.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (self->button3,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_HEIGHT,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
self->button2,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_HEIGHT,
|
|
|
|
1.0,
|
|
|
|
0.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
gtk_constraint_layout_add_constraint (manager,
|
|
|
|
gtk_constraint_new (self->button3,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_BOTTOM,
|
|
|
|
GTK_CONSTRAINT_RELATION_EQ,
|
|
|
|
NULL,
|
|
|
|
GTK_CONSTRAINT_ATTRIBUTE_BOTTOM,
|
|
|
|
1.0,
|
|
|
|
-8.0,
|
|
|
|
GTK_CONSTRAINT_STRENGTH_REQUIRED));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
simple_grid_init (SimpleGrid *self)
|
|
|
|
{
|
|
|
|
GtkWidget *widget = GTK_WIDGET (self);
|
|
|
|
|
|
|
|
self->button1 = gtk_button_new_with_label ("Child 1");
|
|
|
|
gtk_widget_set_parent (self->button1, widget);
|
|
|
|
gtk_widget_set_name (self->button1, "button1");
|
|
|
|
|
|
|
|
self->button2 = gtk_button_new_with_label ("Child 2");
|
|
|
|
gtk_widget_set_parent (self->button2, widget);
|
|
|
|
gtk_widget_set_name (self->button2, "button2");
|
|
|
|
|
|
|
|
self->button3 = gtk_button_new_with_label ("Child 3");
|
|
|
|
gtk_widget_set_parent (self->button3, widget);
|
|
|
|
gtk_widget_set_name (self->button3, "button3");
|
|
|
|
|
|
|
|
GtkLayoutManager *manager = gtk_widget_get_layout_manager (GTK_WIDGET (self));
|
|
|
|
build_constraints (self, GTK_CONSTRAINT_LAYOUT (manager));
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_constraints (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
static GtkWidget *window;
|
|
|
|
|
|
|
|
if (!window)
|
|
|
|
{
|
2020-09-06 02:54:16 +00:00
|
|
|
GtkWidget *box, *grid;
|
2019-06-13 18:33:36 +00:00
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2019-06-13 18:33:36 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (window), gtk_widget_get_display (do_widget));
|
2020-04-30 23:04:08 +00:00
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Constraints");
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
2019-06-13 18:33:36 +00:00
|
|
|
|
|
|
|
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), box);
|
2019-06-13 18:33:36 +00:00
|
|
|
|
|
|
|
grid = g_object_new (simple_grid_get_type (), NULL);
|
|
|
|
gtk_widget_set_hexpand (grid, TRUE);
|
|
|
|
gtk_widget_set_vexpand (grid, TRUE);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), grid);
|
2019-06-13 18:33:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
|
|
|
gtk_widget_show (window);
|
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2019-06-13 18:33:36 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|