Compare commits

...

3 Commits

Author SHA1 Message Date
Matthias Clasen
4eb28908b8 Make grid constraint dynamic
Handle dynamic changes:
- adding/removing children
- changing homogeneity
- attaching/detaching from a layout
2019-07-02 20:28:11 +00:00
Matthias Clasen
138195998d Add two grid constraints demos 2019-07-02 12:55:25 -04:00
Matthias Clasen
0468238871 Add a grid constraint implementation
This is a convenience class that can create
all the constraints to set up a grid.
2019-07-02 12:51:44 -04:00
8 changed files with 1238 additions and 0 deletions

View File

@@ -0,0 +1,178 @@
/* Constraints/Grid
*
* GtkConstraintLayout lets you define complex layouts
* like grids.
*/
#include <glib/gi18n.h>
#include <gtk/gtk.h>
G_DECLARE_FINAL_TYPE (ComplexGrid, complex_grid, COMPLEX, GRID, GtkWidget)
struct _ComplexGrid
{
GtkWidget parent_instance;
GtkWidget *button1, *button2, *button3;
GtkWidget *button4, *button5;
};
G_DEFINE_TYPE (ComplexGrid, complex_grid, GTK_TYPE_WIDGET)
static void
complex_grid_destroy (GtkWidget *widget)
{
ComplexGrid *self = COMPLEX_GRID (widget);
g_clear_pointer (&self->button1, gtk_widget_destroy);
g_clear_pointer (&self->button2, gtk_widget_destroy);
g_clear_pointer (&self->button3, gtk_widget_destroy);
g_clear_pointer (&self->button4, gtk_widget_destroy);
g_clear_pointer (&self->button5, gtk_widget_destroy);
GTK_WIDGET_CLASS (complex_grid_parent_class)->destroy (widget);
}
static void
complex_grid_class_init (ComplexGridClass *klass)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
widget_class->destroy = complex_grid_destroy;
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_CONSTRAINT_LAYOUT);
}
/* Layout:
*
* +--------------------------------------+
* | +-----------+ |
* | | Child 4 | |
* | +-----------+-----------+----------+ |
* | | Child 1 | Child 2 | Child 3 | |
* | +-----------+-----------+----------+ |
* | | Child 5 | |
* | +-----------+ |
* +--------------------------------------+
*
*/
static void
build_constraints (ComplexGrid *self,
GtkConstraintLayout *manager)
{
GtkGridConstraint *constraint;
GtkConstraint *s;
s = gtk_constraint_new (NULL, GTK_CONSTRAINT_ATTRIBUTE_LEFT,
GTK_CONSTRAINT_RELATION_EQ,
self->button1, GTK_CONSTRAINT_ATTRIBUTE_LEFT,
1.0, 0.0,
GTK_CONSTRAINT_STRENGTH_REQUIRED);
gtk_constraint_layout_add_constraint (manager, s);
s = gtk_constraint_new (self->button3, GTK_CONSTRAINT_ATTRIBUTE_RIGHT,
GTK_CONSTRAINT_RELATION_EQ,
NULL, GTK_CONSTRAINT_ATTRIBUTE_RIGHT,
1.0, 0.0,
GTK_CONSTRAINT_STRENGTH_REQUIRED);
gtk_constraint_layout_add_constraint (manager, s);
s = gtk_constraint_new (NULL, GTK_CONSTRAINT_ATTRIBUTE_TOP,
GTK_CONSTRAINT_RELATION_EQ,
self->button4, GTK_CONSTRAINT_ATTRIBUTE_TOP,
1.0, 0.0,
GTK_CONSTRAINT_STRENGTH_REQUIRED);
gtk_constraint_layout_add_constraint (manager, s);
s = gtk_constraint_new (NULL, GTK_CONSTRAINT_ATTRIBUTE_BOTTOM,
GTK_CONSTRAINT_RELATION_EQ,
self->button5, GTK_CONSTRAINT_ATTRIBUTE_BOTTOM,
1.0, 0.0,
GTK_CONSTRAINT_STRENGTH_REQUIRED);
gtk_constraint_layout_add_constraint (manager, s);
constraint = gtk_grid_constraint_new ();
g_object_set (constraint, "column-homogeneous", TRUE, NULL);
gtk_grid_constraint_add (constraint, self->button1, 0, 1, 0, 1);
gtk_grid_constraint_add (constraint, self->button2, 1, 2, 0, 1);
gtk_grid_constraint_add (constraint, self->button3, 2, 3, 0, 1);
gtk_grid_constraint_attach (constraint, manager);
constraint = gtk_grid_constraint_new ();
g_object_set (constraint, "row-homogeneous", TRUE, NULL);
gtk_grid_constraint_add (constraint, self->button4, 0, 1, 0, 1);
gtk_grid_constraint_add (constraint, self->button2, 0, 1, 1, 2);
gtk_grid_constraint_add (constraint, self->button5, 0, 1, 2, 3);
gtk_grid_constraint_attach (constraint, manager);
}
static void
complex_grid_init (ComplexGrid *self)
{
GtkWidget *widget = GTK_WIDGET (self);
GtkLayoutManager *manager = gtk_widget_get_layout_manager (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");
self->button4 = gtk_button_new_with_label ("Child 4");
gtk_widget_set_parent (self->button4, widget);
gtk_widget_set_name (self->button4, "button4");
self->button5 = gtk_button_new_with_label ("Child 5");
gtk_widget_set_parent (self->button5, widget);
gtk_widget_set_name (self->button5, "button5");
build_constraints (self, GTK_CONSTRAINT_LAYOUT (manager));
}
GtkWidget *
do_constraints4 (GtkWidget *do_widget)
{
static GtkWidget *window;
if (!window)
{
GtkWidget *header, *box, *grid, *button;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_display (GTK_WINDOW (window), gtk_widget_get_display (do_widget));
header = gtk_header_bar_new ();
gtk_header_bar_set_title (GTK_HEADER_BAR (header), "Constraints");
gtk_header_bar_set_show_title_buttons (GTK_HEADER_BAR (header), FALSE);
gtk_window_set_titlebar (GTK_WINDOW (window), header);
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
gtk_container_add (GTK_CONTAINER (window), box);
grid = g_object_new (complex_grid_get_type (), NULL);
gtk_widget_set_hexpand (grid, TRUE);
gtk_widget_set_vexpand (grid, TRUE);
gtk_container_add (GTK_CONTAINER (box), grid);
button = gtk_button_new_with_label ("Close");
gtk_container_add (GTK_CONTAINER (box), button);
gtk_widget_set_hexpand (grid, TRUE);
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_widget_destroy), window);
}
if (!gtk_widget_get_visible (window))
gtk_widget_show (window);
else
gtk_widget_destroy (window);
return window;
}

