]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/assistant.c
gtk-demo: Remove demo_find_file() function
[~andy/gtk] / demos / gtk-demo / assistant.c
1 /* Assistant
2  *
3  * Demonstrates a sample multi-step 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
10 static GtkWidget *assistant = NULL;
11 static GtkWidget *progress_bar = NULL;
12
13 static gboolean
14 apply_changes_gradually (gpointer data)
15 {
16   gdouble fraction;
17
18   /* Work, work, work... */
19   fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (progress_bar));
20   fraction += 0.05;
21
22   if (fraction < 1.0)
23     {
24       gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), fraction);
25       return G_SOURCE_CONTINUE;
26     }
27   else
28     {
29       /* Close automatically once changes are fully applied. */
30       gtk_widget_destroy (assistant);
31       assistant = NULL;
32       return G_SOURCE_REMOVE;
33     }
34 }
35
36 static void
37 on_assistant_apply (GtkWidget *widget, gpointer data)
38 {
39   /* Start a timer to simulate changes taking a few seconds to apply. */
40   g_timeout_add (100, apply_changes_gradually, NULL);
41 }
42
43 static void
44 on_assistant_close_cancel (GtkWidget *widget, gpointer data)
45 {
46   GtkWidget **assistant = (GtkWidget **) data;
47
48   gtk_widget_destroy (*assistant);
49   *assistant = NULL;
50 }
51
52 static void
53 on_assistant_prepare (GtkWidget *widget, GtkWidget *page, gpointer data)
54 {
55   gint current_page, n_pages;
56   gchar *title;
57
58   current_page = gtk_assistant_get_current_page (GTK_ASSISTANT (widget));
59   n_pages = gtk_assistant_get_n_pages (GTK_ASSISTANT (widget));
60
61   title = g_strdup_printf ("Sample assistant (%d of %d)", current_page + 1, n_pages);
62   gtk_window_set_title (GTK_WINDOW (widget), title);
63   g_free (title);
64
65   /* The fourth page (counting from zero) is the progress page.  The
66   * user clicked Apply to get here so we tell the assistant to commit,
67   * which means the changes up to this point are permanent and cannot
68   * be cancelled or revisited. */
69   if (current_page == 3)
70       gtk_assistant_commit (GTK_ASSISTANT (widget));
71 }
72
73 static void
74 on_entry_changed (GtkWidget *widget, gpointer data)
75 {
76   GtkAssistant *assistant = GTK_ASSISTANT (data);
77   GtkWidget *current_page;
78   gint page_number;
79   const gchar *text;
80
81   page_number = gtk_assistant_get_current_page (assistant);
82   current_page = gtk_assistant_get_nth_page (assistant, page_number);
83   text = gtk_entry_get_text (GTK_ENTRY (widget));
84
85   if (text && *text)
86     gtk_assistant_set_page_complete (assistant, current_page, TRUE);
87   else
88     gtk_assistant_set_page_complete (assistant, current_page, FALSE);
89 }
90
91 static void
92 create_page1 (GtkWidget *assistant)
93 {
94   GtkWidget *box, *label, *entry;
95
96   box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
97   gtk_container_set_border_width (GTK_CONTAINER (box), 12);
98
99   label = gtk_label_new ("You must fill out this entry to continue:");
100   gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
101
102   entry = gtk_entry_new ();
103   gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
104   gtk_box_pack_start (GTK_BOX (box), entry, TRUE, TRUE, 0);
105   g_signal_connect (G_OBJECT (entry), "changed",
106                     G_CALLBACK (on_entry_changed), assistant);
107
108   gtk_widget_show_all (box);
109   gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);
110   gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "Page 1");
111   gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), box, GTK_ASSISTANT_PAGE_INTRO);
112 }
113
114 static void
115 create_page2 (GtkWidget *assistant)
116 {
117   GtkWidget *box, *checkbutton;
118
119   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
120   gtk_container_set_border_width (GTK_CONTAINER (box), 12);
121
122   checkbutton = gtk_check_button_new_with_label ("This is optional data, you may continue "
123                                                  "even if you do not check this");
124   gtk_box_pack_start (GTK_BOX (box), checkbutton, FALSE, FALSE, 0);
125
126   gtk_widget_show_all (box);
127   gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);
128   gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), box, TRUE);
129   gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "Page 2");
130 }
131
132 static void
133 create_page3 (GtkWidget *assistant)
134 {
135   GtkWidget *label;
136
137   label = gtk_label_new ("This is a confirmation page, press 'Apply' to apply changes");
138
139   gtk_widget_show (label);
140   gtk_assistant_append_page (GTK_ASSISTANT (assistant), label);
141   gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), label, GTK_ASSISTANT_PAGE_CONFIRM);
142   gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), label, TRUE);
143   gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), label, "Confirmation");
144 }
145
146 static void
147 create_page4 (GtkWidget *assistant)
148 {
149   progress_bar = gtk_progress_bar_new ();
150   gtk_widget_set_halign (progress_bar, GTK_ALIGN_CENTER);
151   gtk_widget_set_valign (progress_bar, GTK_ALIGN_CENTER);
152
153   gtk_widget_show (progress_bar);
154   gtk_assistant_append_page (GTK_ASSISTANT (assistant), progress_bar);
155   gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), progress_bar, GTK_ASSISTANT_PAGE_PROGRESS);
156   gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), progress_bar, "Applying changes");
157
158   /* This prevents the assistant window from being
159    * closed while we're "busy" applying changes.
160    */
161   gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), progress_bar, FALSE);
162 }
163
164 GtkWidget*
165 do_assistant (GtkWidget *do_widget)
166 {
167   if (!assistant)
168     {
169       assistant = gtk_assistant_new ();
170
171          gtk_window_set_default_size (GTK_WINDOW (assistant), -1, 300);
172
173       gtk_window_set_screen (GTK_WINDOW (assistant),
174                              gtk_widget_get_screen (do_widget));
175
176       create_page1 (assistant);
177       create_page2 (assistant);
178       create_page3 (assistant);
179       create_page4 (assistant);
180
181       g_signal_connect (G_OBJECT (assistant), "cancel",
182                         G_CALLBACK (on_assistant_close_cancel), &assistant);
183       g_signal_connect (G_OBJECT (assistant), "close",
184                         G_CALLBACK (on_assistant_close_cancel), &assistant);
185       g_signal_connect (G_OBJECT (assistant), "apply",
186                         G_CALLBACK (on_assistant_apply), NULL);
187       g_signal_connect (G_OBJECT (assistant), "prepare",
188                         G_CALLBACK (on_assistant_prepare), NULL);
189     }
190
191   if (!gtk_widget_get_visible (assistant))
192     gtk_widget_show (assistant);
193   else
194     {
195       gtk_widget_destroy (assistant);
196       assistant = NULL;
197     }
198
199   return assistant;
200 }