~ubuntu-branches/ubuntu/maverick/padre/maverick

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package Wx::Perl::Dialog::Simple;

=pod

=head1 NAME

Wx::Perl::Dialog::Simple - a set of simple dialogs (a partial Zenity clone in wxPerl)

=head1 SYNOPSIS

As a module:

  use Wx::Perl::Dialog::Simple;

  my $name = entry(title => "What is your name?");
  message(text => "How are you $name today?\n");

On the command line try

  wxer --help

=head1 General Options

There are some common option for every dialog

title

window-icon  NA

width        NA

height       NA

=head1 METHODS

Dialogs

=head2 entry

Display a text entry dialog

=cut

use 5.008;
use strict;
use warnings;
use Cwd        ();
use Exporter   ();
use File::Spec ();

our $VERSION = '0.61';
our @ISA     = 'Exporter';

# I'd change this, but I suspect it's rather breaky
our @EXPORT = ## no critic (ProhibitAutomaticExportation)
	qw(
	entry
	password
	file_selector
	dir_selector
	dir_picker
	file_picker
	date_picker
	colour_picker
	choice
	single_choice
	message
	calendar
);

# print_out close_app open_frame display_text

use Wx::Perl::Dialog;
use Wx::STC ();

sub entry {
	my (%args) = @_;

	%args = (
		title   => '',
		prompt  => '',
		default => '',
		%args
	);

	my $class = $args{password} ? 'Wx::PasswordEntryDialog' : 'Wx::TextEntryDialog';
	my $dialog = $class->new( undef, $args{prompt}, $args{title}, $args{default} );
	if ( $dialog->ShowModal == Wx::wxID_CANCEL ) {
		return;
	}
	my $resp = $dialog->GetValue;
	$dialog->Destroy;
	return $resp;
}

sub password {
	my (%args) = @_;

	$args{password} = 1;

	return entry(%args);
}

sub file_selector {
	my (%args) = @_;
	%args = (
		title => '',
		%args
	);

	my $dialog = Wx::FileDialog->new( undef, $args{title}, '', "", "*.*", Wx::wxFD_OPEN );
	if ( $^O !~ /win32/i ) {
		$dialog->SetWildcard("*");
	}
	if ( $dialog->ShowModal == Wx::wxID_CANCEL ) {
		return;
	}
	my $filename    = $dialog->GetFilename;
	my $default_dir = $dialog->GetDirectory;

	return File::Spec->catfile( $default_dir, $filename );
}

sub dir_selector {
	my (%args) = @_;
	%args = (
		title => '',
		path  => '',
		%args
	);

	my $dialog = Wx::DirDialog->new( undef, $args{title}, $args{path} );
	if ( $dialog->ShowModal == Wx::wxID_CANCEL ) {
		return;
	}
	my $dir = $dialog->GetPath;

	return $dir;
}

sub date_picker {

	require Wx::DateTime;
	require Wx::Calendar;
	my $date = Wx::DateTime->newFromDMY( 8, 0, 1979, 1, 1, 1, 1 );
	my $calendar = Wx::DatePickerCtrl->new( undef, -1, $date );

	return dialog(
		sub { Wx::DatePickerCtrl->new( $_[0], -1 ) },
		sub { $_[0]; },
		sub { $_[0]->GetValue->Format; },
		{   title => 'Select date',
		},
	);
}

sub colour_picker {

	return dialog(
		sub { Wx::ColourPickerCtrl->new( $_[0], -1 ) },
		sub { $_[0]; },
		sub { my $c = $_[0]->GetColour; return [ $c->Red, $c->Green, $c->Blue ] },
		{   title => 'Select colour',
		},
	);
}

sub file_picker {
	return dialog(
		sub { Wx::FilePickerCtrl->new( $_[0] ) },
		sub { $_[0]->SetPath( Cwd::cwd() ) },    # setup
		sub { $_[0]->GetPath; },                 # get data
		{   title => 'Select file',
		},
	);
}

