2015-08-16 22:04:49 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_LABEL = 1,
|
|
|
|
PROP_ID,
|
|
|
|
LAST_PROPERTY
|
|
|
|
};
|
|
|
|
|
|
|
|
static GParamSpec *properties[LAST_PROPERTY] = { NULL, };
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GObject parent;
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
char *label;
|
2020-07-24 13:54:49 +00:00
|
|
|
int id;
|
2015-08-16 22:04:49 +00:00
|
|
|
} MyObject;
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GObjectClass parent_class;
|
|
|
|
} MyObjectClass;
|
|
|
|
|
2020-03-06 17:05:43 +00:00
|
|
|
static GType my_object_get_type (void);
|
2015-08-16 22:04:49 +00:00
|
|
|
G_DEFINE_TYPE (MyObject, my_object, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_object_init (MyObject *obj)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_object_get_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
MyObject *obj = (MyObject *)object;
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_LABEL:
|
|
|
|
g_value_set_string (value, obj->label);
|
|
|
|
break;
|
|
|
|
case PROP_ID:
|
|
|
|
g_value_set_int (value, obj->id);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_object_set_property (GObject *object,
|
|
|
|
guint property_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
MyObject *obj = (MyObject *)object;
|
|
|
|
|
|
|
|
switch (property_id)
|
|
|
|
{
|
|
|
|
case PROP_LABEL:
|
|
|
|
g_free (obj->label);
|
|
|
|
obj->label = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
case PROP_ID:
|
|
|
|
obj->id = g_value_get_int (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_object_finalize (GObject *obj)
|
|
|
|
{
|
|
|
|
MyObject *object = (MyObject *)obj;
|
|
|
|
|
|
|
|
g_free (object->label);
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (my_object_parent_class)->finalize (obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
my_object_class_init (MyObjectClass *class)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
object_class->get_property = my_object_get_property;
|
|
|
|
object_class->set_property = my_object_set_property;
|
|
|
|
object_class->finalize = my_object_finalize;
|
|
|
|
|
|
|
|
properties[PROP_LABEL] = g_param_spec_string ("label", "label", "label",
|
|
|
|
NULL, G_PARAM_READWRITE);
|
|
|
|
properties[PROP_ID] = g_param_spec_int ("id", "id", "id",
|
|
|
|
0, G_MAXINT, 0, G_PARAM_READWRITE);
|
|
|
|
|
|
|
|
g_object_class_install_properties (object_class, LAST_PROPERTY, properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GtkWidget *
|
|
|
|
create_widget (gpointer item,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
MyObject *obj = (MyObject *)item;
|
|
|
|
GtkWidget *label;
|
|
|
|
|
|
|
|
label = gtk_label_new ("");
|
|
|
|
g_object_bind_property (obj, "label", label, "label", G_BINDING_SYNC_CREATE);
|
|
|
|
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:49 +00:00
|
|
|
static int
|
2015-08-16 22:04:49 +00:00
|
|
|
compare_items (gconstpointer a, gconstpointer b, gpointer data)
|
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int id_a, id_b;
|
2015-08-16 22:04:49 +00:00
|
|
|
|
|
|
|
g_object_get ((gpointer)a, "id", &id_a, NULL);
|
|
|
|
g_object_get ((gpointer)b, "id", &id_b, NULL);
|
|
|
|
|
|
|
|
return id_a - id_b;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
add_some (GtkButton *button, GListStore *store)
|
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int n, i;
|
2015-08-16 22:04:49 +00:00
|
|
|
guint n_items;
|
|
|
|
GObject *obj;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *label;
|
2015-08-16 22:04:49 +00:00
|
|
|
|
|
|
|
for (n = 0; n < 50; n++)
|
|
|
|
{
|
|
|
|
n_items = g_list_model_get_n_items (G_LIST_MODEL (store));
|
|
|
|
i = g_random_int_range (0, MAX (2 * n_items, 1));
|
|
|
|
label = g_strdup_printf ("Added %d", i);
|
|
|
|
obj = g_object_new (my_object_get_type (),
|
|
|
|
"id", i,
|
|
|
|
"label", label,
|
|
|
|
NULL);
|
|
|
|
g_list_store_insert_sorted (store, obj, compare_items, NULL);
|
|
|
|
g_object_unref (obj);
|
|
|
|
g_free (label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
remove_some (GtkButton *button, GListStore *store)
|
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int n, i;
|
2015-08-16 22:04:49 +00:00
|
|
|
guint n_items;
|
|
|
|
|
|
|
|
for (n = 0; n < 50; n++)
|
|
|
|
{
|
|
|
|
n_items = g_list_model_get_n_items (G_LIST_MODEL (store));
|
|
|
|
if (n_items == 0)
|
|
|
|
return;
|
|
|
|
i = g_random_int_range (0, n_items);
|
|
|
|
g_list_store_remove (store, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
{
|
2015-08-16 22:20:12 +00:00
|
|
|
GtkWidget *window, *grid, *sw, *box, *button;
|
2015-08-16 22:04:49 +00:00
|
|
|
GListStore *store;
|
2020-07-24 13:54:49 +00:00
|
|
|
int i;
|
2015-08-16 22:04:49 +00:00
|
|
|
|
2016-12-28 13:53:22 +00:00
|
|
|
gtk_init ();
|
2015-08-16 22:04:49 +00:00
|
|
|
|
|
|
|
store = g_list_store_new (my_object_get_type ());
|
|
|
|
for (i = 0; i < 100; i++)
|
|
|
|
{
|
|
|
|
MyObject *obj;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *label;
|
2015-08-16 22:04:49 +00:00
|
|
|
|
|
|
|
label = g_strdup_printf ("item %d", i);
|
|
|
|
obj = g_object_new (my_object_get_type (),
|
|
|
|
"id", i,
|
|
|
|
"label", label,
|
|
|
|
NULL);
|
|
|
|
g_list_store_append (store, obj);
|
|
|
|
g_free (label);
|
|
|
|
g_object_unref (obj);
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:55:36 +00:00
|
|
|
window = gtk_window_new ();
|
2015-08-16 22:20:12 +00:00
|
|
|
grid = gtk_grid_new ();
|
2020-05-02 21:26:54 +00:00
|
|
|
gtk_window_set_child (GTK_WINDOW (window), grid);
|
2020-06-24 15:25:09 +00:00
|
|
|
sw = gtk_scrolled_window_new ();
|
2015-08-16 22:20:12 +00:00
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_widget_set_hexpand (sw, TRUE);
|
|
|
|
gtk_widget_set_vexpand (sw, TRUE);
|
|
|
|
gtk_grid_attach (GTK_GRID (grid), sw, 0, 0, 1, 1);
|
|
|
|
|
|
|
|
box = gtk_list_box_new ();
|
|
|
|
gtk_list_box_bind_model (GTK_LIST_BOX (box), G_LIST_MODEL (store), create_widget, NULL, NULL);
|
2020-05-02 04:51:20 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), box);
|
2015-08-16 22:20:12 +00:00
|
|
|
|
2020-06-24 15:25:09 +00:00
|
|
|
sw = gtk_scrolled_window_new ();
|
2015-08-16 22:20:12 +00:00
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
|
|
|
|
GTK_POLICY_AUTOMATIC,
|
|
|
|
GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_widget_set_hexpand (sw, TRUE);
|
|
|
|
gtk_widget_set_vexpand (sw, TRUE);
|
|
|
|
gtk_grid_attach (GTK_GRID (grid), sw, 1, 0, 1, 1);
|
|
|
|
|
|
|
|
box = gtk_flow_box_new ();
|
|
|
|
gtk_flow_box_bind_model (GTK_FLOW_BOX (box), G_LIST_MODEL (store), create_widget, NULL, NULL);
|
2020-05-02 04:51:20 +00:00
|
|
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (sw), box);
|
2015-08-16 22:04:49 +00:00
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("Add some");
|
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (add_some), store);
|
|
|
|
gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 1, 1);
|
|
|
|
|
|
|
|
button = gtk_button_new_with_label ("Remove some");
|
|
|
|
g_signal_connect (button, "clicked", G_CALLBACK (remove_some), store);
|
|
|
|
gtk_grid_attach (GTK_GRID (grid), button, 0, 2, 1, 1);
|
|
|
|
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2015-08-16 22:04:49 +00:00
|
|
|
|
2020-02-10 03:24:47 +00:00
|
|
|
while (TRUE)
|
|
|
|
g_main_context_iteration (NULL, TRUE);
|
2015-08-16 22:04:49 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|