From ce6c7bae514b040b801dec0a13f1dbde8f6bc753 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 12 Apr 2020 20:39:03 -0400 Subject: [PATCH] gtk-demo: Add a gears demo --- demos/gtk-demo/demo.gresource.xml | 5 ++ demos/gtk-demo/gears.c | 137 ++++++++++++++++++++++++++++++ demos/gtk-demo/meson.build | 1 + 3 files changed, 143 insertions(+) create mode 100644 demos/gtk-demo/gears.c diff --git a/demos/gtk-demo/demo.gresource.xml b/demos/gtk-demo/demo.gresource.xml index f6477002ce..3afc75c7db 100644 --- a/demos/gtk-demo/demo.gresource.xml +++ b/demos/gtk-demo/demo.gresource.xml @@ -104,6 +104,10 @@ gtkfishbowl.c gtkfishbowl.h + + gtkgears.c + gtkgears.h + iconscroll.ui @@ -176,6 +180,7 @@ font_features.c fontplane.c fontrendering.c + gears.c gestures.c glarea.c headerbar.c diff --git a/demos/gtk-demo/gears.c b/demos/gtk-demo/gears.c new file mode 100644 index 0000000000..3be4aa352b --- /dev/null +++ b/demos/gtk-demo/gears.c @@ -0,0 +1,137 @@ +/* Gears + * + * This is a classic OpenGL demo, running in a GtkGLArea. + */ + + +#include +#include + +#include "gtkgears.h" + +/************************************************************************ + * DEMO CODE * + ************************************************************************/ + +static void +on_axis_value_change (GtkAdjustment *adjustment, + gpointer data) +{ + GtkGears *gears = GTK_GEARS (data); + int axis = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (adjustment), "axis")); + + gtk_gears_set_axis (gears, axis, gtk_adjustment_get_value (adjustment)); +} + + +static GtkWidget * +create_axis_slider (GtkGears *gears, + int axis) +{ + GtkWidget *box, *label, *slider; + GtkAdjustment *adj; + const char *text; + + box = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE); + + switch (axis) + { + case GTK_GEARS_X_AXIS: + text = "X"; + break; + + case GTK_GEARS_Y_AXIS: + text = "Y"; + break; + + case GTK_GEARS_Z_AXIS: + text = "Z"; + break; + + default: + g_assert_not_reached (); + } + + label = gtk_label_new (text); + gtk_container_add (GTK_CONTAINER (box), label); + gtk_widget_show (label); + + adj = gtk_adjustment_new (gtk_gears_get_axis (gears, axis), 0.0, 360.0, 1.0, 12.0, 0.0); + g_object_set_data (G_OBJECT (adj), "axis", GINT_TO_POINTER (axis)); + g_signal_connect (adj, "value-changed", + G_CALLBACK (on_axis_value_change), + gears); + slider = gtk_scale_new (GTK_ORIENTATION_VERTICAL, adj); + gtk_scale_set_draw_value (GTK_SCALE (slider), FALSE); + gtk_container_add (GTK_CONTAINER (box), slider); + gtk_widget_set_vexpand (slider, TRUE); + gtk_widget_show (slider); + + gtk_widget_show (box); + + return box; +} + +GtkWidget * +do_gears (GtkWidget *do_widget) +{ + static GtkWidget *window = NULL; + GtkWidget *box, *hbox, *fps_label, *gears, *overlay, *frame; + int i; + + if (!window) + { + window = gtk_window_new (); + gtk_window_set_title (GTK_WINDOW (window), "Gears"); + gtk_window_set_resizable (GTK_WINDOW (window), TRUE); + gtk_window_set_default_size (GTK_WINDOW (window), 640, 640); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_widget_destroyed), &window); + + overlay = gtk_overlay_new (); + gtk_widget_set_margin_start (overlay, 12); + gtk_widget_set_margin_end (overlay, 12); + gtk_widget_set_margin_top (overlay, 12); + gtk_widget_set_margin_bottom (overlay, 12); + + gtk_container_add (GTK_CONTAINER (window), overlay); + + frame = gtk_frame_new (NULL); + gtk_widget_set_halign (frame, GTK_ALIGN_START); + gtk_widget_set_valign (frame, GTK_ALIGN_START); + gtk_widget_add_css_class (frame, "app-notification"); + gtk_overlay_add_overlay (GTK_OVERLAY (overlay), frame); + + fps_label = gtk_label_new (""); + gtk_widget_set_halign (fps_label, GTK_ALIGN_START); + gtk_container_add (GTK_CONTAINER (frame), fps_label); + + box = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE); + gtk_box_set_spacing (GTK_BOX (box), 6); + gtk_container_add (GTK_CONTAINER (overlay), box); + + hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE); + gtk_box_set_spacing (GTK_BOX (box), 6); + gtk_container_add (GTK_CONTAINER (box), hbox); + + gears = gtk_gears_new (); + gtk_widget_set_hexpand (gears, TRUE); + gtk_widget_set_vexpand (gears, TRUE); + gtk_container_add (GTK_CONTAINER (hbox), gears); + + for (i = 0; i < GTK_GEARS_N_AXIS; i++) + gtk_container_add (GTK_CONTAINER (hbox), create_axis_slider (GTK_GEARS (gears), i)); + + hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE); + gtk_box_set_spacing (GTK_BOX (hbox), 6); + gtk_container_add (GTK_CONTAINER (box), hbox); + + gtk_gears_set_fps_label (GTK_GEARS (gears), GTK_LABEL (fps_label)); + } + + if (!gtk_widget_get_visible (window)) + gtk_widget_show (window); + else + gtk_widget_destroy (window); + + return window; +} diff --git a/demos/gtk-demo/meson.build b/demos/gtk-demo/meson.build index 8c2392c29e..8a4ad34d6e 100644 --- a/demos/gtk-demo/meson.build +++ b/demos/gtk-demo/meson.build @@ -28,6 +28,7 @@ demos = files([ 'fishbowl.c', 'fixed.c', 'fontrendering.c', + 'gears.c', 'gestures.c', 'glarea.c', 'headerbar.c',