[gtk] Make the scrolling demo more versatile
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk] Make the scrolling demo more versatile
- Date: Sat, 12 Oct 2019 02:05:14 +0000 (UTC)
commit 019e3c02ed7f2e277e3c65674d0ce4f7368ca577
Author: Matthias Clasen <mclasen redhat com>
Date: Fri Oct 11 22:04:30 2019 -0400
Make the scrolling demo more versatile
Allow to swap out the content, so we can compare
text and icon scrolling.
demos/gtk-demo/iconscroll.c | 131 ++++++++++++++++++++++++++++++++++++++++---
demos/gtk-demo/iconscroll.ui | 35 ++++++++----
demos/gtk-demo/main.c | 2 +-
3 files changed, 148 insertions(+), 20 deletions(-)
---
diff --git a/demos/gtk-demo/iconscroll.c b/demos/gtk-demo/iconscroll.c
index 5c17d0b6af..6aa649db4b 100644
--- a/demos/gtk-demo/iconscroll.c
+++ b/demos/gtk-demo/iconscroll.c
@@ -1,12 +1,19 @@
-/* Benchmark/Icons
+/* Benchmark/Scrolling
*
- * This demo scrolls a view with many icons.
+ * This demo scrolls a view with various content.
*/
#include <gtk/gtk.h>
static guint tick_cb;
static GtkAdjustment *adjustment;
+static GtkWidget *window = NULL;
+static GtkWidget *scrolledwindow;
+static int selected;
+
+#define N_WIDGET_TYPES 3
+
+
int increment = 5;
static gboolean
@@ -29,26 +36,133 @@ scroll_cb (GtkWidget *widget,
extern GtkWidget *create_icon (void);
static void
-populate (GtkWidget *grid)
+populate_icons (void)
{
+ GtkWidget *grid;
int top, left;
+ grid = gtk_grid_new ();
+ gtk_widget_set_halign (grid, GTK_ALIGN_CENTER);
+ g_object_set (grid, "margin", 10, NULL);
+ gtk_grid_set_row_spacing (GTK_GRID (grid), 10);
+ gtk_grid_set_column_spacing (GTK_GRID (grid), 10);
+
for (top = 0; top < 100; top++)
for (left = 0; left < 15; left++)
gtk_grid_attach (GTK_GRID (grid), create_icon (), left, top, 1, 1);
+
+ gtk_container_add (GTK_CONTAINER (scrolledwindow), grid);
+}
+
+static char *content;
+static gsize content_len;
+
+extern void fontify (GtkTextBuffer *buffer);
+
+static void
+populate_text (gboolean hilight)
+{
+ GtkWidget *textview;
+ GtkTextBuffer *buffer;
+
+ if (!content)
+ {
+ GBytes *bytes;
+
+ bytes = g_resources_lookup_data ("/sources/font_features.c", 0, NULL);
+ content = g_bytes_unref_to_data (bytes, &content_len);
+ }
+
+ buffer = gtk_text_buffer_new (NULL);
+ gtk_text_buffer_set_text (buffer, content, (int)content_len);
+
+ if (hilight)
+ fontify (buffer);
+
+ textview = gtk_text_view_new ();
+ gtk_text_view_set_buffer (GTK_TEXT_VIEW (textview), buffer);
+
+ gtk_container_add (GTK_CONTAINER (scrolledwindow), textview);
+}
+
+static void
+set_widget_type (int type)
+{
+ if (tick_cb)
+ gtk_widget_remove_tick_callback (window, tick_cb);
+
+ if (gtk_bin_get_child (GTK_BIN (scrolledwindow)))
+ gtk_container_remove (GTK_CONTAINER (scrolledwindow),
+ gtk_bin_get_child (GTK_BIN (scrolledwindow)));
+
+ selected = type;
+
+ switch (selected)
+ {
+ case 0:
+ gtk_window_set_title (GTK_WINDOW (window), "Scrolling icons");
+ populate_icons ();
+ break;
+
+ case 1:
+ gtk_window_set_title (GTK_WINDOW (window), "Scrolling plain text");
+ populate_text (FALSE);
+ break;
+
+ case 2:
+ gtk_window_set_title (GTK_WINDOW (window), "Scrolling complex text");
+ populate_text (TRUE);
+ break;
+
+ default:
+ g_assert_not_reached ();
+ }
+
+ tick_cb = gtk_widget_add_tick_callback (window, scroll_cb, NULL, NULL);
+}
+
+static void
+next_clicked_cb (GtkButton *source,
+ gpointer user_data)
+{
+ int new_index;
+
+ if (selected + 1 >= N_WIDGET_TYPES)
+ new_index = 0;
+ else
+ new_index = selected + 1;
+
+
+ set_widget_type (new_index);
+}
+
+static void
+prev_clicked_cb (GtkButton *source,
+ gpointer user_data)
+{
+ int new_index;
+
+ if (selected - 1 < 0)
+ new_index = N_WIDGET_TYPES - 1;
+ else
+ new_index = selected - 1;
+
+ set_widget_type (new_index);
}
GtkWidget *
do_iconscroll (GtkWidget *do_widget)
{
- static GtkWidget *window = NULL;
-
if (!window)
{
GtkBuilder *builder;
- GtkWidget *grid;
builder = gtk_builder_new_from_resource ("/iconscroll/iconscroll.ui");
+ gtk_builder_add_callback_symbols (builder,
+ "next_clicked_cb", G_CALLBACK (next_clicked_cb),
+ "prev_clicked_cb", G_CALLBACK (prev_clicked_cb),
+ NULL);
+ gtk_builder_connect_signals (builder, NULL);
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
@@ -56,11 +170,10 @@ do_iconscroll (GtkWidget *do_widget)
gtk_widget_get_display (do_widget));
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
- grid = GTK_WIDGET (gtk_builder_get_object (builder, "grid"));
- populate (grid);
+ scrolledwindow = GTK_WIDGET (gtk_builder_get_object (builder, "scrolledwindow"));
gtk_widget_realize (window);
adjustment = GTK_ADJUSTMENT (gtk_builder_get_object (builder, "adjustment"));
- tick_cb = gtk_widget_add_tick_callback (window, scroll_cb, NULL, NULL);
+ set_widget_type (0);
}
if (!gtk_widget_get_visible (window))
diff --git a/demos/gtk-demo/iconscroll.ui b/demos/gtk-demo/iconscroll.ui
index d7c68e5f6a..3b62b75541 100644
--- a/demos/gtk-demo/iconscroll.ui
+++ b/demos/gtk-demo/iconscroll.ui
@@ -4,20 +4,35 @@
<property name="resizable">0</property>
<property name="default-width">500</property>
<property name="default-height">500</property>
- <property name="title">Scrolling icons</property>
+ <child type="titlebar">
+ <object class="GtkHeaderBar">
+ <property name="show-title-buttons">1</property>
+ <child>
+ <object class="GtkBox">
+ <style>
+ <class name="linked"/>
+ </style>
+ <child>
+ <object class="GtkButton">
+ <property name="icon-name">pan-start-symbolic</property>
+ <signal name="clicked" handler="prev_clicked_cb"/>
+ </object>
+ </child>
+ <child>
+ <object class="GtkButton">
+ <property name="icon-name">pan-end-symbolic</property>
+ <signal name="clicked" handler="next_clicked_cb"/>
+ </object>
+ </child>
+ </object>
+ </child>
+ </object>
+ </child>
<child>
- <object class="GtkScrolledWindow">
+ <object class="GtkScrolledWindow" id="scrolledwindow">
<property name="hscrollbar-policy">never</property>
<property name="vscrollbar-policy">automatic</property>
<property name="vadjustment"><object class="GtkAdjustment" id="adjustment"/></property>
- <child>
- <object class="GtkGrid" id="grid">
- <property name="halign">center</property>
- <property name="margin">10</property>
- <property name="row-spacing">10</property>
- <property name="orientation">vertical</property>
- </object>
- </child>
</object>
</child>
</object>
diff --git a/demos/gtk-demo/main.c b/demos/gtk-demo/main.c
index f9edf9d6d7..6460f10388 100644
--- a/demos/gtk-demo/main.c
+++ b/demos/gtk-demo/main.c
@@ -487,7 +487,7 @@ parse_chars (gchar *text,
}
/* While not as cool as c-mode, this will do as a quick attempt at highlighting */
-static void
+void
fontify (GtkTextBuffer *source_buffer)
{
GtkTextIter start_iter, next_iter, tmp_iter;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]