From 09fd2d61c47a1e70b4b1f1dd9474dfd2262175e1 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 18 Nov 2020 18:35:56 -0500 Subject: gtk-demo: Rename some demos This is just to make it easier for myself to find the right sources. --- demos/gtk-demo/constraints.ui | 106 ----------- demos/gtk-demo/constraints2.c | 237 ------------------------ demos/gtk-demo/constraints3.c | 160 ----------------- demos/gtk-demo/constraints4.c | 72 -------- demos/gtk-demo/constraints_builder.c | 72 ++++++++ demos/gtk-demo/constraints_builder.ui | 106 +++++++++++ demos/gtk-demo/constraints_interactive.c | 237 ++++++++++++++++++++++++ demos/gtk-demo/constraints_vfl.c | 160 +++++++++++++++++ demos/gtk-demo/demo.gresource.xml | 18 +- demos/gtk-demo/listbox2.c | 71 -------- demos/gtk-demo/listbox2.ui | 298 ------------------------------- demos/gtk-demo/listbox_controls.c | 71 ++++++++ demos/gtk-demo/listbox_controls.ui | 298 +++++++++++++++++++++++++++++++ demos/gtk-demo/meson.build | 10 +- demos/gtk-demo/overlay2.c | 102 ----------- demos/gtk-demo/overlay_decorative.c | 102 +++++++++++ 16 files changed, 1060 insertions(+), 1060 deletions(-) delete mode 100644 demos/gtk-demo/constraints.ui delete mode 100644 demos/gtk-demo/constraints2.c delete mode 100644 demos/gtk-demo/constraints3.c delete mode 100644 demos/gtk-demo/constraints4.c create mode 100644 demos/gtk-demo/constraints_builder.c create mode 100644 demos/gtk-demo/constraints_builder.ui create mode 100644 demos/gtk-demo/constraints_interactive.c create mode 100644 demos/gtk-demo/constraints_vfl.c delete mode 100644 demos/gtk-demo/listbox2.c delete mode 100644 demos/gtk-demo/listbox2.ui create mode 100644 demos/gtk-demo/listbox_controls.c create mode 100644 demos/gtk-demo/listbox_controls.ui delete mode 100644 demos/gtk-demo/overlay2.c create mode 100644 demos/gtk-demo/overlay_decorative.c diff --git a/demos/gtk-demo/constraints.ui b/demos/gtk-demo/constraints.ui deleted file mode 100644 index 1766f931c7..0000000000 --- a/demos/gtk-demo/constraints.ui +++ /dev/null @@ -1,106 +0,0 @@ - - - - Constraints — Builder - 260 - - - - - - - - - - - - - - - - - - - - - - - - - - - Child 1 - - - - - Child 2 - - - - - Child 3 - - - - - - diff --git a/demos/gtk-demo/constraints2.c b/demos/gtk-demo/constraints2.c deleted file mode 100644 index 782ade175b..0000000000 --- a/demos/gtk-demo/constraints2.c +++ /dev/null @@ -1,237 +0,0 @@ -/* Constraints/Interactive Constraints - * #Keywords: GtkConstraintLayout - * - * This example shows how constraints can be updated during user interaction. - * The vertical edge between the buttons can be dragged with the mouse. - */ - -#include -#include - -G_DECLARE_FINAL_TYPE (InteractiveGrid, interactive_grid, INTERACTIVE, GRID, GtkWidget) - -struct _InteractiveGrid -{ - GtkWidget parent_instance; - - GtkWidget *button1, *button2; - GtkWidget *button3; - GtkConstraintGuide *guide; - GtkConstraint *constraint; -}; - -G_DEFINE_TYPE (InteractiveGrid, interactive_grid, GTK_TYPE_WIDGET) - -static void -interactive_grid_dispose (GObject *object) -{ - InteractiveGrid *self = INTERACTIVE_GRID (object); - - g_clear_pointer (&self->button1, gtk_widget_unparent); - g_clear_pointer (&self->button2, gtk_widget_unparent); - g_clear_pointer (&self->button3, gtk_widget_unparent); - - G_OBJECT_CLASS (interactive_grid_parent_class)->dispose (object); -} - -static void -interactive_grid_class_init (InteractiveGridClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); - - object_class->dispose = interactive_grid_dispose; - - gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_CONSTRAINT_LAYOUT); -} - -static void -build_constraints (InteractiveGrid *self, - GtkConstraintLayout *manager) -{ - self->guide = g_object_new (GTK_TYPE_CONSTRAINT_GUIDE, NULL); - gtk_constraint_layout_add_guide (manager, self->guide); - - gtk_constraint_layout_add_constraint (manager, - gtk_constraint_new_constant (GTK_CONSTRAINT_TARGET (self->guide), - GTK_CONSTRAINT_ATTRIBUTE_WIDTH, - GTK_CONSTRAINT_RELATION_EQ, - 0.0, - GTK_CONSTRAINT_STRENGTH_REQUIRED)); - - gtk_constraint_layout_add_constraint (manager, - gtk_constraint_new (NULL, - GTK_CONSTRAINT_ATTRIBUTE_START, - GTK_CONSTRAINT_RELATION_EQ, - GTK_CONSTRAINT_TARGET (self->button1), - GTK_CONSTRAINT_ATTRIBUTE_START, - 1.0, - -8.0, - GTK_CONSTRAINT_STRENGTH_REQUIRED)); - gtk_constraint_layout_add_constraint (manager, - gtk_constraint_new (GTK_CONSTRAINT_TARGET (self->button1), - GTK_CONSTRAINT_ATTRIBUTE_END, - GTK_CONSTRAINT_RELATION_EQ, - GTK_CONSTRAINT_TARGET (self->guide), - GTK_CONSTRAINT_ATTRIBUTE_START, - 1.0, - 0.0, - GTK_CONSTRAINT_STRENGTH_REQUIRED)); - gtk_constraint_layout_add_constraint (manager, - gtk_constraint_new (GTK_CONSTRAINT_TARGET (self->button2), - GTK_CONSTRAINT_ATTRIBUTE_START, - GTK_CONSTRAINT_RELATION_EQ, - GTK_CONSTRAINT_TARGET (self->guide), - GTK_CONSTRAINT_ATTRIBUTE_END, - 1.0, - 0.0, - GTK_CONSTRAINT_STRENGTH_REQUIRED)); - gtk_constraint_layout_add_constraint (manager, - gtk_constraint_new (GTK_CONSTRAINT_TARGET (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, - GTK_CONSTRAINT_TARGET (self->button3), - GTK_CONSTRAINT_ATTRIBUTE_START, - 1.0, - -8.0, - GTK_CONSTRAINT_STRENGTH_REQUIRED)); - - gtk_constraint_layout_add_constraint (manager, - gtk_constraint_new (GTK_CONSTRAINT_TARGET (self->button3), - GTK_CONSTRAINT_ATTRIBUTE_END, - GTK_CONSTRAINT_RELATION_EQ, - GTK_CONSTRAINT_TARGET (self->guide), - GTK_CONSTRAINT_ATTRIBUTE_START, - 1.0, - 0.0, - GTK_CONSTRAINT_STRENGTH_REQUIRED)); - - gtk_constraint_layout_add_constraint (manager, - gtk_constraint_new (NULL, - GTK_CONSTRAINT_ATTRIBUTE_TOP, - GTK_CONSTRAINT_RELATION_EQ, - GTK_CONSTRAINT_TARGET (self->button1), - GTK_CONSTRAINT_ATTRIBUTE_TOP, - 1.0, - -8.0, - GTK_CONSTRAINT_STRENGTH_REQUIRED)); - gtk_constraint_layout_add_constraint (manager, - gtk_constraint_new (GTK_CONSTRAINT_TARGET (self->button2), - GTK_CONSTRAINT_ATTRIBUTE_TOP, - GTK_CONSTRAINT_RELATION_EQ, - GTK_CONSTRAINT_TARGET (self->button1), - GTK_CONSTRAINT_ATTRIBUTE_BOTTOM, - 1.0, - 0.0, - GTK_CONSTRAINT_STRENGTH_REQUIRED)); - gtk_constraint_layout_add_constraint (manager, - gtk_constraint_new (GTK_CONSTRAINT_TARGET (self->button3), - GTK_CONSTRAINT_ATTRIBUTE_TOP, - GTK_CONSTRAINT_RELATION_EQ, - GTK_CONSTRAINT_TARGET (self->button2), - GTK_CONSTRAINT_ATTRIBUTE_BOTTOM, - 1.0, - 0.0, - GTK_CONSTRAINT_STRENGTH_REQUIRED)); - gtk_constraint_layout_add_constraint (manager, - gtk_constraint_new (GTK_CONSTRAINT_TARGET (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 -drag_cb (GtkGestureDrag *drag, - double offset_x, - double offset_y, - InteractiveGrid *self) -{ - GtkConstraintLayout *layout = GTK_CONSTRAINT_LAYOUT (gtk_widget_get_layout_manager (GTK_WIDGET (self))); - double x, y; - - if (self->constraint) - { - gtk_constraint_layout_remove_constraint (layout, self->constraint); - g_clear_object (&self->constraint); - } - - gtk_gesture_drag_get_start_point (drag, &x, &y); - self->constraint = gtk_constraint_new_constant (GTK_CONSTRAINT_TARGET (self->guide), - GTK_CONSTRAINT_ATTRIBUTE_LEFT, - GTK_CONSTRAINT_RELATION_EQ, - x + offset_x, - GTK_CONSTRAINT_STRENGTH_REQUIRED); - gtk_constraint_layout_add_constraint (layout, g_object_ref (self->constraint)); - gtk_widget_queue_allocate (GTK_WIDGET (self)); -} - -static void -interactive_grid_init (InteractiveGrid *self) -{ - GtkWidget *widget = GTK_WIDGET (self); - GtkGesture *drag; - - 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)); - - drag = gtk_gesture_drag_new (); - g_signal_connect (drag, "drag-update", G_CALLBACK (drag_cb), self); - gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (drag)); -} - -GtkWidget * -do_constraints2 (GtkWidget *do_widget) -{ - static GtkWidget *window; - - if (!window) - { - GtkWidget *box, *grid; - - window = gtk_window_new (); - gtk_window_set_display (GTK_WINDOW (window), gtk_widget_get_display (do_widget)); - gtk_window_set_title (GTK_WINDOW (window), "Interactive Constraints"); - gtk_window_set_default_size (GTK_WINDOW (window), 260, -1); - g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); - - box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12); - gtk_window_set_child (GTK_WINDOW (window), box); - - grid = g_object_new (interactive_grid_get_type (), NULL); - gtk_widget_set_hexpand (grid, TRUE); - gtk_widget_set_vexpand (grid, TRUE); - gtk_box_append (GTK_BOX (box), grid); - } - - if (!gtk_widget_get_visible (window)) - gtk_widget_show (window); - else - gtk_window_destroy (GTK_WINDOW (window)); - - return window; -} diff --git a/demos/gtk-demo/constraints3.c b/demos/gtk-demo/constraints3.c deleted file mode 100644 index b0d15dc2ac..0000000000 --- a/demos/gtk-demo/constraints3.c +++ /dev/null @@ -1,160 +0,0 @@ -/* Constraints/VFL - * - * GtkConstraintLayout allows defining constraints using a - * compact syntax called Visual Format Language, or VFL. - * - * A typical example of a VFL specification looks like this: - * - * H:|-[button1(==button2)]-12-[button2]-| - */ - -#include -#include - -G_DECLARE_FINAL_TYPE (VflGrid, vfl_grid, VFL, GRID, GtkWidget) - -struct _VflGrid -{ - GtkWidget parent_instance; - - GtkWidget *button1, *button2; - GtkWidget *button3; -}; - -G_DEFINE_TYPE (VflGrid, vfl_grid, GTK_TYPE_WIDGET) - -static void -vfl_grid_dispose (GObject *object) -{ - VflGrid *self = VFL_GRID (object); - - g_clear_pointer (&self->button1, gtk_widget_unparent); - g_clear_pointer (&self->button2, gtk_widget_unparent); - g_clear_pointer (&self->button3, gtk_widget_unparent); - - G_OBJECT_CLASS (vfl_grid_parent_class)->dispose (object); -} - -static void -vfl_grid_class_init (VflGridClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); - - object_class->dispose = vfl_grid_dispose; - - gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_CONSTRAINT_LAYOUT); -} - -/* Layout: - * - * +-----------------------------+ - * | +-----------+ +-----------+ | - * | | Child 1 | | Child 2 | | - * | +-----------+ +-----------+ | - * | +-------------------------+ | - * | | Child 3 | | - * | +-------------------------+ | - * +-----------------------------+ - * - * Constraints: - * - * super.start = child1.start - 8 - * child1.width = child2.width - * child1.end = child2.start - 12 - * 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 - * - * Visual format: - * - * H:|-8-[view1(==view2)-12-[view2]-8-| - * H:|-8-[view3]-8-| - * V:|-8-[view1]-12-[view3(==view1)]-8-| - * V:|-8-[view2]-12-[view3(==view2)]-8-| - */ -static void -build_constraints (VflGrid *self, - GtkConstraintLayout *manager) -{ - const char * const vfl[] = { - "H:|-[button1(==button2)]-12-[button2]-|", - "H:|-[button3]-|", - "V:|-[button1]-12-[button3(==button1)]-|", - "V:|-[button2]-12-[button3(==button2)]-|", - }; - GError *error = NULL; - - gtk_constraint_layout_add_constraints_from_description (manager, vfl, G_N_ELEMENTS (vfl), - 8, 8, - &error, - "button1", self->button1, - "button2", self->button2, - "button3", self->button3, - NULL); - if (error != NULL) - { - g_printerr ("VFL parsing error:\n%s", error->message); - g_error_free (error); - } -} - -static void -vfl_grid_init (VflGrid *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_constraints3 (GtkWidget *do_widget) -{ - static GtkWidget *window; - - if (!window) - { - GtkWidget *box, *grid; - - window = gtk_window_new (); - gtk_window_set_display (GTK_WINDOW (window), gtk_widget_get_display (do_widget)); - gtk_window_set_title (GTK_WINDOW (window), "Constraints — VFL"); - gtk_window_set_default_size (GTK_WINDOW (window), 260, -1); - g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); - - box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12); - gtk_window_set_child (GTK_WINDOW (window), box); - - grid = g_object_new (vfl_grid_get_type (), NULL); - gtk_widget_set_hexpand (grid, TRUE); - gtk_widget_set_vexpand (grid, TRUE); - gtk_box_append (GTK_BOX (box), grid); - } - - if (!gtk_widget_get_visible (window)) - gtk_widget_show (window); - else - gtk_window_destroy (GTK_WINDOW (window)); - - return window; -} diff --git a/demos/gtk-demo/constraints4.c b/demos/gtk-demo/constraints4.c deleted file mode 100644 index 4b4ca49643..0000000000 --- a/demos/gtk-demo/constraints4.c +++ /dev/null @@ -1,72 +0,0 @@ -/* Constraints/Builder - * - * GtkConstraintLayouts can be created in .ui files, and constraints can be - * set up at that time as well, as this example demonstrates. It uses the - * same setup as the “Simple” constraints demo. - */ - -#include -#include - -G_DECLARE_FINAL_TYPE (ConstraintsGrid, constraints_grid, CONSTRAINTS, GRID, GtkWidget) - -struct _ConstraintsGrid -{ - GtkWidget parent_instance; -}; - -G_DEFINE_TYPE (ConstraintsGrid, constraints_grid, GTK_TYPE_WIDGET) - -static void -constraints_grid_init (ConstraintsGrid *grid) -{ -} - -static void -constraints_grid_dispose (GObject *object) -{ - GtkWidget *widget = GTK_WIDGET (object); - GtkWidget *child; - - while ((child = gtk_widget_get_first_child (widget))) - gtk_widget_unparent (child); - - G_OBJECT_CLASS (constraints_grid_parent_class)->dispose (object); -} - -static void -constraints_grid_class_init (ConstraintsGridClass *klass) -{ - GObjectClass *object_class = G_OBJECT_CLASS (klass); - - object_class->dispose = constraints_grid_dispose; -} - -GtkWidget * -do_constraints4 (GtkWidget *do_widget) -{ - static GtkWidget *window; - - if (!window) - { - GtkBuilder *builder; - - g_type_ensure (constraints_grid_get_type ()); - - builder = gtk_builder_new_from_resource ("/constraints4/constraints.ui"); - - window = GTK_WIDGET (gtk_builder_get_object (builder, "window1")); - gtk_window_set_display (GTK_WINDOW (window), - gtk_widget_get_display (do_widget)); - g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); - - g_object_unref (builder); - } - - if (!gtk_widget_get_visible (window)) - gtk_widget_show (window); - else - gtk_window_destroy (GTK_WINDOW (window)); - - return window; -} diff --git a/demos/gtk-demo/constraints_builder.c b/demos/gtk-demo/constraints_builder.c new file mode 100644 index 0000000000..1233adaed1 --- /dev/null +++ b/demos/gtk-demo/constraints_builder.c @@ -0,0 +1,72 @@ +/* Constraints/Builder + * + * GtkConstraintLayouts can be created in .ui files, and constraints can be + * set up at that time as well, as this example demonstrates. It uses the + * same setup as the “Simple” constraints demo. + */ + +#include +#include + +G_DECLARE_FINAL_TYPE (ConstraintsGrid, constraints_grid, CONSTRAINTS, GRID, GtkWidget) + +struct _ConstraintsGrid +{ + GtkWidget parent_instance; +}; + +G_DEFINE_TYPE (ConstraintsGrid, constraints_grid, GTK_TYPE_WIDGET) + +static void +constraints_grid_init (ConstraintsGrid *grid) +{ +} + +static void +constraints_grid_dispose (GObject *object) +{ + GtkWidget *widget = GTK_WIDGET (object); + GtkWidget *child; + + while ((child = gtk_widget_get_first_child (widget))) + gtk_widget_unparent (child); + + G_OBJECT_CLASS (constraints_grid_parent_class)->dispose (object); +} + +static void +constraints_grid_class_init (ConstraintsGridClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->dispose = constraints_grid_dispose; +} + +GtkWidget * +do_constraints_builder (GtkWidget *do_widget) +{ + static GtkWidget *window; + + if (!window) + { + GtkBuilder *builder; + + g_type_ensure (constraints_grid_get_type ()); + + builder = gtk_builder_new_from_resource ("/constraints_builder/constraints_builder.ui"); + + window = GTK_WIDGET (gtk_builder_get_object (builder, "window1")); + gtk_window_set_display (GTK_WINDOW (window), + gtk_widget_get_display (do_widget)); + g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); + + g_object_unref (builder); + } + + if (!gtk_widget_get_visible (window)) + gtk_widget_show (window); + else + gtk_window_destroy (GTK_WINDOW (window)); + + return window; +} diff --git a/demos/gtk-demo/constraints_builder.ui b/demos/gtk-demo/constraints_builder.ui new file mode 100644 index 0000000000..1766f931c7 --- /dev/null +++ b/demos/gtk-demo/constraints_builder.ui @@ -0,0 +1,106 @@ + + + + Constraints — Builder + 260 + + + + + + + + + + + + + + + + + + + + + + + + + + + Child 1 + + + + + Child 2 + + + + + Child 3 + + + + + + diff --git a/demos/gtk-demo/constraints_interactive.c b/demos/gtk-demo/constraints_interactive.c new file mode 100644 index 0000000000..90262552f6 --- /dev/null +++ b/demos/gtk-demo/constraints_interactive.c @@ -0,0 +1,237 @@ +/* Constraints/Interactive Constraints + * #Keywords: GtkConstraintLayout + * + * This example shows how constraints can be updated during user interaction. + * The vertical edge between the buttons can be dragged with the mouse. + */ + +#include +#include + +G_DECLARE_FINAL_TYPE (InteractiveGrid, interactive_grid, INTERACTIVE, GRID, GtkWidget) + +struct _InteractiveGrid +{ + GtkWidget parent_instance; + + GtkWidget *button1, *button2; + GtkWidget *button3; + GtkConstraintGuide *guide; + GtkConstraint *constraint; +}; + +G_DEFINE_TYPE (InteractiveGrid, interactive_grid, GTK_TYPE_WIDGET) + +static void +interactive_grid_dispose (GObject *object) +{ + InteractiveGrid *self = INTERACTIVE_GRID (object); + + g_clear_pointer (&self->button1, gtk_widget_unparent); + g_clear_pointer (&self->button2, gtk_widget_unparent); + g_clear_pointer (&self->button3, gtk_widget_unparent); + + G_OBJECT_CLASS (interactive_grid_parent_class)->dispose (object); +} + +static void +interactive_grid_class_init (InteractiveGridClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + object_class->dispose = interactive_grid_dispose; + + gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_CONSTRAINT_LAYOUT); +} + +static void +build_constraints (InteractiveGrid *self, + GtkConstraintLayout *manager) +{ + self->guide = g_object_new (GTK_TYPE_CONSTRAINT_GUIDE, NULL); + gtk_constraint_layout_add_guide (manager, self->guide); + + gtk_constraint_layout_add_constraint (manager, + gtk_constraint_new_constant (GTK_CONSTRAINT_TARGET (self->guide), + GTK_CONSTRAINT_ATTRIBUTE_WIDTH, + GTK_CONSTRAINT_RELATION_EQ, + 0.0, + GTK_CONSTRAINT_STRENGTH_REQUIRED)); + + gtk_constraint_layout_add_constraint (manager, + gtk_constraint_new (NULL, + GTK_CONSTRAINT_ATTRIBUTE_START, + GTK_CONSTRAINT_RELATION_EQ, + GTK_CONSTRAINT_TARGET (self->button1), + GTK_CONSTRAINT_ATTRIBUTE_START, + 1.0, + -8.0, + GTK_CONSTRAINT_STRENGTH_REQUIRED)); + gtk_constraint_layout_add_constraint (manager, + gtk_constraint_new (GTK_CONSTRAINT_TARGET (self->button1), + GTK_CONSTRAINT_ATTRIBUTE_END, + GTK_CONSTRAINT_RELATION_EQ, + GTK_CONSTRAINT_TARGET (self->guide), + GTK_CONSTRAINT_ATTRIBUTE_START, + 1.0, + 0.0, + GTK_CONSTRAINT_STRENGTH_REQUIRED)); + gtk_constraint_layout_add_constraint (manager, + gtk_constraint_new (GTK_CONSTRAINT_TARGET (self->button2), + GTK_CONSTRAINT_ATTRIBUTE_START, + GTK_CONSTRAINT_RELATION_EQ, + GTK_CONSTRAINT_TARGET (self->guide), + GTK_CONSTRAINT_ATTRIBUTE_END, + 1.0, + 0.0, + GTK_CONSTRAINT_STRENGTH_REQUIRED)); + gtk_constraint_layout_add_constraint (manager, + gtk_constraint_new (GTK_CONSTRAINT_TARGET (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, + GTK_CONSTRAINT_TARGET (self->button3), + GTK_CONSTRAINT_ATTRIBUTE_START, + 1.0, + -8.0, + GTK_CONSTRAINT_STRENGTH_REQUIRED)); + + gtk_constraint_layout_add_constraint (manager, + gtk_constraint_new (GTK_CONSTRAINT_TARGET (self->button3), + GTK_CONSTRAINT_ATTRIBUTE_END, + GTK_CONSTRAINT_RELATION_EQ, + GTK_CONSTRAINT_TARGET (self->guide), + GTK_CONSTRAINT_ATTRIBUTE_START, + 1.0, + 0.0, + GTK_CONSTRAINT_STRENGTH_REQUIRED)); + + gtk_constraint_layout_add_constraint (manager, + gtk_constraint_new (NULL, + GTK_CONSTRAINT_ATTRIBUTE_TOP, + GTK_CONSTRAINT_RELATION_EQ, + GTK_CONSTRAINT_TARGET (self->button1), + GTK_CONSTRAINT_ATTRIBUTE_TOP, + 1.0, + -8.0, + GTK_CONSTRAINT_STRENGTH_REQUIRED)); + gtk_constraint_layout_add_constraint (manager, + gtk_constraint_new (GTK_CONSTRAINT_TARGET (self->button2), + GTK_CONSTRAINT_ATTRIBUTE_TOP, + GTK_CONSTRAINT_RELATION_EQ, + GTK_CONSTRAINT_TARGET (self->button1), + GTK_CONSTRAINT_ATTRIBUTE_BOTTOM, + 1.0, + 0.0, + GTK_CONSTRAINT_STRENGTH_REQUIRED)); + gtk_constraint_layout_add_constraint (manager, + gtk_constraint_new (GTK_CONSTRAINT_TARGET (self->button3), + GTK_CONSTRAINT_ATTRIBUTE_TOP, + GTK_CONSTRAINT_RELATION_EQ, + GTK_CONSTRAINT_TARGET (self->button2), + GTK_CONSTRAINT_ATTRIBUTE_BOTTOM, + 1.0, + 0.0, + GTK_CONSTRAINT_STRENGTH_REQUIRED)); + gtk_constraint_layout_add_constraint (manager, + gtk_constraint_new (GTK_CONSTRAINT_TARGET (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 +drag_cb (GtkGestureDrag *drag, + double offset_x, + double offset_y, + InteractiveGrid *self) +{ + GtkConstraintLayout *layout = GTK_CONSTRAINT_LAYOUT (gtk_widget_get_layout_manager (GTK_WIDGET (self))); + double x, y; + + if (self->constraint) + { + gtk_constraint_layout_remove_constraint (layout, self->constraint); + g_clear_object (&self->constraint); + } + + gtk_gesture_drag_get_start_point (drag, &x, &y); + self->constraint = gtk_constraint_new_constant (GTK_CONSTRAINT_TARGET (self->guide), + GTK_CONSTRAINT_ATTRIBUTE_LEFT, + GTK_CONSTRAINT_RELATION_EQ, + x + offset_x, + GTK_CONSTRAINT_STRENGTH_REQUIRED); + gtk_constraint_layout_add_constraint (layout, g_object_ref (self->constraint)); + gtk_widget_queue_allocate (GTK_WIDGET (self)); +} + +static void +interactive_grid_init (InteractiveGrid *self) +{ + GtkWidget *widget = GTK_WIDGET (self); + GtkGesture *drag; + + 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)); + + drag = gtk_gesture_drag_new (); + g_signal_connect (drag, "drag-update", G_CALLBACK (drag_cb), self); + gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (drag)); +} + +GtkWidget * +do_constraints_interactive (GtkWidget *do_widget) +{ + static GtkWidget *window; + + if (!window) + { + GtkWidget *box, *grid; + + window = gtk_window_new (); + gtk_window_set_display (GTK_WINDOW (window), gtk_widget_get_display (do_widget)); + gtk_window_set_title (GTK_WINDOW (window), "Interactive Constraints"); + gtk_window_set_default_size (GTK_WINDOW (window), 260, -1); + g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); + + box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12); + gtk_window_set_child (GTK_WINDOW (window), box); + + grid = g_object_new (interactive_grid_get_type (), NULL); + gtk_widget_set_hexpand (grid, TRUE); + gtk_widget_set_vexpand (grid, TRUE); + gtk_box_append (GTK_BOX (box), grid); + } + + if (!gtk_widget_get_visible (window)) + gtk_widget_show (window); + else + gtk_window_destroy (GTK_WINDOW (window)); + + return window; +} diff --git a/demos/gtk-demo/constraints_vfl.c b/demos/gtk-demo/constraints_vfl.c new file mode 100644 index 0000000000..08016f0b48 --- /dev/null +++ b/demos/gtk-demo/constraints_vfl.c @@ -0,0 +1,160 @@ +/* Constraints/VFL + * + * GtkConstraintLayout allows defining constraints using a + * compact syntax called Visual Format Language, or VFL. + * + * A typical example of a VFL specification looks like this: + * + * H:|-[button1(==button2)]-12-[button2]-| + */ + +#include +#include + +G_DECLARE_FINAL_TYPE (VflGrid, vfl_grid, VFL, GRID, GtkWidget) + +struct _VflGrid +{ + GtkWidget parent_instance; + + GtkWidget *button1, *button2; + GtkWidget *button3; +}; + +G_DEFINE_TYPE (VflGrid, vfl_grid, GTK_TYPE_WIDGET) + +static void +vfl_grid_dispose (GObject *object) +{ + VflGrid *self = VFL_GRID (object); + + g_clear_pointer (&self->button1, gtk_widget_unparent); + g_clear_pointer (&self->button2, gtk_widget_unparent); + g_clear_pointer (&self->button3, gtk_widget_unparent); + + G_OBJECT_CLASS (vfl_grid_parent_class)->dispose (object); +} + +static void +vfl_grid_class_init (VflGridClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + + object_class->dispose = vfl_grid_dispose; + + gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_CONSTRAINT_LAYOUT); +} + +/* Layout: + * + * +-----------------------------+ + * | +-----------+ +-----------+ | + * | | Child 1 | | Child 2 | | + * | +-----------+ +-----------+ | + * | +-------------------------+ | + * | | Child 3 | | + * | +-------------------------+ | + * +-----------------------------+ + * + * Constraints: + * + * super.start = child1.start - 8 + * child1.width = child2.width + * child1.end = child2.start - 12 + * 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 + * + * Visual format: + * + * H:|-8-[view1(==view2)-12-[view2]-8-| + * H:|-8-[view3]-8-| + * V:|-8-[view1]-12-[view3(==view1)]-8-| + * V:|-8-[view2]-12-[view3(==view2)]-8-| + */ +static void +build_constraints (VflGrid *self, + GtkConstraintLayout *manager) +{ + const char * const vfl[] = { + "H:|-[button1(==button2)]-12-[button2]-|", + "H:|-[button3]-|", + "V:|-[button1]-12-[button3(==button1)]-|", + "V:|-[button2]-12-[button3(==button2)]-|", + }; + GError *error = NULL; + + gtk_constraint_layout_add_constraints_from_description (manager, vfl, G_N_ELEMENTS (vfl), + 8, 8, + &error, + "button1", self->button1, + "button2", self->button2, + "button3", self->button3, + NULL); + if (error != NULL) + { + g_printerr ("VFL parsing error:\n%s", error->message); + g_error_free (error); + } +} + +static void +vfl_grid_init (VflGrid *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_vfl (GtkWidget *do_widget) +{ + static GtkWidget *window; + + if (!window) + { + GtkWidget *box, *grid; + + window = gtk_window_new (); + gtk_window_set_display (GTK_WINDOW (window), gtk_widget_get_display (do_widget)); + gtk_window_set_title (GTK_WINDOW (window), "Constraints — VFL"); + gtk_window_set_default_size (GTK_WINDOW (window), 260, -1); + g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); + + box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12); + gtk_window_set_child (GTK_WINDOW (window), box); + + grid = g_object_new (vfl_grid_get_type (), NULL); + gtk_widget_set_hexpand (grid, TRUE); + gtk_widget_set_vexpand (grid, TRUE); + gtk_box_append (GTK_BOX (box), grid); + } + + if (!gtk_widget_get_visible (window)) + gtk_widget_show (window); + else + gtk_window_destroy (GTK_WINDOW (window)); + + return window; +} diff --git a/demos/gtk-demo/demo.gresource.xml b/demos/gtk-demo/demo.gresource.xml index 72933ebef6..a0ccb783d9 100644 --- a/demos/gtk-demo/demo.gresource.xml +++ b/demos/gtk-demo/demo.gresource.xml @@ -18,8 +18,8 @@ demoimage.c demoimage.h - - constraints.ui + + constraints_builder.ui css_accordion.css @@ -253,9 +253,9 @@ clipboard.c combobox.c constraints.c - constraints2.c - constraints3.c - constraints4.c + constraints_interactive.c + constraints_vfl.c + constraints_builder.c css_accordion.c css_basics.c css_blendmodes.c @@ -294,7 +294,7 @@ layoutmanager2.c links.c listbox.c - listbox2.c + listbox_controls.c listview_applauncher.c listview_colors.c listview_clocks.c @@ -309,7 +309,7 @@ markup.c menu.c overlay.c - overlay2.c + overlay_decorative.c paint.c pagesetup.c paintable.c @@ -359,8 +359,8 @@ messages.txt apple-red.png - - listbox2.ui + + listbox_controls.ui glarea-gl.fs.glsl diff --git a/demos/gtk-demo/listbox2.c b/demos/gtk-demo/listbox2.c deleted file mode 100644 index 17a79df741..0000000000 --- a/demos/gtk-demo/listbox2.c +++ /dev/null @@ -1,71 +0,0 @@ -/* List Box/Controls - * - * GtkListBox is well-suited for creating “button strips” — lists of - * controls for use in preference dialogs or settings panels. To create - * this style of list, use the .rich-list style class. - */ - -#include - -static GtkWidget *window; -static GtkWidget *switch_widget; -static GtkWidget *check; -static GtkWidget *image; - -static void -row_activated (GtkListBox *list, - GtkListBoxRow *row) -{ - if (gtk_widget_is_ancestor (switch_widget, GTK_WIDGET (row))) - { - gtk_switch_set_active (GTK_SWITCH (switch_widget), - !gtk_switch_get_active (GTK_SWITCH (switch_widget))); - } - else if (gtk_widget_is_ancestor (check, GTK_WIDGET (row))) - { - gtk_check_button_set_active (GTK_CHECK_BUTTON (check), - !gtk_check_button_get_active (GTK_CHECK_BUTTON (check))); - } - else if (gtk_widget_is_ancestor (image, GTK_WIDGET (row))) - { - gtk_widget_set_opacity (image, - 1.0 - gtk_widget_get_opacity (image)); - } -} - -GtkWidget * -do_listbox2 (GtkWidget *do_widget) -{ - if (!window) - { - GtkBuilderScope *scope; - GtkBuilder *builder; - - scope = gtk_builder_cscope_new (); - gtk_builder_cscope_add_callback_symbol (GTK_BUILDER_CSCOPE (scope), - "row_activated", G_CALLBACK (row_activated)); - builder = gtk_builder_new (); - gtk_builder_set_scope (builder, scope); - - gtk_builder_add_from_resource (builder, "/listbox2/listbox2.ui", NULL); - - window = GTK_WIDGET (gtk_builder_get_object (builder, "window")); - gtk_window_set_display (GTK_WINDOW (window), - gtk_widget_get_display (do_widget)); - g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); - - switch_widget = GTK_WIDGET (gtk_builder_get_object (builder, "switch")); - check = GTK_WIDGET (gtk_builder_get_object (builder, "check")); - image = GTK_WIDGET (gtk_builder_get_object (builder, "image")); - - g_object_unref (builder); - g_object_unref (scope); - } - - if (!gtk_widget_get_visible (window)) - gtk_widget_show (window); - else - gtk_window_destroy (GTK_WINDOW (window)); - - return window; -} diff --git a/demos/gtk-demo/listbox2.ui b/demos/gtk-demo/listbox2.ui deleted file mode 100644 index c2c8a1f1cf..0000000000 --- a/demos/gtk-demo/listbox2.ui +++ /dev/null @@ -1,298 +0,0 @@ - - - - List Box — Controls - 400 - - - never - 200 - 0 - 1 - - - 1 - - - vertical - 60 - 60 - 30 - 30 - - - Group 1 - 0 - 10 - - - - - - - - none - 1 - - - - - - - - - - Switch - 0 - start - center - 1 - - - - - end - center - - - - - - - - - - - - - - Check - 0 - start - center - 1 - - - - - end - center - 10 - 10 - 1 - - - - - - - - - - - - - - Click here! - 0 - start - center - 1 - - - - - object-select-symbolic - end - center - 10 - 10 - 0 - - - - - - - - - - - - - - 30 - 10 - Group 2 - 0 - - - - - - - - none - 1 - - - - - 0 - - - - - Scale - 0 - start - center - 1 - - - - - end - center - 0 - 150 - - - 100 - 50 - 1 - 10 - - - - - - - - - - - - 0 - - - - - Spinbutton - 0 - start - center - 1 - - - - - end - center - - - 100 - 50 - 1 - 10 - - - - - - - - - - - 0 - - - - - Dropdown - 0 - start - center - 1 - - - - - end - center - - - - Choice 1 - Choice 2 - Choice 3 - Choice 4 - - - - - - - - - - - - - 0 - - - - - Entry - 0 - start - center - 1 - - - - - end - center - Type here… - - - - - - - - - - - - - - - - - - - - horizontal - - - - - - - - - - - diff --git a/demos/gtk-demo/listbox_controls.c b/demos/gtk-demo/listbox_controls.c new file mode 100644 index 0000000000..5a72622d22 --- /dev/null +++ b/demos/gtk-demo/listbox_controls.c @@ -0,0 +1,71 @@ +/* List Box/Controls + * + * GtkListBox is well-suited for creating “button strips” — lists of + * controls for use in preference dialogs or settings panels. To create + * this style of list, use the .rich-list style class. + */ + +#include + +static GtkWidget *window; +static GtkWidget *switch_widget; +static GtkWidget *check; +static GtkWidget *image; + +static void +row_activated (GtkListBox *list, + GtkListBoxRow *row) +{ + if (gtk_widget_is_ancestor (switch_widget, GTK_WIDGET (row))) + { + gtk_switch_set_active (GTK_SWITCH (switch_widget), + !gtk_switch_get_active (GTK_SWITCH (switch_widget))); + } + else if (gtk_widget_is_ancestor (check, GTK_WIDGET (row))) + { + gtk_check_button_set_active (GTK_CHECK_BUTTON (check), + !gtk_check_button_get_active (GTK_CHECK_BUTTON (check))); + } + else if (gtk_widget_is_ancestor (image, GTK_WIDGET (row))) + { + gtk_widget_set_opacity (image, + 1.0 - gtk_widget_get_opacity (image)); + } +} + +GtkWidget * +do_listbox_controls (GtkWidget *do_widget) +{ + if (!window) + { + GtkBuilderScope *scope; + GtkBuilder *builder; + + scope = gtk_builder_cscope_new (); + gtk_builder_cscope_add_callback_symbol (GTK_BUILDER_CSCOPE (scope), + "row_activated", G_CALLBACK (row_activated)); + builder = gtk_builder_new (); + gtk_builder_set_scope (builder, scope); + + gtk_builder_add_from_resource (builder, "/listbox_controls/listbox_controls.ui", NULL); + + window = GTK_WIDGET (gtk_builder_get_object (builder, "window")); + gtk_window_set_display (GTK_WINDOW (window), + gtk_widget_get_display (do_widget)); + g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); + + switch_widget = GTK_WIDGET (gtk_builder_get_object (builder, "switch")); + check = GTK_WIDGET (gtk_builder_get_object (builder, "check")); + image = GTK_WIDGET (gtk_builder_get_object (builder, "image")); + + g_object_unref (builder); + g_object_unref (scope); + } + + if (!gtk_widget_get_visible (window)) + gtk_widget_show (window); + else + gtk_window_destroy (GTK_WINDOW (window)); + + return window; +} diff --git a/demos/gtk-demo/listbox_controls.ui b/demos/gtk-demo/listbox_controls.ui new file mode 100644 index 0000000000..c2c8a1f1cf --- /dev/null +++ b/demos/gtk-demo/listbox_controls.ui @@ -0,0 +1,298 @@ + + + + List Box — Controls + 400 + + + never + 200 + 0 + 1 + + + 1 + + + vertical + 60 + 60 + 30 + 30 + + + Group 1 + 0 + 10 + + + + + + + + none + 1 + + + + + + + + + + Switch + 0 + start + center + 1 + + + + + end + center + + + + + + + + + + + + + + Check + 0 + start + center + 1 + + + + + end + center + 10 + 10 + 1 + + + + + + + + + + + + + + Click here! + 0 + start + center + 1 + + + + + object-select-symbolic + end + center + 10 + 10 + 0 + + + + + + + + + + + + + + 30 + 10 + Group 2 + 0 + + + + + + + + none + 1 + + + + + 0 + + + + + Scale + 0 + start + center + 1 + + + + + end + center + 0 + 150 + + + 100 + 50 + 1 + 10 + + + + + + + + + + + + 0 + + + + + Spinbutton + 0 + start + center + 1 + + + + + end + center + + + 100 + 50 + 1 + 10 + + + + + + + + + + + 0 + + + + + Dropdown + 0 + start + center + 1 + + + + + end + center + + + + Choice 1 + Choice 2 + Choice 3 + Choice 4 + + + + + + + + + + + + + 0 + + + + + Entry + 0 + start + center + 1 + + + + + end + center + Type here… + + + + + + + + + + + + + + + + + + + + horizontal + + + + + + + + + + + diff --git a/demos/gtk-demo/meson.build b/demos/gtk-demo/meson.build index 93a78c9fca..9076c05383 100644 --- a/demos/gtk-demo/meson.build +++ b/demos/gtk-demo/meson.build @@ -7,9 +7,9 @@ demos = files([ 'clipboard.c', 'combobox.c', 'constraints.c', - 'constraints2.c', - 'constraints3.c', - 'constraints4.c', + 'constraints_interactive.c', + 'constraints_vfl.c', + 'constraints_builder.c', 'css_accordion.c', 'css_basics.c', 'css_blendmodes.c', @@ -44,7 +44,7 @@ demos = files([ 'layoutmanager2.c', 'links.c', 'listbox.c', - 'listbox2.c', + 'listbox_controls.c', 'menu.c', 'flowbox.c', 'list_store.c', @@ -60,7 +60,7 @@ demos = files([ 'listview_words.c', 'markup.c', 'overlay.c', - 'overlay2.c', + 'overlay_decorative.c', 'paint.c', 'paintable.c', 'paintable_animated.c', diff --git a/demos/gtk-demo/overlay2.c b/demos/gtk-demo/overlay2.c deleted file mode 100644 index 11f2e2a243..0000000000 --- a/demos/gtk-demo/overlay2.c +++ /dev/null @@ -1,102 +0,0 @@ -/* Overlay/Decorative Overlay - * #Keywords: GtkOverlay - * - * Another example of an overlay with some decorative - * and some interactive controls. - */ - -#include - -static GtkTextTag *tag; - -static void -margin_changed (GtkAdjustment *adjustment, - GtkTextView *text) -{ - int value; - - value = (int)gtk_adjustment_get_value (adjustment); - gtk_text_view_set_left_margin (GTK_TEXT_VIEW (text), value); - g_object_set (tag, "pixels-above-lines", value, NULL); -} - -GtkWidget * -do_overlay2 (GtkWidget *do_widget) -{ - static GtkWidget *window = NULL; - - if (!window) - { - GtkWidget *overlay; - GtkWidget *sw; - GtkWidget *text; - GtkWidget *image; - GtkWidget *scale; - GtkTextBuffer *buffer; - GtkTextIter start, end; - GtkAdjustment *adjustment; - - window = gtk_window_new (); - gtk_window_set_default_size (GTK_WINDOW (window), 500, 510); - gtk_window_set_title (GTK_WINDOW (window), "Decorative Overlay"); - - overlay = gtk_overlay_new (); - sw = gtk_scrolled_window_new (); - gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), - GTK_POLICY_AUTOMATIC, - GTK_POLICY_AUTOMATIC); - text = gtk_text_view_new (); - buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text)); - - gtk_text_buffer_set_text (buffer, "Dear diary...", -1); - - tag = gtk_text_buffer_create_tag (buffer, "top-margin", - "pixels-above-lines", 0, - NULL); - gtk_text_buffer_get_start_iter (buffer, &start); - end = start; - gtk_text_iter_forward_word_end (&end); - gtk_text_buffer_apply_tag (buffer, tag, &start, &end); - - gtk_window_set_child (GTK_WINDOW (window), overlay); - gtk_overlay_set_child (GTK_OVERLAY (overlay), sw); - gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), text); - g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); - - image = gtk_picture_new_for_resource ("/overlay2/decor1.png"); - gtk_overlay_add_overlay (GTK_OVERLAY (overlay), image); - gtk_widget_set_can_target (image, FALSE); - gtk_widget_set_halign (image, GTK_ALIGN_START); - gtk_widget_set_valign (image, GTK_ALIGN_START); - - image = gtk_picture_new_for_resource ("/overlay2/decor2.png"); - gtk_overlay_add_overlay (GTK_OVERLAY (overlay), image); - gtk_widget_set_can_target (image, FALSE); - gtk_widget_set_halign (image, GTK_ALIGN_END); - gtk_widget_set_valign (image, GTK_ALIGN_END); - - adjustment = gtk_adjustment_new (0, 0, 100, 1, 1, 0); - g_signal_connect (adjustment, "value-changed", - G_CALLBACK (margin_changed), text); - - scale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adjustment); - gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE); - gtk_widget_set_size_request (scale, 120, -1); - gtk_widget_set_margin_start (scale, 20); - gtk_widget_set_margin_end (scale, 20); - gtk_widget_set_margin_bottom (scale, 20); - gtk_overlay_add_overlay (GTK_OVERLAY (overlay), scale); - gtk_widget_set_halign (scale, GTK_ALIGN_START); - gtk_widget_set_valign (scale, GTK_ALIGN_END); - gtk_widget_set_tooltip_text (scale, "Margin"); - - gtk_adjustment_set_value (adjustment, 100); - } - - if (!gtk_widget_get_visible (window)) - gtk_widget_show (window); - else - gtk_window_destroy (GTK_WINDOW (window)); - - return window; -} diff --git a/demos/gtk-demo/overlay_decorative.c b/demos/gtk-demo/overlay_decorative.c new file mode 100644 index 0000000000..8306f52fb1 --- /dev/null +++ b/demos/gtk-demo/overlay_decorative.c @@ -0,0 +1,102 @@ +/* Overlay/Decorative Overlay + * #Keywords: GtkOverlay + * + * Another example of an overlay with some decorative + * and some interactive controls. + */ + +#include + +static GtkTextTag *tag; + +static void +margin_changed (GtkAdjustment *adjustment, + GtkTextView *text) +{ + int value; + + value = (int)gtk_adjustment_get_value (adjustment); + gtk_text_view_set_left_margin (GTK_TEXT_VIEW (text), value); + g_object_set (tag, "pixels-above-lines", value, NULL); +} + +GtkWidget * +do_overlay_decorative (GtkWidget *do_widget) +{ + static GtkWidget *window = NULL; + + if (!window) + { + GtkWidget *overlay; + GtkWidget *sw; + GtkWidget *text; + GtkWidget *image; + GtkWidget *scale; + GtkTextBuffer *buffer; + GtkTextIter start, end; + GtkAdjustment *adjustment; + + window = gtk_window_new (); + gtk_window_set_default_size (GTK_WINDOW (window), 500, 510); + gtk_window_set_title (GTK_WINDOW (window), "Decorative Overlay"); + + overlay = gtk_overlay_new (); + sw = gtk_scrolled_window_new (); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + text = gtk_text_view_new (); + buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (text)); + + gtk_text_buffer_set_text (buffer, "Dear diary...", -1); + + tag = gtk_text_buffer_create_tag (buffer, "top-margin", + "pixels-above-lines", 0, + NULL); + gtk_text_buffer_get_start_iter (buffer, &start); + end = start; + gtk_text_iter_forward_word_end (&end); + gtk_text_buffer_apply_tag (buffer, tag, &start, &end); + + gtk_window_set_child (GTK_WINDOW (window), overlay); + gtk_overlay_set_child (GTK_OVERLAY (overlay), sw); + gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), text); + g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window); + + image = gtk_picture_new_for_resource ("/overlay2/decor1.png"); + gtk_overlay_add_overlay (GTK_OVERLAY (overlay), image); + gtk_widget_set_can_target (image, FALSE); + gtk_widget_set_halign (image, GTK_ALIGN_START); + gtk_widget_set_valign (image, GTK_ALIGN_START); + + image = gtk_picture_new_for_resource ("/overlay2/decor2.png"); + gtk_overlay_add_overlay (GTK_OVERLAY (overlay), image); + gtk_widget_set_can_target (image, FALSE); + gtk_widget_set_halign (image, GTK_ALIGN_END); + gtk_widget_set_valign (image, GTK_ALIGN_END); + + adjustment = gtk_adjustment_new (0, 0, 100, 1, 1, 0); + g_signal_connect (adjustment, "value-changed", + G_CALLBACK (margin_changed), text); + + scale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, adjustment); + gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE); + gtk_widget_set_size_request (scale, 120, -1); + gtk_widget_set_margin_start (scale, 20); + gtk_widget_set_margin_end (scale, 20); + gtk_widget_set_margin_bottom (scale, 20); + gtk_overlay_add_overlay (GTK_OVERLAY (overlay), scale); + gtk_widget_set_halign (scale, GTK_ALIGN_START); + gtk_widget_set_valign (scale, GTK_ALIGN_END); + gtk_widget_set_tooltip_text (scale, "Margin"); + + gtk_adjustment_set_value (adjustment, 100); + } + + if (!gtk_widget_get_visible (window)) + gtk_widget_show (window); + else + gtk_window_destroy (GTK_WINDOW (window)); + + return window; +} -- cgit v1.2.1