1 /* ///////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/treeentry_gtk.c
3 // Purpose: GtkTreeEntry implementation
6 // Copyright: (c) 2006 Ryan Norton
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////// */
13 #define G_GNUC_INTERNAL
17 #include "wx/gtk/treeentry_gtk.h"
22 The main reason for this class is to have a holder for both a string
23 and userdata for us to use in wxListXXX classes.
25 This is transformable to a string for the Gtk implementations,
26 and the string passed in is duplicated and freed upon destruction.
28 As mentioned the real magic here is the transforming it to a string
29 which lets us use it as a entry in a GtkTreeView/GtkListStore
30 and still display it. Otherwise we would need to implement our
35 static void gtk_tree_entry_class_init(void* g_class
, void* class_data
);
36 static void gtk_tree_entry_init (GTypeInstance
* instance
, gpointer g_class
);
37 static void gtk_tree_entry_string_transform_func(const GValue
*src_value
,
39 static void gtk_tree_entry_dispose(GObject
* obj
);
41 static GObjectClass
* parent_class
;
47 return GTK_TREE_ENTRY(g_object_new(GTK_TYPE_TREE_ENTRY
, NULL
));
51 gtk_tree_entry_get_type ()
53 static GType tree_entry_type
= 0;
57 const GTypeInfo tree_entry_info
=
59 sizeof (GtkTreeEntryClass
),
61 NULL
, /* base_finalize */
62 gtk_tree_entry_class_init
,
63 NULL
, /* class_finalize */
64 NULL
, /* class_data */
65 sizeof (GtkTreeEntry
),
67 (GInstanceInitFunc
) gtk_tree_entry_init
, /*instance_init*/
68 NULL
/* value_table */
70 tree_entry_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkTreeEntry",
73 g_value_register_transform_func(tree_entry_type
, G_TYPE_STRING
,
74 gtk_tree_entry_string_transform_func
);
77 return tree_entry_type
;
80 gchar
* gtk_tree_entry_get_collate_key (GtkTreeEntry
* entry
)
82 if (entry
->collate_key
== NULL
)
84 char* temp
= g_utf8_casefold(entry
->label
, -1);
85 entry
->collate_key
= g_utf8_collate_key(temp
, -1);
88 return entry
->collate_key
;
91 gchar
* gtk_tree_entry_get_label (GtkTreeEntry
* entry
)
93 g_assert(GTK_IS_TREE_ENTRY(entry
));
97 gpointer
gtk_tree_entry_get_userdata (GtkTreeEntry
* entry
)
99 g_assert(GTK_IS_TREE_ENTRY(entry
));
100 return entry
->userdata
;
103 void gtk_tree_entry_set_label (GtkTreeEntry
* entry
, const gchar
* label
)
105 g_assert(GTK_IS_TREE_ENTRY(entry
));
107 /* free previous if it exists */
110 g_free(entry
->label
);
111 g_free(entry
->collate_key
);
114 entry
->label
= g_strdup(label
);
115 entry
->collate_key
= NULL
;
118 void gtk_tree_entry_set_userdata (GtkTreeEntry
* entry
, gpointer userdata
)
120 g_assert(GTK_IS_TREE_ENTRY(entry
));
121 entry
->userdata
= userdata
;
124 void gtk_tree_entry_set_destroy_func (GtkTreeEntry
* entry
,
125 GtkTreeEntryDestroy destroy_func
,
126 gpointer destroy_func_data
)
128 g_assert(GTK_IS_TREE_ENTRY(entry
));
129 entry
->destroy_func
= destroy_func
;
130 entry
->destroy_func_data
= destroy_func_data
;
134 static void gtk_tree_entry_class_init(void* g_class
, void* class_data
)
136 GObjectClass
* gobject_class
= G_OBJECT_CLASS(g_class
);
137 gobject_class
->dispose
= gtk_tree_entry_dispose
;
138 parent_class
= G_OBJECT_CLASS(g_type_class_peek_parent(g_class
));
141 static void gtk_tree_entry_init (GTypeInstance
* instance
, gpointer g_class
)
143 GtkTreeEntry
* entry
= (GtkTreeEntry
*) instance
;
147 entry
->collate_key
= NULL
;
148 entry
->userdata
= NULL
;
149 entry
->destroy_func_data
= NULL
;
150 entry
->destroy_func
= NULL
;
153 static void gtk_tree_entry_string_transform_func(const GValue
*src_value
,
157 void* src_ptr
= g_value_peek_pointer(src_value
);
159 /* Make sure src is a treeentry and dest can hold a string */
160 g_assert(GTK_IS_TREE_ENTRY(src_ptr
));
161 g_assert(G_VALUE_HOLDS(dest_value
, G_TYPE_STRING
));
163 entry
= GTK_TREE_ENTRY(src_ptr
);
164 g_value_set_string(dest_value
, entry
->label
);
167 static void gtk_tree_entry_dispose(GObject
* obj
)
171 g_assert(GTK_IS_TREE_ENTRY(obj
));
173 entry
= GTK_TREE_ENTRY(obj
);
175 /* free label if it exists */
178 g_free(entry
->label
);
179 g_free(entry
->collate_key
);
181 entry
->collate_key
= NULL
;
184 /* call destroy callback if it exists */
185 if(entry
->destroy_func
)
187 (*entry
->destroy_func
) (entry
, entry
->destroy_func_data
);
188 entry
->destroy_func
= NULL
;
189 entry
->destroy_func_data
= NULL
;
193 entry
->userdata
= NULL
;
195 parent_class
->dispose(obj
);