Don't shoot the newbie.



Please :)

I really am just starting with GTK and C - I have a brief background in
Python, but that isn't really helping, unfortunately.

I'm trying to do a simple, hello-world type program.  It consists of a
window with a button.  When you push the button, it changes the title of
the window.

Or Might.

The problem appears to be as follows:  I have a callback function which
takes a gpointer, converts it to a GtkWindow and then sets the title.
But GTK splutters and complains that the gpointer isn't a window (yes,
it is initialised) ...

I'd rather not have the function use a global variable, that's just
icky.  Is there a decent way to do this?  (gtk_signal_connect_object
sprang to mind, but it fails similarly).

Apologies again for the stupid question.
Bradley

----

The code is as follows (my apologies for the lack of indentation - blame
Evo.):

#include <gtk/gtk.h>

gint on;

gint title_change(      GtkWidget *widget,
                                        GdkEvent *event,
                                        gpointer *data)
{
        if (on) 
        {
                gtk_window_set_title(GTK_WINDOW (data), (gpointer) "Title ON!");
        }
        else
        {
                gtk_window_set_title(GTK_WINDOW (data), (gpointer) "Title OFF!");;
        }

        if (on) on = 0;
        else on = 1;    
        return TRUE;
}

main(   gint argc,
                gchar *argv[])
{
        /* Definition. */       
        GtkWidget *mainwindow;
        GtkWidget *button;
        

        /* Initialisation. */
        on = 0; 
        gtk_init(&argc, &argv);
        
        mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        title_change(NULL,NULL,(gpointer) mainwindow);
        button = gtk_button_new_with_label((char *) "Change Title");
        gtk_signal_connect_object(GTK_OBJECT(button),"clicked",GTK_SIGNAL_FUNC(title_change), (gpointer) 
mainwindow);
        
        /* Pack. */
        
        gtk_container_add(GTK_CONTAINER (mainwindow), button);
        
        /* Visualize */
        gtk_widget_show(button);
        gtk_widget_show(mainwindow);
        
        gtk_main();

        return ;
}





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