Combo listbox signal capture
- From: author <millward Ms UManitoba CA>
- To: gtk-app-devel-list gnome org
- Subject: Combo listbox signal capture
- Date: Wed, 24 Oct 2001 16:00:07 -0500
How do I capture the signal when any item in a list is selected?
This list is part of a combo box.
I understand that that for a single item you do something like this:
GtkWidget * item;
item = CreateItem( item's name );
gtk_signal_connect(GTK_OBJECT(item), "select",
GTK_SIGNAL_FUNC( callback ), whateverdata );
And the callback is something like:
void callback(GtkWidget * widget, gpointer * data )
{
do whatever
}
Ok. That's fine for a SINGLE item. But what about if you want
ANY item in the list to activate a callback?
What if you have 50 items in lthe list? Do you have to use
CreateItem() for each and every one of them?
For example, my combo box has a list of 5 items. How
do I get any one of them, when clicked, to activate the
same callback?
And what signal do I catch for a button click on an item
in a list? "select" I assume. Y/N?
This is my code so far, but off course, it doesn't quite
work yet.
#include <stdio.h> /* printf() */
#include <gtk/gtk.h>
typedef struct cdata
{
GtkWidget * combo;
GtkWidget * label;
GList * cbitems;
} CData;
/* For GtkItemClass
void ( * select ) (GtkItem * item ); */
static void item_selected(GtkItem * item, CData * data)
{
g_print("\n Item selected was %s \n", data.cbitems );
}
statc void GetComboEntry(GtkWidget * widget,
CData * data)
{
char * val = (char *) NULL;
val = gtk_entry_get_text(
GTK_ENTRY(GTK_COMBO(data->combo)->entry) );
if( val )
{
printf("\nValue selected is %s\n", val);
gtk_label_set_text(GTK_LABEL(data->label), val);
}
} /* end of function GetComboEntry */
int main( int argc, char * argv[] )
{
GtkWidget * window, * button, * vbox;
GtkEntry * entry;
GtkWidget * list;
CData data;
data.cbitems = NULL;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_signal_connect(GTK_OBJECT(window), "delete_event",
GTK_SIGNAL_FUNC(gtk_main_quit),
NULL );
vbox = gtk_vbox_new(FALSE, 0);
gtk_window_position(GTK_WINDOW(window),
GTK_WIN_POS_MOUSE);
data.combo = gtk_combo_new();
data.cbitems = g_list_append(data.cbitems, "Yesterday");
data.cbitems = g_list_append(data.cbitems, "Today");
data.cbitems = g_list_append(data.cbitems, "Tomorrow");
data.cbitems = g_list_append(data.cbitems, "Today + 2");
data.cbitems = g_list_append(data.cbitems, "Today + 3");
data.cbitems = g_list_append(data.cbitems, "Today + 4");
gtk_combo_set_popdown_strings(GTK_COMBO(data.combo),
data.cbitems );
gtk_container_add(GTK_CONTAINER(vbox), data.combo );
/* Entry box NOT editable */
entry = GTK_ENTRY(GTK_COMBO(data.combo)->entry);
gtk_entry_set_editable(entry, FALSE);
gtk_signal_connect(GTK_ITEM(data.combo), "select",
GTK_SIGNAL_FUNC(item_selected), &data );
button = gtk_button_new_with_label("OK");
gtk_signal_connect(GTK_OBJECT(button), "clicked",
GTK_SIGNAL_FUNC(GetComboEntry ), &data);
data.label = gtk_label_new("Date/Year");
gtk_container_add(GTK_CONTAINER(vbox), data.label);
gtk_container_add(GTK_CONTAINER(vbox), button );
gtk_container_add(GTK_CONTAINER(window), vbox);
gtk_widget_show_all(window);
gtk_main();
return 0;
} /* end of program combo_1.c Logan GTK page 777 */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]