]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/assistant.c
f5c69dd021038a581559855e7f5bc857f45ba33e
[~andy/gtk] / demos / gtk-demo / assistant.c
1 /* Assistant
2  *
3  * Demonstrates a sample multistep assistant. Assistants are used to divide
4  * an operation into several simpler sequential steps, and to guide the user
5  * through these steps.
6  */
7
8 #include <gtk/gtk.h>
9 #include "demo-common.h"
10
11 static GtkWidget *assistant = NULL;
12
13 static void
14 on_assistant_apply (GtkWidget *widget, gpointer data)
15 {
16   /* Apply here changes, this is a fictional
17      example, so we just do nothing here */
18 }
19
20 static void
21 on_assistant_close_cancel (GtkWidget *widget, gpointer data)
22 {
23   GtkWidget **assistant = (GtkWidget **) data;
24
25   gtk_widget_destroy (*assistant);
26   *assistant = NULL;
27 }
28
29 static void
30 on_assistant_prepare (GtkWidget *widget, GtkWidget *page, gpointer data)
31 {
32   gint current_page, n_pages;
33   gchar *title;
34
35   current_page = gtk_assistant_get_current_page (GTK_ASSISTANT (widget));
36   n_pages = gtk_assistant_get_n_pages (GTK_ASSISTANT (widget));
37
38   title = g_strdup_printf ("Sample assistant (%d of %d)", current_page + 1, n_pages);
39   gtk_window_set_title (GTK_WINDOW (widget), title);
40   g_free (title);
41 }
42
43 static void
44 on_entry_changed (GtkWidget *widget, gpointer data)
45 {
46   GtkAssistant *assistant = GTK_ASSISTANT (data);
47   GtkWidget *current_page;
48   gint page_number;
49   const gchar *text;
50
51   page_number = gtk_assistant_get_current_page (assistant);
52   current_page = gtk_assistant_get_nth_page (assistant, page_number);
53   text = gtk_entry_get_text (GTK_ENTRY (widget));
54
55   if (text && *text)
56     gtk_assistant_set_page_complete (assistant, current_page, TRUE);
57   else
58     gtk_assistant_set_page_complete (assistant, current_page, FALSE);
59 }
60
61 static void
62 create_page1 (GtkWidget *assistant)
63 {
64   GtkWidget *box, *label, *entry;
65   GdkPixbuf *pixbuf;
66
67   box = gtk_hbox_new (FALSE, 12);
68   gtk_container_set_border_width (GTK_CONTAINER (box), 12);
69
70   label = gtk_label_new ("You must fill out this entry to continue:");
71   gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
72
73   entry = gtk_entry_new ();
74   gtk_box_pack_start (GTK_BOX (box), entry, TRUE, TRUE, 0);
75   g_signal_connect (G_OBJECT (entry), "changed",
76                     G_CALLBACK (on_entry_changed), assistant);
77
78   gtk_widget_show_all (box);
79   gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);
80   gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "Page 1");
81   gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), box, GTK_ASSISTANT_PAGE_INTRO);
82
83   pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG, NULL);
84   gtk_assistant_set_page_header_image (GTK_ASSISTANT (assistant), box, pixbuf);
85   g_object_unref (pixbuf);
86 }
87
88 static void
89 create_page2 (GtkWidget *assistant)
90 {
91   GtkWidget *box, *checkbutton;
92   GdkPixbuf *pixbuf;
93
94   box = gtk_vbox_new (12, FALSE);
95   gtk_container_set_border_width (GTK_CONTAINER (box), 12);
96
97   checkbutton = gtk_check_button_new_with_label ("This is optional data, you may continue "
98                                                  "even if you do not check this");
99   gtk_box_pack_start (GTK_BOX (box), checkbutton, FALSE, FALSE, 0);
100
101   gtk_widget_show_all (box);
102   gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);
103   gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), box, TRUE);
104   gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "Page 2");
105
106   pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG, NULL);
107   gtk_assistant_set_page_header_image (GTK_ASSISTANT (assistant), box, pixbuf);
108   g_object_unref (pixbuf);
109 }
110
111 static void
112 create_page3 (GtkWidget *assistant)
113 {
114   GtkWidget *label;
115   GdkPixbuf *pixbuf;
116
117   label = gtk_label_new ("This is a confirmation page, press 'Apply' to apply changes");
118
119   gtk_widget_show (label);
120   gtk_assistant_append_page (GTK_ASSISTANT (assistant), label);
121   gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), label, GTK_ASSISTANT_PAGE_CONFIRM);
122   gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), label, TRUE);
123   gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), label, "Confirmation");
124
125   pixbuf = gtk_widget_render_icon (assistant, GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG, NULL);
126   gtk_assistant_set_page_header_image (GTK_ASSISTANT (assistant), label, pixbuf);
127   g_object_unref (pixbuf);
128 }
129
130 GtkWidget*
131 do_assistant (GtkWidget *do_widget)
132 {
133   if (!assistant)
134     {
135       assistant = gtk_assistant_new ();
136
137          gtk_window_set_default_size (GTK_WINDOW (assistant), -1, 300);
138
139       gtk_window_set_screen (GTK_WINDOW (assistant),
140                              gtk_widget_get_screen (do_widget));
141
142       create_page1 (assistant);
143       create_page2 (assistant);
144       create_page3 (assistant);
145
146       g_signal_connect (G_OBJECT (assistant), "cancel",
147                         G_CALLBACK (on_assistant_close_cancel), &assistant);
148       g_signal_connect (G_OBJECT (assistant), "close",
149                         G_CALLBACK (on_assistant_close_cancel), &assistant);
150       g_signal_connect (G_OBJECT (assistant), "apply",
151                         G_CALLBACK (on_assistant_apply), NULL);
152       g_signal_connect (G_OBJECT (assistant), "prepare",
153                         G_CALLBACK (on_assistant_prepare), NULL);
154     }
155
156   if (!GTK_WIDGET_VISIBLE (assistant))
157     gtk_widget_show (assistant);
158   else
159     {
160       gtk_widget_destroy (assistant);
161       assistant = NULL;
162     }
163
164   return assistant;
165 }