Re: Newbie question regarding gtk_signal_connet

Nghia wrote:
> Ok, I was advised to use structures to solve the problem which I almost
> did except for a segmentation fault during the passing the structure to
> gtk_singal_connect. Here is what I used
> struct widget {
>   GtkWidget *intput;
>   GtkWidget *output;
> };
> void button_one_clicked(GtkWidget *button, struct widget
> *widget_holder){
> }
>  gtk_signal_connect(GTK_OBJECT(button_one, "clicked",
> GTK_SIGNAL_FUNC(button_one_clicked), widget_holder);
> But the moment the function button_one_clicked is run it crashes, even
> before it loads any variables (I've checked this by making the function
> empty).
> Is there something I did wrong? gcc didn't return any errors.

Not sure what's wrong, it looks about right but then there's not much
code in your message so it's hard to tell.

I wrote some demo code for someone else which was still on my disk so I
modified it to do what you wanted and I've attached it to this message.

Strictly speaking, you don't need to allocate and free the MyWindow
structure in main() - it could just live on the stack and be passed to
the callback(s) by reference. That's not true if the function that
builds the window ends before the window is destroyed which is common
when creating a dialog during a callback.

Hope this helps ... more :-)


Matt Goodall, Software Engineer  |  Isotek Electronics Ltd          |  Claro House, Servia Road          |  Leeds, LS7 1NL
Tel: +44 113 234320              |  England
#include <gtk/gtk.h>

struct MyWindow
    GtkWidget *window;
    GtkWidget *entry;
    GtkWidget *button;
    GtkWidget *label;

typedef struct MyWindow MyWindow;

static void window_destroy(GtkObject *object, gpointer *user_data);
static void button_clicked(GtkButton *button, gpointer *user_data);

int main( int argc, char *argv[] )
    MyWindow *data;
    GtkWidget *vbox;
    /* Initialise GTK */
    gtk_init( &argc, &argv );

    /* Allocate storage for the window */
    data = g_new0(MyWindow, 1);
    /* Create the window */
    data->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    /* Create a VBox to hold the *real* widgets */
    vbox = gtk_vbox_new(FALSE,5);
    gtk_container_add(GTK_CONTAINER(data->window), vbox);

    /* Create the entry */
    data->entry = gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(vbox), data->entry, FALSE, FALSE, 0);

    /* Create the button */
    data->button = gtk_button_new_with_label("I'm a button");
    gtk_box_pack_start(GTK_BOX(vbox), data->button, FALSE, FALSE, 0);

    /* Create the label */
    data->label = gtk_label_new("--empty--");
    gtk_box_pack_start(GTK_BOX(vbox), data->label, FALSE, FALSE, 0);

    /* Attach callbacks */
        GTK_OBJECT(data->button), "clicked",
        GTK_SIGNAL_FUNC(button_clicked), data);

        GTK_OBJECT(data->window), "destroy",
        GTK_SIGNAL_FUNC(window_destroy), data );

    /* Show the window */

    /* Run the GTK main loop */

    return 0;

static void button_clicked(GtkButton *button, gpointer *user_data)
    MyWindow *data;

    /* Get window data */
    data = (MyWindow*) user_data;
    /* Move text from entry to label */
        GTK_LABEL(data->label), gtk_entry_get_text(GTK_ENTRY(data->entry)));

static void window_destroy(GtkObject *object, gpointer *user_data)
    MyWindow *data;

    /* Get window data ... */
    data = (MyWindow*) user_data;

    /* ... and free it */

    /* End the program */

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]