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