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*/
61 tree_entry_type
= g_type_register_static (G_TYPE_OBJECT
, "GtkTreeEntry",
64 g_value_register_transform_func(tree_entry_type
, G_TYPE_STRING
,
65 gtk_tree_entry_string_transform_func
);
68 return tree_entry_type
;
71 gchar
* gtk_tree_entry_get_collate_key (GtkTreeEntry
* entry
)
73 return entry
->collate_key
;
76 gchar
* gtk_tree_entry_get_label (GtkTreeEntry
* entry
)
78 g_assert(GTK_IS_TREE_ENTRY(entry
));
82 gpointer
gtk_tree_entry_get_userdata (GtkTreeEntry
* entry
)
84 g_assert(GTK_IS_TREE_ENTRY(entry
));
85 return entry
->userdata
;
88 void gtk_tree_entry_set_label (GtkTreeEntry
* entry
, const gchar
* label
)
90 g_assert(GTK_IS_TREE_ENTRY(entry
));
92 /* free previous if it exists */
96 g_free(entry
->collate_key
);
99 entry
->label
= g_strdup(label
);
100 entry
->collate_key
= g_utf8_collate_key(label
, -1); /* -1 == null terminated */
103 void gtk_tree_entry_set_userdata (GtkTreeEntry
* entry
, gpointer userdata
)
105 g_assert(GTK_IS_TREE_ENTRY(entry
));
106 entry
->userdata
= userdata
;
109 void gtk_tree_entry_set_destroy_func (GtkTreeEntry
* entry
,
110 GtkTreeEntryDestroy destroy_func
,
111 gpointer destroy_func_data
)
113 g_assert(GTK_IS_TREE_ENTRY(entry
));
114 entry
->destroy_func
= destroy_func
;
115 entry
->destroy_func_data
= destroy_func_data
;
119 static void gtk_tree_entry_class_init(GtkTreeEntryClass
* klass
)
121 GObjectClass
* gobject_class
= G_OBJECT_CLASS(klass
);
122 gobject_class
->dispose
= gtk_tree_entry_dispose
;
125 static void gtk_tree_entry_init (GTypeInstance
* instance
, gpointer g_class
)
127 GtkTreeEntry
* entry
= (GtkTreeEntry
*) instance
;
131 entry
->collate_key
= NULL
;
132 entry
->userdata
= NULL
;
133 entry
->destroy_func_data
= NULL
;
134 entry
->destroy_func
= NULL
;
137 static void gtk_tree_entry_string_transform_func(const GValue
*src_value
,
140 /* Make sure src is a treeentry and dest can hold a string */
141 g_assert(GTK_IS_TREE_ENTRY(src_value
->data
[0].v_pointer
));
142 g_assert(G_VALUE_HOLDS(dest_value
, G_TYPE_STRING
));
144 /* TODO: Use strdup here or just pass it? */
145 GtkTreeEntry
* entry
= GTK_TREE_ENTRY(src_value
->data
[0].v_pointer
);
147 g_value_set_string(dest_value
, g_strdup(entry
->label
));
150 static void gtk_tree_entry_dispose(GObject
* obj
)
152 g_assert(GTK_IS_TREE_ENTRY(obj
));
154 GtkTreeEntry
* entry
= GTK_TREE_ENTRY(obj
);
156 /* free label if it exists */
159 g_free(entry
->label
);
160 g_free(entry
->collate_key
);
162 entry
->collate_key
= NULL
;
165 /* call destroy callback if it exists */
166 if(entry
->destroy_func
)
168 (*entry
->destroy_func
) (entry
, entry
->destroy_func_data
);
169 entry
->destroy_func
= NULL
;
170 entry
->destroy_func_data
= NULL
;
174 entry
->userdata
= NULL
;