-
Notifications
You must be signed in to change notification settings - Fork 12
/
assistant.pl
executable file
·164 lines (134 loc) · 4.41 KB
/
assistant.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/perl
#
# Assistant demo
#
# Demonstrates a sample multi-step assistant. Assistants
# are used to divide an operation into several simpler sequential
# steps, and to guide the user through these steps.
#
# Perl version by Dave M <dave.nerd@gmail.com>
use strict;
use warnings;
use Gtk3 '-init';
use Glib 'TRUE', 'FALSE';
my $pb; # Gtk3::ProgressBar
my $assistant = Gtk3::Assistant->new();
$assistant->set_default_size( -1, 300 );
my $icon = 'gtk-logo-rgb.gif';
if ( -e $icon ) {
my $pixbuf = Gtk3::Gdk::Pixbuf->new_from_file($icon);
my $transparent = $pixbuf->add_alpha( TRUE, 0xff, 0xff, 0xff );
$assistant->set_icon($transparent);
}
create_page1();
create_page2();
create_page3();
create_page4();
$assistant->signal_connect(
destroy => sub {
# If this is part of a package (gtk3-demo), then
# $assistant->destroy();
# otherwise:
Gtk3->main_quit;
} );
$assistant->signal_connect( cancel => \&on_assistant_close_cancel );
$assistant->signal_connect( close => \&on_assistant_close_cancel );
$assistant->signal_connect( apply => \&on_assistant_apply );
$assistant->signal_connect( prepare => \&on_assistant_prepare );
$assistant->show_all();
Gtk3->main();
sub apply_changes_gradually {
my $fraction = $pb->get_fraction();
$fraction += 0.05;
if ( $fraction < 1.0 ) {
$pb->set_fraction($fraction);
return TRUE;
} else {
$assistant->destroy();
return FALSE;
}
}
sub on_assistant_apply {
Glib::Timeout->add( 100, \&apply_changes_gradually );
}
sub on_assistant_close_cancel {
$assistant->destroy();
}
sub on_assistant_prepare {
my ( $current_page, $num_pages );
$current_page = $assistant->get_current_page();
$num_pages = $assistant->get_n_pages();
my $title = sprintf 'Sample assistant (%d of %d)',
$current_page + 1, $num_pages;
$assistant->set_title($title);
if ( $current_page == 3 ) {
$assistant->commit;
}
}
sub on_entry_changed {
my ($data) = @_;
my $page_num = $assistant->get_current_page();
my $current_page = $assistant->get_nth_page($page_num);
if ($data) {
$assistant->set_page_complete( $current_page, TRUE );
} else {
$assistant->set_page_complete( $current_page, FALSE );
}
}
sub create_page1 {
my $box = Gtk3::Box->new( 'horizontal', 12 );
$box->set_border_width(12);
$box->set_homogeneous(TRUE);
$box->pack_start(
Gtk3::Label->new('You must fill out this entry to continue:'),
FALSE, FALSE, 0 );
my $entry = Gtk3::Entry->new();
$entry->set_activates_default(TRUE);
$box->pack_start( $entry, TRUE, TRUE, 0 );
$entry->signal_connect(
changed => sub {
on_entry_changed( $entry->get_text );
} );
$box->show_all();
$assistant->append_page($box);
$assistant->set_page_title( $box, 'Page 1' );
$assistant->set_page_type( $box, 'intro' );
}
sub create_page2 {
my $box = Gtk3::Box->new( 'vertical', 12 );
$box->set_border_width(12);
$box->set_homogeneous(TRUE);
my $cb = Gtk3::CheckButton->new_with_label(
'This is optional data, you may continue '
. 'even if you do not check this' );
$box->pack_start( $cb, FALSE, FALSE, 0 );
$box->show_all();
$assistant->append_page($box);
$assistant->set_page_complete( $box, TRUE );
$assistant->set_page_title( $box, 'Page 2' );
}
sub create_page3 {
my $label = Gtk3::Label->new( q { This is a confirmation page. }
. q { Press 'Apply' to save changes. } );
$label->show();
$assistant->append_page($label);
$assistant->set_page_type( $label, 'confirm' );
$assistant->set_page_complete( $label, TRUE );
$assistant->set_page_title( $label, 'Confirmation' );
}
sub create_page4 {
$pb = Gtk3::ProgressBar->new();
$pb->set_halign('center');
$pb->set_valign('center');
$pb->set_fraction(0.0);
$pb->show();
$assistant->append_page($pb);
$assistant->set_page_type( $pb, 'progress' );
$assistant->set_page_complete( $pb, TRUE );
$assistant->set_page_title( $pb, 'Applying changes...' );
$assistant->set_page_complete( $pb, FALSE );
}
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public License
# as published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.