2006-01-28 06:03:50 +00:00
|
|
|
/* Assistant
|
|
|
|
*
|
2011-05-06 18:55:58 +00:00
|
|
|
* Demonstrates a sample multi-step assistant. Assistants are used to divide
|
2006-01-28 06:03:50 +00:00
|
|
|
* an operation into several simpler sequential steps, and to guide the user
|
|
|
|
* through these steps.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2010-06-02 20:00:16 +00:00
|
|
|
static GtkWidget *progress_bar = NULL;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
apply_changes_gradually (gpointer data)
|
|
|
|
{
|
2020-07-24 20:32:16 +00:00
|
|
|
double fraction;
|
2010-06-02 20:00:16 +00:00
|
|
|
|
|
|
|
/* Work, work, work... */
|
|
|
|
fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (progress_bar));
|
|
|
|
fraction += 0.05;
|
|
|
|
|
|
|
|
if (fraction < 1.0)
|
|
|
|
{
|
|
|
|
gtk_progress_bar_set_fraction (GTK_PROGRESS_BAR (progress_bar), fraction);
|
2012-01-31 00:12:27 +00:00
|
|
|
return G_SOURCE_CONTINUE;
|
2010-06-02 20:00:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Close automatically once changes are fully applied. */
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (data));
|
2012-01-31 00:12:27 +00:00
|
|
|
return G_SOURCE_REMOVE;
|
2010-06-02 20:00:16 +00:00
|
|
|
}
|
|
|
|
}
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
on_assistant_apply (GtkWidget *widget, gpointer data)
|
|
|
|
{
|
2010-06-02 20:00:16 +00:00
|
|
|
/* Start a timer to simulate changes taking a few seconds to apply. */
|
2020-03-07 14:01:21 +00:00
|
|
|
g_timeout_add (100, apply_changes_gradually, widget);
|
2006-01-28 06:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_assistant_close_cancel (GtkWidget *widget, gpointer data)
|
|
|
|
{
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (widget));
|
2006-01-28 06:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_assistant_prepare (GtkWidget *widget, GtkWidget *page, gpointer data)
|
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int current_page, n_pages;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *title;
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
current_page = gtk_assistant_get_current_page (GTK_ASSISTANT (widget));
|
|
|
|
n_pages = gtk_assistant_get_n_pages (GTK_ASSISTANT (widget));
|
|
|
|
|
|
|
|
title = g_strdup_printf ("Sample assistant (%d of %d)", current_page + 1, n_pages);
|
|
|
|
gtk_window_set_title (GTK_WINDOW (widget), title);
|
|
|
|
g_free (title);
|
2010-06-02 20:00:16 +00:00
|
|
|
|
|
|
|
/* The fourth page (counting from zero) is the progress page. The
|
|
|
|
* user clicked Apply to get here so we tell the assistant to commit,
|
|
|
|
* which means the changes up to this point are permanent and cannot
|
|
|
|
* be cancelled or revisited. */
|
|
|
|
if (current_page == 3)
|
|
|
|
gtk_assistant_commit (GTK_ASSISTANT (widget));
|
2006-01-28 06:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
on_entry_changed (GtkWidget *widget, gpointer data)
|
|
|
|
{
|
|
|
|
GtkAssistant *assistant = GTK_ASSISTANT (data);
|
|
|
|
GtkWidget *current_page;
|
2020-07-24 13:54:49 +00:00
|
|
|
int page_number;
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *text;
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
page_number = gtk_assistant_get_current_page (assistant);
|
|
|
|
current_page = gtk_assistant_get_nth_page (assistant, page_number);
|
2019-02-28 19:31:36 +00:00
|
|
|
text = gtk_editable_get_text (GTK_EDITABLE (widget));
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
if (text && *text)
|
|
|
|
gtk_assistant_set_page_complete (assistant, current_page, TRUE);
|
|
|
|
else
|
|
|
|
gtk_assistant_set_page_complete (assistant, current_page, FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_page1 (GtkWidget *assistant)
|
|
|
|
{
|
|
|
|
GtkWidget *box, *label, *entry;
|
|
|
|
|
2010-10-31 17:07:20 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
|
2020-02-26 01:57:20 +00:00
|
|
|
gtk_widget_set_margin_start (box, 12);
|
|
|
|
gtk_widget_set_margin_end (box, 12);
|
|
|
|
gtk_widget_set_margin_top (box, 12);
|
|
|
|
gtk_widget_set_margin_bottom (box, 12);
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
label = gtk_label_new ("You must fill out this entry to continue:");
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), label);
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
entry = gtk_entry_new ();
|
2011-08-31 15:03:45 +00:00
|
|
|
gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
|
2015-11-18 00:48:02 +00:00
|
|
|
gtk_widget_set_valign (entry, GTK_ALIGN_CENTER);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), entry);
|
2006-01-28 06:03:50 +00:00
|
|
|
g_signal_connect (G_OBJECT (entry), "changed",
|
2011-05-06 18:55:58 +00:00
|
|
|
G_CALLBACK (on_entry_changed), assistant);
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);
|
|
|
|
gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "Page 1");
|
|
|
|
gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), box, GTK_ASSISTANT_PAGE_INTRO);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_page2 (GtkWidget *assistant)
|
|
|
|
{
|
|
|
|
GtkWidget *box, *checkbutton;
|
|
|
|
|
2019-12-24 02:58:45 +00:00
|
|
|
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
|
2020-02-26 01:57:20 +00:00
|
|
|
gtk_widget_set_margin_start (box, 12);
|
|
|
|
gtk_widget_set_margin_end (box, 12);
|
|
|
|
gtk_widget_set_margin_top (box, 12);
|
|
|
|
gtk_widget_set_margin_bottom (box, 12);
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
checkbutton = gtk_check_button_new_with_label ("This is optional data, you may continue "
|
2011-05-06 18:55:58 +00:00
|
|
|
"even if you do not check this");
|
2019-12-24 02:58:45 +00:00
|
|
|
gtk_widget_set_valign (checkbutton, GTK_ALIGN_CENTER);
|
2020-05-09 12:26:52 +00:00
|
|
|
gtk_box_append (GTK_BOX (box), checkbutton);
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);
|
|
|
|
gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), box, TRUE);
|
|
|
|
gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, "Page 2");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
create_page3 (GtkWidget *assistant)
|
|
|
|
{
|
|
|
|
GtkWidget *label;
|
|
|
|
|
|
|
|
label = gtk_label_new ("This is a confirmation page, press 'Apply' to apply changes");
|
|
|
|
|
|
|
|
gtk_widget_show (label);
|
|
|
|
gtk_assistant_append_page (GTK_ASSISTANT (assistant), label);
|
|
|
|
gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), label, GTK_ASSISTANT_PAGE_CONFIRM);
|
|
|
|
gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), label, TRUE);
|
|
|
|
gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), label, "Confirmation");
|
|
|
|
}
|
|
|
|
|
2010-06-02 20:00:16 +00:00
|
|
|
static void
|
|
|
|
create_page4 (GtkWidget *assistant)
|
|
|
|
{
|
|
|
|
progress_bar = gtk_progress_bar_new ();
|
2017-10-06 23:38:16 +00:00
|
|
|
gtk_widget_set_halign (progress_bar, GTK_ALIGN_FILL);
|
2011-09-01 17:27:14 +00:00
|
|
|
gtk_widget_set_valign (progress_bar, GTK_ALIGN_CENTER);
|
2017-10-06 23:38:16 +00:00
|
|
|
gtk_widget_set_hexpand (progress_bar, TRUE);
|
|
|
|
gtk_widget_set_margin_start (progress_bar, 40);
|
|
|
|
gtk_widget_set_margin_end (progress_bar, 40);
|
2010-06-02 20:00:16 +00:00
|
|
|
|
2011-09-01 17:27:14 +00:00
|
|
|
gtk_widget_show (progress_bar);
|
|
|
|
gtk_assistant_append_page (GTK_ASSISTANT (assistant), progress_bar);
|
|
|
|
gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), progress_bar, GTK_ASSISTANT_PAGE_PROGRESS);
|
|
|
|
gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), progress_bar, "Applying changes");
|
2010-06-02 20:00:16 +00:00
|
|
|
|
|
|
|
/* This prevents the assistant window from being
|
2011-05-06 18:55:58 +00:00
|
|
|
* closed while we're "busy" applying changes.
|
|
|
|
*/
|
2011-09-01 17:27:14 +00:00
|
|
|
gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), progress_bar, FALSE);
|
2010-06-02 20:00:16 +00:00
|
|
|
}
|
|
|
|
|
2006-01-28 06:03:50 +00:00
|
|
|
GtkWidget*
|
|
|
|
do_assistant (GtkWidget *do_widget)
|
|
|
|
{
|
2020-03-07 14:01:21 +00:00
|
|
|
static GtkWidget *assistant;
|
|
|
|
|
2006-01-28 06:03:50 +00:00
|
|
|
if (!assistant)
|
|
|
|
{
|
|
|
|
assistant = gtk_assistant_new ();
|
|
|
|
|
2015-06-28 13:23:28 +00:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (assistant), -1, 300);
|
2006-01-28 06:03:50 +00:00
|
|
|
|
2017-10-31 06:41:15 +00:00
|
|
|
gtk_window_set_display (GTK_WINDOW (assistant),
|
|
|
|
gtk_widget_get_display (do_widget));
|
2020-05-09 16:03:11 +00:00
|
|
|
g_object_add_weak_pointer (G_OBJECT (assistant), (gpointer *)&assistant);
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
create_page1 (assistant);
|
|
|
|
create_page2 (assistant);
|
|
|
|
create_page3 (assistant);
|
2010-06-02 20:00:16 +00:00
|
|
|
create_page4 (assistant);
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
g_signal_connect (G_OBJECT (assistant), "cancel",
|
2020-04-04 16:31:43 +00:00
|
|
|
G_CALLBACK (on_assistant_close_cancel), NULL);
|
2006-01-28 06:03:50 +00:00
|
|
|
g_signal_connect (G_OBJECT (assistant), "close",
|
2020-04-04 16:31:43 +00:00
|
|
|
G_CALLBACK (on_assistant_close_cancel), NULL);
|
2006-01-28 06:03:50 +00:00
|
|
|
g_signal_connect (G_OBJECT (assistant), "apply",
|
2011-05-06 18:55:58 +00:00
|
|
|
G_CALLBACK (on_assistant_apply), NULL);
|
2006-01-28 06:03:50 +00:00
|
|
|
g_signal_connect (G_OBJECT (assistant), "prepare",
|
2011-05-06 18:55:58 +00:00
|
|
|
G_CALLBACK (on_assistant_prepare), NULL);
|
2006-01-28 06:03:50 +00:00
|
|
|
}
|
|
|
|
|
2010-03-01 06:47:38 +00:00
|
|
|
if (!gtk_widget_get_visible (assistant))
|
2006-01-28 06:03:50 +00:00
|
|
|
gtk_widget_show (assistant);
|
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (assistant));
|
2006-01-28 06:03:50 +00:00
|
|
|
|
|
|
|
return assistant;
|
|
|
|
}
|