View File

@@ -0,0 +1,354 @@
/* Constraints/Words
*
* GtkConstraintLayout lets you define big grids.
*/
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#define WORDS_TYPE_BASE (words_base_get_type ())
#define WORDS_TYPE_GRID (words_grid_get_type ())
#define WORDS_TYPE_CONSTRAINT (words_constraint_get_type ())
typedef struct
{
GtkWidget parent_instance;
} WordsBase;
typedef WordsBase WordsGrid;
typedef WordsBase WordsConstraint;
typedef GtkWidgetClass WordsBaseClass;
typedef GtkWidgetClass WordsGridClass;
typedef GtkWidgetClass WordsConstraintClass;
G_DEFINE_TYPE (WordsBase, words_base, GTK_TYPE_WIDGET)
G_DEFINE_TYPE (WordsGrid, words_grid, WORDS_TYPE_BASE)
G_DEFINE_TYPE (WordsConstraint, words_constraint, WORDS_TYPE_BASE)
static void
words_grid_init (WordsGrid *words)
{
}
static void
words_grid_class_init (WordsGridClass *class)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_GRID_LAYOUT);
}
static void
words_constraint_init (WordsGrid *words)
{
}
static void
words_constraint_class_init (WordsConstraintClass *class)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_CONSTRAINT_LAYOUT);
}
static void
word_base_dispose (GObject *object)
{
GtkWidget *self = GTK_WIDGET (object);
GtkWidget *child;
while ((child = gtk_widget_get_first_child (self)) != NULL)
gtk_widget_unparent (child);
G_OBJECT_CLASS (words_base_parent_class)->dispose (object);
}
static void
words_base_class_init (WordsBaseClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->dispose = word_base_dispose;
}
static int num_words = 100;
static gboolean use_constraints = FALSE;
static void
read_words (WordsBase *self)
{
GBytes *data;
const char *words;
int left, top;
GtkWidget *child = NULL;
GtkLayoutManager *layout = gtk_widget_get_layout_manager (GTK_WIDGET (self));
GtkGridConstraint *grid;
GtkConstraint *constraint;
int count;
int rightmost;
GtkWidget *right_child = NULL;
gboolean use_constraint = GTK_IS_CONSTRAINT_LAYOUT (layout);
if (use_constraint)
{
grid = gtk_grid_constraint_new ();
g_object_set (grid,
"row-homogeneous", TRUE,
"column-homogeneous", FALSE,
NULL);
}
else
{
gtk_grid_layout_set_row_homogeneous (GTK_GRID_LAYOUT (layout), TRUE);
gtk_grid_layout_set_column_homogeneous (GTK_GRID_LAYOUT (layout), FALSE);
}
data = g_resources_lookup_data ("/constraints5/words", 0, NULL);
words = g_bytes_get_data (data, NULL);
count = 0;
rightmost = 0;
left = 0;
top = 0;
while (words && words[0])
{
char *p = strchr (words, '\n');
char *word;
int len;
if (p)
{
word = strndup (words, p - words);
words = p + 1;
}
else
{
word = strdup (words);
words = NULL;
}
len = strlen (word);
child = gtk_button_new_with_label (word);
if (left + len > 50)
{
top++;
left = 0;
}
gtk_widget_set_parent (child, GTK_WIDGET (self));
if (left + len > rightmost)
{
rightmost = left + len;
right_child = child;
}
if (use_constraint)
{
gtk_grid_constraint_add (grid, child,
left, left + len,
top, top + 1);
if (left == 0 && top == 0)
{
constraint = gtk_constraint_new (NULL,
GTK_CONSTRAINT_ATTRIBUTE_TOP,
GTK_CONSTRAINT_RELATION_EQ,
child,
GTK_CONSTRAINT_ATTRIBUTE_TOP,
1.0,
0.0,
GTK_CONSTRAINT_STRENGTH_REQUIRED);
gtk_constraint_layout_add_constraint (GTK_CONSTRAINT_LAYOUT (layout),
constraint);
constraint = gtk_constraint_new (NULL,
GTK_CONSTRAINT_ATTRIBUTE_LEFT,
GTK_CONSTRAINT_RELATION_EQ,
child,
GTK_CONSTRAINT_ATTRIBUTE_LEFT,
1.0,
0.0,
GTK_CONSTRAINT_STRENGTH_REQUIRED);
gtk_constraint_layout_add_constraint (GTK_CONSTRAINT_LAYOUT (layout),
constraint);
}
}
else
{
GtkGridLayoutChild *grid_child = GTK_GRID_LAYOUT_CHILD (gtk_layout_manager_get_layout_child (layout, child));
g_object_set (grid_child,
"left-attach", left,
"top-attach", top,
"column-span", len,
"row-span", 1,
NULL);
}
left = left + len;
count++;
if (count >= num_words)
break;
}
if (use_constraint)
{
constraint = gtk_constraint_new (NULL,
GTK_CONSTRAINT_ATTRIBUTE_RIGHT,
GTK_CONSTRAINT_RELATION_EQ,
right_child,
GTK_CONSTRAINT_ATTRIBUTE_RIGHT,
1.0,
0.0,
GTK_CONSTRAINT_STRENGTH_REQUIRED);
gtk_constraint_layout_add_constraint (GTK_CONSTRAINT_LAYOUT (layout),
constraint);
constraint = gtk_constraint_new (NULL,
GTK_CONSTRAINT_ATTRIBUTE_BOTTOM,
GTK_CONSTRAINT_RELATION_EQ,
child,
GTK_CONSTRAINT_ATTRIBUTE_BOTTOM,
1.0,
0.0,
GTK_CONSTRAINT_STRENGTH_REQUIRED);
gtk_constraint_layout_add_constraint (GTK_CONSTRAINT_LAYOUT (layout),
constraint);
gtk_grid_constraint_attach (grid, GTK_CONSTRAINT_LAYOUT (layout));
}
g_bytes_unref (data);
}
static void
words_base_init (WordsBase *self)
{
read_words (self);
}
static void
show_words (GtkWidget *parent)
{
GtkWidget *window;
GtkWidget *header, *box, *grid, *button;
GtkWidget *swin;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_transient_for (GTK_WINDOW (window),
GTK_WINDOW (gtk_widget_get_root (parent)));
gtk_window_set_modal (GTK_WINDOW (window), TRUE);
header = gtk_header_bar_new ();
gtk_header_bar_set_title (GTK_HEADER_BAR (header), use_constraints ? "Constraints" : "Grid");
gtk_header_bar_set_show_title_buttons (GTK_HEADER_BAR (header), FALSE);
gtk_window_set_titlebar (GTK_WINDOW (window), header);
gtk_window_set_resizable (GTK_WINDOW (window), TRUE);
gtk_window_set_default_size (GTK_WINDOW (window), 600, 400);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (window), box);
swin = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (swin),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_propagate_natural_width (GTK_SCROLLED_WINDOW (swin), TRUE);
gtk_scrolled_window_set_propagate_natural_height (GTK_SCROLLED_WINDOW (swin), TRUE);
gtk_widget_set_hexpand (swin, TRUE);
gtk_widget_set_vexpand (swin, TRUE);
gtk_widget_set_halign (swin, GTK_ALIGN_FILL);
gtk_widget_set_valign (swin, GTK_ALIGN_FILL);
gtk_container_add (GTK_CONTAINER (box), swin);
if (use_constraints)
grid = g_object_new (WORDS_TYPE_CONSTRAINT, NULL);
else
grid = g_object_new (WORDS_TYPE_GRID, NULL);
gtk_widget_set_halign (swin, GTK_ALIGN_START);
gtk_widget_set_valign (swin, GTK_ALIGN_START);
gtk_container_add (GTK_CONTAINER (swin), grid);
button = gtk_button_new_with_label ("Close");
gtk_container_add (GTK_CONTAINER (box), button);
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (gtk_widget_destroy), window);
gtk_widget_show (window);
}
static void
use_constraints_cb (GtkButton *button)
{
use_constraints = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
}
static void
word_count_cb (GtkSpinButton *button)
{
num_words = gtk_spin_button_get_value_as_int (button);
}
GtkWidget *
do_constraints5 (GtkWidget *do_widget)
{
static GtkWidget *window;
if (!window)
{
GtkWidget *header, *grid, *button, *label;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
header = gtk_header_bar_new ();
gtk_header_bar_set_title (GTK_HEADER_BAR (header), "Words");
gtk_header_bar_set_show_title_buttons (GTK_HEADER_BAR (header), TRUE);
gtk_window_set_titlebar (GTK_WINDOW (window), header);
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
grid = gtk_grid_new ();
g_object_set (grid,
"margin", 12,
"row-spacing", 12,
"column-spacing", 6,
"halign", GTK_ALIGN_FILL,
"valign", GTK_ALIGN_FILL,
"hexpand", TRUE,
"vexpand", TRUE,
NULL);
gtk_container_add (GTK_CONTAINER (window), grid);
label = gtk_label_new ("Constraints:");
gtk_label_set_xalign (GTK_LABEL (label), 1.0);
gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
button = gtk_check_button_new ();
g_signal_connect (button, "clicked", G_CALLBACK (use_constraints_cb), NULL);
gtk_grid_attach (GTK_GRID (grid), button, 1, 0, 1, 1);
label = gtk_label_new ("Words:");
gtk_label_set_xalign (GTK_LABEL (label), 1.0);
gtk_grid_attach (GTK_GRID (grid), label, 0, 1, 1, 1);
button = gtk_spin_button_new_with_range (0, 1300, 1);
g_signal_connect (button, "value-changed", G_CALLBACK (word_count_cb), NULL);
gtk_spin_button_set_value (GTK_SPIN_BUTTON (button), 10);
gtk_grid_attach (GTK_GRID (grid), button, 1, 1, 1, 1);
button = gtk_button_new_with_label ("Show");
gtk_widget_set_halign (button, GTK_ALIGN_END);
gtk_widget_set_valign (button, GTK_ALIGN_END);
g_signal_connect_swapped (button, "clicked",
G_CALLBACK (show_words), window);
gtk_grid_attach (GTK_GRID (grid), button, 0, 2, 2, 1);
}
if (!gtk_widget_get_visible (window))
gtk_widget_show (window);
else
gtk_widget_destroy (window);
return window;
}

