File Coverage

blib/lib/Wx/DemoModules/wxDND.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/wxDND.pm
3             ## Purpose: wxPerl demo helper for Drag and Drop
4             ## Author: Mattia Barbon
5             ## Modified by:
6             ## Created: 12/09/2001
7             ## RCS-ID: $Id: wxDND.pm 2189 2007-08-21 18:15:31Z mbarbon $
8             ## Copyright: (c) 2001, 2004, 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   1746 use Wx::DND;
  0            
  0            
14              
15             package Wx::DemoModules::wxDND;
16              
17             use strict;
18             use base qw(Wx::Panel);
19              
20             use Wx qw(wxNullBitmap wxTheApp wxICON_HAND wxRED);
21              
22             my $tree;
23              
24             sub new {
25             my $class = shift;
26             my $this = $class->SUPER::new( @_ );
27              
28             # text drop target
29             Wx::StaticText->new( $this, -1, 'Drop text in listbox', [ 10, 10 ] );
30             my $droptext = Wx::ListBox->new( $this, -1, [ 10 , 40 ], [ 150, 90 ] );
31             $droptext->SetDropTarget
32             ( Wx::DemoModules::wxDND::TextDropTarget->new( $droptext ) );
33              
34             # bitmap drop target
35             Wx::StaticText->new( $this, -1, 'Drop bitmap below', [ 180, 10 ] );
36             my $window = Wx::Panel->new( $this, -1, [ 180, 40 ], [ 80, 50 ] );
37             $window->SetBackgroundColour( wxRED );
38             my $dropbitmap = Wx::StaticBitmap->new( $this, -1, wxNullBitmap,
39             [ 180, 100 ], [ 200, 200 ] );
40             $window->SetDropTarget
41             ( Wx::DemoModules::wxDND::BitmapDropTarget->new( $dropbitmap ) );
42             $dropbitmap->SetBitmap( Wx::Bitmap->new( wxTheApp->GetStdIcon( wxICON_HAND ) ) );
43              
44             # files drop target
45             Wx::StaticText->new( $this, -1, 'Drop files below', [ 10, 140 ] );
46             my $dropfiles = Wx::ListBox->new( $this, -1, [ 10, 170 ], [ 150, 50 ] );
47             $dropfiles->SetDropTarget
48             ( Wx::DemoModules::wxDND::FilesDropTarget->new( $dropfiles ) );
49              
50             # drop source
51             my $dragsource =
52             Wx::DemoModules::wxDND::DropSource->new( $this, -1, [ 10, 230 ] );
53              
54             # tree control; you can drop on items
55             Wx::StaticText->new( $this, -1, 'Drop bitmap in tree below', [ 300, 10 ] );
56             $tree = Wx::TreeCtrl->new( $this, -1, [ 300, 40 ], [ 100, 90 ] );
57             my $root = $tree->AddRoot( "Drop here" );
58             $tree->AppendItem( $root, "and here" );
59             $tree->AppendItem( $root, "and here" );
60             $tree->AppendItem( $root, "and here" );
61             $tree->Expand( $root );
62             $tree->SetDropTarget
63             ( Wx::DemoModules::wxDND::TreeDropTarget->new( $tree, $dropbitmap ) );
64              
65             # native perl data drop target
66             Wx::StaticText->new( $this, -1, 'Drop data below', [ 300, 140 ] );
67             my $dropdata = Wx::TextCtrl->new( $this, -1, '', [ 300, 170 ], [ 100, 50 ] );
68             $dropdata->SetDropTarget
69             ( Wx::DemoModules::wxDND::DataDropTarget->new( $dropdata ) );
70              
71             # drop data source
72             my $dragdatasource =
73             Wx::DemoModules::wxDND::DropDataSource->new( $this, -1, [ 300, 230 ] );
74              
75             return $this;
76             }
77              
78             sub add_to_tags { qw(dnd) }
79             sub title { 'Drag and drop' }
80              
81             package Wx::DemoModules::wxDND::TreeDropTarget;
82              
83             use strict;
84             use base qw(Wx::DropTarget);
85              
86             sub new {
87             my $class = shift;
88             my $tree = shift;
89             my $canvas = shift;
90             my $this = $class->SUPER::new;
91              
92             my $data = Wx::BitmapDataObject->new;
93             $this->SetDataObject( $data );
94             $this->{TREE} = $tree;
95             $this->{DATA} = $data;
96             $this->{CANVAS} = $canvas;
97              
98             return $this;
99             }
100              
101             sub data { $_[0]->{DATA} }
102             sub canvas { $_[0]->{CANVAS} }
103              
104             use Wx qw(:treectrl wxDragNone wxDragCopy);
105              
106             # give visual feedback: select the item we're on
107             # ( probably better forms of feedback are possible )
108             # also return the desired action to make the OS display an appropriate
109             # "can drop here" icon
110             sub OnDragOver {
111             my( $this, $x, $y, $desired ) = @_;
112             my $tree = $this->{TREE};
113              
114             my( $item, $flags ) = $tree->HitTest( [$x, $y] );
115             if( $flags & wxTREE_HITTEST_ONITEMLABEL ) {
116             $tree->SelectItem( $item );
117             return $desired;
118             } else {
119             $tree->Unselect();
120             return wxDragNone;
121             }
122             }
123              
124             sub OnData {
125             my( $this, $x, $y, $def ) = @_;
126              
127             $this->GetData;
128             $this->canvas->SetBitmap( $this->data->GetBitmap );
129              
130             return $def;
131             }
132              
133             package Wx::DemoModules::wxDND::DropSource;
134              
135             use strict;
136             use base qw(Wx::Window);
137              
138             use Wx::Event qw(EVT_LEFT_DOWN EVT_PAINT);
139              
140             use Wx::DemoModules::lib::DataObjects;
141              
142             sub new {
143             my $class = shift;
144             my $this = $class->SUPER::new( @_[0,1,2], [200,50] );
145              
146             EVT_PAINT( $this, \&OnPaint );
147             EVT_LEFT_DOWN( $this, \&OnDrag );
148              
149             return $this;
150             }
151              
152             sub OnPaint {
153             my( $this, $event ) = @_;
154             my $dc = Wx::PaintDC->new( $this );
155              
156             $dc->DrawText( "Drag text/bitmap from here", 2, 2 );
157             }
158              
159             sub OnDrag {
160             my( $this, $event ) = @_;
161              
162             my $data = get_text_bitmap_data_object();
163             my $source = Wx::DropSource->new( $this );
164             $source->SetData( $data );
165             Wx::LogMessage( "Status: %d", $source->DoDragDrop( 1 ) );
166             }
167              
168             package Wx::DemoModules::wxDND::TextDropTarget;
169              
170             use strict;
171             use base qw(Wx::TextDropTarget);
172              
173             sub new {
174             my $class = shift;
175             my $listbox = shift;
176             my $this = $class->SUPER::new( @_ );
177              
178             $this->{LISTBOX} = $listbox;
179              
180             return $this;
181             }
182              
183             sub OnDropText {
184             my( $this, $x, $y, $data ) = @_;
185              
186             $data =~ s/[\r\n]+$//;
187             Wx::LogMessage( "Dropped text: '$data'" );
188             $this->{LISTBOX}->InsertItems( [ $data ], 0 );
189              
190             return 1;
191             }
192              
193             package Wx::DemoModules::wxDND::FilesDropTarget;
194              
195             use strict;
196             use base qw(Wx::FileDropTarget);
197              
198             sub new {
199             my $class = shift;
200             my $listbox = shift;
201             my $this = $class->SUPER::new( @_ );
202              
203             $this->{LISTBOX} = $listbox;
204              
205             return $this;
206             }
207              
208             sub OnDropFiles {
209             my( $this, $x, $y, $files ) = @_;
210              
211             $this->{LISTBOX}->Clear;
212             Wx::LogMessage( "Dropped files at ($x, $y)" );
213             foreach my $i ( @$files ) {
214             $this->{LISTBOX}->Append( $i );
215             }
216              
217             return 1;
218             }
219              
220             package Wx::DemoModules::wxDND::BitmapDropTarget;
221              
222             use strict;
223             use base qw(Wx::DropTarget);
224              
225             sub new {
226             my $class = shift;
227             my $this = $class->SUPER::new;
228              
229             my $data = Wx::BitmapDataObject->new;
230             $this->SetDataObject( $data );
231             $this->{DATA} = $data;
232             $this->{CANVAS} = $_[0];
233              
234             return $this;
235             }
236              
237             sub data { $_[0]->{DATA} }
238             sub canvas { $_[0]->{CANVAS} }
239              
240             sub OnData {
241             my( $this, $x, $y, $def ) = @_;
242              
243             $this->GetData;
244             $this->canvas->SetBitmap( $this->data->GetBitmap );
245              
246             return $def;
247             }
248              
249             package Wx::DemoModules::wxDND::DropDataSource;
250              
251             use strict;
252             use base qw(Wx::Window);
253              
254             use Wx::Event qw(EVT_LEFT_DOWN EVT_PAINT);
255              
256             use Wx::DemoModules::lib::DataObjects;
257              
258             sub new {
259             my $class = shift;
260             my $this = $class->SUPER::new( @_[0,1,2], [200,50] );
261              
262             EVT_PAINT( $this, \&OnPaint );
263             EVT_LEFT_DOWN( $this, \&OnDrag );
264              
265             return $this;
266             }
267              
268             sub OnPaint {
269             my( $this, $event ) = @_;
270             my $dc = Wx::PaintDC->new( $this );
271             $dc->DrawText( "Drag perl data from here", 2, 2 );
272             }
273              
274             sub OnDrag {
275             my( $this, $event ) = @_;
276              
277             my $PerlData = { fruit => 'lemon', colour => 'yellow' };
278             my $data = get_perl_data_object( $PerlData );
279             my $source = Wx::DropSource->new( $this );
280             $source->SetData( $data );
281             Wx::LogMessage( "OnDrag Status: %d", $source->DoDragDrop( 1 ) );
282             }
283              
284             package Wx::DemoModules::wxDND::DataDropTarget;
285              
286             use strict;
287             use base qw(Wx::DropTarget);
288             use Wx::DemoModules::lib::DataObjects;
289              
290             sub new {
291             my $class = shift;
292             my $this = $class->SUPER::new;
293              
294             my $data = get_perl_data_object();
295             $this->SetDataObject( $data );
296             $this->{DATA} = $data;
297             $this->{TEXTCTRL} = $_[0];
298              
299             return $this;
300             }
301              
302             sub data { $_[0]->{DATA} }
303             sub textctrl { $_[0]->{TEXTCTRL} }
304              
305             sub OnData {
306             my( $this, $x, $y, $def ) = @_;
307              
308             Wx::LogMessage( "Dropped perl data at ($x, $y)" );
309             $this->GetData;
310             my $PerlData = $this->data->GetPerlData;
311             my $text = '';
312             foreach (keys %$PerlData) {
313             $text .= "$_ = $PerlData->{$_} ";
314             }
315             Wx::LogMessage( "( $text )" );
316              
317             $this->textctrl->SetValue( $text );
318              
319             return $def;
320             }
321              
322             1;