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