GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Mon Dec 22, 2014 1:23 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Show dialog in gtk3 with glade
PostPosted: Mon Mar 24, 2014 7:24 pm 
Offline
Familiar Face

Joined: Sat Mar 22, 2014 1:47 pm
Posts: 5
Hey guys so I started learning C and GTK like a month a go and I started building my first gtk program and I here I am because I need some help from you

I decided to use glade for building interface of my program but there is like 10 tutorials which cover glade and gtk and I can not find example on how to display dialog, any dialog like filechooser or about

In Glade 3.8.0 Im using I creted window in that window I have button after that I have filechooserdialog and two buttons inside OK nad Cancel when I click button I wold like to show filechooserdialog and to get path and file name from it. Here is my code which I used from here

Code:
#include <gtk/gtk.h>


static void
show_dialog(GtkWidget *widget,
             gpointer   data)
{
  /*Show Dialog*/
}

int
main (int   argc,
      char *argv[])
{
  GtkBuilder *builder;
  GObject *window;
  GObject *filedialog;
  GObject *button;

  gtk_init (&argc, &argv);

  /* Construct a GtkBuilder instance and load our UI description */
  builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "builder.glade", NULL);

  /* Connect signal handlers to the constructed widgets. */
  window = gtk_builder_get_object (builder, "window");
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

  filedialog = gtk_builder_get_object (builder, "filechooserdialog");

  button = gtk_builder_get_object (builder, "button1");
  g_signal_connect (button, "clicked", G_CALLBACK (show_dialog), NULL);

  button = gtk_builder_get_object (builder, "quit");
  g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);

    /* Destroy builder */
  g_object_unref( G_OBJECT( builder ) );

  gtk_main ();

  return 0;
}


So i found gtk_dialog_run() but I do not know how to use it do show dialog what argument do I need to pass to it ?
If I compare this code to gtk2 code example there is no gtk_widget_show( window ); and gtk_builder_connect_signals( builder, NULL ); is this ok/normal ?

There is several examples how to show dialog but they use
Code:
GtkWidget *dialog = gtk_file_chooser_dialog_new (_("Open image"),
                                                    GTK_WINDOW (toplevel),
                                                    GTK_FILE_CHOOSER_ACTION_OPEN,
                                                    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
                                                    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                                    NULL);


which I do not need I would like to use filechoserdialog from glade. Many tnx in advice on any sugestion, cheers !


Top
 Profile  
 
 Post subject: Re: Show dialog in gtk3 with glade
PostPosted: Tue Mar 25, 2014 7:04 pm 
Offline
Familiar Face

Joined: Tue Oct 08, 2013 5:16 am
Posts: 20
Why not just show it the same way you show the main window?
It's the same thing . . .
1. get the dialog from the glade file
2. run it
"Easy peasy", as they say.

Edit: There is one thing, you might want to "hide" rather than "destroy" the dialog after your done with it.


Top
 Profile  
 
 Post subject: Re: Show dialog in gtk3 with glade
PostPosted: Tue Mar 25, 2014 9:53 pm 
Offline
Familiar Face

Joined: Sat Mar 22, 2014 1:47 pm
Posts: 5
taupist wrote:
Why not just show it the same way you show the main window?
It's the same thing . . .
1. get the dialog from the glade file
2. run it
"Easy peasy", as they say.

Edit: There is one thing, you might want to "hide" rather than "destroy" the dialog after your done with it.


I could do it like this but its a mess

Code:
#include <gtk/gtk.h>

static void
show_dialog ( GtkButton *button,
              gpointer   user_data )
{
    GtkBuilder *builder;
    GtkWidget *filechooser;
    builder = gtk_builder_new();
    gtk_builder_add_from_file( builder, "ui.glade", NULL );
    filechooser = GTK_WIDGET( gtk_builder_get_object( builder, "filechooserdialog1" ) );

    /* Run dialog */
/*
    gtk_dialog_run( GTK_DIALOG( filechooser ) );
*/
    gtk_widget_show(filechooser);

}


