File: 20_ctx_threads.t

package info (click to toggle)
libwx-perl 1%3A0.9909-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,912 kB
  • sloc: cpp: 9,728; perl: 8,182; ansic: 626; makefile: 41
file content (140 lines) | stat: -rwxr-xr-x 3,602 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/perl -w

# Create a frame
# Create and use some locally scoped contexts and GCDC's in the EVT_PAINT handler
# After this, create and use some some globally scoped contexts and GCDCs using
# the ClientDC base.
# create and join thread;

use strict;
use Config;
use if !$Config{useithreads} => 'Test::More' => skip_all => 'no threads';
use threads;
use Wx qw(:everything);
use if !Wx::wxTHREADS(), 'Test::More' => skip_all => 'No thread support';
use if !defined(&Wx::GraphicsContext::Create) => 'Test::More' => skip_all => 'no GraphicsContext';
use lib './t';
use Test::More 'tests' => 9;

package MyFrame;
use Test::More;
use Wx qw( :everything );
use base 'Wx::Frame';
use Wx::Event qw(EVT_PAINT);

my ( $context, $contextdc, $clientdc );

our $eventpaintcalled;

sub new {
  my $class = shift;
  my $self = $class->SUPER::new( undef, -1, 'Test' );
  $eventpaintcalled = 0;
  EVT_PAINT($self, \&on_paint);
  return $self;
}

sub on_draw {
  my $self = shift;

  ok(1, 'on draw called');
  
  # persistent items
  
  my $usegcdc = (Wx::wxVERSION > 2.0080075) ? 1 : 0;
  
  # Cannot CREATE graphics context from a DC outside paint event on wxMAC
  # but we can 'Create' from a Window OR from GCDC (which creates from a window)  
  
  $clientdc = Wx::ClientDC->new($self);
    
  # check out default renderer redisp
 
  $context = Wx::GraphicsRenderer::GetDefaultRenderer()->CreateContext( $self );
  # we should not really use a Wx::GCDC AND create a GraphicsContext
  $contextdc = Wx::GCDC->new($clientdc) if $usegcdc;
    
  $context->SetFont( Wx::SystemSettings::GetFont( wxSYS_SYSTEM_FONT ), wxBLACK );
  $context->DrawText('Test',20,20)  if Wx::wxMAC();

  if($usegcdc) {
      $contextdc->SetFont( Wx::SystemSettings::GetFont( wxSYS_SYSTEM_FONT ) );
      $contextdc->DrawText('Test',20,50) if !Wx::wxMAC();
  }
  
  ok($eventpaintcalled, 'event paint called before on_draw'); # ensure we fail if EVT_PAINT was not called

  can_ok('Wx::GraphicsContext', (qw( CLONE DESTROY ) ));  
    
  SKIP: {
    skip "No wxGCDC", 1 if !$usegcdc;
    can_ok('Wx::GCDC', (qw( CLONE DESTROY GetGraphicsContext) ));
  }

  SKIP: {
    skip "No SetGraphicsContext on wxMAC or No wxGCDC", 1 if( Wx::wxMAC() || (!$usegcdc));
    can_ok('Wx::GCDC', (qw( SetGraphicsContext) ));
  }

  my $t = threads->create
       ( sub {
             ok( 1, 'In thread' );
             } );
  ok( 1, 'Before join' );
  $t->join;
  ok( 1, 'After join' );
}

sub on_paint {
  my($self, $event) = @_;
  # some items to drop out of scope
  my $usegcdc = (Wx::wxVERSION > 2.0080075) ? 1 : 0;
  my $dc = Wx::PaintDC->new($self);
  
  my $gcdc = Wx::GCDC->new($dc) if $usegcdc;
  
  # if we use a Wx::GCDC - NEVER create a Graphics Context in OnPaint
  my $ctx = ( $usegcdc ) ? $gcdc->GetGraphicsContext : Wx::GraphicsContext::Create( $dc );
  
  $ctx->SetFont( Wx::SystemSettings::GetFont( wxSYS_SYSTEM_FONT ), wxBLACK );
  $ctx->DrawText('Test',20,20);
  if($usegcdc) {
    $gcdc->SetFont( Wx::SystemSettings::GetFont( wxSYS_SYSTEM_FONT ) );
    $gcdc->DrawText('Test',20,50);
  }
  $eventpaintcalled = 1;
}


package MyApp;
use Wx qw( :everything );
use base qw(Wx::App);


sub OnInit {
  my $self = shift;
  my $mwin = MyFrame->new(undef, -1);
  $self->SetTopWindow($mwin);
  $mwin->Show(1);
  return 1;
}


package main;
use Wx qw ( :everything );

my $app = MyApp->new;
my $win = $app->GetTopWindow;

my $timer = Wx::Timer->new( $win );

Wx::Event::EVT_TIMER( $win, -1, sub { $win->on_draw; wxTheApp->ExitMainLoop } );

$timer->Start( 500, 1 );

$app->MainLoop;

$win->Destroy;

END { ok(1, 'Global destruction OK'); }