3 # test for client data handling in various controls
8 use Tests_Helper qw(in_frame);
9 use Wx::Event qw(EVT_BUTTON);
16 die "want a CODE reference" unless ref $code eq 'CODE';
18 return bless [ $code ], $class;
21 sub DESTROY { &{$_[0][0]} }
25 use Test::More 'tests' => 53;
28 #use base 'Wx::Frame';
31 sub tdata($) { Wx::TreeItemData->new( MyClass->new( $_[0] ) ) }
32 sub cdata($) { MyClass->new( $_[0] ) }
37 ############################################################################
39 ############################################################################
41 my $tree = Wx::TreeCtrl->new( $this, -1 );
42 my $root = $tree->AddRoot( 'Root', -1, -1,
43 Wx::TreeItemData->new( 'Frobnicate' ) );
45 my $trdata = $tree->GetItemData( $root );
46 my $data = $trdata->GetData();
47 is( $data, 'Frobnicate', "Wx::TreeItemData::GetData" );
48 $data = $trdata->GetData();
50 is( $data, 'Frobnicate', "Wx::TreeItemData::GetData (again)" );
51 $data = $tree->GetPlData( $root );
52 is( $data, 'Frobnicate', "Wx::TreeCtrl::GetPlData" );
54 $trdata = $tree->GetItemData( $root );
55 $trdata->SetData( 'Baz' );
56 $trdata = $tree->GetItemData( $root );
57 $data = $trdata->GetData();
58 is( $data, 'Baz', "Wx::TreeItemData::SetData" );
59 $tree->SetItemData( $root, Wx::TreeItemData->new( 'Boo' ) );
60 $data = $tree->GetPlData( $root );
61 is( $data, 'Boo', "Wx::TreeCtrl::SetItemData" );
62 $tree->SetPlData( $root, 'XyZ' );
63 $data = $tree->GetPlData( $root );
64 is( $data, 'XyZ', "Wx::TreeCtrl::SetPlData" );
66 # test deleting and setting again
67 my( $deleting, $setting, $ctrldelete ) = ( 0, 0, 0 );
69 my $item1 = $tree->AppendItem( $root, 'An item', -1, -1,
70 tdata sub { $deleting = 1 } );
71 my $item2 = $tree->AppendItem( $root, 'An item', -1, -1,
72 tdata sub { $setting = 1 } );
73 my $item3 = $tree->AppendItem( $root, 'An item', -1, -1,
74 tdata sub { $ctrldelete = 1 } );
76 $tree->Delete( $item1 );
77 ok( $deleting, 'WxTreeCtrl: deleting an item deletes the data' );
78 $tree->SetItemData( $item2, Wx::TreeItemData->new( 'foo' ) );
79 ok( $setting, 'Wx::TreeCtrl: setting again item data deletes old data' );
80 # and hope the tree is deleted NOW
82 ok( $ctrldelete, 'Wx::TreeCtrl: deleting the tree deletes the data' );
84 ############################################################################
86 ############################################################################
88 my $list = Wx::ListBox->new( $this, -1 );
89 my $combo = Wx::ComboBox->new( $this, -1, 'foo' );
90 my $choice = Wx::Choice->new( $this, -1 );
91 my $checklist = Wx::CheckListBox->new( $this, -1, [-1, -1], [-1, -1], [1] );
94 if( defined &Wx::PlOwnerDrawnComboBox::new ) {
95 $odncombo = Wx::PlOwnerDrawnComboBox->new( $this, -1, 'foo', [-1, -1],
99 # test deleting and setting again
100 for my $x ( [ $list, 'Wx::ListBox' ],
101 [ $choice, 'Wx::Choice' ],
102 [ $combo, 'Wx::ComboBox' ],
103 [ $checklist, 'Wx::CheckListBox' ],
104 [ $odncombo, 'Wx::OwnerDrawnComboBox' ],
107 my( $list, $name ) = @$x;
108 ( $deleting, $setting, $ctrldelete ) = ( 0, 0, 0 );
110 skip( $x->[1] . ": not available", 8 )
112 skip( "wxMSW wxCheckListBox can't store client data yet", 8 )
113 if Wx::wxMSW && $name eq 'Wx::CheckListBox';
117 # diag "starting tests for $name";
120 $list->Append( 'An item', $data );
121 $list->SetClientData( 0, $data ); # workaround bug in HEAD
122 $list->Append( 'An item' );
124 $data = 'Frobnication';
126 is( $list->GetClientData( 0 ), 'Foo', "$name: some client data" );
127 is( $list->GetClientData( 1 ), undef, "$name: no client data" );
128 $list->SetClientData( 0, 'Bar' );
129 $list->SetClientData( 1, 'Baz' );
130 is( $list->GetClientData( 0 ), 'Bar', "$name: setting client data" );
131 is( $list->GetClientData( 1 ), 'Baz',
132 "$name: setting client data (again)" );
135 $list->SetClientData( 0, \$x );
137 is( ${$list->GetClientData( 0 )}, 2,
138 "$name: client data is a reference" );
140 $list->Append( 'An item', cdata sub { $setting = 1 } );
141 $list->Append( 'An item', cdata sub { $ctrldelete = 1 } );
142 $list->Append( 'An item', cdata sub { $deleting = 1 } );
145 skip "delayed on Mac", 1 if Wx::wxMAC && $list->isa( 'Wx::ListBox' );
147 ok( $deleting, "$name: deleting an item deletes the data" );
149 $list->SetClientData( 2, 'foo' );
150 ok( $setting, "$name: setting again item data deletes old data" );
152 # and hope the control is deleted NOW
154 skip "delayed on Mac/MSW", 1 if ( Wx::wxMAC || Wx::wxMSW ) && $list->isa( 'Wx::ListBox' );
155 ok( $ctrldelete, "$name: deleting the control deletes the data" );
160 ############################################################################
162 ############################################################################
164 my $listctrl = Wx::ListCtrl->new( $this, -1, [-1, -1], [-1, -1],
166 $listctrl->InsertColumn( 1, "Type" );
168 $listctrl->InsertStringItem( 0, 'text0' );
169 $listctrl->InsertStringItem( 1, 'text1' );
170 $listctrl->InsertStringItem( 2, 'text2' );
172 $listctrl->SetItemData( 0, 123 );
173 $listctrl->SetItemData( 1, 456 );
174 $listctrl->SetItemData( 2, 789 );
176 is( $listctrl->GetItemData( 0 ), 123, "Wx::ListCtrl first item data" );
177 is( $listctrl->GetItemData( 1 ), 456, "Wx::ListCtrl second item data" );
178 is( $listctrl->GetItemData( 2 ), 789, "Wx::ListCtrl third item data" );
180 $listctrl->SetItemData( 1, 135 );
182 is( $listctrl->GetItemData( 1 ), 135, "Wx::ListCtrl, changing item data" );