Ok, I changed the GTK_COMBO to GTK_COMBO_BOX with no luck - still the old familiar segmentation fault. Here's the code for my test program. It's only purpose is to make clear how multiple widgets can be passed to and used in a callback function.
Code:
#include <gtk/gtk.h>
#include <glib-object.h>
typedef struct {
GtkWidget *label, *label2, *combo_box;
} AppWidgets;
static gboolean delete_event( GtkWidget *widget, GdkEvent *event, gpointer data);
static void callback_button_pressed (GtkWidget *widget, GtkComboBox *combo_box, AppWidgets *w);
int main( int argc,
char *argv[] )
{
/* Create needed widgets */
GtkWidget *window;
GtkWidget *table;
GtkWidget *button;
GtkWidget *image;
GtkWidget *combo_box;
GtkWidget *label;
GtkWidget *label2;
/* Create a new AppWidgets object */
AppWidgets *w = g_slice_new (AppWidgets);
/* Initialize GTK */
gtk_init (&argc, &argv);
/* Create window and set a title and borders for it */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_set_application_name("Test program");
gtk_container_set_border_width (GTK_CONTAINER (window), 20);
gtk_widget_set_size_request (window, 712, 400);
/* Set a handler for delete_event that immediately
* exits GTK. */
g_signal_connect (G_OBJECT (window), "delete_event",
G_CALLBACK (delete_event), NULL);
/* Create table and add it to window */
table = gtk_table_new (5, 5, TRUE);
gtk_container_add (GTK_CONTAINER (window), table);
/* Create image and add it into the table */
image = gtk_image_new_from_file("logo.png");
gtk_table_attach_defaults (GTK_TABLE (table), image, 0, 5, 0,1);
gtk_widget_show(image);
/* Create the labels and attach them to the table */
label = gtk_label_new ("This is the first label");
gtk_table_attach_defaults(GTK_TABLE (table), label, 0, 3, 1, 2);
gtk_widget_show (label);
label2 = gtk_label_new ("This is the second label");
gtk_table_attach_defaults (GTK_TABLE (table), label2, 0, 3, 2, 3);
gtk_widget_show (label2);
/* Create button and add it into the table */
button = gtk_button_new_with_label("Push me");
gtk_table_attach_defaults (GTK_TABLE (table), button, 3, 5, 1, 2);
gtk_widget_show (button);
/* Create combo box and add it into the table */
combo_box = gtk_combo_box_new_text();
gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "Entry 1");
gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "Entry 2");
gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), "Entry 3");
gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
gtk_table_attach_defaults (GTK_TABLE (table), combo_box, 3, 5, 2, 3);
gtk_widget_show (combo_box);
/* Attach a signal handler to the button */
g_signal_connect (G_OBJECT (button), "clicked",
G_CALLBACK (callback_button_pressed), (gpointer) w);
/* Show all widgets in the Window */
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main ();
return 0;
}
/* Callback to close the window */
static gboolean delete_event( GtkWidget *widget, GdkEvent *event, gpointer data)
{
gtk_main_quit ();
return FALSE;
}
/* Callback for button pressing */
static void callback_button_pressed (GtkWidget *widget, GtkComboBox *combo_box, AppWidgets *w)
{
const gchar *str;
str = gtk_combo_box_get_active_text (GTK_COMBO_BOX (w->combo_box));
g_print("The text was %s\n", str);
}
I'm not sure if I should use "gtk_combo_box_get_active_text(GTK_BIN(GTK_COMBO_BOX (w->combo_box))->child)". Anyway, it also gives me segmentation fault.
When I run the debugger I get this kind of info:
Quote:
Program received signal SIGSEGV, Segmentation fault.
0x080490ae in callback_button_pressed (widget=0x80806e0, combo_box=0x804bf00,
w=0x0) at main2.c:98
98 str = gtk_combo_box_get_active_text (GTK_COMBO_BOX (w->combo_box));
(gdb) where
#0 0x080490ae in callback_button_pressed (widget=0x80806e0,
combo_box=0x804bf00, w=0x0) at main2.c:98
#1 0xb79ae423 in g_cclosure_marshal_VOID__VOID ()
from /usr/lib/libgobject-2.0.so.0
#2 0xb79a279f in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#3 0xb79b12ea in g_signal_stop_emission () from /usr/lib/libgobject-2.0.so.0
#4 0xb79b2b19 in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#5 0xb79b2e89 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#6 0xb7cca49f in gtk_button_clicked () from /usr/lib/libgtk-x11-2.0.so.0
#7 0xb7ccbcda in _gtk_button_set_depressed ()
from /usr/lib/libgtk-x11-2.0.so.0
#8 0xb79ae423 in g_cclosure_marshal_VOID__VOID ()
from /usr/lib/libgobject-2.0.so.0
#9 0xb79a216f in g_cclosure_new_swap () from /usr/lib/libgobject-2.0.so.0
#10 0xb79a279f in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#11 0xb79b15cc in g_signal_stop_emission () from /usr/lib/libgobject-2.0.so.0
#12 0xb79b2b19 in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#13 0xb79b2e89 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#14 0xb7cca41c in gtk_button_released () from /usr/lib/libgtk-x11-2.0.so.0
#15 0xb7ccb38c in _gtk_button_paint () from /usr/lib/libgtk-x11-2.0.so.0
#16 0xb7d8b8e0 in _gtk_marshal_BOOLEAN__BOXED ()
from /usr/lib/libgtk-x11-2.0.so.0
#17 0xb79a216f in g_cclosure_new_swap () from /usr/lib/libgobject-2.0.so.0
---Type <return> to continue, or q <return> to quit---
#18 0xb79a279f in g_closure_invoke () from /usr/lib/libgobject-2.0.so.0
#19 0xb79b19ce in g_signal_stop_emission () from /usr/lib/libgobject-2.0.so.0
#20 0xb79b2886 in g_signal_emit_valist () from /usr/lib/libgobject-2.0.so.0
#21 0xb79b2e89 in g_signal_emit () from /usr/lib/libgobject-2.0.so.0
#22 0xb7e6ddcf in gtk_widget_activate () from /usr/lib/libgtk-x11-2.0.so.0
#23 0xb7d8a05d in gtk_propagate_event () from /usr/lib/libgtk-x11-2.0.so.0
#24 0xb7d8a46b in gtk_main_do_event () from /usr/lib/libgtk-x11-2.0.so.0
#25 0xb7c2ddec in _gdk_events_queue () from /usr/lib/libgdk-x11-2.0.so.0
#26 0xb79328d6 in g_main_context_dispatch () from /usr/lib/libglib-2.0.so.0
#27 0xb7935996 in g_main_context_check () from /usr/lib/libglib-2.0.so.0
#28 0xb7935cb8 in g_main_loop_run () from /usr/lib/libglib-2.0.so.0
#29 0xb7d89765 in gtk_main () from /usr/lib/libgtk-x11-2.0.so.0
#30 0x08049082 in main (argc=1, argv=0xbff5fa44) at main2.c:81
What comes to your book, I definitely buy it if I decide to jump in to the world of GTK. I'm just wondering why there are no *good* tutorials about GTK either in printed form or online. For example, I got the idea to use the GTK_COMBO from the GTK.org's tutorial, which I assumed to be up-to-date...