0

In GTK+, is it possible to access the GtkWidget -- text entry for file name in GtkFileChooser? I want to disable the editable attribute of the text entry using gtk_entry_set_editable.

2 Answers 2

1

As far as I know, no.

What do you ultimately want to achieve? Perhaps there is another approach.

Sign up to request clarification or add additional context in comments.

3 Comments

my purpose is to check if the file specified in the text entry exists. For example, the concerned file name in the entry is qqaa that can't be changed by user(this is why I want to set the editable attribute). When user pushes OK button, it will check if qqaa exists in the path. If yes, go on; otherwise, give a warning and stay in the file chooser.
Just set the file chooser to choose a folder instead of a file.
Thank ptomato. Set file chooser to choose a folder and then check if the concerning file exists in the folder. It is really another approach for my purpose.
1

If one had a legitimate reason to get a pointer to the GtkEntry, then derive from GtkFileChooserDialog, which will probably mutate into a GtkFileChooserDefault. GObject will complain about an illegal cast when checking type instance even though it works and the data of derived object can be accessed without errors, use GTK_FILE_CHOOSER instead of MY_FILE_CHOOSER to avoid the warning messages and a local static for the entry pointer. The entry widget is NOT accessible during construction. Here is the pertinent code:

static GtkEntry *chooser_entry; static void my_file_chooser_finalize (GObject *object) { chooser_entry = NULL; (G_OBJECT_CLASS (my_file_chooser_parent_class))->finalize (object); } static void my_file_chooser_init (MyFileChooser *self) { chooser_entry = NULL; } static void look_for_entry(GtkWidget *widget, void *self) { if (GTK_IS_ENTRY(widget)) { chooser_entry = (GtkEntry*)widget; } else if (GTK_IS_CONTAINER(widget)) { gtk_container_forall ( GTK_CONTAINER (widget), look_for_entry, self); } } static void file_chooser_find_entry (GtkWidget *chooser) { GList *children, *iter; /* Get all objects inside the dialog */ children = gtk_container_get_children (GTK_CONTAINER (chooser)); for (iter = children; iter; iter = iter->next) { if (GTK_IS_CONTAINER(iter->data)) { gtk_container_forall ( GTK_CONTAINER (iter->data), look_for_entry, chooser); if (chooser_entry != NULL) { break; } } } g_list_free (children); } GtkEntry *my_file_chooser_get_entry (GtkWidget *widget) { if (chooser_entry == NULL) { file_chooser_find_entry (widget); } return chooser_entry; } char *my_file_chooser_get_entry_text(GtkWidget *widget) { char *text; GtkEntry *entry; text = NULL; if (GTK_IS_FILE_CHOOSER(widget)) { entry = my_file_chooser_get_entry(widget); if (GTK_IS_ENTRY(entry)) { if (gtk_entry_get_text_length (entry)) { text = g_strdup (gtk_entry_get_text(entry)); } } } return text; } 

Maybe not ideal, but works.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.