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 3:47 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: file selection
PostPosted: Fri Mar 23, 2007 8:46 am 
Offline
GTK+ Guru

Joined: Thu Nov 16, 2006 5:08 am
Posts: 259
Location: INDIA
hi all
i am doing on file selection concept
i got a problem with this code
initially ok_button must be sensitive FALSE
after selecting a file in the list ok_button have set sensitive TRUE
is it possible.
how can i do this

here is the code
which i tried

Code:
#include <gtk/gtk.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/sockios.h>
#include <string.h>
#include <math.h>
#include <dirent.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>

GtkWidget *filew;
GtkWidget *selection_entry;

/* Get the selected filename and print it to the console */
static void file_ok_sel( GtkWidget        *w,
                         GtkFileSelection *fs )
{
     char *file;
     file=gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs));

     g_print ("%s\n",file );

 
}

static void set_selected(GtkWidget        *w,
                         GtkFileSelection *fs,
                         GtkEntry        *entry)
{
  // char *file1;
  /*  file1=gtk_file_selection_get_filename(GTK_FILE_SELECTION(filew));
    if(GTK_FILE_SELECTION (filew)->selection_entry!=NULL)
    { */   
//   gtk_widget_set_sensitive(G_OBJECT (GTK_FILE_SELECTION (filew)->ok_button),TRUE);
    //} 
   
    char *file1;
   // file1=gtk_file_selection_get_filename(GTK_FILE_SELECTION(fs));
   // gtk_entry_set_text (GTK_FILE_SELECTION (filew)->selection_entry,file1);

   file1=gtk_entry_get_text (GTK_FILE_SELECTION (fs)->selection_entry);
     g_print ("file1 = %s\n",file1 );

}

int main( int   argc,
          char *argv[] )
{
    //GtkWidget *filew;
   
    gtk_init (&argc, &argv);
   
    /* Create a new file selection widget */
    filew = gtk_file_selection_new ("File selection");
   
    g_signal_connect (G_OBJECT (filew), "destroy",
                 G_CALLBACK (gtk_main_quit), NULL);
    /* Connect the ok_button to file_ok_sel function */
    g_signal_connect (G_OBJECT (GTK_FILE_SELECTION (filew)->ok_button),
            "clicked", G_CALLBACK (file_ok_sel), (gpointer) filew);

   /* Ensure that the dialog box is destroyed when the user clicks a button. */
    g_signal_connect_swapped (GTK_FILE_SELECTION (filew)->ok_button,
                             "clicked",
                             G_CALLBACK (gtk_widget_destroy),
                             (gpointer) filew);


    gtk_widget_set_sensitive(G_OBJECT (GTK_FILE_SELECTION (filew)->ok_button),FALSE);
   
    /* Connect the cancel_button to destroy the widget */
    g_signal_connect_swapped (G_OBJECT (GTK_FILE_SELECTION (filew)->cancel_button),
                         "clicked", G_CALLBACK (gtk_widget_destroy),
               G_OBJECT (filew));

    g_signal_connect(G_OBJECT(GTK_FILE_SELECTION (filew)->selection_entry), "insert_text",
           G_CALLBACK(set_selected),
           G_OBJECT (filew));


    /* Lets set the filename, as if this were a save dialog, and we are giving
     a default filename */
    gtk_file_selection_set_filename (GTK_FILE_SELECTION(filew),
                 " ");

    // gtk_file_selection_hide_fileop_buttons(GTK_FILE_SELECTION(filew));

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


can please provide latest file selection gui code.
can you please help me

thank you in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 1:06 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
So, basically what you want is to disable the OK button until the user selects a file. Well, I have a few comments on this.

First, GtkFileSelection is depreciated. Instead, you should use the GtkFileChooser family of widgets. If you use GtkFileChooserDialog with an action of GTK_FILE_CHOOSER_ACTION_OPEN, the dialog will not allow the OK button to be pressed until the user selects a file. If I'm understanding what you are saying, this should suit your purposes.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
 Post subject: thank you for quick replay
PostPosted: Fri Mar 23, 2007 1:36 pm 
Offline
GTK+ Guru

Joined: Thu Nov 16, 2006 5:08 am
Posts: 259
Location: INDIA
openldev wrote:
So, basically what you want is to disable the OK button until the user selects a file. Well, I have a few comments on this.

First, GtkFileSelection is depreciated. Instead, you should use the GtkFileChooser family of widgets. If you use GtkFileChooserDialog with an action of GTK_FILE_CHOOSER_ACTION_OPEN, the dialog will not allow the OK button to be pressed until the user selects a file. If I'm understanding what you are saying, this should suit your purposes.


can you please provide the an example code for file chooser

thank you in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 23, 2007 2:51 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
The following code creates a GtkFileChooserDialog that is used to open a file. You will have to put in your file chooser window title and parent window. When the Open button is pressed, the filename is retrieved. The case statement falls through to default so that it will always be destroyed at the end.

Code:
GtkWidget *dialog;
gint result;

dialog = gtk_file_chooser_dialog_new ("Window Title", GTK_WINDOW (parent_window), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_OK, NULL);

gint result = gtk_dialog_run (GTK_DIALOG (dialog));
switch (result)
{
case (GTK_RESPONSE_OK):
  gchar *file = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
  /* Use the filename here. */
default:
  gtk_widget_destroy (dialog);
}


A few more notes ... you will have to free the string returned by gtk_file_chooser_get_filename() when you are done with g_free(). Also, GtkFileChooserDialog implements the GtkFileChooser interface, so you can use any of its functions or properties with GtkFileChooserDialog.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


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: Google [Bot] and 4 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group