GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

file selection

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
ramesh
Familiar Face


Joined: 16 Nov 2006
Posts: 40

PostPosted: Fri Mar 23, 2007 8:46 am    Post subject: file selection Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

#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
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Fri Mar 23, 2007 1:06 pm    Post subject: Reply with quote

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.
Back to top
ramesh
Familiar Face


Joined: 16 Nov 2006
Posts: 40

PostPosted: Fri Mar 23, 2007 1:36 pm    Post subject: thank you for quick replay Reply with quote

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
Back to top
openldev
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 376
Location: State College, Pennsylvania

PostPosted: Fri Mar 23, 2007 2:51 pm    Post subject: Reply with quote

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: (Plaintext)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP