Commit 62ec9797 by PotatoGim

wxPerl 트위터 예제

parent fcb43570
#!/usr/bin/env perl
# NOTE: http://damienlearnsperl.blogspot.kr/2009/03/wxperl-adding-gui-to-twitter-script.html
use Wx;
package MyApp;
use Wx qw/:everything/;
use base qw/Wx::App/;
sub OnInit
{
my $self = shift;
my $dialog = MyDialog->new("Twit", wxDefaultPosition);
$self->SetTopWindow($dialog);
$dialog->Show(1);
1;
}
1;
package MyDialog;
use Wx qw/:everything/;
use Wx::Event qw/:everything/;
use base qw/Wx::Dialog/;
use constant MAX_POST_LENGTH => 140;
sub new
{
my $class = shift;
# Main window
my $form_width = 480;
my $form_height = 175;
my $yline1 = 10;
my $yline2 = 30;
my $yline3 = 70;
my $yline4 = 90;
my $self = $class->SUPER::new(
undef, # parent
-1, # id
$_[0], # title
$_[1], # position [x, y]
[$form_width, $form_height] # size [width, height]
);
# Display the Wx icon on the application window
$self->SetIcon(Wx::GetWxPerlIcon());
# Static box to contain message text box + twitter and identica checkboxes
# A static box is a rectangle drawn around other panel items to denote a
# logical grouping of items.
Wx::StaticBox->new(
$self, # parent window
-1, # window identifier (-1: default)
"Status", # text to be displayed in the static box
[10, $yline1], # window position [x, y]
[$form_width-25, 120] # checkbox size [width, height]
);
# Text control to enter message to twit
$self->{text} = Wx::TextCtrl->new(
# parent window
$self,
# control identifier
-1,
# default text value
"Type your message here",
# text control position [x, y]
[20, $yline2],
# text control size [width, height]
[435, 35],
# style: wxTE_MULTILINE=The text control allows muliple lines
wxTE_MULTILINE
);
# A wxEVT_COMMAND_TEXT_MAXLEN event is generated when the number of
# characters in the text control
$self->{text}->SetMaxLength(MAX_POST_LENGTH);
Wx::StaticText->new(
$self,
-1,
" characters left",
[$form_width-95, $ylines[2]]
);
$self->{static_text}{CharsLeft} = Wx::StaticText->new(
$self,
-1,
MAX_POST_LENGTH - $self->{text}->GetLastPosition,
[$form_width-115, $ylines[2]],
wxDefaultSize,
Wx::wxALIGN_RIGHT
);
EVT_TEXT_MAXLEN($self, $self->{text}, \&MaxTextReached);
EVT_TEXT($self, $self->{text}, \&CountCharsLeft);
# Static text placed in front of the twitter and identica check boxes
Wx::StaticText->new($self, -1, "Send to:", [20, $yline3]);
# twitter check box: enabled by default
$self->{checkbox}{twitter} = Wx::CheckBox->new(
$self,
-1,
"Twitter.com",
[80, $yline3]
);
# Checked by default
$self->{checkbox}{twitter}->SetValue(1);
# identica check box: enabled by default
$self->{checkbox}{identica} = Wx::CheckBox->new(
$self,
-1,
"Identi.ca",
[200, $yline3]
);
# Checked by default
$self->{checkbox}{identica}->SetValue(1);
# End of static box
# "Send" button
$self->{button}{Send} = Wx::Button->new(
$self,
-1,
"S E N D U P D A T E",
[20, $yline4],
[$form_width-45, 30]
);
# Event associated to "Send" button
EVT_BUTTON($self, $self->{button}{Send}, \&SendMsg);
EVT_CLOSE($self, \&OnClose);
return $self;
}
sub SendMsg
{
my ($self, $event) = @_;
my $twitter, $identica;
$twitter = $self->{checkbox}{twitter}->GetValue();
$identica = $self->{checkbox}{identica}->GetValue();
# For now, just display a MessageBox to test event on button press
Wx::MessageBox(
# text
"Test:\ntwitter is set to $twitter\nidentica is set to $identica",
# title in titlebar
"Caption",
# buttons to display on form
wxOK,
# parent
$self
);
}
sub CountCharsLeft
{
my ($self, $event) = @_;
my $chars_left = MAX_POST_LENGTH - $self->{text}->GetLastPosition();
$self->{static_text}{CharsLeft}->SetLabel($chars_left);
}
sub MaxTextReached
{
my ($self, $event) = @_;
Wx::MessageBox(
"You have reached the maximum number of characters",
"Update too big",
wxOK,
$self
);
}
sub OnClose
{
my ($self, $event) = @_;
$self->Destroy();
}
1;
package main;
my $app = MyApp->new();
$app->MainLoop();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment