Permalink
Browse files

Added 3 new example files - ScreenCapture(2) & wxWizard

  • Loading branch information...
1 parent 67fc4ff commit b6083a3ded45c6dbdfee2cb41833cbf666e3f788 @jmlynesjr committed Jan 7, 2013
Showing with 326 additions and 0 deletions.
  1. +6 −0 FILE-INDEX.txt
  2. +121 −0 ScreenCapture.pl
  3. +139 −0 WxScreenCapture.pm
  4. +60 −0 wxWizard.pl
View
@@ -51,6 +51,12 @@ Ported to wxPerl by: James M. Lynes, Jr., September 26,2012
44. CppTrial-pg340.pl : # C++ Example from pg 340 - HTML Window - Displays an HTML file
45. CppTrial-pg347.pl : # C++ Example from pg 347 - Simple Grid Example
+Additional Example programs and modules
+---------------------------------------
+ScreenCapture.pl - Captures the screen to a file
+WxScreenCapture.pm - Screen capture program above in module form
+wxWizard.pl - Example of a simple wizard(simple version of pg333)
+
Additional support files used by the example code
-------------------------------------------------
cut.xpm
View
@@ -0,0 +1,121 @@
+#! /usr/bin/perl
+#
+# Capture Screen to a file. Written in wxPerl. Tested on Citrus Perl 5.16 and wxWidgets 2.8.x.
+#
+# This script draws and captures a sample graphic.
+# Take_Screenshot needs to be generalized into a module
+# that can be used by any application.
+#
+# To capture the complete sample graphic, the frame size had to be set to the screen size
+# of [1024x768]. Using wxDefaultSize caused the screen to paint in two passes
+# and the capture to clip.
+#
+# Reference: GetScreenShot C++ code page 139 of "The wxBook" -
+# Cross-Platform GUI Programming with wxWidgets -
+# Smart, Hock, and Csomor
+#
+# Original author: "PodMaster" from Perl Monks-2003 (no idea of real identity-not active for 6+ years)
+#
+# Modified by: James M. Lynes. Jr.
+# Last Modified Date: October 21, 2012
+#
+#
+use strict;
+use warnings;
+use Wx qw(:everything);
+use Wx::Event qw( EVT_PAINT );
+#
+# Main Application
+#
+my $app = Wx::SimpleApp->new;
+my $frame = Wx::Frame->new(undef, -1, "Screen Capture Example", [0,0], [1024,768] , wxDEFAULT_FRAME_STYLE);
+EVT_PAINT( $frame, \&onPaint);
+my $file = file_entry_dialog_standard($frame);
+$frame->Show;
+take_screenshot($frame, $file);
+$app->MainLoop;
+#
+# Generate a sample graphic screen to capture
+#
+sub onPaint{
+ my($self,$event)=@_;
+ my $screen = Wx::PaintDC->new($self);
+ $screen->SetBackgroundMode( wxTRANSPARENT );
+ $screen->SetFont(Wx::Font->new( 12, wxFONTFAMILY_ROMAN, wxNORMAL, wxBOLD));
+
+ for(0..17){
+ my $c = $_ * 15;
+
+ $screen->SetTextForeground( Wx::Colour->newRGB(0,0,$c));
+ $screen->DrawRotatedText("wxPerl",100 ,100 ,$c);
+ }
+ $screen->DrawRotatedText("wxPerl and PodMaster",000 ,400 ,0);
+ $screen->DrawRotatedText("are messing up your screen",000 ,450 ,0);
+
+ for(0..17){
+ my $c = $_ * 15;
+
+ $screen->SetTextForeground( Wx::Colour->newRGB(0,$c,0));
+ $screen->DrawRotatedText("wxPerl",200 ,200 ,$c);
+ }
+ $screen->DrawRotatedText("wxPerl and PodMaster",100 ,500 ,0);
+ $screen->DrawRotatedText("are messing up your screen",100 ,550 ,0);
+
+ for(0..17){
+ my $c = $_ * 15;
+
+ $screen->SetTextForeground( Wx::Colour->newRGB($c,0,0));
+ $screen->DrawRotatedText("wxPerl",300 ,300 ,$c);
+ }
+ $screen->DrawRotatedText("wxPerl and PodMaster",200 ,600 ,0);
+ $screen->DrawRotatedText("are messing up your screen",200 ,650 ,0);
+}
+#
+# Copy the screen to the output file-similar to wxBook pg 139 example.
+#
+sub take_screenshot {
+ my($self, $file ) = @_;
+ $self->Refresh; # without Refresh and Update
+ $self->Update; # the underlying window is captured
+ my $screen = Wx::ScreenDC->new();
+ my( $x, $y) = $screen->GetSizeWH();
+ my $bitmap = Wx::Bitmap->new($x,$y,-1);
+ my $memory = Wx::MemoryDC->new();
+ $memory->SelectObject( $bitmap );
+ $memory->Blit(0,0,$x,$y, $screen, 0, 0); # copy the screen to the bitmap
+ $bitmap->SaveFile( $file , wxBITMAP_TYPE_BMP ) ; # copy the bitmap to the output file
+}
+#
+# Ask for the output filename - simple text entry dialog
+#
+sub file_entry_dialog_simple {
+ my( $self ) = @_;
+ my $textvalue = "capture.bmp";
+ my $dialog = Wx::TextEntryDialog->new
+ ( $self, "Enter the Screen Capture output filename\n(Cancel will use the default filename shown below)\n",
+ "Screen Capture Output File Entry",
+ $textvalue, wxOK | wxCANCEL );
+ if( $dialog->ShowModal == wxID_OK ) {
+ $textvalue = $dialog->GetValue;
+ }
+ $dialog->Destroy;
+ return $textvalue;
+}
+#
+# Ask for the output filename - standard file dialog
+#
+sub file_entry_dialog_standard {
+ my ( $self ) = @_;
+ my $caption = "Choose a Screen Capture Output Filename";
+ my $wildcard = "*.bmp";
+ my $defaultDir = ".";
+ my $defaultFilename = "capture.bmp";
+ my $filevalue = $defaultFilename;
+ my $fileDialog = Wx::FileDialog->new($self, $caption, $defaultDir,
+ $defaultFilename, $wildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
+ if ($fileDialog->ShowModal() == wxID_OK ) {
+ $filevalue = $fileDialog->GetPath();
+ }
+ $fileDialog->Destroy;
+ return $filevalue;
+}
View
@@ -0,0 +1,139 @@
+use strict;
+use warnings;
+
+package WxScreenCapture;
+use parent qw(Exporter);
+
+our @EXPORT = qw(take_screenshot file_entry_dialog_simple file_entry_dialog_standard);
+
+use Wx qw(:everything);
+use Wx::Event qw( EVT_PAINT );
+
+=head2
+#
+# Copy the screen to the output file-similar to wxBook pg 139 example.
+#
+=cut
+sub take_screenshot {
+ my($self, $file ) = @_;
+ $self->Refresh; # without Refresh and Update
+ $self->Update; # the underlying window is captured
+ my $screen = Wx::ScreenDC->new();
+ my( $x, $y) = $screen->GetSizeWH();
+ my $bitmap = Wx::Bitmap->new($x,$y,-1);
+ my $memory = Wx::MemoryDC->new();
+ $memory->SelectObject( $bitmap );
+ $memory->Blit(0,0,$x,$y, $screen, 0, 0); # copy the screen to the bitmap
+ $bitmap->SaveFile( $file , wxBITMAP_TYPE_BMP ) ; # copy the bitmap to the output file
+}
+
+=head2
+#
+# Ask for the output filename - simple text entry dialog
+#
+=cut
+sub file_entry_dialog_simple {
+ my( $self ) = @_;
+ my $textvalue = "capture.bmp";
+ my $dialog = Wx::TextEntryDialog->new
+ ( $self, "Enter the Screen Capture output filename\n(Cancel will use the default filename shown below)\n",
+ "Screen Capture Output File Entry",
+ $textvalue, wxOK | wxCANCEL );
+ if( $dialog->ShowModal == wxID_OK ) {
+ $textvalue = $dialog->GetValue;
+ }
+ $dialog->Destroy;
+ return $textvalue;
+}
+
+=head2
+#
+# Ask for the output filename - standard file dialog
+#
+=cut
+sub file_entry_dialog_standard {
+ my ( $self ) = @_;
+ my $caption = "Choose a Screen Capture Output Filename";
+ my $wildcard = "*.bmp";
+ my $defaultDir = ".";
+ my $defaultFilename = "capture.bmp";
+ my $filevalue = $defaultFilename;
+ my $fileDialog = Wx::FileDialog->new($self, $caption, $defaultDir,
+ $defaultFilename, $wildcard, wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
+ if ($fileDialog->ShowModal() == wxID_OK ) {
+ $filevalue = $fileDialog->GetPath();
+ }
+ $fileDialog->Destroy;
+ return $filevalue;
+}
+1;
+__END__
+
+=head1 NAME
+
+WxScreenCapture.pm
+
+=head1 SYNOPSIS
+
+Module to capture a screen to a file. Written in wxPerl.
+
+=head1 DESCRIPTION
+
+This module exports three subroutines: take_screenshot, file_entry_dialog_simple, and file_entry_dialog_standard. take_screenshot uses Wx::ScreenDC and Wx::MemoryDC device contexts and a Wx::Bitmap. Blit copies the ScreenDC to the MemoryDC Bitmap which is then copied to an output file. file_entry_dialog_simple provides a text_entry_dialog for requesting the capture filename. file_entry_dialog_standard provides the full directory/file entry screen for requesting the capture pathname.
+
+If the frame you wish to capture is smaller than the full screen, the background windows will also be captured.
+
+=head1 USAGE
+
+use Wx qw(:everything);
+
+use Wx::Event qw( EVT_PAINT );
+
+use WxScreenCapture;
+
+my $app = Wx::SimpleApp->new;
+
+my $frame = Wx::Frame->new(undef, -1, "Screen Capture Module Example", [0,0], [1024,768] , wxDEFAULT_FRAME_STYLE);
+
+EVT_PAINT( $frame, \&onPaint);
+
+my $file = file_entry_dialog_standard($frame);
+
+$frame->Show;
+
+take_screenshot($frame, $file);
+
+$app->MainLoop;
+
+sub onPaint{} #paint the screen to be captured here.
+
+=head1 AUTHOR
+
+James M. Lynes, Jr. <jmlynesjr@gmail.com>
+
+Lakeland, Florida USA October 21,2012.
+
+Original author: "PodMaster" from Perl Monks-2003 (no idea of real identity-not active for 6+ years)
+
+=head1 BUGS/FEATURES
+
+To capture the complete sample graphic(not included in this module definition), the frame size
+had to be set to the screen size of [1024x768]. Using wxDefaultSize caused the screen to paint in two passes
+and the capture to clip. If the frame is not defined large enough, parts of underlying
+windows will also be captured.
+
+=head1 SEE ALSO
+
+"The wxBook" - Cross-Platform GUI Programming with wxWidgets -
+ Smart, Hock, and Csomor
+
+The wxWidgets documentation L<http://www.wxwidgets.org/>
+
+The Citrus Perl Distribution L<http://www.citrusperl.com/>
+
+=head1 LICENSE
+
+This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
+
+=cut
+
View
@@ -0,0 +1,60 @@
+#!/usr/bin/perl
+
+# wxWizard.pl
+# Adapted from wxperl_demo.pl from the wxPerl distribution
+# James M. Lynes Jr. - Last Modified 1/6/2013
+
+use 5.010;
+use strict;
+use warnings;
+
+use Wx qw(:everything);
+use Wx::Event qw(EVT_WIZARD_PAGE_CHANGED EVT_BUTTON EVT_WIZARD_FINISHED EVT_WIZARD_CANCEL);
+
+# create the WxApplication
+
+my $app = Wx::SimpleApp->new;
+
+ my $frame = Wx::Frame->new( undef, -1,'wxWizard.pl', wxDefaultPosition, wxDefaultSize );
+
+ my $panel = Wx::Panel->new($frame, -1, wxDefaultPosition, wxDefaultSize);
+ Wx::StaticText->new($panel, -1, "WxWizard Sample Program", Wx::Point->new(100,100),
+ Wx::Size->new(200,200), wxALIGN_CENTER);
+
+ my $button = Wx::Button->new( $panel, -1, "Start Wizard", Wx::Point->new(150, 225), wxDefaultSize );
+
+ Wx::InitAllImageHandlers();
+ my $bmp = Wx::Bitmap->new("logo.png", wxBITMAP_TYPE_PNG);
+ my $wizard = Wx::Wizard->new( $panel, -1, "Wizard Example", $bmp );
+
+ # first page
+ my $page1 = Wx::WizardPageSimple->new( $wizard );
+ Wx::TextCtrl->new( $page1, -1, "First page", wxDefaultPosition, Wx::Size->new(200,25) );
+
+ # second page
+ my $page2 = Wx::WizardPageSimple->new( $wizard );
+ Wx::TextCtrl->new( $page2, -1, "Second page", wxDefaultPosition, Wx::Size->new(200,25) );
+
+ # third page
+ my $page3 = Wx::WizardPageSimple->new( $wizard );
+ Wx::TextCtrl->new( $page3, -1, "Third page", wxDefaultPosition, Wx::Size->new(200,25) );
+
+ # fourth page
+ my $page4 = Wx::WizardPageSimple->new( $wizard );
+ Wx::TextCtrl->new( $page4, -1, "Fourth page", wxDefaultPosition, Wx::Size->new(200,25) );
+
+ Wx::WizardPageSimple::Chain( $page1, $page2 );
+ Wx::WizardPageSimple::Chain( $page2, $page3 );
+ Wx::WizardPageSimple::Chain( $page3, $page4 );
+
+ EVT_BUTTON( $panel, $button, sub {$wizard->RunWizard( $page1 ); } );
+ EVT_WIZARD_CANCEL( $panel, $wizard, sub {Wx::Wizard->Destroy();} );
+ EVT_WIZARD_FINISHED($panel, $wizard, sub { Wx::Wizard->Destroy(); } );
+
+# Testing Messages comment out matching event above
+# EVT_WIZARD_FINISHED($panel, $wizard, sub { Wx::LogMessage( "Wizard Canceled" ); } );
+# EVT_WIZARD_PAGE_CHANGED( $panel, $wizard, sub {Wx::LogMessage( "Wizard page changed" ); } );
+# EVT_WIZARD_CANCEL( $panel, $wizard, sub {Wx::LogMessage( "Wizard Canceled" ); } );
+
+$frame->Show;
+$app->MainLoop;

0 comments on commit b6083a3

Please sign in to comment.