View File

@@ -59,6 +59,9 @@
<file>cssview.css</file>
<file>reset.css</file>
</gresource>
<gresource prefix="/constraints5">
<file>words</file>
</gresource>
<gresource prefix="/cursors">
<file>cursors.ui</file>
<file>alias_cursor.png</file>
@@ -153,6 +156,8 @@
<file>constraints.c</file>
<file>constraints2.c</file>
<file>constraints3.c</file>
<file>constraints4.c</file>
<file>constraints5.c</file>
<file>css_accordion.c</file>
<file>css_basics.c</file>
<file>css_blendmodes.c</file>

View File

@@ -11,6 +11,8 @@ demos = files([
'constraints.c',
'constraints2.c',
'constraints3.c',
'constraints4.c',
'constraints5.c',
'css_accordion.c',
'css_basics.c',
'css_blendmodes.c',

View File

@@ -132,6 +132,7 @@
#include <gtk/gtkgesturezoom.h>
#include <gtk/gtkglarea.h>
#include <gtk/gtkgrid.h>
#include <gtk/gtkgridconstraint.h>
#include <gtk/gtkgridlayout.h>
#include <gtk/gtkheaderbar.h>
#include <gtk/gtkicontheme.h>

635
gtk/gtkgridconstraint.c Normal file
View File

@@ -0,0 +1,635 @@
/* gtkgridconstraint.c: Make a grid with constraints
* Copyright 2019 Red Hat, inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Matthias Clasen
*/
#include "config.h"
#include "gtkgridconstraint.h"
#include "gtkintl.h"
#include "gtktypebuiltins.h"
enum {
POS_LEFT,
POS_RIGHT,
POS_TOP,
POS_BOTTOM,
LAST_POS,
SIZE_WIDTH = LAST_POS,
SIZE_HEIGHT,
LAST_CONSTRAINT
};
/* We maintain constraints of the form:
*
* child.top = row_x
* child.bottom = row_y
*
* (and similar for columns). We avoid introducing
* extra variables for rows and columns by keeping
* track of the first child we encounter that ends
* at a given position, and using that instead:
*
* child.top = first.bottom
*
* (assuming that @first is the first child we saw
* that ends at row x (and has its bottom edge there).
*
* For homogeneous grids, we additionally maintain
* constraints of the form:
*
* a.width / a.colspan = b.width / b.colspan
*
* (and similar for heights). We only maintain
* these relations between a child and its
* predecessor in the list of children.
*/
typedef struct {
GtkConstraintTarget *child;
int left;
int right;
int top;
int bottom;
/* We hold a ref on these */
GtkConstraint *constraints[LAST_CONSTRAINT];
} GtkGridConstraintChild;
typedef struct {
GtkConstraintTarget *target;
GtkConstraintAttribute attr;
} Attach;
struct _GtkGridConstraint {
GObject parent;
GtkConstraintLayout *layout;
gboolean row_homogeneous;
gboolean column_homogeneous;
/* List<GtkGridConstraintChild>, owned */
GPtrArray *children;
/* List<GtkConstaint>, not owned */
GPtrArray *constraints;
/* Array<Attach>, not owning Attach.target */
GArray *rows;
GArray *cols;
};
enum {
PROP_ROW_HOMOGENEOUS = 1,
PROP_COLUMN_HOMOGENEOUS,
N_PROPERTIES
};
static GParamSpec *obj_props[N_PROPERTIES];
G_DEFINE_TYPE (GtkGridConstraint, gtk_grid_constraint, G_TYPE_OBJECT)
static void set_row_homogeneous (GtkGridConstraint *self,
gboolean homogeneous);
static void set_column_homogeneous (GtkGridConstraint *self,
gboolean homogeneous);
static void
gtk_constraint_set_property (GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkGridConstraint *self = GTK_GRID_CONSTRAINT (gobject);
switch (prop_id)
{
case PROP_ROW_HOMOGENEOUS:
set_row_homogeneous (self, g_value_get_boolean (value));
break;
case PROP_COLUMN_HOMOGENEOUS:
set_column_homogeneous (self, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break;
}
}
static void
gtk_constraint_get_property (GObject *gobject,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkGridConstraint *self = GTK_GRID_CONSTRAINT (gobject);
switch (prop_id)
{
case PROP_ROW_HOMOGENEOUS:
g_value_set_boolean (value, self->row_homogeneous);
break;
case PROP_COLUMN_HOMOGENEOUS:
g_value_set_boolean (value, self->column_homogeneous);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break;
}
}
static void
gtk_grid_constraint_init (GtkGridConstraint *self)
{
self->children = g_ptr_array_new_with_free_func (g_free);
self->constraints = g_ptr_array_new ();
self->rows = g_array_new (FALSE, TRUE, sizeof (Attach));
self->cols = g_array_new (FALSE, TRUE, sizeof (Attach));
}
static void
gtk_constraint_finalize (GObject *gobject)
{
GtkGridConstraint *self = GTK_GRID_CONSTRAINT (gobject);
g_array_free (self->rows, TRUE);
g_array_free (self->cols, TRUE);
if (self->layout)
gtk_grid_constraint_detach (self);
g_assert (self->constraints->len == 0);
g_ptr_array_free (self->constraints, TRUE);
g_ptr_array_free (self->children, TRUE);
G_OBJECT_CLASS (gtk_grid_constraint_parent_class)->finalize (gobject);
}
static void
gtk_grid_constraint_class_init (GtkGridConstraintClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->set_property = gtk_constraint_set_property;
gobject_class->get_property = gtk_constraint_get_property;
gobject_class->finalize = gtk_constraint_finalize;
/**
* GtkGridConstraint:row-homogeneous:
*
* Whether to make all rows the same height.
*/
obj_props[PROP_ROW_HOMOGENEOUS] =
g_param_spec_boolean ("row-homogeneous",
P_("Row homogeneous"),
P_("Row homogeneous"),
FALSE,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
/**
* GtkGridConstraint:column-homogeneous:
*
* Whether to make all columns the same width.
*/
obj_props[PROP_COLUMN_HOMOGENEOUS] =
g_param_spec_boolean ("column-homogeneous",
P_("Column homogeneous"),
P_("Column homogeneous"),
FALSE,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class, N_PROPERTIES, obj_props);
}
static void
remove_child_constraint (GtkGridConstraint *self,
GtkGridConstraintChild *child,
int pos)
{
if (child->constraints[pos] == NULL)
return;
if (self->layout)
gtk_constraint_layout_remove_constraint (self->layout,
child->constraints[pos]);
g_object_unref (child->constraints[pos]);
child->constraints[pos] = NULL;
}
/* Ensure that the child variable @var is placed
* at the grid edge @pos. If we already have a variable
* that needs to end up there, we use it to assert
* var = vars[top], otherwise we put @var in the
* the list of variables.
*
* @attr may be one of LEFT/RIGHT/TOP/BOTTOM here.
*/
static void
add_child_constraint (GtkGridConstraint *self,
GtkGridConstraintChild *child,
GtkConstraintAttribute attr,
int pos)
{
Attach *attach;
GArray *vars;
int cpos;
switch ((int)attr)
{
case GTK_CONSTRAINT_ATTRIBUTE_LEFT:
vars = self->cols;
cpos = POS_LEFT;
break;
case GTK_CONSTRAINT_ATTRIBUTE_RIGHT:
vars = self->cols;
cpos = POS_RIGHT;
break;
case GTK_CONSTRAINT_ATTRIBUTE_TOP:
vars = self->rows;
cpos = POS_TOP;
break;
case GTK_CONSTRAINT_ATTRIBUTE_BOTTOM:
vars = self->rows;
cpos = POS_BOTTOM;
break;
default:
g_assert_not_reached ();
}
if (vars->len <= pos)
g_array_set_size (vars, pos + 1);
attach = &g_array_index (vars, Attach, pos);
if (attach->target == NULL)
{
attach->target = child->child;
attach->attr = attr;
}
else
{
g_assert (child->constraints[cpos] == NULL);
child->constraints[cpos] = gtk_constraint_new (child->child, attr,
GTK_CONSTRAINT_RELATION_EQ,
attach->target, attach->attr,
1.0, 0.0,
GTK_CONSTRAINT_STRENGTH_REQUIRED);
if (self->layout)
gtk_constraint_layout_add_constraint (self->layout,
child->constraints[cpos]);
}
}
/* Create the contraint:
*
* child1.width/child1.colspan = child2.width/child2.colspan
*
* @attr can be WIDTH or HEIGHT here.
*/
static void
add_homogeneous_constraint (GtkGridConstraint *self,
GtkGridConstraintChild *child1,
GtkGridConstraintChild *child2,
GtkConstraintAttribute attr)
{
int span1, span2;
int pos;
if (attr == GTK_CONSTRAINT_ATTRIBUTE_WIDTH)
{
pos = SIZE_WIDTH;
span1 = child1->right - child1->left;
span2 = child2->right - child2->left;
}
else
{
pos = SIZE_HEIGHT;
span1 = child1->bottom - child1->top;
span2 = child2->bottom - child2->top;
}
g_assert (child1->constraints[pos] == NULL);
child1->constraints[pos] = gtk_constraint_new (child1->child, attr,
GTK_CONSTRAINT_RELATION_EQ,
child2->child, attr,
(double) span1 / (double) span2,
0.0,
GTK_CONSTRAINT_STRENGTH_REQUIRED);
if (self->layout)
gtk_constraint_layout_add_constraint (self->layout,
child1->constraints[pos]);
}
static void
set_row_homogeneous (GtkGridConstraint *self,
gboolean homogeneous)
{
GtkGridConstraintChild *child1;
GtkGridConstraintChild *child2;
int i;
if (self->row_homogeneous == homogeneous)
return;
self->row_homogeneous = homogeneous;
for (i = 1; i < self->children->len; i++)
{
child1 = g_ptr_array_index (self->children, i);
child2 = g_ptr_array_index (self->children, i - 1);
if (homogeneous)
add_homogeneous_constraint (self, child1, child2, GTK_CONSTRAINT_ATTRIBUTE_HEIGHT);
else
remove_child_constraint (self, child1, SIZE_HEIGHT);
}
g_object_notify (G_OBJECT (self), "row-homogeneous");
}
static void
set_column_homogeneous (GtkGridConstraint *self,
gboolean homogeneous)
{
GtkGridConstraintChild *child1;
GtkGridConstraintChild *child2;
int i;
if (self->column_homogeneous == homogeneous)
return;
self->column_homogeneous = homogeneous;
for (i = 1; i < self->children->len; i++)
{
child1 = g_ptr_array_index (self->children, i);
child2 = g_ptr_array_index (self->children, i - 1);
if (homogeneous)
add_homogeneous_constraint (self, child1, child2, GTK_CONSTRAINT_ATTRIBUTE_WIDTH);
else
remove_child_constraint (self, child1, SIZE_WIDTH);
}
g_object_notify (G_OBJECT (self), "column-homogeneous");
}
/* Fix up attachment constraints for the removal of @target.
* We are fixing up the attachment at row/col @rows and
* position @pos. If @target was not the representative
* for this position, there is nothing to do (all of @targets
* constraints are already removed). Otherwise, look over
* all children that are attached to @target for this position,
* pick a new representative, and fix up the constraints
* for all others to attach to the new representative.
*/
static void
fix_up_attach (GtkGridConstraint *self,
GtkConstraintTarget *target,
gboolean rows,
int pos)
{
int i;
Attach *attach;
if (rows)
attach = &g_array_index (self->rows, Attach, pos);
else
attach = &g_array_index (self->cols, Attach, pos);
if (attach->target != target)
return;
attach->target = NULL;
for (i = 0; i < self->children->len; i++)
{
int cpos;
GtkGridConstraintChild *child;
GtkConstraintAttribute attr;
child = g_ptr_array_index (self->children, i);
if (rows && child->top == pos)
{
cpos = POS_TOP;
attr = GTK_CONSTRAINT_ATTRIBUTE_TOP;
}
else if (rows && child->bottom == pos)
{
cpos = POS_BOTTOM;
attr = GTK_CONSTRAINT_ATTRIBUTE_BOTTOM;
}
else if (!rows && child->left == pos)
{
cpos = POS_LEFT;
attr = GTK_CONSTRAINT_ATTRIBUTE_LEFT;
}
else if (!rows && child->right == pos)
{
cpos = POS_LEFT;
attr = GTK_CONSTRAINT_ATTRIBUTE_LEFT;
}
else
continue;
remove_child_constraint (self, child, cpos);
if (attach->target == NULL)
{
attach->target = child->child;
attach->attr = attr;
}
else
{
child->constraints[cpos] =
gtk_constraint_new (child->child, attr,
GTK_CONSTRAINT_RELATION_EQ,
attach->target, attach->attr,
1.0, 0.0,
GTK_CONSTRAINT_STRENGTH_REQUIRED);
if (self->layout)
gtk_constraint_layout_add_constraint (self->layout,
g_object_ref (child->constraints[cpos]));
}
}
}
GtkGridConstraint *
gtk_grid_constraint_new (void)
{
return g_object_new (GTK_TYPE_GRID_CONSTRAINT, NULL);
}
void
gtk_grid_constraint_add (GtkGridConstraint *self,
GtkWidget *child,
int left,
int right,
int top,
int bottom)
{
GtkGridConstraintChild *child1;
GtkGridConstraintChild *child2;
g_return_if_fail (GTK_IS_GRID_CONSTRAINT (self));
g_return_if_fail (GTK_IS_WIDGET (child));
g_return_if_fail (left < right);
g_return_if_fail (top < bottom);
g_return_if_fail (self->layout == NULL);
child1 = g_new0 (GtkGridConstraintChild, 1);
child1->child = GTK_CONSTRAINT_TARGET (child);
child1->left = left;
child1->right = right;
child1->top = top;
child1->bottom = bottom;
if (self->children->len > 0)
child2 = g_ptr_array_index (self->children, self->children->len - 1);
else
child2 = NULL;
add_child_constraint (self, child1, GTK_CONSTRAINT_ATTRIBUTE_TOP, top);
add_child_constraint (self, child1, GTK_CONSTRAINT_ATTRIBUTE_BOTTOM, bottom);
add_child_constraint (self, child1, GTK_CONSTRAINT_ATTRIBUTE_LEFT, left);
add_child_constraint (self, child1, GTK_CONSTRAINT_ATTRIBUTE_RIGHT, right);
if (self->row_homogeneous && child2)
add_homogeneous_constraint (self, child1, child2, GTK_CONSTRAINT_ATTRIBUTE_HEIGHT);
if (self->column_homogeneous && child2)
add_homogeneous_constraint (self, child1, child2, GTK_CONSTRAINT_ATTRIBUTE_WIDTH);
g_ptr_array_add (self->children, child1);
}
void
gtk_grid_constraint_remove (GtkGridConstraint *self,
GtkWidget *child)
{
GtkGridConstraintChild *child1 = NULL;
GtkGridConstraintChild *next = NULL;
GtkGridConstraintChild *prev = NULL;
GtkConstraintTarget *target = (GtkConstraintTarget *)child;
int i;
int left, right, top, bottom;
for (i = 0; i < self->children->len; i++)
{
child1 = g_ptr_array_index (self->children, i);
if (child1->child == target)
break;
child1 = NULL;
}
if (child1 == NULL)
return;
if (i > 0)
prev = g_ptr_array_index (self->children, i - 1);
if (i + 1 < self->children->len)
next = g_ptr_array_index (self->children, i + 1);
for (i = 0; i < LAST_CONSTRAINT; i++)
remove_child_constraint (self, child1, i);
top = child1->top;
bottom = child1->bottom;
left = child1->left;
right = child1->right;
g_ptr_array_remove (self->children, child1);
fix_up_attach (self, target, TRUE, top);
fix_up_attach (self, target, TRUE, bottom);
fix_up_attach (self, target, FALSE, right);
fix_up_attach (self, target, FALSE, left);
if (self->column_homogeneous && next)
{
remove_child_constraint (self, next, SIZE_WIDTH);
if (prev)
add_homogeneous_constraint (self, next, prev, GTK_CONSTRAINT_ATTRIBUTE_WIDTH);
}
if (self->row_homogeneous && next)
{
remove_child_constraint (self, next, SIZE_HEIGHT);
if (prev)
add_homogeneous_constraint (self, next, prev, GTK_CONSTRAINT_ATTRIBUTE_HEIGHT);
}
}
gboolean
gtk_grid_constraint_is_attached (GtkGridConstraint *self)
{
return self->layout != NULL;
}
void
gtk_grid_constraint_attach (GtkGridConstraint *self,
GtkConstraintLayout *layout)
{
int i, j;
g_return_if_fail (self->layout == NULL);
self->layout = layout;
for (i = 0; i < self->children->len; i++)
{
GtkGridConstraintChild *child = g_ptr_array_index (self->children, i);
for (j = 0; j < LAST_CONSTRAINT; j++)
{
if (child->constraints[j] != NULL)
gtk_constraint_layout_add_constraint (self->layout,
g_object_ref (child->constraints[j]));
}
}
}
void
gtk_grid_constraint_detach (GtkGridConstraint *self)
{
int i, j;
if (self->layout == NULL)
return;
for (i = 0; i < self->children->len; i++)
{
GtkGridConstraintChild *child = g_ptr_array_index (self->children, i);
for (j = 0; j < LAST_CONSTRAINT; j++)
{
if (child->constraints[j] != NULL)
gtk_constraint_layout_remove_constraint (self->layout,
child->constraints[j]);
}
}
self->layout = NULL;
}

62
gtk/gtkgridconstraint.h Normal file
View File

@@ -0,0 +1,62 @@
/*
* Copyright 2019 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GTK_GRID_CONSTRAINT_H__
#define __GTK_GRID_CONSTRAINT_H__
#include <gtk/gtkwidget.h>
#include <gtk/gtkconstraintlayout.h>
G_BEGIN_DECLS
#define GTK_TYPE_GRID_CONSTRAINT (gtk_grid_constraint_get_type ())
/**
* GtkGridConstraint:
*
* An object used for managing constraints for children in
* a constraints layout that are to be arranged in a grid.
*/
GDK_AVAILABLE_IN_ALL
G_DECLARE_FINAL_TYPE (GtkGridConstraint, gtk_grid_constraint, GTK, GRID_CONSTRAINT, GObject)
GDK_AVAILABLE_IN_ALL
GtkGridConstraint * gtk_grid_constraint_new (void);
GDK_AVAILABLE_IN_ALL
void gtk_grid_constraint_add (GtkGridConstraint *self,
GtkWidget *child,
int left,
int right,
int top,
int bottom);
GDK_AVAILABLE_IN_ALL
void gtk_grid_constraint_remove (GtkGridConstraint *self,
GtkWidget *child);
GDK_AVAILABLE_IN_ALL
void gtk_grid_constraint_attach (GtkGridConstraint *self,
GtkConstraintLayout *layout);
GDK_AVAILABLE_IN_ALL
void gtk_grid_constraint_detach (GtkGridConstraint *self);
GDK_AVAILABLE_IN_ALL
gboolean gtk_grid_constraint_is_attached (GtkGridConstraint *self);
G_END_DECLS
#endif /* __GTK_GRID_CONSTRAINT_H__ */

View File

@@ -254,6 +254,7 @@ gtk_public_sources = files([
'gtkgesturezoom.c',
'gtkglarea.c',
'gtkgrid.c',
'gtkgridconstraint.c',
'gtkgridlayout.c',
'gtkheaderbar.c',
'gtkicontheme.c',