Newbie from Hell



Hi all,

I am trying to create a simple window that uses a dialog to confirm exit from the window.

Here is my code so far, taken from the GTK+ tutorial and the API:

#include <gtk/gtk.h>
static gboolean delete_event( GtkWidget *widget,
                             GdkEvent  *event,
                             gpointer   data )
{
   /* If you return FALSE in the "delete_event" signal handler,
    * GTK will emit the "destroy" signal. Returning TRUE means
    * you don't want the window to be destroyed.
    * This is useful for popping up 'are you sure you want to quit?'
    * type dialogs. */
   g_print ("delete event occurred\n");
   /* Create a dialog box for closing the main window */
   GtkWidget *dialog;
   dialog = gtk_dialog_new_with_buttons ("Are you sure you want to quit?",
                                           /*GTK_WINDOW (main_window),*/
                                           NULL,
                                           GTK_DIALOG_DESTROY_WITH_PARENT,
                                           GTK_STOCK_CANCEL,
                                           GTK_RESPONSE_CANCEL,
                                           GTK_STOCK_QUIT,
                                           GTK_RESPONSE_ACCEPT,
                                           NULL);
   gtk_widget_show_all(dialog);
   gint result = gtk_dialog_run(GTK_DIALOG(dialog));
   switch (result) {
       case GTK_RESPONSE_ACCEPT:
       case GTK_RESPONSE_DELETE_EVENT:
       gtk_widget_destroy(dialog);
       case GTK_RESPONSE_NONE:
       break;
       case GTK_RESPONSE_CANCEL:
       gtk_widget_destroy(dialog);
       break;
   }
   /* Change TRUE to FALSE and the main window will be destroyed with
    * a "delete_event". */
   return TRUE;
}
/* Another callback */
static void destroy( GtkWidget *widget,
                    gpointer   data )
{
   gtk_main_quit ();
}
int main(int argc, char *argv[]) {
   /* GtkWidget is the storage type for widgets */
   GtkWidget *main_window;
   /* This is called in all GTK applications. Arguments are parsed
    * from the command line and are returned to the application. */
   gtk_init (&argc, &argv);
   /* Create a new window */
   main_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   /* When the window is given the "delete_event" signal (this is given
    * by the window manager, usually by the "close" option, or on the
    * titlebar), we ask it to call the delete_event () function
    * as defined above. The data passed to the callback
    * function is NULL and is ignored in the callback function. */
   g_signal_connect (G_OBJECT (main_window), "delete_event",
             G_CALLBACK (delete_event), NULL);
   /* Here we connect the "destroy" event to a signal handler.
    * This event occurs when we call gtk_widget_destroy() on the window,
    * or if we return FALSE in the "delete_event" callback. */
   g_signal_connect (G_OBJECT (main_window), "destroy",
             G_CALLBACK (destroy), NULL);
   /* This sets the title of our new window */
   gtk_window_set_title (GTK_WINDOW (main_window), "INSERT TITLE");
   /* Sets the border width of the window. */
   gtk_container_set_border_width (GTK_CONTAINER (main_window), 10);
   /* Show the window */
   gtk_widget_show (main_window);
   /* All GTK applications must have a gtk_main(). Control ends here
    * and waits for an event to occur (like a key press or
    * mouse event). */
   gtk_main ();
   return 0;
}
So I get my dialog but what I really don't understand is how to make it kill my main window when I click on the quit button.
   dialog = gtk_dialog_new_with_buttons ("Are you sure you want to quit?",
                                           /*GTK_WINDOW (main_window),*/
                                           NULL,

I know I need to be using the main_window argument above but I am really not sure how to structure my code. Whenever I try to use it i get a main_window not declared warning.

Can someone help me out?

Many thanks,

Peter.





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