1 /* ///////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/treeentry_gtk.c
3 // Purpose: GtkTreeEntry implementation
6 // Copyright: (c) 2006 Ryan Norton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////// */
10 #include "wx/gtk/treeentry_gtk.h"
15 The main reason for this class is to have a holder for both a string
16 and userdata for us to use in wxListXXX classes.
18 This is transformable to a string for the Gtk implementations,
19 and the string passed in is duplicated and freed upon destruction.
21 As mentioned the real magic here is the transforming it to a string
22 which lets us use it as a entry in a GtkTreeView/GtkListStore
23 and still display it. Otherwise we would need to implement our
28 static void gtk_tree_entry_class_init(GtkTreeEntryClass
* klass
);
29 static void gtk_tree_entry_init (GTypeInstance
* instance
, gpointer g_class
);
30 static void gtk_tree_entry_string_transform_func(const GValue
*src_value
,
32 static void gtk_tree_entry_dispose(GObject
* obj
);
39 return GTK_TREE_ENTRY(g_object_new(GTK_TYPE_TREE_ENTRY
, NULL
));
43 gtk_tree_entry_get_type ()
45 static GtkType tree_entry_type
= 0;
49 static const GTypeInfo tree_entry_info
=
51 sizeof (GtkTreeEntryClass
),
53 NULL
, /* base_finalize */
54 (GClassInitFunc
) gtk_tree_entry_class_init
, /* class_init */
55 NULL
, /* class_finalize */
56 NULL
, /* class_data */
57 sizeof (GtkTreeEntry
),
59 (GInstanceInitFunc
) gtk_tree_entry_init
, /*instance_init*/
60 NULL
/* value_table */
62 tree_entry_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkTreeEntry",
65 g_value_register_transform_func(tree_entry_type
, G_TYPE_STRING
,
66 gtk_tree_entry_string_transform_func
);
69 return tree_entry_type
;
72 gchar
* gtk_tree_entry_get_collate_key (GtkTreeEntry
* entry
)
74 return entry
->collate_key
;
77 gchar
* gtk_tree_entry_get_label (GtkTreeEntry
* entry
)
79 g_assert(GTK_IS_TREE_ENTRY(entry
));
83 gpointer
gtk_tree_entry_get_userdata (GtkTreeEntry
* entry
)
85 g_assert(GTK_IS_TREE_ENTRY(entry
));
86 return entry
->userdata
;
89 void gtk_tree_entry_set_label (GtkTreeEntry
* entry
, const gchar
* label
)
91 g_assert(GTK_IS_TREE_ENTRY(entry
));
93 /* free previous if it exists */
97 g_free(entry
->collate_key
);
100 entry
->label
= g_strdup(label
);
101 entry
->collate_key
= g_utf8_collate_key(label
, -1); /* -1 == null terminated */
104 void gtk_tree_entry_set_userdata (GtkTreeEntry
* entry
, gpointer userdata
)
106 g_assert(GTK_IS_TREE_ENTRY(entry
));
107 entry
->userdata
= userdata
;
110 void gtk_tree_entry_set_destroy_func (GtkTreeEntry
* entry
,
111 GtkTreeEntryDestroy destroy_func
,
112 gpointer destroy_func_data
)
114 g_assert(GTK_IS_TREE_ENTRY(entry
));
115 entry
->destroy_func
= destroy_func
;
116 entry
->destroy_func_data
= destroy_func_data
;
120 static void gtk_tree_entry_class_init(GtkTreeEntryClass
* klass
)
122 GObjectClass
* gobject_class
= G_OBJECT_CLASS(klass
);
123 gobject_class
->dispose
= gtk_tree_entry_dispose
;
126 static void gtk_tree_entry_init (GTypeInstance
* instance
, gpointer g_class
)
128 GtkTreeEntry
* entry
= (GtkTreeEntry
*) instance
;
132 entry
->collate_key
= NULL
;
133 entry
->userdata
= NULL
;
134 entry
->destroy_func_data
= NULL
;
135 entry
->destroy_func
= NULL
;
138 static void gtk_tree_entry_string_transform_func(const GValue
*src_value
,
141 /* Make sure src is a treeentry and dest can hold a string */
142 g_assert(GTK_IS_TREE_ENTRY(src_value
->data
[0].v_pointer
));
143 g_assert(G_VALUE_HOLDS(dest_value
, G_TYPE_STRING
));
145 /* TODO: Use strdup here or just pass it? */
146 GtkTreeEntry
* entry
= GTK_TREE_ENTRY(src_value
->data
[0].v_pointer
);
148 g_value_set_string(dest_value
, entry
->label
);
151 static void gtk_tree_entry_dispose(GObject
* obj
)
153 g_assert(GTK_IS_TREE_ENTRY(obj
));
155 GtkTreeEntry
* entry
= GTK_TREE_ENTRY(obj
);
157 /* free label if it exists */
160 g_free(entry
->label
);
161 g_free(entry
->collate_key
);
163 entry
->collate_key
= NULL
;
166 /* call destroy callback if it exists */
167 if(entry
->destroy_func
)
169 (*entry
->destroy_func
) (entry
, entry
->destroy_func_data
);
170 entry
->destroy_func
= NULL
;
171 entry
->destroy_func_data
= NULL
;
175 entry
->userdata
= NULL
;