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