cgtkgtkassistant

Print variable in GTK+ assistant


I want to ask the user in a GTK+ assistant for his name, save this name in a global variable and use this variable in the same assistant on a different site. My problem is that the assistant is completly created and changes on new sites have no effect.

Example: I initialisize the global variable "name" with "empty". Now, the assistant create all sites but displays only the first site. The user writes down his name in the "entryfield" and the variable "name" now contains his name. The assistant now displays the second site which should contain the users name, but it displays the string "empty".

I need to "refresh" this already created entry with the new variable. Sorry for my bad english, I hope I can explain my problem a little bit. :)

Complete sample code:

#include <gtk/gtk.h>
#include <string.h>

static void entry_changed    (GtkEditable*, GtkAssistant*);
static void assistant_cancel (GtkAssistant*, gpointer);
static void assistant_close  (GtkAssistant*, gpointer);

char *name = "empty";

typedef struct {
  GtkWidget *widget;
  gint index;
  const gchar *title;
  GtkAssistantPageType type;
  gboolean complete;
} PageInfo;

int main (int argc,
          char *argv[])
{
  GtkWidget *assistant, *entry, *label;
  guint i;
  PageInfo page[5] = {
    { NULL, -1, "Introduction",           GTK_ASSISTANT_PAGE_INTRO,    TRUE},
    { NULL, -1, NULL,                     GTK_ASSISTANT_PAGE_CONTENT,  FALSE},
    { NULL, -1, NULL,                     GTK_ASSISTANT_PAGE_CONTENT,  TRUE},
    { NULL, -1, "Confirmation",           GTK_ASSISTANT_PAGE_CONFIRM,  TRUE},
  };

  gtk_init (&argc, &argv);

  /* Create a new assistant widget with no pages. */
  assistant = gtk_assistant_new ();
  gtk_widget_set_size_request (assistant, 450, 300);
  gtk_window_set_title (GTK_WINDOW (assistant), "GtkAssistant Example");
  g_signal_connect (G_OBJECT (assistant), "destroy",
                    G_CALLBACK (gtk_main_quit), NULL);

  page[0].widget = gtk_label_new ("This is an example of a GtkAssistant. By\n"\
                                  "clicking the forward button, you can continue\n"\
                                  "to the next section!");
  page[1].widget = gtk_hbox_new (FALSE, 5);
  page[2].widget = gtk_entry_new();
  page[3].widget = gtk_label_new ("finish!");

  /* Create the necessary widgets for the second page. */
  label = gtk_label_new ("Your Name: ");
  entry = gtk_entry_new ();
  gtk_box_pack_start (GTK_BOX (page[1].widget), label, FALSE, FALSE, 5);
  gtk_box_pack_start (GTK_BOX (page[1].widget), entry, FALSE, FALSE, 5);

  gtk_entry_set_text(GTK_ENTRY(page[2].widget), name);

  /* Add four pages to the GtkAssistant dialog. */
  for (i = 0; i < 4; i++)
  {
    page[i].index = gtk_assistant_append_page (GTK_ASSISTANT (assistant),
                                               page[i].widget);
    gtk_assistant_set_page_title (GTK_ASSISTANT (assistant),
                                  page[i].widget, page[i].title);
    gtk_assistant_set_page_type (GTK_ASSISTANT (assistant),
                                  page[i].widget, page[i].type);

    /* Set the introduction and conclusion pages as complete so they can be
     * incremented or closed. */
    gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant),
                                     page[i].widget, page[i].complete);
  }

  /* Update whether pages 2 through 4 are complete based upon whether there is
   * text in the GtkEntry, the check button is active, or the progress bar
   * is completely filled. */
  g_signal_connect (G_OBJECT (entry), "changed",
                    G_CALLBACK (entry_changed), (gpointer) assistant);
  g_signal_connect (G_OBJECT (assistant), "cancel",
                    G_CALLBACK (assistant_cancel), NULL);
  g_signal_connect (G_OBJECT (assistant), "close",
                    G_CALLBACK (assistant_close), NULL);
  gtk_widget_show_all (assistant);
  gtk_main ();
  return 0;
}

/* If there is text in the GtkEntry, set the page as complete. Otherwise,
 * stop the user from progressing the next page. */
static void
entry_changed (GtkEditable *entry,
               GtkAssistant *assistant)
{
  const gchar *text = gtk_entry_get_text (GTK_ENTRY (entry));
  gint num = gtk_assistant_get_current_page (assistant);
  GtkWidget *page = gtk_assistant_get_nth_page (assistant, num);
  gtk_assistant_set_page_complete (assistant, page, (strlen (text) > 0));

  name = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
}

/* If the dialog is cancelled, delete it from memory and then clean up after
 * the Assistant structure. */
static void
assistant_cancel (GtkAssistant *assistant,
                  gpointer data)
{
  gtk_widget_destroy (GTK_WIDGET (assistant));
}

/* This function is where you would apply the changes and destroy the assistant. */
static void
assistant_close (GtkAssistant *assistant,
                 gpointer data)
{
  g_print ("You would apply your changes now!\n");
  gtk_widget_destroy (GTK_WIDGET (assistant));
}

Solution

  • in entry_changed, you will have to call gtk_entry_set_text(GTK_ENTRY(page[2].widget), name); again, as you only set the entry text once, which is the only thing that matters. Otherwise the default value you set when setting up the 3rd page sticks.