GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Jun 29, 2016 12:04 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: how can i list the file and folders in a directory ?
PostPosted: Thu Dec 31, 2015 12:25 pm 
Offline

Joined: Mon Dec 28, 2015 1:39 pm
Posts: 3
I want to display all the file or folder in side a directory ? I got some code is it correct ?


Code:
#include <gtk/gtk.h>
#include<gdk-pixbuf/gdk-pixbuf.h>
#include <glib/gerror.h>
void store_filename (GtkWidget *widget, gpointer user_data) {
   GtkWidget *file_selector = GTK_WIDGET (user_data);
   const gchar *selected_filename;
   selected_filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selector));
   g_print ("Selected filename: %s\n", "home/all3axis/Pictures" );
}
void create_file_selection (void) {
   GtkWidget *file_selector;
   /* Create the selector */
   file_selector = gtk_file_selection_new ("home/all3axis/Pictures");
   g_signal_connect (GTK_FILE_SELECTION (file_selector)->ok_button,
                     "clicked",
                     G_CALLBACK (store_filename),
                     file_selector);
   /* Ensure that the dialog box is destroyed when the user clicks a button. */
   g_signal_connect_swapped (GTK_FILE_SELECTION (file_selector)->ok_button,
                             "clicked",
                             G_CALLBACK (gtk_widget_destroy),
                             file_selector);
   g_signal_connect_swapped (GTK_FILE_SELECTION (file_selector)->cancel_button,
                             "clicked",
                             G_CALLBACK (gtk_widget_destroy),
                             file_selector);
   /* Display that dialog */
   gtk_widget_show (file_selector);
}
int
main( int    argc,
      char **argv )
{   
      GtkWidget  *window,
                  *button;

   
      gtk_init (&argc, &argv);   



      gtk_widget_show (window);               
   
      gtk_main ();
      return 0;
}


how can I connect the the main function to other function ?


Top
 Profile  
 
 Post subject: Re: how can i list the file and folders in a directory ?
PostPosted: Thu Dec 31, 2015 7:16 pm 
Offline
Never Seen the Sunlight

Joined: Sat Jul 26, 2014 11:43 pm
Posts: 389
What operating system and version of GTK+ are you developing with? I see that you are using example code from

https://developer.gnome.org/gtk2/stable ... ction.html

GTK2. The GtkFileSelection is no longer used in GTK3. If you can, you want to be using GTK3 since there are a lot of improvements and some differences.

To get things to work in GTK2, you can test the following out.

Code:
//gcc -Wall file_select1.c -o file_select1 `pkg-config --cflags --libs gtk+-2.0`

#include <gtk/gtk.h>

void store_filename (GtkWidget *widget, gpointer user_data) {
   GtkWidget *file_selector = GTK_WIDGET (user_data);
   const gchar *selected_filename;
   selected_filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selector));
   g_print ("Selected filename: %s\n", selected_filename );
}
void create_file_selection (GtkWidget *widget, gpointer user_data) {
   GtkWidget *file_selector;
   /* Create the selector */
   file_selector = gtk_file_selection_new ("Please select a file for editing.");
   g_signal_connect (GTK_FILE_SELECTION (file_selector)->ok_button,
                     "clicked",
                     G_CALLBACK (store_filename),
                     file_selector);
   /* Ensure that the dialog box is destroyed when the user clicks a button. */
   g_signal_connect_swapped (GTK_FILE_SELECTION (file_selector)->ok_button,
                             "clicked",
                             G_CALLBACK (gtk_widget_destroy),
                             file_selector);
   g_signal_connect_swapped (GTK_FILE_SELECTION (file_selector)->cancel_button,
                             "clicked",
                             G_CALLBACK (gtk_widget_destroy),
                             file_selector);
   /* Display that dialog */
   gtk_widget_show (file_selector);
}
int
main( int    argc,
      char **argv )
{   
      GtkWidget  *window,
                  *button;
   
      gtk_init (&argc, &argv);   

      window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
      gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
      gtk_window_set_default_size(GTK_WINDOW(window), 200, 100);
      gtk_window_set_title(GTK_WINDOW(window), "File Selection");
      g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

      button=gtk_button_new_with_label("File Selection");
      g_signal_connect(button, "clicked", G_CALLBACK(create_file_selection), NULL);

      gtk_container_add(GTK_CONTAINER(window), button);

      gtk_widget_show_all (window);               
   
      gtk_main ();
      return 0;
}


Top
 Profile  
 
 Post subject: Re: how can i list the file and folders in a directory ?
PostPosted: Thu Dec 31, 2015 7:48 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 1031
Location: UK
Hoi,

You should really use GtkFileChooserDialog which was introduced into GTK+ with version 2.4 (March 2004). GtkFileSelection is really left over from GTK version 1.

Documentation and an example is at https://developer.gnome.org/gtk2/stable/GtkFileChooserDialog.html

Also direct access to the internal structures of GTK+ should be avoided and is not possible in GTK+ v3. GTK+ v2.24 is the last in the GTK+ version 2 series and is only getting bug fixes.

As cecashon said in his post you should use GTK+ v3 for new code. It works on all the main operating systems so you should be OK unless you have some embedded system (those that required unusual graphics drivers).

_________________
E.


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

All times are UTC


Who is online

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