warem
warem

Reputation: 1683

how to access the text entry in GtkFileChooser

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.

Upvotes: 0

Views: 362

Answers (2)

Wiley
Wiley

Reputation: 526

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.

Upvotes: 1

ptomato
ptomato

Reputation: 57850

As far as I know, no.

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

Upvotes: 1

Related Questions