diff options
author | Matthias Clasen <mclasen@redhat.com> | 2020-06-20 12:37:13 -0400 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2020-06-20 12:37:13 -0400 |
commit | a0fc293d1ec74173bfa08d0172547270f5b06fb0 (patch) | |
tree | b0a0b2c8b72a95afdd6430e56f0a96912fef548d | |
parent | 00f0177d25dffb58d1cc2e5742ecccea0f729c42 (diff) | |
download | gtk+-a0fc293d1ec74173bfa08d0172547270f5b06fb0.tar.gz |
gtk-demo: Make the colors demo populate incrementally too
-rw-r--r-- | demos/gtk-demo/listview_colors.c | 106 |
1 files changed, 70 insertions, 36 deletions
diff --git a/demos/gtk-demo/listview_colors.c b/demos/gtk-demo/listview_colors.c index 68ccd80e27..443b704c69 100644 --- a/demos/gtk-demo/listview_colors.c +++ b/demos/gtk-demo/listview_colors.c @@ -242,49 +242,83 @@ gtk_color_new (const char *name, return result; } -static GListModel * -create_colors_model (void) +typedef struct { - GListStore *result; - GtkColor *color; GBytes *data; + GListStore *store; char **lines; + guint n; guint i; +} ColorData; - result = g_list_store_new (GTK_TYPE_COLOR); - data = g_resources_lookup_data ("/listview_colors/color.names.txt", 0, NULL); - lines = g_strsplit (g_bytes_get_data (data, NULL), "\n", 0); +static void +free_color_data (gpointer data) +{ + ColorData *cd = data; - for (i = 0; lines[i]; i++) - { - const char *name; - char **fields; - int red, green, blue; - int h, s, v; - - if (lines[i][0] == '#' || lines[i][0] == '\0') - continue; - - fields = g_strsplit (lines[i], " ", 0); - name = fields[1]; - red = atoi (fields[3]); - green = atoi (fields[4]); - blue = atoi (fields[5]); - h = atoi (fields[9]); - s = atoi (fields[10]); - v = atoi (fields[11]); - - color = gtk_color_new (name, red / 255., green / 255., blue / 255., h, s, v); - g_list_store_append (result, color); - g_object_unref (color); - - g_strfreev (fields); - } - g_strfreev (lines); + g_bytes_unref (cd->data); + g_object_unref (cd->store); + g_strfreev (cd->lines); + g_free (cd); +} + +static gboolean +add_one_color (GtkWidget *widget, + GdkFrameClock *clock, + gpointer data) +{ + ColorData *cd = data; + const char *name; + char **fields; + int red, green, blue; + int h, s, v; + GtkColor *color; + + g_print ("%u/%u\n", cd->i, cd->n); + + if (cd->lines[cd->i][0] == '#' || cd->lines[cd->i][0] == '\0') + goto out; + + fields = g_strsplit (cd->lines[cd->i], " ", 0); + name = fields[1]; + red = atoi (fields[3]); + green = atoi (fields[4]); + blue = atoi (fields[5]); + h = atoi (fields[9]); + s = atoi (fields[10]); + v = atoi (fields[11]); + + color = gtk_color_new (name, red / 255., green / 255., blue / 255., h, s, v); + g_list_store_append (cd->store, color); + g_object_unref (color); + + g_strfreev (fields); + +out: + cd->i++; + + if (cd->i == cd->n) + return G_SOURCE_REMOVE; + + return G_SOURCE_CONTINUE; +} + +static GListModel * +create_colors_model (GtkWidget *view) +{ + ColorData *cd; + + cd = g_new (ColorData, 1); + + cd->store = g_list_store_new (GTK_TYPE_COLOR); + cd->data = g_resources_lookup_data ("/listview_colors/color.names.txt", 0, NULL); + cd->lines = g_strsplit (g_bytes_get_data (cd->data, NULL), "\n", 0); + cd->n = g_strv_length (cd->lines); + cd->i = 0; - g_bytes_unref (data); + gtk_widget_add_tick_callback (view, add_one_color, cd, free_color_data); - return G_LIST_MODEL (result); + return G_LIST_MODEL (g_object_ref (cd->store)); } static char * @@ -438,7 +472,7 @@ create_color_grid (void) gtk_grid_view_set_max_columns (GTK_GRID_VIEW (gridview), 24); gtk_grid_view_set_enable_rubberband (GTK_GRID_VIEW (gridview), TRUE); - model = G_LIST_MODEL (gtk_sort_list_model_new (create_colors_model (), NULL)); + model = G_LIST_MODEL (gtk_sort_list_model_new (create_colors_model (gridview), NULL)); selection = G_LIST_MODEL (gtk_property_selection_new (model, "selected")); gtk_grid_view_set_model (GTK_GRID_VIEW (gridview), selection); g_object_unref (selection); |