GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Aug 24, 2016 8:28 am

All times are UTC

Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Passing Window To Callback Function
PostPosted: Thu Aug 09, 2012 6:03 pm 
Familiar Face

Joined: Fri Aug 03, 2012 12:06 am
Posts: 5
I am trying to pass a window object to a callback function, delete everything in that window, and put new stuff in. No matter what I seem to do, when I pass the window into the callback, I get a segfault. Here is the current iteration of my code that now passes a struct to the callback.

#include <gtk/gtk.h>

typedef struct start_dialog_type {
   GtkWidget   *window;
   GtkWidget   *button;
   GtkWidget   *logo;
} StartDialog;

static void enter_main(GtkWidget *widget, GdkEventButton *event, gpointer data)
   GtkWidget   *box;
   GtkWidget   *button;
   GtkWidget   *window;


   box = gtk_hbox_new(FALSE, 10);
   button = gtk_button_new_with_label("Button1");
   gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);

   button = gtk_button_new_with_label("Button2");
   gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);

   button = gtk_button_new_with_label("Button3");
   gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   gtk_window_set_default_size(GTK_WINDOW(window), 480, 270);


   //gtk_container_add(GTK_CONTAINER(((StartDialog *) data)->window), box); //Doesn't work, throws a seg fault
   gtk_container_add(GTK_CONTAINER(window), box);

int main (int argc, char *argv[])

   GdkCursor   *cur;
   StartDialog start;
   gtk_init (&argc, &argv);
* start.window
* *******/

   start.window = gtk_window_new(GTK_WINDOW_POPUP);


   gtk_window_set_default_size(GTK_WINDOW(start.window), 480, 270);


   start.logo = gtk_image_new_from_file("./logo.png");

* start.button
* *******/

   start.button = gtk_button_new();

   g_signal_connect(start.button, "clicked", G_CALLBACK(enter_main), &start);

   gtk_container_add(GTK_CONTAINER(start.button), start.logo);

* Display and start event loop
* *******/

   gtk_widget_show (start.window);       

   cur = gdk_cursor_new(GDK_BLANK_CURSOR);
   gdk_window_set_cursor(gtk_widget_get_window(GTK_WIDGET(start.window)), cur); //Must be placed after the main window gets shown.
   gdk_cursor_unref(cur); //Removes cursor object from memory once it has been set

   gtk_main ();
   return 0;

I am currently cross-compiling this for an ARM board. Thank you for any help you can provide!

 Post subject: Re: Passing Window To Callback Function
PostPosted: Thu Aug 09, 2012 7:21 pm 
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia

You're having troubles because GtkButton::clicked signal handler must have prototype:
void handler (GtkButton *button, gpointer data)

Try fixing that and I'm sure most of your troubles will go away.


Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Google Adsense [Bot], Yahoo [Bot] and 3 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group