]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/treeentry_gtk.c
cleanup
[wxWidgets.git] / src / gtk / treeentry_gtk.c
1 /* ///////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/treeentry_gtk.c
3 // Purpose: GtkTreeEntry implementation
4 // Author: Ryan Norton
5 // Id: $Id$
6 // Copyright: (c) 2006 Ryan Norton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////// */
9
10 #include "wx/gtk/treeentry_gtk.h"
11
12 /*
13 GtkTreeEntry
14
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.
17
18 This is transformable to a string for the Gtk implementations,
19 and the string passed in is duplicated and freed upon destruction.
20
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
24 own model etc..
25 */
26
27 /* forwards */
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,
31 GValue *dest_value);
32 static void gtk_tree_entry_dispose(GObject* obj);
33
34
35 /* public */
36 GtkTreeEntry*
37 gtk_tree_entry_new()
38 {
39 return GTK_TREE_ENTRY(g_object_new(GTK_TYPE_TREE_ENTRY, NULL));
40 }
41
42 GtkType
43 gtk_tree_entry_get_type ()
44 {
45 static GtkType tree_entry_type = 0;
46
47 if (!tree_entry_type)
48 {
49 const GTypeInfo tree_entry_info =
50 {
51 sizeof (GtkTreeEntryClass),
52 NULL, /* base_init */
53 NULL, /* base_finalize */
54 (GClassInitFunc) gtk_tree_entry_class_init, /* class_init */
55 NULL, /* class_finalize */
56 NULL, /* class_data */
57 sizeof (GtkTreeEntry),
58 16, /* n_preallocs */
59 (GInstanceInitFunc) gtk_tree_entry_init, /*instance_init*/
60 NULL /* value_table */
61 };
62 tree_entry_type = g_type_register_static (G_TYPE_OBJECT, "GtkTreeEntry",
63 &tree_entry_info,
64 (GTypeFlags)0);
65 g_value_register_transform_func(tree_entry_type, G_TYPE_STRING,
66 gtk_tree_entry_string_transform_func);
67 }
68
69 return tree_entry_type;
70 }
71
72 gchar* gtk_tree_entry_get_collate_key (GtkTreeEntry* entry)
73 {
74 return entry->collate_key;
75 }
76
77 gchar* gtk_tree_entry_get_label (GtkTreeEntry* entry)
78 {
79 g_assert(GTK_IS_TREE_ENTRY(entry));
80 return entry->label;
81 }
82
83 gpointer gtk_tree_entry_get_userdata (GtkTreeEntry* entry)
84 {
85 g_assert(GTK_IS_TREE_ENTRY(entry));
86 return entry->userdata;
87 }
88
89 void gtk_tree_entry_set_label (GtkTreeEntry* entry, const gchar* label)
90 {
91 g_assert(GTK_IS_TREE_ENTRY(entry));
92
93 /* free previous if it exists */
94 if(entry->label)
95 {
96 g_free(entry->label);
97 g_free(entry->collate_key);
98 }
99
100 entry->label = g_strdup(label);
101 entry->collate_key = g_utf8_collate_key(label, -1); /* -1 == null terminated */
102 }
103
104 void gtk_tree_entry_set_userdata (GtkTreeEntry* entry, gpointer userdata)
105 {
106 g_assert(GTK_IS_TREE_ENTRY(entry));
107 entry->userdata = userdata;
108 }
109
110 void gtk_tree_entry_set_destroy_func (GtkTreeEntry* entry,
111 GtkTreeEntryDestroy destroy_func,
112 gpointer destroy_func_data)
113 {
114 g_assert(GTK_IS_TREE_ENTRY(entry));
115 entry->destroy_func = destroy_func;
116 entry->destroy_func_data = destroy_func_data;
117 }
118
119 /* private */
120 static void gtk_tree_entry_class_init(GtkTreeEntryClass* klass)
121 {
122 GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
123 gobject_class->dispose = gtk_tree_entry_dispose;
124 }
125
126 static void gtk_tree_entry_init (GTypeInstance* instance, gpointer g_class)
127 {
128 GtkTreeEntry* entry = (GtkTreeEntry*) instance;
129
130 /* clear */
131 entry->label = NULL;
132 entry->collate_key = NULL;
133 entry->userdata = NULL;
134 entry->destroy_func_data = NULL;
135 entry->destroy_func = NULL;
136 }
137
138 static void gtk_tree_entry_string_transform_func(const GValue *src_value,
139 GValue *dest_value)
140 {
141 GtkTreeEntry *entry;
142
143 /* Make sure src is a treeentry and dest can hold a string */
144 g_assert(GTK_IS_TREE_ENTRY(src_value->data[0].v_pointer));
145 g_assert(G_VALUE_HOLDS(dest_value, G_TYPE_STRING));
146
147 /* TODO: Use strdup here or just pass it? */
148 entry = GTK_TREE_ENTRY(src_value->data[0].v_pointer);
149
150 g_value_set_string(dest_value, entry->label);
151 }
152
153 static void gtk_tree_entry_dispose(GObject* obj)
154 {
155 GtkTreeEntry *entry;
156
157 g_assert(GTK_IS_TREE_ENTRY(obj));
158
159 entry = GTK_TREE_ENTRY(obj);
160
161 /* free label if it exists */
162 if(entry->label)
163 {
164 g_free(entry->label);
165 g_free(entry->collate_key);
166 entry->label = NULL;
167 entry->collate_key = NULL;
168 }
169
170 /* call destroy callback if it exists */
171 if(entry->destroy_func)
172 {
173 (*entry->destroy_func) (entry, entry->destroy_func_data);
174 entry->destroy_func = NULL;
175 entry->destroy_func_data = NULL;
176 }
177
178 /* clear userdata */
179 entry->userdata = NULL;
180 }