GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Oct 31, 2014 4:29 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Code doesn't wait for button to be clicked before executing
PostPosted: Mon Oct 17, 2011 2:43 pm 
Offline

Joined: Mon Oct 17, 2011 1:55 pm
Posts: 2
Hello,

First, I apologize if this topic has already been discussed here; I searched the forum for similar topics, but couldn't find any.

I've created a GTK dialog box with two buttons. Clicking either of the buttons sets or clears a flag, which is supposed to drive the program's execution flow down one of two paths; this flag (an int variable) is set to zero initially in the dialog box initialization code. Unfortunately, the program doesn't stop executing code when the dialog box appears, but takes the initial value of the flag and always follows one of the two execution paths.

What should I do to stop the code from executing further until one of the buttons is clicked?

Dialog box initialization function code:

Code:
int diaginit()
{
        // gtk_init and gtk_main are called elsewhere, in a higher-level function
        // dialog, label, yes and no are global GtkWidgets

   int flag = 0;

   dialog = gtk_dialog_new();
   gtk_window_set_title(GTK_WINDOW(dialog), "Backup Confirmation");
   gtk_container_set_border_width(GTK_CONTAINER(dialog), 10);
   gtk_widget_set_size_request(dialog, -1, -1);

   label = gtk_label_new("Are you sure you want to back up these values?\n");
   gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);   
   yes = gtk_button_new_with_label("Yes");
   no = gtk_button_new_with_label("No");

   gtk_signal_connect(GTK_OBJECT(yes), "clicked", GTK_SIGNAL_FUNC(init_backup), NULL);
   gtk_signal_connect(GTK_OBJECT(no), "clicked", GTK_SIGNAL_FUNC(kill_backup), NULL);
   gtk_signal_connect(GTK_OBJECT(dialog), "delete_event", GTK_SIGNAL_FUNC(destroyapp), NULL);

   gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), yes);
   gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), no);
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);

   gtk_widget_show_all(dialog);

   return 0;
}

static void init_backup()
{
   fprintf(stderr, "Initiating MU data backup now...\n");
        fflush(stdout);   
   gtk_widget_destroy(dialog);            
   flag = 1;                  // drops control back to the main() routine
}

static void kill_backup()
{
   fprintf(stderr, "Not going to back up the MU data now.\n");
        fflush(stdout);   
   gtk_widget_destroy(dialog);            
        flag = 0;                  // drops control back to the main() routine
}

gint destroyapp(GtkWidget *widget, gpointer gdata)
{
  g_print("Quitting...\n");
  return(FALSE);
}



The code which calls this dialog function:

Code:
                  ......
                  diaginit();                  // makes the warning dialog box
        printf("Made it this far. Flag = %d.\n", flag);
        fflush(stdout);

             printf("The current state of yes and no are %d and %d, respectively.\n", gtk_widget_get_state(yes), gtk_widget_get_state(no));

             if(flag == 1)
             {
                  ......
                  }

                  else
                  {
                  ......
                  }


Thanks in advance for any help.


Top
 Profile  
 
 Post subject: Re: Code doesn't wait for button to be clicked before execut
PostPosted: Mon Oct 17, 2011 6:45 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 768
Location: UK
Hi and welcome,

There are many bits missing from your code example so I will have to guess what they should be!

In your dialog initialisation function you create your dialog, add the label and buttons and connect signals. In the code that calls the dialog initialisation control is never passed back to GTK to show and work with the dialog before testing for the results. Therefore you will always get the same result of the initial value.

I think that you could simplify all your code to
Code:
void ask_backup()
{
    /* Create the dialog with message and buttons */
    GtkWidget *dialog = gtk_message_dialog_new(
        NULL, GTK_DIALOG_MODAL,
        GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
        "Are you sure you want to back up these values?"
    );
    /* set the window title note not all window managers will show this message */
    gtk_window_set_title(GTK_WINDOW(dialog), "Backup Confirmation");

    /* Display and run the dialog and then act on the response */
    switch (gtk_dialog_run(GTK_DIALOG(dialog))) {
    case GTK_RESPONSE_YES:
        /* YES button clicked */
        break;
    case GTK_RESPONSE_NO:
        /* NO button clicked */
        break;
    default:
        /* Some thing else has happened, such as window minimised deleted etc..... */
        break;
    }

   /* destroy the dialog */
    gtk_widget_destroy (dialog);
}

E.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Code doesn't wait for button to be clicked before execut
PostPosted: Tue Oct 18, 2011 3:24 pm 
Offline

Joined: Mon Oct 17, 2011 1:55 pm
Posts: 2
That did the trick! Many thanks!

So the problem seems to have been that control was not passed back to GTK properly? Most of the tutorials I'd found on basic GTK dialog windows via Googling used gtk_main to do this; are there any tutorials you are aware of that discuss this "passing of control" in more depth?

Thanks again. :)


Top
 Profile  
 
 Post subject: Re: Code doesn't wait for button to be clicked before execut
PostPosted: Wed Oct 19, 2011 6:49 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 768
Location: UK
Good to know that it worked for you. Basically what you described was to have a modal dialogue window what asks a question then does whatever is needed depending on the response.

There is a list of tutorials and many more information here in this forum LINK. One good one is http://zetcode.com/tutorials/gtktutorial/, there is a section on dialogue windows and it talks about modal and non-modal .

E.

_________________
E.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Tanite and 1 guest


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