Commit 74fa188c by PotatoGim

wxPerl 예제를 통한 실습 중

parent 74afaa19
#!/usr/bin/env perl
#=============================================================================
# FILE: adding_widgets.pl
# USAGE: ./adding_widgets.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# ORGANIZATION:
# DATE: 2015년 08월 22일 21시 05분 32초
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Wx;
use wxPerl::Constructors;
package MyApp;
use base 'Wx::App';
sub OnInit
{
my $self = shift;
my $frame = wxPerl::Frame->new(
undef,
"A wxPerl Application - P.G.",
style => &Wx::wxMAXIMIZE_BOX | &Wx::wxCLOSE_BOX
);
my $button = wxPerl::Button->new($frame, 'Click Me');
Wx::Event::EVT_BUTTON(
$button,
-1,
sub {
my ($b, $evt) = @_;
$b->SetLabel("Clicked");
$b->Disable;
}
);
$frame->Show;
}
MyApp->new->MainLoop;
#!/usr/bin/env perl
#=============================================================================
# FILE: sizers.pl
# USAGE: ./sizers.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# ORGANIZATION:
# DATE: 2015년 08월 22일 21시 30분 57초
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Wx qw/:id/;
package MyApp;
use base qw/Wx::App/;
sub OnInit
{
my $self = shift;
my $frame = Wx::Frame->new(
undef,
-1,
'A wxPerl Application',
&Wx::wxDefaultPosition,
&Wx::wxDefaultSize,
&Wx::wxMAXIMIZE_BOX | &Wx::wxCLOSE_BOX
);
$frame->SetMinSize([120, 80]);
my $sizer = Wx::BoxSizer->new(&Wx::wxVERTICAL);
my $button1 = Wx::Button->new($frame, -1, "Click Me");
my $button2 = Wx::Button->new($frame, -1, "DO NOT CLICK");
Wx::Event::EVT_BUTTON(
$button1,
-1,
sub {
my ($b, $evt) = @_;
$b->SetLabel("Clicked");
$b->Disable;
}
);
Wx::Event::EVT_BUTTON(
$button2,
-1,
sub {
&Wx::wxTheApp->ExitMainLoop;
}
);
$sizer->Add($button1, 2, &Wx::wxEXPAND);
$sizer->Add($button2, 0, &Wx::wxEXPAND);
$frame->SetSizer($sizer);
$frame->Show();
}
MyApp->new->MainLoop();
#!/usr/bin/env perl
#=============================================================================
# FILE: constructor.pl
# USAGE: ./constructors.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# ORGANIZATION:
# DATE: 2015년 08월 22일 21시 05분 32초
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Wx;
use wxPerl::Constructors;
package MyApp;
use base 'Wx::App';
sub OnInit
{
my $self = shift;
my $frame = wxPerl::Frame->new(
undef,
"A wxPerl Application - 김지현",
style => &Wx::wxMAXIMIZE_BOX | &Wx::wxCLOSE_BOX
);
$frame->Show;
}
MyApp->new->MainLoop;
#!/usr/bin/env perl
#=============================================================================
# FILE: first.pl
# USAGE: ./first.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# ORGANIZATION:
# DATE: 2015년 08월 22일 20시 33분 26초
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Wx;
package MyApp;
use base 'Wx::App';
sub OnInit {
my $self = shift;
my $frame = Wx::Frame->new(
undef, # parent window
-1, # window ID
'A wxPerl Application - 김지현', # window title
&Wx::wxDefaultPosition, # window position
&Wx::wxDefaultSize, # window size
&Wx::wxMAXIMIZE_BOX | &Wx::wxCLOSE_BOX # window style
);
$frame->Show;
}
MyApp->new->MainLoop;
#!/usr/bin/env perl
#=============================================================================
# FILE: gridsizers.pl
# USAGE: ./gridsizers.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# ORGANIZATION:
# DATE: 2015년 08월 23일 11시 47분 18초
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
package MyApp;
use strict;
use warnings;
use utf8;
use base qw/Wx::App/;
use Wx qw/:everything/;
sub OnInit
{
my $self = shift;
my ($frame, $sizer);
$frame = Wx::Frame->new(
undef, -1, "A Frame", wxDefaultPosition, wxSIZE(300, 200)
);
$sizer = $self->init_sizer($frame);
$frame->SetAutoLayout(0);
$frame->SetSizer($sizer);
$self->SetTopWindow($frame);
$frame->Show(1);
}
sub init_sizer
{
my ($self, $frame) = @_;
my ($sizer, $button);
$sizer = Wx::GridSizer->new(2, 3);
foreach my $row (1 .. 2) {
foreach my $col (1 .. 3) {
$button = Wx::Button->new($frame, -1, "($row, $col)");
$sizer->Add($button);
}
}
return $sizer;
}
1;
package main;
use strict;
use warnings;
use utf8;
MyApp->new()->MainLoop();
#!/usr/bin/env perl
#=============================================================================
# FILE: memorydc.pl
# USAGE: ./memorydc.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# ORGANIZATION:
# DATE: 2015년 08월 23일 02시 21분 28초
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
package MyFrame;
use Wx qw/:everything/;
use Wx::Event qw/EVT_LEFT_UP/;
use base qw/Wx::Frame/;
our $gl_self;
sub new
{
my ($self, $parent, $id, $title, $pos, $size, $style, $name) = @_;
$parent = undef unless defined $parent;
$id = wxID_ANY unless defined $id;
$title = "" unless defined $title;
$pos = wxDefaultPosition unless defined $pos;
$size = wxDefaultSize unless defined $size;
$name = "" unless defined $name;
$style = wxDEFAULT_FRAME_STYLE unless defined $style;
$self = $self->SUPER::new($parent, $id, $title, $pos, $size, $style, $name);
$self->SetTitle("Drawing on image");
$gl_self = $self; # Set Global variable for use in EVT handler
# Image for drawing on
$self->{image1} = Wx::Image->new("/home/potatogim/Pictures/1436504455XrPopi48i8BMYITbt.gif", wxBITMAP_TYPE_ANY, -1);
$self->{Loc_Photo_Bmp} = Wx::Bitmap->new($self->{image1});
$self->{bitmap_1} = Wx::StaticBitmap->new($self, wxID_ANY, $self->{Loc_Photo_Bmp});
# Button to draw image
$self->{bitmap_1}->SetCursor(wxCROSS_CURSOR);
$self->{bitmap_1}->Connect(wxID_ANY, wxID_ANY, wxEVT_LEFT_UP, \&on_button);
# Sizer
$self->{sizer_1} = Wx::BoxSizer->new(wxVERTICAL);
$self->{sizer_1}->Add($self->{bitmap_1}, 0, 0, 0);
$self->SetSizer($self->{sizer_1});
$self->{sizer_1}->Fit($self);
$self->Layout();
return $self;
}
sub on_button
{
my ($self, $event) = @_;
# Select it into a memory dc
if (defined($gl_self->{Loc_Photo_Bmp}))
{
my $mdc = Wx::MemoryDC->new();
my $pen = Wx::Pen->new(Wx::Colour->new(255, 255, 255), 3, wxSOLID);
$mdc->SelectObject($gl_self->{Loc_Photo_Bmp});
$mdc->SetPen($pen);
$mdc->SetBrush(wxTRANSPARENT_BRUSH);
# Determine mouse event
my $r = 50;
my $x = $event->GetX();
my $y = $event->GetY();
# Draw circle round mouse event
$mdc->DrawCircle($x, $y, $r);
$mdc->SelectObject(wxNullBitmap); # deselect the bitmap out of the DC
$gl_self->{bitmap_1}->SetBitmap($gl_self->{Loc_Photo_Bmp});
$gl_self->{bitmap_1}->SetCursor(wxSTANDARD_CURSOR);
$gl_self->{bitmap_1}->Disconnect(wxID_ANY, wxID_ANY, wxEVT_LEFT_DOWN);
$gl_self->{bitmap_1}->Disconnect(wxID_ANY, wxID_ANY, wxEVT_LEFT_UP);
}
$event->Skip();
return $self;
}
1;
package main;
unless (caller)
{
local *Wx::App::OnInit = sub { return 1; };
my $app = Wx::App->new();
Wx::InitAllImageHandlers();
my $frame_1 = MyFrame->new();
$app->SetTopWindow($frame_1);
$frame_1->Show(1);
$app->MainLoop();
}
#!/usr/bin/env perl
#=============================================================================
# FILE: miniimage.pl
# USAGE: ./miniimage.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# ORGANIZATION:
# DATE: 2015년 08월 23일 02시 45분 31초
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
use Wx;
my $filename = "/home/potatogim/Pictures/749088d144a92b0aac89a49d64cadffa.jpg";
package MyApp;
use Wx qw/:everything/;
use Wx::Event qw/EVT_MENU/;
use base qw/Wx::App/;
sub OnInit
{
my $self = @_;
my $frame = MyFrame->new("Mini-image demo", [-1, -1], [-1, -1]);
unless ($frame) {
warn "Unable to create frame -- exiting.";
return undef;
}
$frame->Show(1);
return 1;
}
package MyFrame;
use Wx qw/:everything/;
use Wx::Event qw/:everything/;
use IO::File;
use base qw/Wx::Frame/;
sub new
{
my $class = shift;
my $self = $class->SUPER::new(undef, -1, $_[0], $_[1], $_[2]);
# replace the filename with something appropriate.
my $file = IO::File->new($filename, "r");
unless ($file) {
print "Can't load $filename.";
return undef;
}
binmode $file;
my $handler = Wx::JPEGHandler->new();
my $image = Wx::Image->new();
$handler->LoadFile($image, $file);
my $bmp = Wx::Bitmap->new($image);
if ($bmp->Ok())
{
# create a static bitmap called ImageViewer
# that displays the selected image.
$self->{ImageViewer} = Wx::StaticBitmap->new($self, -1, $bmp);
}
$self->{ScaleImage} = 0;
$self->{TreeCtrl} = MyTreeCtrl->new($self, -1);
# allow wxperl to manage control sizing & placement
$self->SetAutoLayout(1);
# Layout constraints provide the guides for wxperl's autolayout.
my $b1 = Wx::LayoutConstraints->new();
my $b2 = Wx::LayoutConstraints->new();
# These constraints define the placement and
# dimensions of the controls they're bound to.
# and can be either absolute, or relative to
# other constrols
$b1->left->Absolute(0);
$b1->top->Absolute(0);
$b1->width->PercentOf($self, wxWidth, 50);
$b1->height->PercentOf($self, wxHeight, 100);
$self->{TreeCtrl}->SetConstraints($b1);
$b2->left->RightOf($self->{TreeCtrl});
$b2->top->Absolute(0);
$b2->width->PercentOf($self, wxWidth, 50);
$b2->height->PercentOf($self, wxHeight, 100);
$self->{ImageViewer}->SetConstraints($b2);
# Set up the menu bar
my $file_menu = Wx::Menu->new();
my ($OPEN_NEW_DIR, $REMOVE_DIR, $SCALE_IMAGE, $APP_QUIT) = (1 .. 100);
$file_menu->Append($OPEN_NEW_DIR, "&Open A Directory\tCtrl-O");
$file_menu->AppendSeparator();
$file_menu->Append($SCALE_IMAGE, "&Scale Images to Window\tCtrl-S", "", 1);
$file_menu->AppendSeparator();
$file_menu->Append($APP_QUIT, "E&xit\tCtrl-x", "Exit Application");
# Note that even though there are 6 options, only 4 of them are active
# as they're the only ones bound to event handlers.
EVT_MENU($self, $OPEN_NEW_DIR, \&OnDirDialog);
EVT_MENU($self, $SCALE_IMAGE, \&Set_Scale);
EVT_MENU($self, $APP_QUIT, sub { $_[0]->Close(1); });
my $menubar = Wx::MenuBar->new();
$menubar->Append($file_menu, "&File");
$self->SetMenuBar($menubar);
# return the frame object to the calling application.
return $self;
}
# Set_Scale changes a scalar flag which will determine
# if a displayed image is resized to fit the full dimenstions
# of the image control.
sub Set_Scale
{
my $self = shift;
# yes, I could have used NOT here.
$self->{ScaleImage} ^= 1;
}
# OnDirDialog scans a user specified directory for images.
sub OnDirDialog
{
my ($self, $event) = @_;
my $dialog = Wx::DirDialog->new($self);
unless ($dialog->ShowModal() == wxID_CANCEL)
{
$self->{TreeCtrl}->ScanDir($dialog->GetPath());
}
$dialog->Destroy();
}
package MyTreeCtrl;
use Wx qw/:everything/;
use Wx::Event qw/:everything/;
use LWP::UserAgent;
use LWP::MediaTypes qw/guess_media_type/;
use HTML::SimpleLinkExtor;
use base qw/Wx::TreeCtrl/;
sub new
{
my $class = shift;
my $self = $class->SUPER::new(@_);
EVT_TREE_SEL_CHANGED($self, $self, \&OnChange);
# add one default item
my $root = $self->AddRoot("Image listings");
return $self;
}
# ScanDir adds a child node to the treeview that
# lists the directory, adn then sets each image file to
# be a child of that newly created node.
sub ScanDir
{
my $self = shift;
my $dir = shift;
print "$dir\n";
$dir =~ tr/\\/\//;
$dir .= "/" if ($dir !~ /[\/]$/);
my (@files) = glob "$dir*.jpg $dir*.gif";
my $item = $self->AppendItem($self->GetRootItem, "$dir ($#files) files");
$self->AppendItem($item, $_) foreach (@files);
}
# event handler for selecting different images.
sub OnChange
{
my ($self, $event) = @_;
my $item = $event->GetItem();
# return if user clicked on the root node.
return if ($item == $self->GetRootItem());
my $txt = $self->GetItemText($item);
my $ext;
my $handler;
$txt = reverse($txt);
$ext = lc(reverse($1)) if ($txt =~ m/^(.+)\./);
$txt = reverse($txt);
printf "%s / %s\n", length($txt) ? $txt : "", length($ext) ? $ext : "";
return if (!defined($ext) || !length($ext) || $ext !~ m/^(jpg|gif)$/);
# this is cheesy, but the autosensing handler doesn't work,
# or I can't get it to work appropriately.
$handler = ($ext eq "jpg") ? Wx::JPEGHandler->new() : Wx::GIFHandler->new();
# create file
my $file = IO::File->new($txt, "r") or return undef;
binmode $file;
# exit if problems occur during handler creation
return unless $handler;
# and stuff into the image handler.
my $image = Wx::Image->new();
$handler->LoadFile($image, $file) or warn "Can't load file!\n";
my $bmp = Wx::Bitmap->new($image);
if ($bmp->Ok())
{
# create a static bitmap called ImageViewer
# that displays the selected image.
my $parent = $self->GetParent();
my $img = $parent->{ImageViewer};
my $size = $img->GetSize();
$img->SetBitmap($bmp);
$img->SetSize($size) if ($parent->{ScaleImage});
$img->Refresh();
}
}
package main;
my $app = MyApp->new();
$app->MainLoop();
#!/usr/bin/env perl
#=============================================================================
# FILE: progress.pl
# USAGE: ./progress.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# ORGANIZATION:
# DATE: 2015년 08월 23일 02시 01분 38초
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
use strict;
use warnings;
use utf8;
package MyApp;
use base qw/Wx::App/;
use Wx qw/:everything/;
use LWP::UserAgent;
sub OnInit
{
my ($this) = @_;
my $frame = Wx::Frame->new(undef, -1, "wxPerl", wxDefaultPosition, [200, 100]);
$frame->{TXT} = Wx::TextCtrl->new($frame, -1, "");
$frame->Show(1);
download($frame, "http://cpan.org/modules/01modules.index.html");
}
sub download
{
my ($frame, $from) = @_;
my $data = "";
my $ua = LWP::UserAgent->new();
my $content_size = $ua->head($from)->headers()->header("content-length");
my $pd = Wx::ProgressDialog->new(
"Downloading File",
"",
$content_size,
$frame,
wxPD_AUTO_HIDE | wxPD_APP_MODAL | wxPD_ELAPSED_TIME | wxPD_ESTIMATED_TIME
| wxPD_REMAINING_TIME
);
$ua->get($from, ":content_cb" => sub {
$data .= $_[0];
$pd->Update(length($data))
});
$frame->{TXT}->SetValue(sprintf("Downloaded %d bytes", length($data)));
$pd->Destroy();
}
MyApp->new()->MainLoop();
#!/usr/bin/env perl
#=============================================================================
# FILE: sizers.pl
# USAGE: ./sizers.pl
# OPTIONS:
# BRIEF:
# AUTHOR: Ji-Hyeon Gim <potatogim@potatogim.net>
# ORGANIZATION:
# DATE: 2015년 08월 23일 11시 47분 18초
# VERSION: 1.0
# LICENSE: Artistic License 2.0
# NOTE:
#=============================================================================
package MyApp;
use strict;
use warnings;
use utf8;
use base qw/Wx::App/;
use Wx qw/:everything/;
sub OnInit
{
my $self = shift;
my ($frame, $sizer);
$frame = Wx::Frame->new(
undef, -1, "A Frame", wxDefaultPosition, wxSIZE(300, 200)
);
$sizer = $self->init_sizer($frame);
$frame->SetAutoLayout(1);
$frame->SetSizer($sizer);
$self->SetTopWindow($frame);
$frame->Show(1);