int
main( int    argc,
      char **argv )
{
    GtkBuilder *builder;
    GtkWidget  *window;

    GtkWidget *button;
    GtkWidget *filechooser;
    GtkWidget *about;

    gtk_init( &argc, &argv );

    /* Create builder and load interface */
    builder = gtk_builder_new();
    gtk_builder_add_from_file( builder, "ui.glade", NULL );

    /* Obtain widgets that we need */
    window = GTK_WIDGET( gtk_builder_get_object( builder, "window1" ) );
    g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);

    about = GTK_WIDGET( gtk_builder_get_object( builder, "aboutdialog1" ) );

    button = GTK_WIDGET(gtk_builder_get_object (builder, "button2") );
    g_signal_connect(button, "clicked",G_CALLBACK( show_dialog ), NULL );

    /* Connect callbacks */
    gtk_builder_connect_signals( builder, NULL );

    /* Destroy builder */
    g_object_unref( G_OBJECT( builder ) );

    /* Show main window and start main loop */
    gtk_widget_show( window );

    gtk_main();

    return( 0 );
}



what I would like is to have is filechooser in main and just display it with static void show_dialog ( GtkButton *button, gpointer user_data )


Top
 Profile  
 
 Post subject: Re: Show dialog in gtk3 with glade
PostPosted: Thu Mar 27, 2014 8:54 pm 
Offline
Familiar Face

Joined: Tue Oct 08, 2013 5:16 am
Posts: 20
I really don't know C, but I can't understand why you can't just do this;
Code:
    about = GTK_WIDGET( gtk_builder_get_object( builder, "aboutdialog1" ) );
    filechooser = GTK_WIDGET( gtk_builder_get_object( builder, "filechooserdialog1" ) );


Top
 Profile  
 
 Post subject: Re: Show dialog in gtk3 with glade
PostPosted: Fri Mar 28, 2014 10:13 pm 
Offline
Familiar Face

Joined: Sat Mar 22, 2014 1:47 pm
Posts: 5
taupist wrote:
I really don't know C, but I can't understand why you can't just do this;
Code:
    about = GTK_WIDGET( gtk_builder_get_object( builder, "aboutdialog1" ) );
    filechooser = GTK_WIDGET( gtk_builder_get_object( builder, "filechooserdialog1" ) );


I can, is just fine to do that but in main I have to initialized builder I need to do all this GtkBuilder *builder; builder = gtk_builder_new(); gtk_builder_add_from_file( builder, "ui.glade", NULL ); and when connecting signals from button which call function show_dialog I need to call new function g_signal_connect(button, "clicked",G_CALLBACK( show_dialog ), NULL ); and in then new function I need to initialize again builder .glade file so I have to do same thing twice and I would like to not to do that twice, maybe I can have it globaly avalible some how. And than I can display dialog just like this
Code:
static void
show_dialog(GtkWidget *widget,
             gpointer   data)
{
    gtk_dialog_run( GTK_DIALOG( filechooser ) );
}


and not to type all this
Code:
static void
show_dialog ( GtkButton *button,
         gint       response_id,
              gpointer   user_data )
{
    GtkBuilder *builder;
    GtkDialog *filechooser;
    builder = gtk_builder_new();

    gtk_builder_add_from_file( builder, "ui.glade", NULL );
    filechooser = GTK_DIALOG( gtk_builder_get_object( builder, "filechooserdialog1" ) );

    gtk_builder_connect_signals( builder,NULL );

    gtk_dialog_run( GTK_DIALOG( filechooser ) );


just to display dialog


Top
 Profile  
 
 Post subject: Re: Show dialog in gtk3 with glade
PostPosted: Sun Mar 30, 2014 9:53 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 799
Location: UK
Hello,

There are many ways to connects signals and display the widgets when using GtkBuilder.

With the signals you can connect them within the Glade file or manually in the source code.

The widgets can be displayed automatically when you build the widget in GtkBuilder. This depends on the settings within the Glade file.

With respect to your code so far you can use global variables, but after a while it will become rather cumbersome. You will also need to take a closer look at your memory management although this is not clear from your code.

_________________
E.


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] 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