GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sun Apr 20, 2014 8:17 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Help with dialog
PostPosted: Thu Nov 08, 2012 12:26 am 
Offline

Joined: Thu Nov 08, 2012 12:10 am
Posts: 4
Hi, I am new to GTK, have some experience with Perl but I am tryng to work on enbeded system and GUI application on Perl (Tk) are so slow that I decided to look at GTK. Well, slow progress, but I am stuck on one problem. I created Message dialog with 2 buttons, "yes" and "no", Now I am trying to connect signal but to what whithin dialog widget?


Code:
GtkWidget *dialog;
  dialog = gtk_message_dialog_new(GTK_WINDOW(window),
            GTK_DIALOG_DESTROY_WITH_PARENT,
            GTK_MESSAGE_QUESTION,
            GTK_BUTTONS_YES_NO,
            "If you agree press Yes if no press No");
  gtk_window_set_title(GTK_WINDOW(dialog), "YesNoDialog");
  gtk_dialog_run(GTK_DIALOG(dialog));
 
   g_signal_connect(G_OBJECT(GTK_STOCK_YES), "clicked",
     G_CALLBACK(Yes_clicked), NULL);

  gtk_widget_destroy(dialog);


Well after long search over the net I did as abve, I use GTK_STOCK_YES but it does not work. So I am stuck and frustrated. I am sure the answer is very easy but will appreciate help. Also what would be a good tutorial for GTK+ on line??

Thanks in advance
Robert


Top
 Profile  
 
 Post subject: Re: Help with dialog
PostPosted: Thu Nov 08, 2012 5:07 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
There is two different ways of doing dialogs, modal or non-modal.
A modal dialog will take over the application and freeze control until there is a response. Non modal dialogs allow control to be returned to interact with the min window etc.
If you want a modal dialog then you use gtk_dialog_run and then check the response type as to which button was pushed (See http://developer.gnome.org/gtk/2.24/GtkDialog.html#gtk-dialog-run for an example) After this you destroy the dialog widget.
If you want it to be non-modal then you DON'T use gtk_dialog_run but signal connect to each button as you would for any other widget. Destroying the dialog widget only occurs due to these signals .
Your code looks more like a modal dialog. In this case you only need to signal connect the destroy button and determine the button as per the link above.

zetcode has a number of tutorials on gtk, though they are gtk2, but recent enough that adapting to 3 isn't hard. Links can be found on the documentation page on the official gtk site.


Top
 Profile  
 
 Post subject: Re: Help with dialog
PostPosted: Thu Nov 08, 2012 6:10 am 
Offline
GTK+ Guru

Joined: Fri Mar 25, 2011 5:16 pm
Posts: 176
Location: USA
Here's the specific page but the main Zetcode page as well as links to a lot of other tutorials is listed on page 1 here in "Gtk+ Tutorials & Resources"
http://zetcode.com/tutorials/gtktutorial/gtkdialogs/


Top
 Profile  
 
 Post subject: SOLVED. Re: Help with dialog
PostPosted: Fri Nov 09, 2012 11:25 pm 
Offline

Joined: Thu Nov 08, 2012 12:10 am
Posts: 4
Thanks for your help guys, I started reading "Foundations of GTK+ Development". Clearly I am deficient with C and need to improve it.
Here is my solution:
Code:
#include <gtk/gtk.h>

void okPressed()
{
  g_print("OK clicked\n");
 
}

void cancelPressed()
{
    g_print("CANCEL clicked\n");
}

int main( int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *dialog;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title(GTK_WINDOW(window), "Top level window");
  gtk_window_set_default_size(GTK_WINDOW(window), 400, 300);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
   
   gtk_widget_show(window);
   
  dialog = gtk_message_dialog_new(GTK_WINDOW(window),
            GTK_DIALOG_DESTROY_WITH_PARENT,
            GTK_MESSAGE_QUESTION,
            GTK_BUTTONS_OK_CANCEL,
         "Make your choice");
         
  gtk_window_set_title(GTK_WINDOW(dialog), "Ok, cancel dialog");
 
  gint result;
 
result =  gtk_dialog_run(GTK_DIALOG(dialog));
 
if (result == GTK_RESPONSE_OK)
   {
      okPressed();
   }

if (result == GTK_RESPONSE_CANCEL)
   {
      cancelPressed();
   }
 
  gtk_widget_destroy(dialog);

  g_signal_connect_swapped(G_OBJECT(window), "destroy",
      G_CALLBACK(gtk_main_quit), NULL);

  gtk_main();
  return 0;
}

It works for me.

Robert


Top
 Profile  
 
 Post subject: Re: Help with dialog
PostPosted: Sun Nov 11, 2012 3:25 am 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
Don't forget to add a g_signal_connect destroy for the dialog window as well.
For more efficient code you should use an "else if" for the second if statement as well as in-lining your ok and cancel callbacks (unless there will be significant code reuse).
I don't see anything really wrong with the way you write your c code. Seems you have got the dialog working fine. Reading the book will help, but even for basic stuff there will be always times when you can't seem to figure out why things don't work out (and even when you do sometimes having a different perspective helps), so feel free to keep the questions coming.


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

All times are UTC


Who is online

Users browsing this forum: No registered users 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