sub dir_picker {
	return dialog(
		sub { Wx::DirPickerCtrl->new( $_[0] ) },
		sub { $_[0]->SetPath( Cwd::cwd() ) },    # setup
		sub { $_[0]->GetPath; },                 # get data
		{   title => 'Select directory',
		},
	);
}

=pod

=head2 dialog

Generic dialog, with two buttons and a place for some control.
It needs 4 parameters: 3 subroutines and a hash-ref

  dialog(
      sub { create_and_return_the_control },
      sub { setup_data_in_the control },
      sub { fetch_and_return_data_from_the_control },
      {
          title => "",
          other arguments,
      }
  );

=cut

sub dialog {
	my ( $control, $setup, $getdata, $args ) = @_;

	$args ||= {};
	$args->{title} ||= '';

	my $dialog = Wx::Dialog->new( undef, -1, $args->{title} );
	my $ctrl   = $control->($dialog);
	my $ok     = Wx::Button->new( $dialog, Wx::wxID_OK, '' );
	my $cancel = Wx::Button->new( $dialog, Wx::wxID_CANCEL, '', [ -1, -1 ], $ok->GetSize );

	my $box     = Wx::BoxSizer->new(Wx::wxVERTICAL);
	my $top     = Wx::BoxSizer->new(Wx::wxHORIZONTAL);
	my $buttons = Wx::BoxSizer->new(Wx::wxHORIZONTAL);
	$box->Add($top);
	$box->Add($buttons);
	$top->Add($ctrl);
	$buttons->Add($ok);
	$buttons->Add($cancel);
	$ok->SetDefault;
	$dialog->SetSizer($box);

	my ( $bw, $bh ) = $ok->GetSizeWH;
	my ( $w,  $h )  = $ctrl->GetSizeWH;
	$dialog->SetSize( $bw * 2, $h + $bh + 20 );

	$setup->($ctrl);

	if ( $dialog->ShowModal == Wx::wxID_CANCEL ) {
		return;
	}

	my $data = $getdata->($ctrl);

	$dialog->Destroy;

	return $data;
}

sub choice {
	my (%args) = @_;
	%args = (
		title   => '',
		message => '',
		choices => [],

		%args
	);

	my $dialog = Wx::MultiChoiceDialog->new( undef, $args{message}, $args{title}, $args{choices} );
	if ( $dialog->ShowModal == Wx::wxID_CANCEL ) {
		return;
	}
	return map { $args{choices}[$_] } $dialog->GetSelections;
}

sub single_choice {
	my (%args) = @_;
	%args = (
		title   => '',
		message => '',
		choices => [],

		%args
	);

	my $dialog = Wx::SingleChoiceDialog->new( undef, $args{message}, $args{title}, $args{choices} );
	if ( $dialog->ShowModal == Wx::wxID_CANCEL ) {
		return;
	}
	return $args{choices}[ $dialog->GetSelection ];
}

#=head2 print_out
#
#=cut

#sub print_out {
#    my ($output, $text) = @_;
#    $output->AddText($text);
#    #$Wx::Perl::Dialog::Simple::app->Yield;
#    return;
#}
#

sub message {
	my (%args) = @_;

	%args = (
		title => '',
		text  => '',

		%args
	);

	Wx::MessageBox( $args{text}, $args{title}, Wx::wxOK | Wx::wxCENTRE );

	return;
}

1;

=pod

=head1 SUPPORT

See L<http://padre.perlide.org/>

=head1 COPYRIGHT & LICENSE

Copyright 2008-2010 The Padre development team as listed in Padre.pm.

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the
LICENSE file included with this module.

=head1 CREDITS and THANKS

To Mattia Barbon for providing wxPerl.

The idea was taken from the Zenity project.

=cut

# Copyright 2008-2010 The Padre development team as listed in Padre.pm.
# LICENSE
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl 5 itself.