34 #include <glib/gi18n.h>
39 #include "gnc-general-select.h"
51 static void gnc_general_select_dispose (GObject *
object);
52 static void gnc_general_select_finalize (GObject *
object);
54 static GtkHBoxClass *parent_class;
55 static guint general_select_signals[LAST_SIGNAL];
64 gnc_general_select_get_type (
void)
66 static GType general_select_type = 0;
68 if (general_select_type == 0)
70 static const GTypeInfo general_select_info =
75 (GClassInitFunc) gnc_general_select_class_init,
80 (GInstanceInitFunc) gnc_general_select_init,
84 general_select_type = g_type_register_static(GTK_TYPE_HBOX,
86 &general_select_info, 0);
89 return general_select_type;
93 gnc_general_select_forall (GtkContainer *container, gboolean include_internals,
94 GtkCallback callback, gpointer callback_data)
96 g_return_if_fail (container != NULL);
97 g_return_if_fail (GNC_IS_GENERAL_SELECT (container));
98 g_return_if_fail (callback != NULL);
102 if (!include_internals)
105 if (!GTK_CONTAINER_CLASS (parent_class)->forall)
108 GTK_CONTAINER_CLASS (parent_class)->forall (container,
117 GObjectClass *object_class = (GObjectClass *) klass;
118 GtkContainerClass *container_class = (GtkContainerClass *) klass;
120 object_class = (GObjectClass*) klass;
122 parent_class = g_type_class_ref(GTK_TYPE_HBOX);
124 general_select_signals[SELECTION_CHANGED] =
125 g_signal_new(
"changed",
126 G_TYPE_FROM_CLASS(object_class),
131 g_cclosure_marshal_VOID__VOID,
134 container_class->forall = gnc_general_select_forall;
136 object_class->dispose = gnc_general_select_dispose;
137 object_class->finalize = gnc_general_select_finalize;
139 klass->changed = NULL;
145 gsl->disposed = FALSE;
146 gsl->selected_item = NULL;
150 gnc_general_select_finalize (GObject *
object)
152 g_return_if_fail (
object != NULL);
153 g_return_if_fail (GNC_IS_GENERAL_SELECT (
object));
155 if (G_OBJECT_CLASS (parent_class)->finalize)
156 G_OBJECT_CLASS (parent_class)->finalize (
object);
160 gnc_general_select_dispose (GObject *
object)
164 g_return_if_fail (
object != NULL);
165 g_return_if_fail (GNC_IS_GENERAL_SELECT (
object));
167 gsl = GNC_GENERAL_SELECT (
object);
172 gsl->disposed = TRUE;
175 gtk_widget_destroy(GTK_WIDGET(gsl->entry));
178 gtk_widget_destroy(GTK_WIDGET(gsl->button));
182 if (G_OBJECT_CLASS (parent_class)->dispose)
183 G_OBJECT_CLASS (parent_class)->dispose (
object);
187 select_cb(GtkButton * button, gpointer user_data)
190 gpointer new_selection;
193 toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
195 new_selection = (gsl->new_select)(gsl->cb_arg, gsl->selected_item,
199 if (new_selection == NULL)
202 gnc_general_select_set_selected (gsl, new_selection);
208 gsl->entry = gtk_entry_new ();
209 gtk_editable_set_editable (GTK_EDITABLE (gsl->entry), FALSE);
210 gtk_box_pack_start (GTK_BOX (gsl), gsl->entry, TRUE, TRUE, 0);
211 gtk_widget_show (gsl->entry);
213 if (type == GNC_GENERAL_SELECT_TYPE_SELECT)
214 gsl->button = gtk_button_new_with_label (_(
"Select..."));
215 else if (type == GNC_GENERAL_SELECT_TYPE_EDIT)
216 gsl->button = gtk_button_new_with_label (_(
"Edit..."));
217 else if (type == GNC_GENERAL_SELECT_TYPE_VIEW)
218 gsl->button = gtk_button_new_with_label (_(
"View..."));
220 gtk_box_pack_start (GTK_BOX (gsl), gsl->button, FALSE, FALSE, 0);
221 g_signal_connect (G_OBJECT (gsl->button),
"clicked",
222 G_CALLBACK (select_cb), gsl);
223 gtk_widget_show (gsl->button);
235 gnc_general_select_new (GNCGeneralSelectType type,
236 GNCGeneralSelectGetStringCB get_string,
237 GNCGeneralSelectNewSelectCB new_select,
241 g_return_val_if_fail (get_string != NULL, NULL);
242 g_return_val_if_fail (new_select != NULL, NULL);
244 gsl = g_object_new(GNC_TYPE_GENERAL_SELECT, NULL, NULL);
246 create_children (gsl, type);
247 gsl->get_string = get_string;
248 gsl->new_select = new_select;
249 gsl->cb_arg = cb_arg;
251 return GTK_WIDGET (gsl);
262 gnc_general_select_get_printname (
GNCGeneralSelect *gsl, gpointer selection)
264 g_return_val_if_fail (gsl != NULL, NULL);
265 g_return_val_if_fail (selection != NULL, NULL);
267 return (gsl->get_string)(selection);
284 g_return_if_fail(gsl != NULL);
285 g_return_if_fail(GNC_IS_GENERAL_SELECT(gsl));
287 gsl->selected_item = selection;
289 if (selection == NULL)
292 text = gnc_general_select_get_printname(gsl, selection);
294 gtk_entry_set_text(GTK_ENTRY(gsl->entry), text);
296 g_signal_emit(gsl, general_select_signals[SELECTION_CHANGED], 0);
308 g_return_val_if_fail(gsl != NULL, NULL);
309 g_return_val_if_fail(GNC_IS_GENERAL_SELECT(gsl), NULL);
311 return gsl->selected_item;
323 gnc_general_select_make_mnemonic_target (
GNCGeneralSelect *gsl, GtkWidget *label)
325 g_return_if_fail(gsl);
326 g_return_if_fail(GNC_IS_GENERAL_SELECT(gsl));
327 g_return_if_fail(label);
329 gtk_label_set_mnemonic_widget (GTK_LABEL(label), gsl->entry);