File Coverage

blib/lib/Wx/DemoModules/wxPrinting.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #############################################################################
2             ## Name: lib/Wx/DemoModules/wxPrinting.pm
3             ## Purpose: Printing demo
4             ## Author: Mattia Barbon
5             ## Modified by:
6             ## Created: 12/09/2001
7             ## RCS-ID: $Id: wxPrinting.pm 3505 2013-07-03 12:17:43Z mdootson $
8             ## Copyright: (c) 2001, 2003, 2005-2006 Mattia Barbon
9             ## Licence: This program is free software; you can redistribute it and/or
10             ## modify it under the same terms as Perl itself
11             #############################################################################
12              
13 1     1   1796 use Wx::Print;
  0            
  0            
14              
15             package Wx::DemoModules::wxPrinting;
16              
17             use strict;
18             use base qw(Wx::Panel);
19              
20             use Wx qw(:sizer);
21             use Wx::Event qw(EVT_BUTTON);
22              
23             sub new {
24             my $class = shift;
25             my $this = $class->SUPER::new( @_ );
26              
27             my $top = Wx::BoxSizer->new( wxVERTICAL );
28             my $canvas = Wx::DemoModules::wxPrinting::Canvas->new( $this, -1 );
29              
30             my $preview = Wx::Button->new( $this, -1, "Preview" );
31             my $print = Wx::Button->new( $this, -1, "Print" );
32              
33             my $buttons = Wx::BoxSizer->new( wxHORIZONTAL );
34             $buttons->Add( $preview, 0, wxALL, 5 );
35             $buttons->Add( $print, 0, wxALL, 5 );
36              
37             $top->Add( $canvas, 1, wxGROW );
38             $top->Add( $buttons, 0, wxGROW );
39              
40             $this->SetSizer( $top );
41             $this->SetAutoLayout( 1 );
42              
43             $this->{CANVAS} = $canvas;
44              
45             EVT_BUTTON( $this, $preview, \&OnPreview );
46             EVT_BUTTON( $this, $print, \&OnPrint );
47              
48             return $this;
49             }
50              
51             sub canvas { $_[0]->{CANVAS} }
52              
53             use Wx qw(wxTheApp);
54              
55             sub OnPreview {
56             my( $this, $event ) = @_;
57              
58             my $prev = Wx::DemoModules::wxPrinting::Printout->new( $this->canvas, "Preview" );
59             my $print = Wx::DemoModules::wxPrinting::Printout->new( $this->canvas, "Print" );
60             my $preview = Wx::PrintPreview->new( $prev, $print );
61             my $frame = Wx::DemoModules::wxPrinting::PreviewFrame->new( $preview, wxTheApp->GetTopWindow,
62             "Printing Demo Preview", [-1, -1], [600, -1] );
63             $frame->Initialize();
64              
65             $frame->Show( 1 );
66             }
67              
68             sub OnPrint {
69             my( $this, $event ) = @_;
70              
71             my $printer = Wx::Printer->new;
72             my $printout = Wx::DemoModules::wxPrinting::Printout->new( $this->canvas, "Print" );
73             $printer->Print( $this, $printout, 1 );
74              
75             $printout->Destroy;
76             }
77              
78             sub add_to_tags { qw(misc) }
79             sub title { 'Printing' }
80              
81             package Wx::DemoModules::wxPrinting::PreviewFrame;
82              
83             use strict;
84             use base 'Wx::PlPreviewFrame';
85              
86             sub Initialize {
87             Wx::LogMessage( 'Wx::DemoModules::wxPrinting::PreviewFrame::Initialize' );
88              
89             $_[0]->SUPER::Initialize;
90             }
91              
92             sub CreateControlBar {
93             Wx::LogMessage( 'Wx::DemoModules::wxPrinting::PreviewFrame::CreateControlBar' );
94              
95             $_[0]->SetPreviewControlBar
96             ( Wx::DemoModules::wxPrinting::ControlBar->new( $_[0]->GetPrintPreview, $_[0] ) );
97             $_[0]->GetPreviewControlBar->CreateButtons;
98             }
99              
100             package Wx::DemoModules::wxPrinting::ControlBar;
101              
102             use strict;
103             use base 'Wx::PlPreviewControlBar';
104              
105             sub new {
106             Wx::LogMessage( 'Wx::DemoModules::wxPrinting::ControlBar::new' );
107              
108             $_[0]->SUPER::new( $_[1], 0xffffffff, $_[2], [0, 0], [400, 40] );
109             }
110              
111             sub CreateButtons {
112             Wx::LogMessage( 'Wx::DemoModules::wxPrinting::ControlBar::CreateButtons' );
113              
114             shift->SUPER::CreateButtons;
115             }
116              
117             package Wx::DemoModules::wxPrinting::Printout;
118              
119             use strict;
120             use base qw(Wx::Printout);
121              
122             sub new {
123             my $class = shift;
124             my $canvas = shift;
125             my $this = $class->SUPER::new( @_ );
126              
127             $this->{CANVAS} = $canvas;
128              
129             return $this;
130             }
131              
132             sub GetPageInfo {
133             my $this = shift;
134              
135             Wx::LogMessage( "GetPageInfo" );
136              
137             return ( 1, 2, 1, 2 );
138             }
139              
140             sub HasPage {
141             my $this = shift;
142              
143             Wx::LogMessage( "HasPage: %d", $_[0] );
144              
145             return $_[0] == 1 || $_[0] == 2;
146             }
147              
148             sub OnBeginDocument {
149             my $this = shift;
150              
151             Wx::LogMessage( "OnBeginDocument: %d, %d", @_ );
152              
153             return $this->SUPER::OnBeginDocument( @_ );
154             }
155              
156             sub OnEndDocument {
157             my $this = shift;
158              
159             Wx::LogMessage( "OnEndDocument" );
160              
161             return $this->SUPER::OnEndDocument();
162             }
163              
164             sub OnBeginPrinting {
165             my $this = shift;
166              
167             Wx::LogMessage( "OnBeginPrinting" );
168              
169             return $this->SUPER::OnBeginPrinting();
170             }
171              
172             sub OnEndPrinting {
173             my $this = shift;
174              
175             Wx::LogMessage( "OnEndPrinting" );
176              
177             return $this->SUPER::OnEndPrinting();
178             }
179              
180             sub OnPrintPage {
181             my( $this, $page ) = @_;
182             my $dc = $this->GetDC();
183              
184             # we need to set the appropriate scale
185             my( $x_size, $y_size ) = ( $Wx::DemoModules::wxPrinting::Canvas::x_size,
186             $Wx::DemoModules::wxPrinting::Canvas::y_size );
187              
188             my( $xmargin, $ymargin ) = ( 50, 50 );
189             # total size ( borders on top/bottom, left/right )
190             my( $xsize, $ysize ) = ( $x_size + 2 * $xmargin, $y_size + 2 * $ymargin );
191              
192             # dc size
193             my( $xdc, $ydc ) = $dc->GetSizeWH();
194              
195             # calculate the scale
196             my( $xscale, $yscale ) = ( $xdc / $xsize, $ydc / $ysize );
197             my $scale = ( $xscale < $yscale ) ? $xscale : $yscale;
198             # center the image
199             my( $xoff, $yoff ) = ( ( $xdc - ( $scale * $x_size ) ) / 2.0,
200             ( $ydc - ( $scale * $y_size ) ) / 2.0 );
201              
202             # set the DC origin / scale
203             $dc->SetUserScale( $scale, $scale );
204             $dc->SetDeviceOrigin( $xoff, $yoff );
205              
206             if( $page == 1 ) { $this->{CANVAS}->OnDraw( $dc ); }
207             if( $page == 2 ) { } # empty page
208            
209             return $page == 1 || $page == 2;
210             }
211              
212             package Wx::DemoModules::wxPrinting::Canvas;
213              
214             use strict;
215             use base qw(Wx::ScrolledWindow);
216              
217             use Wx qw(wxCURSOR_PENCIL wxWHITE);
218             use Wx::Event qw(EVT_MOTION EVT_LEFT_DOWN EVT_LEFT_UP);
219              
220             use vars qw($x_size $y_size);
221              
222             ( $x_size, $y_size ) = ( 800, 800 );
223              
224             sub new {
225             my $class = shift;
226             my $this = $class->SUPER::new( @_ );
227              
228             $this->SetScrollbars( 1, 1, $x_size, $y_size );
229             $this->SetBackgroundColour( wxWHITE );
230             $this->SetCursor( Wx::Cursor->new( wxCURSOR_PENCIL ) );
231              
232             EVT_MOTION( $this, \&OnMouseMove );
233             EVT_LEFT_DOWN( $this, \&OnButton );
234             EVT_LEFT_UP( $this, \&OnButton );
235              
236             return $this;
237             }
238              
239             use Wx qw(:font);
240             use Wx qw(:colour :pen);
241              
242             sub OnDraw {
243             my $this = shift;
244             my $dc = shift;
245             # my $font = Wx::Font->new( 20, wxSCRIPT, wxSLANT, wxBOLD );
246              
247             # $dc->SetFont( $font );
248             $dc->DrawRotatedText( "Draw Here", 200, 200, 35 );
249              
250             $dc->DrawEllipse( 20, 20, 50, 50 );
251             $dc->DrawEllipse( 20, $y_size - 50 - 20, 50, 50 );
252             $dc->DrawEllipse( $x_size - 50 - 20, 20, 50, 50 );
253             $dc->DrawEllipse( $x_size - 50 - 20, $y_size - 50 - 20, 50, 50 );
254              
255             $dc->SetPen( Wx::Pen->new( wxRED, 5, wxSOLID ) );
256             # wxGTK does not like DrawLines in this context
257             foreach my $i ( @{$this->{LINES}} ) {
258             my $prev;
259              
260             foreach my $j ( @$i ) {
261             if( $j != ${$i}[0] ) {
262             $dc->DrawLine( @$prev, @$j );
263             # $dc->DrawLines( $i );
264             }
265             $prev = $j;
266             }
267             }
268             }
269              
270             sub OnMouseMove {
271             my( $this, $event ) = @_;
272              
273             return unless $event->Dragging;
274              
275             my $dc = Wx::ClientDC->new( $this );
276             $this->PrepareDC( $dc );
277             my $pos = $event->GetLogicalPosition( $dc );
278             my( $x, $y ) = ( $pos->x, $pos->y );
279              
280             push @{$this->{CURRENT_LINE}}, [ $x, $y ];
281             my $elems = @{$this->{CURRENT_LINE}};
282              
283             $dc->SetPen( Wx::Pen->new( wxRED, 5, wxSOLID ) );
284             $dc->DrawLine( @{$this->{CURRENT_LINE}[$elems-2]},
285             @{$this->{CURRENT_LINE}[$elems-1]} );
286              
287             }
288              
289             sub OnButton {
290             my( $this, $event ) = @_;
291              
292             my $dc = Wx::ClientDC->new( $this );
293             $this->PrepareDC( $dc );
294             my $pos = $event->GetLogicalPosition( $dc );
295             my( $x, $y ) = ( $pos->x, $pos->y );
296              
297             if( $event->LeftUp ) {
298             push @{$this->{CURRENT_LINE}}, [ $x, $y ];
299             push @{$this->{LINES}}, $this->{CURRENT_LINE};
300             $this->ReleaseMouse();
301             } else {
302             $this->{CURRENT_LINE} = [ [ $x, $y ] ];
303             $this->CaptureMouse();
304             }
305              
306             $dc->SetPen( Wx::Pen->new( wxRED, 5, wxSOLID ) );
307             $dc->DrawLine( $x, $y, $x, $y );
308             }
309              
310             1;