Debian Codegraph @

t/listbox.t

raw
1 #!/usr/bin/perl -w
2 # -*- perl -*-
3
4 # This file is the translation of a Tcl script to test out the "listbox"
5 # command of Tk. It is organized in the standard fashion for Tcl tests.
6 #
7 # Copyright (c) 1993-1994 The Regents of the University of California.
8 # Copyright (c) 1994-1997 Sun Microsystems, Inc.
9 # Copyright (c) 1998-1999 by Scriptics Corporation.
10 # All rights reserved.
11 #
12 # RCS: @(#) $Id: listbox.t,v 1.2 2002/04/17 21:06:12 eserte Exp $
13 #
14 # Translated to perl by Slaven Rezic
15 #
16
17 use strict;
18 use vars qw($Listbox);
19
20 use Tk;
21 use Tk::Config ();
22 my $Xft = $Tk::Config::xlib =~ /-lXft\b/;
23
24 use FindBin;
25 use lib "$FindBin::RealBin";
26 use TkTest qw(is_float wm_info set_have_fixed_font with_fixed_font);
27
28 use Getopt::Long;
29
30 my $v;
31 my $visual;
32
33 BEGIN {
34 $Listbox = "Listbox";
35 #$Listbox = "TextList";
36 GetOptions("listboxclass=s" => \$Listbox,
37 "v" => \$v,
38 "visual" => \$visual,
39 )
40 or die "usage: $0 [-listboxclass baseclass] [-v] [-visual]";
41
42 eval "use Tk::$Listbox";
43 }
44
45 BEGIN {
46 if (!eval q{
47 use Test::More;
48 1;
49 }) {
50 print "1..0 # skip: no Test::More module\n";
51 exit;
52 }
53 }
54
55 plan tests => 537;
56
57 my $partial_top;
58 my $partial_lb;
59
60 my $listvar_impl = $Tk::VERSION >= 804;
61
62 my $mw = new MainWindow;
63 $mw->geometry('+10+10');
64 $mw->raise;
65 ## Always use the X11 font, even with Xft, otherwise the measurements would
66 ## be wrong.
67 #my $fixed = $Xft ? '{Adobe Courier} -12' : 'Courier -12';
68 my $fixed = "-adobe-courier-medium-r-normal--12-120-75-75-m-*-iso8859-1";
69 ok(Tk::Exists($mw));
70
71 my %wm_info = wm_info($mw);
72 my $wm_name = $wm_info{name};
73
74 my $kwin_problems = defined $wm_name && $wm_name eq 'KWin';
75 my $fluxbox_problems = defined $wm_name && $wm_name eq 'Fluxbox';
76 #my $metacity_problems = defined $wm_name && $wm_name eq 'Metacity';
77 #my $xfwm4_problems = defined $wm_name && $wm_name eq 'Xfwm4';
78
79 # This is probably the same problem as in t/entry.t
80 sub TODO_xscrollcommand_problem (&) {
81 my $code = shift;
82 local $TODO;
83 #$TODO = "May fail under some conditions (another grab?) on KDE" if !$TODO && $kwin_problems;
84 #$TODO = "May fail under some conditions (another grab?) on Metacity" if !$TODO && $metacity_problems;
85 #$TODO = "May fail under some conditions (another grab?) on Fluxbox" if !$TODO && $fluxbox_problems;
86 #$TODO = "May fail under some conditions (another grab?) on xfwm4" if !$TODO && $xfwm4_problems;
87 $TODO = "May fail under some conditions" if $Tk::platform eq 'unix'; # may happen even on fvwm on heavy load
88 local $Test::Builder::Level = $Test::Builder::Level + 2;
89 $code->();
90 }
91
92 sub TODO_fluxbox_problem (&) {
93 my $code = shift;
94 local $TODO = $TODO;
95 $TODO = "May fail under some conditions on Fluxbox" if !$TODO && $fluxbox_problems;
96 local $Test::Builder::Level = $Test::Builder::Level + 2;
97 $code->();
98 }
99
100 sub TODO_kwin_problem (&) {
101 my $code = shift;
102 local $TODO = $TODO;
103 $TODO = "May fail under some conditions on kwin" if !$TODO && $kwin_problems;
104 local $Test::Builder::Level = $Test::Builder::Level + 2;
105 $code->();
106 }
107
108 # Create entries in the option database to be sure that geometry options
109 # like border width have predictable values.
110 $mw->optionAdd("*Toplevel.borderWidth",0);
111 $mw->optionAdd("*$Listbox.borderWidth",2);
112 $mw->optionAdd("*$Listbox.highlightThickness",2);
113 ## Again, prefer the X11 font.
114 #$mw->optionAdd("*$Listbox.font",
115 # $Xft ? '{Adobe Helvetica} -12 bold' :'Helvetica -12 bold');
116 $mw->optionAdd("*$Listbox.font", 'Helvetica -12 bold');
117
118 my $lb = $mw->$Listbox->pack;
119 ok(Tk::Exists($lb), "Listbox exists");
120 isa_ok($lb, "Tk::$Listbox");
121 $lb->update;
122
123 my $skip_font_test;
124 {
125 my %fa = ($mw->fontActual($lb->cget(-font)),
126 $mw->fontMetrics($lb->cget(-font)));
127 my %expected = (
128 "-weight" => "bold",
129 "-underline" => 0,
130 "-family" => "helvetica",
131 "-slant" => "roman",
132 "-size" => -12,
133 "-overstrike" => 0,
134 "-ascent" => 11,
135 "-descent" => 3,
136 "-linespace" => 14,
137 "-fixed" => 0,
138 );
139 while(my($key,$val) = each %expected) {
140 if (lc $val ne lc $fa{$key}) {
141 diag "Value $key does not match: got $fa{$key}, expected $val\n" if $v;
142 $skip_font_test = "font-related tests (proportional font not std helvetica)";
143 last;
144 }
145 }
146 }
147
148 {
149 my $fixed_lb = $mw->$Listbox(-font => $fixed);
150 my %fa = ($mw->fontActual($fixed_lb->cget(-font)),
151 $mw->fontMetrics($fixed_lb->cget(-font)));
152 my %expected = (
153 "-weight" => "normal",
154 "-underline" => 0,
155 "-family" => "courier",
156 "-slant" => "roman",
157 "-size" => -12,
158 "-overstrike" => 0,
159 "-ascent" => 10,
160 "-descent" => 3,
161 "-linespace" => 13,
162 "-fixed" => 1,
163 );
164 my $have_fixed_font = 1;
165 while(my($key,$val) = each %expected) {
166 if (lc $val ne lc $fa{$key}) {
167 diag "Value $key does not match: got $fa{$key}, expected $val\n" if $v;
168 $have_fixed_font = 0;
169 last;
170 }
171 }
172 set_have_fixed_font($have_fixed_font);
173 $fixed_lb->destroy;
174 }
175
176 resetGridInfo();
177
178 $mw->Photo("testimage", -file => Tk->findINC("Xcamel.gif"));
179
180 use constant SKIP_CGET => 5;
181 use constant SKIP_CONF => 6;
182
183 my $testVariable;
184
185 foreach my $test
186 (
187 ['-activestyle', 'under', 'underline', 'foo',
188 'bad activestyle "foo": must be dotbox, none, or underline'],
189 ['-background', '#ff0000', '#ff0000', 'non-existent',
190 'unknown color name "non-existent"'],
191 [qw{-bd 4 4 badValue}, q{bad screen distance "badValue"}],
192 ['-bg', '#ff0000', '#ff0000', 'non-existent',
193 'unknown color name "non-existent"'],
194 [qw{-borderwidth 1.3 1 badValue}, q{bad screen distance "badValue"}],
195 [qw{-cursor arrow arrow badValue}, q{bad cursor spec "badValue"}],
196 [qw{-exportselection yes 1}, ""], # Perl/Tk thinks different about booleans: "xyzzy", q{expected boolean value but got "xyzzy"}],
197 ['-fg', '#110022', '#110022', 'bogus', q{unknown color name "bogus"}],
198 ['-font', 'Helvetica 12', 'Helvetica 12', '', "font \"\" doesn't exist", 1, 1],
199 ['-foreground', '#110022', '#110022', 'bogus',
200 q{unknown color name "bogus"}],
201 [qw{-height 30 30 20p}, "'20p' isn't numeric"], # in Tcl/Tk nowadays: expected integer but got "20p"
202 ['-highlightbackground', '#112233', '#112233', 'ugly',
203 q{unknown color name "ugly"}],
204 ['-highlightcolor', '#123456', '#123456', 'bogus',
205 q{unknown color name "bogus"}],
206 [qw{-highlightthickness 6 6 bogus}, q{bad screen distance "bogus"}],
207 [qw{-highlightthickness -2 0}, '', ''],
208 ['-offset', '1,1', '1,1', 'wrongside',
209 'bad offset "wrongside": expected "x,y", n, ne, e, se, s, sw, w, nw, or center'],
210 [qw{-relief groove groove 1.5},
211 ($Listbox eq 'TextList'
212 ? q{bad relief type "1.5": must be flat, groove, raised, ridge, solid, or sunken}
213 : ($Tk::VERSION < 803
214 ? q{bad relief type "1.5": must be flat, groove, raised, ridge, solid, or sunken}
215 : q{bad relief "1.5": must be flat, groove, raised, ridge, solid, or sunken})
216 )],
217 ['-selectbackground', '#110022', '#110022', 'bogus',
218 q{unknown color name "bogus"}],
219 [qw{-selectborderwidth 1.3 1 badValue},
220 q{bad screen distance "badValue"}],
221 ['-selectforeground', '#654321', '#654321', 'bogus',
222 q{unknown color name "bogus"}],
223 [qw{-selectmode string string}, '', ''],
224 [qw{-setgrid false 0}, "", "lousy",
225 q{expected boolean value but got "lousy"}],
226 ['-state', 'disabled', 'disabled', 'foo',
227 ($Listbox eq 'TextList'
228 ? q{bad state value "foo": must be normal or disabled}
229 : 'bad state "foo": must be disabled, or normal'
230 )],
231 ['-takefocus', "any string", "any string", '', ''],
232 ['-tile', 'testimage', 'testimage', 'non-existant',
233 'image "non-existant" doesn\'t exist'],
234 [qw{-width 45 45 3p}, "'3p' isn't numeric"], # In Tcl/Tk nowadays q{expected integer but got "3p"}
235 [qw(-xscrollcommand), q{Some command}, q{Some command}, '', undef, 1, 1],
236 [qw(-yscrollcommand), q{Another command}, q{Another command}, '', undef, 1, 1],
237 [qw{-listvar}, \$testVariable, \$testVariable, '', undef],
238 ) {
239 my $name = $test->[0];
240
241 SKIP: {
242 skip("$name test not supported for $Listbox", 4)
243 if ($Listbox eq 'TextList' &&
244 $name =~ /^-(activestyle|bg|fg|foreground|height|selectborderwidth|listvar)$/);
245
246 skip("$name not implemented on $Tk::VERSION", 4)
247 if ($Listbox eq 'Listbox' && $Tk::VERSION < 804 &&
248 $name =~ /^-(activestyle)$/);
249
250 skip("*TODO* $name not yet implemented on $Tk::VERSION", 4)
251 if ($Tk::VERSION >= 804 &&
252 $name =~ /^-(tile|offset)$/);
253
254 $lb->configure($name, $test->[1]);
255 pass("configure set for $name");
256 SKIP: {
257 skip("configure test for $name", 1) if $test->[SKIP_CONF];
258 is(($lb->configure($name))[4],$test->[2], "configuration option $name");
259 }
260 SKIP: {
261 skip("cget test for $name", 1) if $test->[SKIP_CGET];
262 is($lb->cget($name), $test->[2], "cget call with $name");
263 }
264 SKIP: {
265 skip("error test for $name", 1) if $test->[3] eq "";
266 eval {
267 $lb->configure($name, $test->[3]);
268 };
269 like($@,qr/$test->[4]/,"error message for $name");
270 }
271
272 $lb->configure($name, ($lb->configure($name))[3]);
273 }
274 }
275
276 SKIP: {
277 skip("only for Listbox, not for $Listbox", 1)
278 if ($Listbox ne 'Listbox');
279
280 eval { Tk::listbox() };
281 like($@,qr/Usage \$widget->listbox(...)/, "error message");
282 }
283
284 {
285 eval {
286 $lb->destroy;
287 $lb = $mw->$Listbox;
288 };
289 ok(Tk::Exists($lb));
290 is($lb->class, "$Listbox", "Tk class $Listbox");
291 }
292
293 {
294 eval {
295 $lb->destroy;
296 $lb = $mw->$Listbox(-gorp => "foo");
297 };
298 like($@,
299 ($Tk::VERSION < 803)
300 ? qr/Bad option \`-gorp\'/
301 : qr/unknown option \"-gorp\"/,
302 "error message");
303 }
304
305 ok(!Tk::Exists($lb));
306
307 $lb = $mw->$Listbox(-width => 20, -height => 5, -bd => 4,
308 -highlightthickness => 1,
309 -selectborderwidth => 2)->pack;
310 $lb->insert(0,
311 'el0','el1','el2','el3','el4','el5','el6','el7','el8','el9','el10',
312 'el11','el12','el13','el14','el15','el16','el17');
313 $lb->update;
314 eval { $lb->activate };
315 like($@,qr/wrong \# args: should be "\.listbox.* activate index"/,
316 "Listbox activate error message");
317
318 eval { $lb->activate("fooey") };
319 like($@,qr/bad listbox index "fooey": must be active, anchor, end, \@x,y, or a number/);
320
321 $lb->activate(3);
322 is($lb->index("active"), 3, "Listbox activate");
323
324 $lb->activate(-1);
325 is($lb->index("active"), 0);
326
327 $lb->activate(30);
328 is($lb->index("active"), 17);
329
330 $lb->activate("end");
331 is($lb->index("active"), 17);
332
333 eval { $lb->bbox };
334 like($@, qr/wrong \# args: should be "\.listbox.* bbox index"/,
335 "Listbox bbox error message");
336
337 eval { $lb->bbox(qw/a b/) };
338 like($@, qr/wrong \# args: should be "\.listbox.* bbox index"/);
339
340 eval { $lb->bbox("fooey") };
341 like($@,qr/bad listbox index "fooey": must be active, anchor, end, \@x,y, or a number/);
342
343 $lb->yview(3);
344 $lb->update;
345 is($lb->bbox(2), undef, "Listbox bbox");
346 is($lb->bbox(8), undef);
347
348 # Used to generate a core dump before a bug was fixed (the last
349 # element would be on-screen if it existed, but it doesn't exist).
350 eval {
351 my $l2 = $mw->$Listbox;
352 $l2->pack(-side => "top");
353 $l2->waitVisibility;
354 my $x = $l2->bbox(0);
355 $l2->destroy;
356 };
357 is($@, '', "No core dump with bbox");
358
359 $lb->yview(3);
360 $lb->update;
361 SKIP: {
362 skip($skip_font_test, 2) if $skip_font_test;
363 is_deeply([$lb->bbox(3)], [qw(7 7 17 14)]);
364 is_deeply([$lb->bbox(4)], [qw(7 26 17 14)]);
365 }
366
367 $lb->yview(0);
368 $lb->update;
369 is($lb->bbox(-1), undef);
370 SKIP: {
371 skip($skip_font_test, 1) if $skip_font_test;
372 is_deeply([$lb->bbox(0)], [qw(7 7 17 14)]);
373 }
374
375 $lb->yview("end");
376 $lb->update;
377 SKIP: {
378 skip($skip_font_test, 2) if $skip_font_test;
379 is_deeply([$lb->bbox(17)], [qw(7 83 24 14)]);
380 is_deeply([$lb->bbox("end")], [qw(7 83 24 14)]);
381 }
382 is($lb->bbox(18), undef);
383
384 {
385 my $t = $mw->Toplevel;
386 $t->geometry("+0+0");
387 my $lb = $t->$Listbox(-width => 10,
388 -height => 5);
389 $lb->insert(0, "Short", "Somewhat longer",
390 "Really, quite a whole lot longer than can possibly fit on the screen",
391 "Short");
392 $lb->pack;
393 $lb->update;
394 $lb->xview(moveto => 0.2);
395 SKIP: {
396 skip($skip_font_test, 1) if $skip_font_test;
397 is_deeply([$lb->bbox(2)], [qw(-72 39 393 14)]);
398 $t->destroy;
399 }
400 }
401
402 mkPartial();
403 SKIP: {
404 skip($skip_font_test, 2) if $skip_font_test;
405 is_deeply([$partial_lb->bbox(3)], [qw(5 56 24 14)]);
406 is_deeply([$partial_lb->bbox(4)], [qw(5 73 23 14)]);
407 }
408
409 eval { $lb->cget };
410 like($@,qr/wrong \# args: should be \"\.listbox.* cget option\"/,
411 "Listbox cget message");
412
413 eval { $lb->cget(qw/a b/) };
414 like($@,qr/wrong \# args: should be \"\.listbox.* cget option\"/);
415
416 eval { $lb->cget(-gorp) };
417 like($@,qr/unknown option "-gorp"/);
418
419 is($lb->cget(-setgrid), 0);
420 # XXX why 25 in Tk800? probably because of less configuration options!
421 is(scalar @{[$lb->configure]}, ($Tk::VERSION < 803 ? 25 : 27), "Listbox configure");
422 is_deeply([$lb->configure(-setgrid)],
423 [qw(-setgrid setGrid SetGrid 0 0)]);
424 eval { $lb->configure(-gorp) };
425 like($@,qr/unknown option "-gorp"/);
426
427 {
428 my $oldbd = $lb->cget(-bd);
429 my $oldht = $lb->cget(-highlightthickness);
430 $lb->configure(-bd => 3, -highlightthickness => 0);
431 is($lb->cget(-bd), 3);
432 is($lb->cget(-highlightthickness), 0);
433 $lb->configure(-bd => $oldbd);
434 $lb->configure(-highlightthickness => $oldht);
435 }
436
437 eval { $lb->curselection("a") };
438 like($@,qr/wrong \# args: should be \"\.listbox.* curselection\"/,
439 "Listbox curselection error message");
440
441 $lb->selection("clear", 0, "end");
442 $lb->selection("set", 3, 6);
443 $lb->selection("set", 9);
444 is_deeply([$lb->curselection], [qw(3 4 5 6 9)],
445 "Listbox curselection");
446
447 # alternative perl/Tk methods
448 $lb->selectionClear(0, "end");
449 $lb->selectionSet(3, 6);
450 $lb->selectionSet(9);
451 is_deeply([$lb->curselection], [qw(3 4 5 6 9)]);
452
453 eval { $lb->delete };
454 like($@,qr/wrong \# args: should be \"\.listbox.* delete firstIndex \?lastIndex\?\"/,
455 "Listbox delete error message");
456
457 eval { $lb->delete(qw/a b c/) };
458 like($@,qr/wrong \# args: should be \"\.listbox.* delete firstIndex \?lastIndex\?\"/);
459
460 eval { $lb->delete("badindex") };
461 like($@,qr/bad listbox index "badindex": must be active, anchor, end, \@x,y, or a number/);
462
463 eval { $lb->delete(2, "123ab") };
464 like($@,qr/bad listbox index "123ab": must be active, anchor, end, \@x,y, or a number/);
465
466 {
467 my $l2 = $mw->$Listbox;
468 $l2->insert(0, qw(el0 el1 el2 el3 el4 el5 el6 el7));
469 $l2->delete(3);
470 is($l2->get(2), "el2", "Listbox delete element");
471 is($l2->get(3), "el4");
472 is($l2->index("end"), "7");
473 $l2->destroy;
474 }
475
476 {
477 my $l2 = $mw->$Listbox;
478 $l2->insert(0, qw(el0 el1 el2 el3 el4 el5 el6 el7));
479 $l2->delete(2, 4);
480 is($l2->get(1), "el1");
481 is($l2->get(2), "el5");
482 is($l2->index("end"), "5");
483 $l2->destroy;
484 }
485
486 {
487 my $l2 = $mw->$Listbox;
488 $l2->insert(0, qw(el0 el1 el2 el3 el4 el5 el6 el7));
489 $l2->delete(-3, 2);
490 is_deeply([$l2->get(0, "end")], [qw(el3 el4 el5 el6 el7)]);
491 $l2->destroy;
492 }
493
494 {
495 my $l2 = $mw->$Listbox;
496 $l2->insert(0, qw(el0 el1 el2 el3 el4 el5 el6 el7));
497 $l2->delete(-3, -1);
498 is_deeply([$l2->get(0, "end")], [map { "el$_" } (0 .. 7)]);
499 is(scalar @{[$l2->get(0, "end")]}, 8);
500 $l2->destroy;
501 }
502
503 {
504 my $l2 = $mw->$Listbox;
505 $l2->insert(0, qw(el0 el1 el2 el3 el4 el5 el6 el7));
506 $l2->delete(2, "end");
507 is_deeply([$l2->get(0, "end")], [qw(el0 el1)]);
508 is(scalar @{[$l2->get(0, "end")]}, 2);
509 $l2->destroy;
510 }
511
512 {
513 my $l2 = $mw->$Listbox;
514 $l2->insert(0, qw(el0 el1 el2 el3 el4 el5 el6 el7));
515 $l2->delete(5, 20);
516 is_deeply([$l2->get(0, "end")], [qw(el0 el1 el2 el3 el4)]);
517 is(scalar @{[$l2->get(0, "end")]}, 5);
518 $l2->destroy;
519 }
520
521 {
522 my $l2 = $mw->$Listbox;
523 $l2->insert(0, qw(el0 el1 el2 el3 el4 el5 el6 el7));
524 $l2->delete("end", 20);
525 is_deeply([$l2->get(0, "end")], [qw(el0 el1 el2 el3 el4 el5 el6)]);
526 is(scalar @{[$l2->get(0, "end")]}, 7);
527 $l2->destroy;
528 }
529
530 {
531 my $l2 = $mw->$Listbox;
532 $l2->insert(0, qw(el0 el1 el2 el3 el4 el5 el6 el7));
533 $l2->delete(8, 20);
534 is_deeply([$l2->get(0, "end")], [qw(el0 el1 el2 el3 el4 el5 el6 el7)]);
535 is(scalar @{[$l2->get(0, "end")]}, 8);
536 $l2->destroy;
537 }
538
539 eval { $lb->get };
540 like($@, $Tk::VERSION < 803
541 ? qr/wrong \# args: should be \"\.listbox.* get first \?last\?\"/
542 : qr/wrong \# args: should be \"\.listbox.* get firstIndex \?lastIndex\?\"/,
543 "Listbox get error message");
544
545 eval { $lb->get(qw/a b c/) };
546 like($@, $Tk::VERSION < 803
547 ? qr/wrong \# args: should be \"\.listbox.* get first \?last\?\"/
548 : qr/wrong \# args: should be \"\.listbox.* get firstIndex \?lastIndex\?\"/);
549
550 eval { $lb->get("badindex") };
551 like($@ ,qr/bad listbox index "badindex": must be active, anchor, end, \@x,y, or a number/);
552
553 eval { $lb->get("end", "bogus") };
554 like($@ ,qr/bad listbox index "bogus": must be active, anchor, end, \@x,y, or a number/);
555
556 {
557 my $l2 = $mw->$Listbox;
558 $l2->insert(0, qw(el0 el1 el2 el3 el4 el5 el6 el7));
559 is($l2->get(0), "el0");
560 is($l2->get(3), "el3");
561 # This is valid in perl/Tk, but not Tcl/Tk
562 is($lb->get("3.4"), "el3", "get with float");
563 is($l2->get("end"), "el7");
564 $l2->destroy;
565 }
566
567 {
568 my $l2 = $mw->$Listbox;
569 is($l2->get(0), undef);
570 is($l2->get("end"), undef);
571 $l2->destroy;
572 }
573
574 {
575 my $l2 = $mw->$Listbox;
576 $l2->insert(0, qw(el0 el1 el2), "two words", qw(el4 el5 el6 el7));
577 is($l2->get(3), "two words");
578 is(($l2->get(3, "end"))[0], "two words");
579 is_deeply([$l2->get(3, "end")], ['two words', qw(el4 el5 el6 el7)]);
580 }
581
582 is($lb->get(-1), undef);
583 is($lb->get(-2, -1), undef);
584 is_deeply([$lb->get(-2, 3)], [qw(el0 el1 el2 el3)]);
585 is(scalar @{[ $lb->get(-2, 3) ]}, 4);
586
587 is_deeply([$lb->get(12, "end")], [qw(el12 el13 el14 el15 el16 el17)]);
588 is(scalar @{[ $lb->get(12, "end") ]}, 6);
589 is_deeply([$lb->get(12, 20)], [qw(el12 el13 el14 el15 el16 el17)]);
590 is(scalar @{[ $lb->get(12, 20) ]}, 6);
591
592 is($lb->get("end"), "el17");
593 is($lb->get(30), undef);
594 is_deeply([$lb->get(30, 35)], []);
595
596 eval { $lb->index };
597 like($@ ,qr/wrong \# args: should be \"\.listbox.* index index\"/,
598 "Listbox index error message");
599
600 eval { $lb->index(qw/a b/) };
601 like($@ ,qr/wrong \# args: should be \"\.listbox.* index index\"/);
602
603 eval { $lb->index(qw/@/) };
604 like($@ ,qr/bad listbox index "\@": must be active, anchor, end, \@x,y, or a number/);
605
606 is($lb->index(2), 2);
607 is($lb->index(-1), -1);
608 is($lb->index("end"), 18);
609 is($lb->index(34), 34);
610
611 eval { $lb->insert };
612 like($@ ,qr/wrong \# args: should be \"\.listbox.* insert index \?element element \.\.\.\?\"/,
613 "Listbox insert error message");
614
615 eval { $lb->insert("badindex") };
616 like($@ ,qr/bad listbox index "badindex": must be active, anchor, end, \@x,y, or a number/);
617
618 {
619 my $l2 = $mw->$Listbox;
620 $l2->insert("end", qw/a b c d e/);
621 $l2->insert(3, qw/x y z/);
622 is_deeply([$l2->get(0, "end")], [qw(a b c x y z d e)], "Listbox insert");
623 is(scalar @{[ $l2->get(0, "end") ]}, 8);
624 $l2->destroy;
625 }
626
627 {
628 my $l2 = $mw->$Listbox;
629 $l2->insert("end", qw/a b c/);
630 $l2->insert(-1, qw/x/);
631 is_deeply([$l2->get(0, "end")], [qw(x a b c)]);
632 is(scalar @{[ $l2->get(0, "end") ]}, 4);
633 $l2->destroy;
634 }
635
636 {
637 my $l2 = $mw->$Listbox;
638 $l2->insert("end", qw/a b c/);
639 $l2->insert("end", qw/x/);
640 is_deeply([$l2->get(0, "end")], [qw(a b c x)]);
641 is(scalar @{[ $l2->get(0, "end") ]}, 4);
642 $l2->destroy;
643 }
644
645 {
646 my $l2 = $mw->$Listbox;
647 $l2->insert("end", qw/a b c/);
648 $l2->insert(43, qw/x/);
649 is_deeply([$l2->get(0, "end")], [qw(a b c x)]);
650 is(scalar @{[ $l2->get(0, "end") ]}, 4);
651 $l2->insert(4, qw/y/);
652 is_deeply([$l2->get(0, "end")], [qw(a b c x y)]);
653 $l2->insert(6, qw/z/);
654 is_deeply([$l2->get(0, "end")], [qw(a b c x y z)]);
655 $l2->destroy;
656 }
657
658 SKIP: {
659 skip("nearest not yet implemented", 4)
660 if $Listbox eq 'TextList';
661
662 eval { $lb->nearest };
663 like($@ ,qr/wrong \# args: should be \"\.listbox.* nearest y\"/,
664 "Listbox nearest error message");
665
666 eval { $lb->nearest(qw/a b/) };
667 like($@ ,qr/wrong \# args: should be \"\.listbox.* nearest y\"/);
668
669 eval { $lb->nearest("badindex") };
670 like($@ ,qr/\'badindex\' isn\'t numeric/);
671
672 $lb->yview(3);
673 is($lb->nearest(1000), 7, "Listbox nearest");
674 }
675
676 eval { $lb->scan };
677 like($@,qr/wrong \# args: should be \"\.listbox.* scan mark\|dragto x y\"/,
678 "Listbox scan error message");
679
680 eval { $lb->scan(qw/a b/) };
681 like($@,qr/wrong \# args: should be \"\.listbox.* scan mark\|dragto x y\"/);
682
683 eval { $lb->scan(qw/a b c d/) };
684 like($@,qr/wrong \# args: should be \"\.listbox.* scan mark\|dragto x y\"/);
685
686 eval { $lb->scan(qw/foo bogus 2/) };
687 like($@ ,qr/\'bogus\' isn\'t numeric/);
688
689 eval { $lb->scan(qw/foo 2 3/) };
690 like($@, $Tk::VERSION < 803
691 ? qr/bad scan option \"foo\": must be mark or dragto/
692 : qr/bad option \"foo\": must be mark, or dragto/);
693
694 {
695 my $t = $mw->Toplevel;
696 $t->geometry("+0+0");
697 my $lb = $t->$Listbox(-width => 10, -height => 5);
698 $lb->insert(0, "Short", "Somewhat longer",
699 "Really, quite a whole lot longer than can possibly fit on the screen", "Short",
700 qw/a b c d e f g h i j/);
701 $lb->pack;
702 $lb->update;
703 $lb->scan("mark", 100, 140);
704 $lb->scan("dragto", 90, 137);
705 $lb->scan("dragto", "90.1", "137.1"); # ok in Perl/Tk, an error in Tcl/Tk
706 $lb->update;
707 SKIP: {
708 skip($skip_font_test, 2) if $skip_font_test;
709 like(join(",",$lb->xview), qr/^0\.24936.*,0\.42748.*$/, "Listbox scan");
710 like(join(",",$lb->yview), qr/^0\.071428.*,0\.428571.*$/);
711 }
712 $t->destroy;
713 }
714
715 eval { $lb->see };
716 like($@ ,qr/wrong \# args: should be \"\.listbox.* see index\"/,
717 "Listbox see error message");
718
719 eval { $lb->see("a","b") };
720 like($@ ,qr/wrong \# args: should be \"\.listbox.* see index\"/);
721
722 eval { $lb->see("badindex") };
723 like($@ ,qr/bad listbox index "badindex": must be active, anchor, end, \@x,y, or a number/);
724
725 $lb->yview(7);
726 $lb->see(7);
727 is($lb->index('@0,0'), 7, "Listbox see");
728
729 $lb->yview(7);
730 $lb->see(11);
731 is($lb->index('@0,0'), 7);
732
733 $lb->yview(7);
734 $lb->see(6);
735 is($lb->index('@0,0'), 6);
736
737 $lb->yview(7);
738 $lb->see(5);
739 is($lb->index('@0,0'), 3);
740
741 $lb->yview(7);
742 $lb->see(12);
743 is($lb->index('@0,0'), 8);
744
745 $lb->yview(7);
746 $lb->see(13);
747 is($lb->index('@0,0'), 11);
748
749 $lb->yview(7);
750 $lb->see(-1);
751 is($lb->index('@0,0'), 0);
752
753 $lb->yview(7);
754 $lb->see("end");
755 is($lb->index('@0,0'), 13);
756
757 $lb->yview(7);
758 $lb->see(322);
759 is($lb->index('@0,0'), 13);
760
761 mkPartial();
762 $partial_lb->see(4);
763 TODO_fluxbox_problem {
764 TODO_kwin_problem {
765 is($partial_lb->index('@0,0'), 1);
766 };
767 };
768
769 eval { $lb->selection };
770 like($@ ,qr/wrong \# args: should be \"\.listbox.* selection option index \?index\?\"/,
771 "Listbox selection error message");
772
773 eval { $lb->selection("a") };
774 like($@ ,qr/wrong \# args: should be \"\.listbox.* selection option index \?index\?\"/);
775
776 eval { $lb->selection(qw/a b c d/) };
777 like($@ ,qr/wrong \# args: should be \"\.listbox.* selection option index \?index\?\"/);
778
779 eval { $lb->selection(qw/a bogus/) };
780 like($@ ,qr/bad listbox index \"bogus\": must be active, anchor, end, \@x,y, or a number/);
781
782 eval { $lb->selection(qw/a 0 lousy/) };
783 like($@ ,qr/bad listbox index \"lousy\": must be active, anchor, end, \@x,y, or a number/);
784
785 eval { $lb->selection(qw/anchor 0 0/) };
786 like($@ ,qr/wrong \# args: should be \"\.listbox.* selection anchor index\"/);
787
788 SKIP: {
789 skip("anchor index not yet implemented", 5)
790 if $Listbox eq 'TextList';
791
792 $lb->selection("anchor", 5);
793 is($lb->index("anchor"), 5, "Listbox selection");
794 $lb->selectionAnchor(0);
795 is($lb->index("anchor"), 0);
796
797 $lb->selectionAnchor(-1);
798 is($lb->index("anchor"), 0);
799 $lb->selectionAnchor("end");
800 is($lb->index("anchor"), 17);
801 $lb->selectionAnchor(44);
802 is($lb->index("anchor"), 17);
803 }
804
805 $lb->selection("clear", 0, "end");
806 $lb->selection("set", 2, 8);
807 $lb->selection("clear", 3, 4);
808 is_deeply([$lb->curselection], [2,5,6,7,8]);
809
810 $lb->selectionClear(0, "end");
811 $lb->selectionSet(2, 8);
812 $lb->selectionClear(3, 4);
813 is_deeply([$lb->curselection], [2,5,6,7,8]);
814
815 eval { $lb->selection(qw/includes 0 0/) };
816 like($@ ,qr/wrong \# args: should be \"\.listbox.* selection includes index\"/,
817 "Tk selection includes error message");
818
819 $lb->selectionClear(0, "end");
820 $lb->selectionSet(2,8);
821 $lb->selectionClear(4);
822 is($lb->selection("includes", 3), 1, "Listbox selection includes");
823 is($lb->selection("includes", 4), 0);
824 is($lb->selection("includes", 5), 1);
825 is($lb->selectionIncludes(3), 1);
826
827 $lb->selectionSet(0, "end");
828 is($lb->selectionIncludes(-1), 0);
829
830 $lb->selectionClear(0, "end");
831 $lb->selectionSet("end");
832 is($lb->selection("includes", "end"), 1);
833
834 $lb->selectionClear(0, "end");
835 $lb->selectionSet("end");
836 is($lb->selection("includes", 44), 0);
837
838 {
839 my $l2 = $mw->$Listbox;
840 is($l2->selectionIncludes(0), 0);
841 $l2->destroy;
842 }
843
844 $lb->selection(qw(clear 0 end));
845 $lb->selection(qw(set 2));
846 $lb->selection(qw(set 5 7));
847 is_deeply([$lb->curselection], [qw(2 5 6 7)]);
848 is(scalar @{[$lb->curselection]}, 4);
849 $lb->selection(qw(set 5 7));
850 is_deeply([$lb->curselection], [qw(2 5 6 7)]);
851 is(scalar @{[$lb->curselection]}, 4);
852
853 eval { $lb->selection(qw/badOption 0 0/) };
854 like($@, qr/bad option \"badOption\": must be anchor, clear, includes, or set/,
855 "Listbox selection error message");
856
857 eval { $lb->size(qw/a/) };
858 like($@ ,qr/wrong \# args: should be \"\.listbox.* size\"/,
859 "Listbox size error message");
860
861 is($lb->size, 18, "Listbox size");
862
863 {
864 my $l2 = $mw->$Listbox;
865 $l2->update;
866 is(($l2->xview)[0], 0, "xview, first value");
867 is(($l2->xview)[1], 1, "xview, second value");
868 $l2->destroy;
869 }
870
871 eval { $lb->destroy };
872 $lb = $mw->$Listbox(-width => 10, -height => 5, -font => $fixed);
873 $lb->insert(qw/0 a b c d e f g h i j k l m n o p q r s t/);
874 $lb->pack;
875 $lb->update;
876 is(($lb->xview)[0], 0);
877 is(($lb->xview)[1], 1);
878
879 eval { $lb->destroy };
880 $lb = $mw->$Listbox(-width => 10, -height => 5, -font => $fixed);
881 $lb->insert(qw/0 a b c d e f g h i j k l m n o p q r s t/);
882 $lb->insert(qw/1 0123456789a123456789b123456789c123456789d123456789/);
883 $lb->pack;
884 $lb->update;
885
886 SKIP: {
887 skip("xview not fully implemented", 1)
888 if $Listbox eq 'TextList';
889
890 $lb->xview(4);
891 with_fixed_font { is_float(join(",",$lb->xview), "0.08,0.28", "Listbox xview with floats") };
892 }
893
894 eval { $lb->xview("foo") };
895 like($@ ,qr/\'foo\' isn\'t numeric/,
896 "Listbox xview error message");
897
898 eval { $lb->xview("zoom", "a", "b") };
899 like($@ ,qr/unknown option \"zoom\": must be moveto or scroll/);
900
901 SKIP: {
902 skip("xview not fully implemented", 5)
903 if $Listbox eq 'TextList';
904
905 $lb->xview(0);
906 $lb->xview(moveto => 0.4);
907 $lb->update;
908 with_fixed_font { is_float(($lb->xview)[0], 0.4) };
909 with_fixed_font { is_float(($lb->xview)[1], 0.6) };
910
911 $lb->xview(0);
912 $lb->xview(scroll => 2, "units");
913 $lb->update;
914 with_fixed_font { is_float("@{[ $lb->xview ]}", '0.04 0.24') };
915
916 $lb->xview(30);
917 $lb->xview(scroll => -1, "pages");
918 $lb->update;
919 with_fixed_font { is_float("@{[ $lb->xview ]}", '0.44 0.64') };
920
921 $lb->configure(-width => 1);
922 $lb->update;
923 $lb->xview(30);
924 $lb->xview("scroll", -4, "pages");
925 $lb->update;
926 with_fixed_font { is_float("@{[ $lb->xview ]}", '0.52 0.54') };
927 }
928
929 eval { $lb->destroy };
930 $lb = $mw->$Listbox->pack;
931 $lb->update;
932 is(($lb->yview)[0], 0);
933 is(($lb->yview)[1], 1);
934
935 eval { $lb->destroy };
936 $lb = $mw->$Listbox->pack;
937 $lb->insert(0, "el1");
938 $lb->update;
939 is(($lb->yview)[0], 0);
940 is(($lb->yview)[1], 1);
941
942 eval { $lb->destroy };
943 $lb = $mw->$Listbox(-width => 10, -height => 5, -font => $fixed);
944 $lb->insert(0,'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
945 'p','q','r','s','t');
946 $lb->pack;
947 $lb->update;
948 $lb->yview(4);
949 $lb->update;
950 is_float(($lb->yview)[0], 0.2);
951 is_float(($lb->yview)[1], 0.45);
952
953 mkPartial();
954 is(($partial_lb->yview)[0], 0);
955 like(($partial_lb->yview)[1] ,qr/^0\.\d+$/,
956 "yview returned " . (($partial_lb->yview)[1]));
957
958 eval { $lb->yview("foo") };
959 like($@ ,qr/\Qbad listbox index "foo": must be active, anchor, end, \E\@\Qx,y, or a number/,
960 "Listbox yview error message");
961
962 eval { $lb->yview("foo", "a", "b") };
963 like($@ ,qr/unknown option \"foo\": must be moveto or scroll/);
964
965 $lb->yview(0);
966 $lb->yview(moveto => 0.31);
967 is_float("@{[ $lb->yview ]}", "0.3 0.55");
968
969 $lb->yview(2);
970 $lb->yview(scroll => 2 => "pages");
971 is_float("@{[ $lb->yview ]}", "0.4 0.65");
972
973 $lb->yview(10);
974 $lb->yview(scroll => -3 => "units");
975 is_float("@{[ $lb->yview ]}", "0.35 0.6");
976
977 $lb->configure(-height => 2);
978 $lb->update;
979 $lb->yview(15);
980 $lb->yview(scroll => -4 => "pages");
981 is_float("@{[ $lb->yview ]}", "0.55 0.65");
982
983 # No tests for DestroyListbox: I can't come up with anything to test
984 # in this procedure.
985
986 eval { $lb->destroy };
987 $lb = $mw->$Listbox(-setgrid => 1, -width => 25, -height => 15);
988 $lb->pack;
989 $mw->update;
990 like(getsize($mw), qr/^\d+x\d+$/);
991 $lb->configure(-setgrid => 0);
992 $mw->update;
993 like(getsize($mw), qr/^\d+x\d+$/);
994
995 resetGridInfo();
996
997 $lb->configure(-highlightthickness => -3);
998 is($lb->cget(-highlightthickness), 0);
999
1000 $lb->configure(-exportselection => 0);
1001 $lb->delete(0, "end");
1002 $lb->insert(0, qw(el0 el1 el2 el3 el4 el5 el6 el7 el8));
1003 $lb->selection("set", 3, 5);
1004 $lb->configure(-exportselection => 1);
1005 is($mw->SelectionGet, "el3\nel4\nel5");
1006
1007 my $e = $mw->Entry;
1008 $e->insert(0, "abc");
1009 $e->selection("from", 0);
1010 $e->selection("to", 2);
1011 $lb->configure(-exportselection => 0);
1012 $lb->delete(0, "end");
1013 $lb->insert(0, qw(el0 el1 el2 el3 el4 el5 el6 el7 el8));
1014 $lb->selectionSet(3, 5);
1015 $lb->selectionClear(3, 5);
1016 $lb->configure(-exportselection => 1);
1017 is($mw->SelectionOwner, $e);
1018 is($mw->SelectionGet, "ab");
1019 $e->destroy;
1020
1021 $mw->SelectionClear;
1022 $lb->configure(-exportselection => 1);
1023 $lb->delete(0, "end");
1024 $lb->insert(qw(0 el0 el1 el2 el3 el4 el5 el6 el7 el8));
1025 $lb->selection("set", 1, 1);
1026 is($mw->SelectionGet, "el1");
1027 is(join(',',$lb->curselection), "1"); # join forces list context
1028 $lb->configure(-exportselection => 0);
1029 eval { $mw->SelectionGet };
1030 like($@ ,qr/PRIMARY selection doesn\'t exist or form \"(UTF8_)?STRING\" not defined/,
1031 "SelectionGet, error message");
1032 is(join(',',$lb->curselection), "1"); # join forces list context
1033 $lb->selection("clear", 0, "end");
1034 eval { $mw->SelectionGet };
1035 like($@ ,qr/PRIMARY selection doesn\'t exist or form \"(UTF8_)?STRING\" not defined/);
1036 is($lb->curselection, undef, "Empty curselection");
1037 $lb->selection("set", 1, 3);
1038 eval { $mw->SelectionGet };
1039 like($@ ,qr/PRIMARY selection doesn\'t exist or form \"(UTF8_)?STRING\" not defined/);
1040 is_deeply([$lb->curselection], [qw(1 2 3)]);
1041 $lb->configure(-exportselection => 1);
1042 is($mw->SelectionGet, "el1\nel2\nel3");
1043 is_deeply([$lb->curselection], [qw(1 2 3)]);
1044
1045 $lb->destroy;
1046 $mw->geometry("300x300");
1047 $mw->update;
1048 $mw->geometry("");
1049 $mw->withdraw;
1050 $lb = $mw->$Listbox(-font => $fixed, -width => 15, -height => 20);
1051 $lb->pack;
1052 $lb->update;
1053 $mw->deiconify;
1054 like(getsize($mw), qr/^\d+x\d+$/);
1055 $lb->configure(-setgrid => 1);
1056 $mw->update;
1057 like(getsize($mw), qr/^\d+x\d+$/);
1058
1059 $lb->destroy;
1060 $mw->withdraw;
1061 $lb = $mw->$Listbox(-font => $fixed, -width => 30, -height => 20,
1062 -setgrid => 1);
1063 $mw->geometry("+0+0");
1064 $lb->pack;
1065 $mw->update;
1066 $mw->deiconify;
1067 {
1068 local $TODO = "Tests may fail (window-manager related?)";
1069
1070 is(getsize($mw), "30x20");
1071 $mw->geometry("26x15");
1072 $mw->update;
1073 is(getsize($mw), "26x15");
1074 $lb->configure(-setgrid => 1);
1075 $lb->update;
1076 is(getsize($mw), "26x15");
1077 }
1078
1079 $mw->geometry("");
1080 $lb->destroy;
1081 resetGridInfo();
1082
1083 my @log;
1084
1085 $lb = $mw->$Listbox(-width => 15, -height => 20,
1086 -xscrollcommand => sub { record("x", @_) },
1087 -yscrollcommand => [qw/record y/],
1088 )->pack;
1089 $lb->update;
1090 $lb->configure(-fg => "black");
1091 @log = ();
1092 $lb->update;
1093 is($log[0], "y 0 1");
1094 is($log[1], "x 0 1");
1095
1096 $lb->destroy;
1097
1098 SKIP: {
1099 skip("no -listvar in older Tks", 12)
1100 if !$listvar_impl;
1101 {
1102 my @x = qw/a b c d/;
1103 my $lb = $mw->$Listbox(-listvar => \@x);
1104 is_deeply([$lb->get(0, "end")], [qw(a b c d)], "-listvar after initial setting");
1105 $lb->destroy;
1106 }
1107
1108 {
1109 my @x = qw(a b c d);
1110 my $lb = $mw->$Listbox;
1111 $lb->insert(qw(end 1 2 3 4));
1112 $lb->configure(-listvar => \@x);
1113 is_deeply([$lb->get(qw(0 end))], [qw(a b c d)],
1114 q{ConfigureListbox, no listvar -> existing listvar});
1115 $lb->destroy;
1116 }
1117
1118 {
1119 local $TODO = "Not yet implemented in Perl/Tk";
1120
1121 my @x = qw(a b c d);
1122 my $lb = $mw->$Listbox(-listvar => \@x);
1123 $lb->configure(-listvar => undef);
1124 $lb->insert(qw(end 1 2 3 4));
1125 is_deeply([@x], [qw(a b c d)],
1126 q{ConfigureListbox procedure, listvar -> no listvar});
1127 is_deeply([$lb->get(qw(0 end))], [qw(a b c d 1 2 3 4)]);
1128 $lb->destroy;
1129 }
1130
1131 if (0) { # TODO: dumps core!!!
1132 my @x = qw(a b c d);
1133 my @y = (1..4);
1134 my $lb = $mw->$Listbox(-listvar => \@x);
1135 $lb->configure(-listvar => \@x);
1136 $lb->configure(-listvar => \@y);
1137 $lb->insert(qw(end 5 6 7 8));
1138 is_deeply(\@x, [qw(a b c d)],
1139 q{ConfigureListbox procedure, listvar -> different listvar});
1140 is_deeply(\@y, [qw(1 2 3 4 5 6 7 8)]);
1141 $lb->destroy;
1142 }
1143
1144 {
1145 local $TODO = "Not yet implemented in Perl/Tk";
1146
1147 my @x;
1148 my $lb = $mw->$Listbox;
1149 $lb->insert(qw(end a b c d));
1150 $lb->configure(-listvar => \@x);
1151 is_deeply(\@x, [qw(a b c d)],
1152 q{ConfigureListbox, no listvar -> non-existant listvar});
1153 $lb->destroy;
1154 }
1155
1156 {
1157 my @x;
1158 my $lb = $mw->$Listbox(-listvar => \@x);
1159 is_deeply(\@x, [], q{ConfigureListbox, non-existant listvar});
1160 }
1161
1162 {
1163 local $TODO = "Not yet implemented in Perl/Tk";
1164
1165 my @x = qw(a b c d);
1166 my @y;
1167 my $lb = $mw->$Listbox(-listvar => \@x);
1168 $lb->configure(-listvar => \@y);
1169 is_deeply(\@y, [qw(a b c d)],
1170 q{ConfigureListbox, listvar -> non-existant listvar});
1171 $lb->destroy;
1172 }
1173
1174
1175 {
1176 my @x = qw(a b c d);
1177 my $lb = $mw->$Listbox(-listvar => \@x);
1178 $lb->configure(-listvar => \@x);
1179 is_deeply(\@x, [qw(a b c d)],
1180 q{ConfigureListbox, listvar -> same listvar});
1181 $lb->destroy;
1182 }
1183
1184 {
1185 my $lb = $mw->$Listbox;
1186 $lb->insert(qw(end a b c d));
1187 $lb->configure(-listvar => undef);
1188 is_deeply([$lb->get(qw(0 end))], [qw(a b c d)],
1189 q{ConfigureListbox, no listvar -> no listvar});
1190 }
1191
1192 {
1193 local $TODO = "Not yet implemented in Perl/Tk";
1194
1195 my $lb = $mw->$Listbox;
1196 $lb->insert(qw(end a b c d));
1197 my $x = "this is not an array";
1198 eval { $lb->configure(-listvar => \$x) };
1199 like($@, qr{Should have an error message about invalid type});
1200 is_deeply([$lb->get(qw(0 end))], [qw(a b c d)],
1201 q{ConfigureListbox, no listvar -> bad listvar});
1202 is($lb->cget(-listvar), undef);
1203 }
1204 }
1205
1206 # No tests for DisplayListbox: I don't know how to test this procedure.
1207
1208 Tk::catch { $lb->destroy if Tk::Exists($lb) };
1209 $lb = $mw->$Listbox(-font => $fixed, -width => 15, -height => 20)->pack;
1210 {
1211 with_fixed_font { is($lb->reqwidth, 115, "Reqwidth with fixed font") };
1212 with_fixed_font { is($lb->reqheight, 328, "Reqheight with fixed font") };
1213 }
1214
1215 eval { $lb->destroy };
1216 $lb = $mw->$Listbox(-font => $fixed, -width => 0, -height => 10)->pack;
1217 $lb->update;
1218 {
1219 with_fixed_font { is($lb->reqwidth, 17) };
1220 with_fixed_font { is($lb->reqheight, 168) };
1221 }
1222
1223 eval { $lb->destroy };
1224 $lb = $mw->$Listbox(-font => $fixed, -width => 0, -height => 10,
1225 -bd => 3)->pack;
1226 $lb->insert(0, "Short", "Really much longer", "Longer");
1227 $lb->update;
1228 {
1229 with_fixed_font { is($lb->reqwidth, 138) };
1230 with_fixed_font { is($lb->reqheight, 170) };
1231 }
1232
1233 eval { $lb->destroy };
1234 $lb = $mw->$Listbox(-font => $fixed, -width => 10, -height => 0,
1235 )->pack;
1236 $lb->update;
1237 {
1238 with_fixed_font { is($lb->reqwidth, 80) };
1239 with_fixed_font { is($lb->reqheight, 24) };
1240 }
1241
1242 eval { $lb->destroy };
1243 $lb = $mw->$Listbox(-font => $fixed, -width => 10, -height => 0,
1244 -highlightthickness => 0)->pack;
1245 $lb->insert(0, "Short", "Really much longer", "Longer");
1246 $lb->update;
1247 {
1248 with_fixed_font { is($lb->reqwidth, 76) };
1249 with_fixed_font { is($lb->reqheight, 52) };
1250 }
1251
1252 eval { $lb->destroy };
1253 # If "0" in selected font had 0 width, caused divide-by-zero error.
1254 $lb = $mw->$Listbox(-font => '{open look glyph}')->pack;
1255 $lb->update;
1256
1257 eval { $lb->destroy };
1258 $lb = $mw->$Listbox(-height => 2,
1259 -xscrollcommand => sub { record("x", @_) },
1260 -yscrollcommand => sub { record("y", @_) })->pack;
1261 $lb->update;
1262
1263 $lb->delete(0, "end");
1264 $lb->insert(qw/end a b c d/);
1265 $lb->insert(qw/5 x y z/);
1266 $lb->insert(qw/2 A/);
1267 $lb->insert(qw/0 q r s/);
1268 is_deeply([$lb->get(qw/0 end/)], [qw(q r s a b A c d x y z)]);
1269
1270 $lb->delete(qw/0 end/);
1271 $lb->insert(qw/0 a b c d e f g h i j/);
1272 $lb->selection(qw/anchor 2/);
1273 $lb->insert(qw/2 A B/);
1274 is($lb->index(qw/anchor/), 4);
1275
1276 $lb->delete(qw/0 end/);
1277 $lb->insert(qw/0 a b c d e f g h i j/);
1278 $lb->selection(qw/anchor 2/);
1279 $lb->insert(qw/3 A B/);
1280 is($lb->index(qw/anchor/), 2);
1281
1282 $lb->delete(qw/0 end/);
1283 $lb->insert(qw/0 a b c d e f g h i j/);
1284 $lb->yview(qw/3/);
1285 $lb->update;
1286 $lb->insert(qw/2 A B/);
1287 is($lb->index(q/@0,0/), 5);
1288
1289 $lb->delete(qw/0 end/);
1290 $lb->insert(qw/0 a b c d e f g h i j/);
1291 $lb->yview(qw/3/);
1292 $lb->update;
1293 $lb->insert(qw/3 A B/);
1294 is($lb->index(q/@0,0/), 3);
1295
1296 $lb->delete(qw/0 end/);
1297 $lb->insert(qw/0 a b c d e f g h i j/);
1298 $lb->activate(qw/5/);
1299 $lb->insert(qw/5 A B/);
1300 is($lb->index(qw/active/), 7);
1301
1302 $lb->delete(qw/0 end/);
1303 $lb->insert(qw/0 a b c d e f g h i j/);
1304 $lb->activate(qw/5/);
1305 $lb->insert(qw/6 A B/);
1306 is($lb->index(qw/active/), 5);
1307
1308 $lb->delete(qw/0 end/);
1309 $lb->insert(qw/0 a b c/);
1310 is($lb->index(qw/active/), 2);
1311
1312 $lb->delete(qw/0 end/);
1313 $lb->insert(qw/0/);
1314 is($lb->index(qw/active/), 0);
1315
1316 $lb->delete(qw/0 end/);
1317 $lb->insert(qw/0 a b/, "two words", qw/c d e f g h i j/);
1318 $lb->update;
1319 @log = ();
1320 $lb->insert(qw/0 word/);
1321 $lb->update;
1322 like("@log",qr/^y 0 0\.\d+/);
1323
1324 $lb->delete(qw/0 end/);
1325 $lb->insert(qw/0 a b/, "two words", qw/c d e f g h i j/);
1326 $lb->update;
1327 @log = ();
1328 $lb->insert(0, "much longer entry");
1329 $lb->update;
1330 like("$log[0]",qr/^y 0 0\.\d+/);
1331 like("$log[1]", qr/x 0 \d[\d\.]*/);
1332
1333 SKIP: {
1334 skip($skip_font_test, 4) if $skip_font_test;
1335 my $l2 = $mw->$Listbox(-width => 0, -height => 0)->pack(-side => "top");
1336 $l2->insert(0, "a", "b", "two words", "c", "d");
1337 is($l2->reqwidth, 80);
1338 is($l2->reqheight, 93);
1339 $l2->insert(0, "much longer entry");
1340 is($l2->reqwidth, 122);
1341 is($l2->reqheight, 110);
1342 $l2->destroy;
1343 }
1344
1345 SKIP: {
1346 skip("-listvar not implemented in older Tks", 1)
1347 if !$listvar_impl;
1348 skip("dumps core *TODO*", 1);
1349
1350 my @x = qw(a b c d);
1351 my $l2 = $mw->$Listbox(-listvar => \@x);
1352 $l2->insert(0, 1 .. 4);
1353 is_deeply([@x], [qw(1 2 3 4 a b c d)]);
1354 $l2->destroy;
1355 }
1356
1357 {
1358 my $l2 = $mw->$Listbox;
1359 $l2->insert(0, 0 .. 4);
1360 $l2->selection("set", 2, 4);
1361 $l2->insert(0, "a");
1362 is_deeply([ $l2->curselection ], [qw(3 4 5)],
1363 "curselection after selection set");
1364 is(scalar @{[ $l2->curselection ]}, 3);
1365 $l2->destroy;
1366 }
1367
1368 $lb->delete(0, "end");
1369 $lb->insert(0, qw/a b c d e f g h i j/);
1370 $lb->selectionSet(1, 6);
1371 $lb->delete(4, 3);
1372 is($lb->size, 10, "size after negative delete");
1373 is($mw->SelectionGet, "b
1374 c
1375 d
1376 e
1377 f
1378 g", "SelectionGet after selectionSet");
1379
1380 $lb->delete(qw/0 end/);
1381 $lb->insert(qw/0 a b c d e f g h i j/);
1382 $lb->selection(qw/set 3 6/);
1383 $lb->delete(qw/4 4/);
1384 is($lb->size, 9);
1385 is($lb->get(4), "f");
1386 is_deeply([ $lb->curselection ], [3,4,5]);
1387 is(scalar @{[ $lb->curselection ]}, 3);
1388 is(($lb->curselection)[0], 3);
1389 is(($lb->curselection)[-1], 5);
1390
1391 $lb->delete(qw/0 end/);
1392 $lb->insert(qw/0 a b c d e f g h i j/);
1393 $lb->delete(qw/0 3/);
1394 is($lb->size, 6);
1395 is($lb->get(0), "e");
1396 is($lb->get(1), "f");
1397
1398 $lb->delete(qw/0 end/);
1399 $lb->insert(qw/0 a b c d e f g h i j/);
1400 $lb->delete(qw/8 1000/);
1401 is($lb->size, 8);
1402 is($lb->get(7), "h");
1403
1404 $lb-> delete(0, qw/end/);
1405 $lb->insert(qw/0 a b c d e f g h i j/);
1406 $lb->selection(qw/anchor 2/);
1407 $lb->delete(qw/0 1/);
1408 is($lb->index(qw/anchor/), 0);
1409
1410 $lb->delete(qw/0 end/);
1411 $lb->insert(qw/0 a b c d e f g h i j/);
1412 $lb->selection(qw/anchor 2/);
1413 $lb->delete(qw/2/);
1414 is($lb->index(qw/anchor/), 2);
1415
1416 $lb->delete(qw/0 end/);
1417 $lb->insert(qw/0 a b c d e f g h i j/);
1418 $lb->selection(qw/anchor 4/);
1419 $lb->delete(qw/2 5/);
1420 is($lb->index(qw/anchor/), 2);
1421
1422 $lb->delete(qw/0 end/);
1423 $lb->insert(qw/0 a b c d e f g h i j/);
1424 $lb->selection(qw/anchor 3/);
1425 $lb->delete(qw/4 5/);
1426 is($lb->index(qw/anchor/), 3);
1427
1428 $lb->delete(qw/0 end/);
1429 $lb->insert(qw/0 a b c d e f g h i j/);
1430 $lb->yview(qw/3/);
1431 $lb->update;
1432 $lb->delete(qw/1 2/);
1433 is($lb->index(q/@0,0/), 1);
1434
1435 $lb->delete(qw/0 end/);
1436 $lb->insert(qw/0 a b c d e f g h i j/);
1437 $lb->yview(qw/3/);
1438 $lb->update;
1439 $lb->delete(qw/3 4/);
1440 is($lb->index(q/@0,0/), 3);
1441
1442 $lb->delete(qw/0 end/);
1443 $lb->insert(qw/0 a b c d e f g h i j/);
1444 $lb->yview(qw/3/);
1445 $lb->update;
1446 $lb->delete(qw/4 6/);
1447 is($lb->index(q/@0,0/), 3);
1448
1449 $lb->delete(qw/0 end/);
1450 $lb->insert(qw/0 a b c d e f g h i j/);
1451 $lb->yview(qw/3/);
1452 $lb->update;
1453 $lb->delete(qw/3 end/);
1454 like($lb->index(q/@0,0/), qr/^[12]$/);
1455
1456 mkPartial();
1457 $partial_lb->yview(8);
1458 $mw->update;
1459 $partial_lb->delete(10, 13);
1460 like($partial_lb->index('@0,0'), qr/^[67]$/);
1461
1462 $lb->delete(qw/0 end/);
1463 $lb->insert(qw/0 a b c d e f g h i j/);
1464 $lb->activate(qw/6/);
1465 $lb->delete(qw/3 4/);
1466 is($lb->index(qw/active/), 4);
1467
1468 $lb->delete(qw/0 end/);
1469 $lb->insert(qw/0 a b c d e f g h i j/);
1470 $lb->activate(qw/6/);
1471 $lb->delete(qw/5 7/);
1472 is($lb->index(qw/active/), 5);
1473
1474 $lb->delete(qw/0 end/);
1475 $lb->insert(qw/0 a b c d e f g h i j/);
1476 $lb->activate(qw/6/);
1477 $lb->delete(qw/5 end/);
1478 is($lb->index(qw/active/), 4);
1479
1480 $lb->delete(qw/0 end/);
1481 $lb->insert(qw/0 a b c d e f g h i j/);
1482 $lb->activate(qw/6/);
1483 $lb->delete(qw/0 end/);
1484 is($lb->index(qw/active/), 0);
1485
1486 $lb->delete(qw/0 end/);
1487 $lb->insert(qw/0 a b c/, "two words", qw/d e f g h i j/);
1488 $lb->update;
1489 @log = ();
1490 $lb->delete(qw/4 6/);
1491 $lb->update;
1492 like($log[0], qr/y 0 0\.\d+/);
1493
1494 $lb->delete(qw/0 end/);
1495 $lb->insert(qw/0 a b c/, "two words", qw/d e f g h i j/);
1496 $lb->update;
1497 @log = ();
1498 $lb->delete(qw/3/);
1499 $lb->update;
1500 like($log[0], qr/^y 0 0\.\d+$/);
1501 is($log[1], "x 0 1");
1502
1503 SKIP: {
1504 skip($skip_font_test, 4) if $skip_font_test;
1505 my $l2 = $mw->$Listbox(-width => 0, -height => 0)->pack(-side => "top");
1506 $l2->insert(0, "a", "b", "two words", qw/c d e f g/);
1507 is($l2->reqwidth, 80);
1508 is($l2->reqheight, 144);
1509 $l2->delete(2, 4);
1510 is($l2->reqwidth, 17);
1511 is($l2->reqheight, 93);
1512 $l2->destroy;
1513 }
1514
1515 SKIP: {
1516 skip("-listvar not implemented in older Tks", 1)
1517 if !$listvar_impl;
1518 skip("dumps core *TODO*", 1);
1519
1520 my @x = qw(a b c d);
1521 my $l2 = $mw->Listbox(-listvar => \@x);
1522 $l2->delete(0, 1);
1523 is_deeply(\@x, ["c","d"],
1524 q{DeleteEls procedure, check -listvar update});
1525 }
1526
1527 $lb->destroy;
1528 $lb = $mw->$Listbox(-setgrid => 1)->pack;
1529 $lb->update;
1530 like(getsize($mw), qr/^\d+x\d+$/); # still worth it ?
1531 $lb->destroy;
1532 like(getsize($mw), qr/^\d+x\d+$/); # still worth it ?
1533 ok(!Tk::Exists($lb), "Listbox is destroyed");
1534
1535 resetGridInfo();
1536
1537 $lb = $mw->$Listbox(-height => 5, -width => 10);
1538 $lb->insert(qw/0 a b c/, "A string that is very very long",
1539 qw/ d e f g h i j k/);
1540 $lb->pack;
1541 $lb->update;
1542 $lb->place(qw/-width 50 -height 80/);
1543 $lb->update;
1544 SKIP: {
1545 skip($skip_font_test, 2) if $skip_font_test;
1546 like(join(" ", $lb->xview), qr/^0 0\.2222/);
1547 like(join(" ", $lb->yview), qr/^0 0\.3333/);
1548 }
1549
1550 map { $_->destroy } $mw->children;
1551 my $l1 = $mw->$Listbox(-bg => "#543210");
1552 my $l2 = $l1;
1553 like(join(",", map { $_->PathName } $mw->children) ,qr/^\.listbox\d*$/);
1554 is($l2->cget(-bg), "#543210");
1555 $l2->destroy;
1556
1557 my $top = $mw->Toplevel;
1558 $top->geometry("+0+0");
1559 my $top_lb = $top->$Listbox(-setgrid => 1,
1560 -width => 20,
1561 -height => 10)->pack;
1562 $top_lb->update;
1563 like($top->geometry, qr/20x10\+\d+\+\d+/);
1564 $top_lb->destroy;
1565 SKIP: {
1566 skip($skip_font_test, 1) if $skip_font_test;
1567 like($top->geometry, qr/150x178\+\d+\+\d+/, "Geometry");
1568 }
1569
1570 $lb = $mw->$Listbox->pack;
1571 $lb->delete(0, "end");
1572 $lb->insert(qw/0 el0 el1 el2 el3 el4 el5 el6 el7 el8 el9 el10 el11/);
1573 $lb->activate(3);
1574 is($lb->index("active"), 3);
1575 $lb->activate(6);
1576 is($lb->index("active"), 6);
1577
1578 $lb->selection(qw/anchor 2/);
1579 is($lb->index(qw/anchor/), 2);
1580
1581 $lb->insert(qw/end A B C D E/);
1582 $lb->selection(qw/anchor end/);
1583 $lb->delete(qw/12 end/);
1584 is($lb->index("anchor"), 12);
1585 is($lb->index("end"), 12);
1586
1587 eval { $lb->index("a") };
1588 like($@ ,qr/bad listbox index \"a\": must be active, anchor, end, \@x,y, or a number/, "Listbox index error message");
1589
1590 eval { $lb->index("\@") };
1591 like($@ ,qr/bad listbox index \"\@\": must be active, anchor, end, \@x,y, or a number/);
1592
1593 eval { $lb->index("\@foo") };
1594 like($@ ,qr/bad listbox index \"\@foo\": must be active, anchor, end, \@x,y, or a number/);
1595
1596 eval { $lb->index("\@1x3") };
1597 like($@ ,qr/bad listbox index \"\@1x3\": must be active, anchor, end, \@x,y, or a number/);
1598
1599 eval { $lb->index("\@1,") };
1600 like($@ ,qr/bad listbox index \"\@1,\": must be active, anchor, end, \@x,y, or a number/);
1601
1602 eval { $lb->index("\@1,foo") };
1603 like($@ ,qr/bad listbox index \"\@1,foo\": must be active, anchor, end, \@x,y, or a number/);
1604
1605 eval { $lb->index("\@1,2x") };
1606 like($@ ,qr/bad listbox index \"\@1,2x\": must be active, anchor, end, \@x,y, or a number/);
1607
1608 eval { $lb->index("1xy") };
1609 like($@ ,qr/bad listbox index \"1xy\": must be active, anchor, end, \@x,y, or a number/);
1610
1611 is($lb->index("end"), 12);
1612
1613 is($lb->get(qw/end/), "el11");
1614
1615 $lb->delete(qw/0 end/);
1616 is($lb->index(qw/end/), 0);
1617
1618 $lb->delete(qw/0 end/);
1619 $lb->insert(qw/0 el0 el1 el2 el3 el4 el5 el6 el7 el8 el9 el10 el11/);
1620 $mw->geometry(""); # XXX hack to force a fully visible listbox, see http://rt.cpan.org/Ticket/Display.html?id=31290
1621 $lb->update;
1622
1623 SKIP: {
1624 skip($skip_font_test, 2) if $skip_font_test;
1625
1626 is($lb->index(q/@5,57/), 3);
1627 is($lb->index(q/@5,58/), 3);
1628 }
1629
1630 is($lb->index(qw/3/), 3);
1631 is($lb->index(qw/20/), 20);
1632
1633 is($lb->get(qw/20/), undef);
1634
1635 is($lb->index(qw/-2/), -2);
1636
1637 $lb->delete(qw/0 end/);
1638 is($lb->index(qw/1/), 1);
1639
1640 $lb->destroy;
1641 $lb = $mw->$Listbox(-height => 5)->pack;
1642 $lb->insert(qw/0 a b c d e f g h i j/);
1643 $lb->yview(qw/3/);
1644 $lb->update;
1645 is($lb->index(q/@0,0/), 3);
1646 $lb->yview(qw/-1/);
1647 $lb->update;
1648 is($lb->index(q/@0,0/), 0);
1649
1650 $lb->destroy;
1651 $lb = $mw->$Listbox(qw/-height 5/)->pack;
1652 $lb->insert(qw/0 a b c d e f g h i j/);
1653 $lb->yview(qw/3/);
1654 $lb->update;
1655 is($lb->index(q/@0,0/), 3);
1656 $lb->yview(qw/20/);
1657 $lb->update;
1658 is($lb->index(q/@0,0/), 5);
1659
1660 $lb->destroy;
1661 $lb = $mw->$Listbox(qw/-height 5 -yscrollcommand/, [qw/record y/])->pack;
1662 $lb->insert(qw/0 a b c d e f g h i j/);
1663 $lb->update;
1664 @log = ();
1665 $lb->yview(qw/2/);
1666 $lb->update;
1667 is_float("@{[ $lb->yview ]}", "0.2 0.7");
1668 is_float($log[0], "y 0.2 0.7");
1669
1670 $lb->destroy;
1671 $lb = $mw->$Listbox(qw/-height 5 -yscrollcommand/, [qw/record y/])->pack;
1672 $lb->insert(qw/0 a b c d e f g h i j/);
1673 $lb->update;
1674 @log = ();
1675 $lb->yview(qw/8/);
1676 $lb->update;
1677 is_float("@{[ $lb->yview ]}", "0.5 1");
1678 is_float($log[0], "y 0.5 1");
1679
1680 $lb->destroy;
1681 $lb = $mw->$Listbox(qw/-height 5 -yscrollcommand/, [qw/record y/])->pack;
1682 $lb->insert(qw/0 a b c d e f g h i j/);
1683 $lb->yview(qw/3/);
1684 $lb->update;
1685 @log = ();
1686 $lb->yview(qw/3/);
1687 $lb->update;
1688 is_float("@{[ $lb->yview ]}", "0.3 0.8");
1689 is(scalar @log, 0);
1690
1691 mkPartial();
1692 $partial_lb->yview(13);
1693 like($partial_lb->index('@0,0'), qr/^1[01]$/);
1694
1695 $lb->destroy;
1696 $lb = $mw->$Listbox(-font => $fixed,
1697 -xscrollcommand => ["record", "x"],
1698 -width => 10);
1699 $lb->insert(qw/0 0123456789a123456789b123456789c123456789d123456789e123456789f123456789g123456789h123456789i123456789/);
1700 $lb->pack;
1701 $lb->update;
1702
1703 @log = ();
1704 $lb->xview(qw/99/);
1705 $lb->update;
1706 with_fixed_font { is_float("@{[ $lb->xview ]}", "0.9 1") };
1707 with_fixed_font { is_float(($lb->xview)[0], 0.9) };
1708 is(($lb->xview)[1], 1);
1709 with_fixed_font { is_float($log[0], "x 0.9 1") };
1710
1711 @log = ();
1712 $lb->xview(qw/moveto -.25/);
1713 $lb->update;
1714 with_fixed_font { is_float("@{[ $lb->xview ]}", "0 0.1") };
1715 with_fixed_font { is_float($log[0], "x 0 0.1") };
1716
1717 $lb->xview(qw/10/);
1718 $lb->update;
1719 @log = ();
1720 $lb->xview(qw/10/);
1721 $lb->update;
1722 with_fixed_font { is_float("@{[ $lb->xview ]}", "0.1 0.2") };
1723 is(scalar @log, 0);
1724
1725 $lb->destroy;
1726 $lb = $mw->$Listbox(-font => $fixed, -width => 10, -height => 5)->pack;
1727 $lb->insert(qw/0 a bb c d e f g h i j k l m n o p q r s/);
1728 $lb->insert(qw/0 0123456789a123456789b123456789c123456789d123456789/);
1729 $lb->update;
1730 my $width = ($lb->bbox(2))[2] - ($lb->bbox(1))[2];
1731 my $height = ($lb->bbox(2))[1] - ($lb->bbox(1))[1];
1732
1733 $lb->yview(qw/0/);
1734 $lb->xview(qw/0/);
1735 $lb->scan(qw/mark 10 20/);
1736 $lb->scan(qw/dragto/, 10-$width, 20-$height);
1737 $lb->update;
1738 with_fixed_font { is_float("@{[ $lb->xview ]}", "0.2 0.4") };
1739 is_float("@{[ $lb->yview ]}", "0.5 0.75");
1740
1741 $lb->yview(qw/5/);
1742 $lb->xview(qw/10/);
1743 $lb->scan(qw/mark 10 20/);
1744 $lb->scan(qw/dragto 20 40/);
1745 $lb->update;
1746 with_fixed_font { is_float("@{[ $lb->xview ]}", "0 0.2") };
1747 is_float("@{[ $lb->yview ]}", "0 0.25");
1748
1749 $lb->scan(qw/dragto/, 20-$width, 40-$height);
1750 $lb->update;
1751 with_fixed_font { is_float("@{[ $lb->xview ]}", "0.2 0.4") };
1752 with_fixed_font { is_float(join(',',$lb->xview), "0.2,0.4") }; # just to prove it is a list
1753 is_float("@{[ $lb->yview ]}", "0.5 0.75");
1754 is_float(join(',',$lb->yview), "0.5,0.75"); # just to prove it is a list
1755
1756 $lb->yview(qw/moveto 1.0/);
1757 $lb->xview(qw/moveto 1.0/);
1758 $lb->scan(qw/mark 10 20/);
1759 $lb->scan(qw/dragto 5 10/);
1760 $lb->update;
1761 with_fixed_font { is_float("@{[ $lb->xview ]}", "0.8 1") };
1762 is_float("@{[ $lb->yview ]}", "0.75 1");
1763 $lb->scan(qw/dragto/, 5+$width, 10+$height);
1764 $lb->update;
1765 with_fixed_font { is_float("@{[ $lb->xview ]}", "0.64 0.84") };
1766 is_float("@{[ $lb->yview ]}", "0.25 0.5");
1767
1768 mkPartial();
1769 is($partial_lb->nearest($partial_lb->height), 4);
1770
1771 $lb->destroy;
1772 $lb = $mw->$Listbox(-font => $fixed,
1773 -width => 20,
1774 -height => 10);
1775 $lb->insert(qw/0 a b c d e f g h i j k l m n o p q r s t/);
1776 $lb->yview(qw/4/);
1777 $lb->pack;
1778 $lb->update;
1779
1780 {
1781 with_fixed_font { is($lb->index(q/@50,0/), 4) };
1782 with_fixed_font { is($lb->index(q/@50,35/), 5) };
1783 with_fixed_font { is($lb->index(q/@50,36/), 6) };
1784 }
1785
1786 like($lb->index(q/@50,200/), qr/^\d+/);
1787
1788 $lb->delete(qw/0 end/);
1789 $lb->insert(qw/0 a b c d e f g h i j k l m n o p/);
1790 $lb->selection(qw/set 2 4/);
1791 $lb->selection(qw/set 7 12/);
1792 $lb->selection(qw/clear 4 7/);
1793 is_deeply([ $lb->curselection ], [qw(2 3 8 9 10 11 12)]);
1794
1795 $lb->delete(qw/0 end/);
1796 $lb->insert(qw/0 a b c d e f g h i j k l m n o p/);
1797
1798 $e = $mw->Entry;
1799 $e->insert(0, "This is some text");
1800 $e->selection(qw/from 0/);
1801 $e->selection(qw/to 7/);
1802 $lb->selection(qw/clear 2 4/);
1803 is($mw->SelectionOwner, $e);
1804 $lb->selection(qw/set 3/);
1805 is($mw->SelectionOwner, $lb);
1806 is($mw->SelectionGet, "d");
1807
1808 $lb->delete(qw/0 end/);
1809 $lb->selection(qw/clear 0 end/);
1810 $lb->selection(qw/set 0 end/);
1811 is($lb->curselection, undef);
1812
1813 $lb->delete(qw/0 end/);
1814 $lb->insert(qw/0 a b c d e f/);
1815 $lb->selection(qw/clear 0 end/);
1816 $lb->selection(qw/set -2 -1/);
1817 is($lb->curselection, undef);
1818
1819 $lb->delete(qw/0 end/);
1820 $lb->insert(qw/0 a b c d e f/);
1821 $lb->selection(qw/clear 0 end/);
1822 $lb->selection(qw/set -1 3/);
1823 is_deeply([$lb->curselection], [0,1,2,3]);
1824
1825 $lb->delete(qw/0 end/);
1826 $lb->insert(qw/0 a b c d e f/);
1827 $lb->selection(qw/clear 0 end/);
1828 $lb->selection(qw/set 2 4/);
1829 is_deeply([$lb->curselection], [qw(2 3 4)]);
1830
1831 $lb->delete(qw/0 end/);
1832 $lb->insert(qw/0 a b c d e f/);
1833 $lb->selection(qw/clear 0 end/);
1834 $lb->selection(qw/set 4 end/);
1835 is_deeply([$lb->curselection], [4, 5]);
1836
1837 $lb->delete(qw/0 end/);
1838 $lb->insert(qw/0 a b c d e f/);
1839 $lb->selection(qw/clear 0 end/);
1840 $lb->selection(qw/set 4 30/);
1841 is_deeply([$lb->curselection], [4, 5]);
1842
1843 $lb->delete(qw/0 end/);
1844 $lb->insert(qw/0 a b c d e f/);
1845 $lb->selection(qw/clear 0 end/);
1846 $lb->selection(qw/set end 30/);
1847 is(join(",", $lb->curselection), 5);
1848 is(scalar @{[ $lb->curselection ]}, 1);
1849
1850 $lb->delete(qw/0 end/);
1851 $lb->insert(qw/0 a b c d e f/);
1852 $lb->selection(qw/clear 0 end/);
1853 $lb->selection(qw/set 20 25/);
1854 is($lb->curselection, undef);
1855
1856 $lb->delete(qw/0 end/);
1857 $lb->insert(qw/0 a b c/, "two words", qw/ e f g h i \ k l m n o p/);
1858 $lb->selection(qw/set 2 4/);
1859 $lb->selection(qw/set 9/);
1860 $lb->selection(qw/set 11 12/);
1861 is($mw->SelectionGet, "c\ntwo words\ne\n\\\nl\nm");
1862
1863 $lb->delete(qw/0 end/);
1864 $lb->insert(qw/0 a b c/, "two words", qw/ e f g h i \ k l m n o p/);
1865 $lb->selection(qw/set 3/);
1866 is($mw->SelectionGet, "two words");
1867
1868 my $long = "This is quite a long string\n" x 11;
1869 $lb->delete(qw/0 end/);
1870 $lb->insert(0, "1$long", "2$long", "3$long", "4$long", "5$long");
1871 $lb->selection(qw/set 0 end/);
1872 is($mw->SelectionGet, "1$long\n2$long\n3$long\n4$long\n5$long");
1873
1874 $lb->delete(qw/0 end/);
1875 $lb->insert(qw/0 a b c d e/);
1876 $lb->selection(qw/set 0 end/);
1877 $e->destroy;
1878 $e = $mw->Entry;
1879 $e->insert(0, "This is some text");
1880 $e->selection(qw/from 0/);
1881 $e->selection(qw/to 5/);
1882 is($lb->curselection, undef);
1883
1884 $lb->delete(qw/0 end/);
1885 $lb->insert(qw/0 a b c d e/);
1886 $lb->selection(qw/set 0 end/);
1887 $lb->configure(qw/-exportselection 0/);
1888 $e->destroy;
1889 $e = $top->Entry;
1890 $e->insert(0, "This is some text");
1891 $e->selection(qw/from 0/);
1892 $e->selection(qw/to 5/);
1893 is_deeply([$lb->curselection], [qw(0 1 2 3 4)]);
1894
1895 $lb->destroy;
1896 $lb = $mw->$Listbox(-font => $fixed, -width => 10, -height => 5);
1897 $lb->pack;
1898 $lb->update;
1899
1900 $lb->configure(qw/-yscrollcommand/, [qw/record y/]);
1901 @log = ();
1902 $lb->insert(qw/0 a b c/);
1903 $lb->update;
1904 $lb->insert(qw/end d e f g h/);
1905 $lb->update;
1906 $lb->delete(qw/0 end/);
1907 $lb->update;
1908 is($log[0], "y 0 1");
1909 TODO_fluxbox_problem {
1910 is_float($log[1], "y 0 0.625");
1911 is($log[2], "y 0 1");
1912 };
1913
1914 mkPartial();
1915 $partial_lb->configure(-yscrollcommand => ["record", "y"]);
1916 @log = ();
1917 $partial_lb->yview(3);
1918 $partial_lb->update;
1919 like($log[0], qr/^y 0\.2(0000+\d+)? 0\.\d+/);
1920
1921 my @x = ();
1922
1923 sub Tk::Error {
1924 push @x, @_;
1925 }
1926
1927 # XXX dumps core with 5.7.0 and 803.023
1928 $lb->configure(qw/-yscrollcommand gorp/);
1929 $lb->insert(qw/0 foo/);
1930 $lb->update;
1931 like("@x" ,qr/Undefined subroutine &main::gorp called.*vertical scrolling command executed by listbox/s);
1932
1933 $lb->destroy;
1934 $lb = $mw->$Listbox(-font => $fixed, qw/-width 10 -height 5/)->pack;
1935 $lb->update;
1936
1937 $lb->configure(qw/-xscrollcommand/, ["record", "x"]);
1938 @log = ();
1939 $lb->insert(qw/0 abc/);
1940 $lb->update;
1941 $lb->insert(qw/0/, "This is a much longer string...");
1942 $lb->update;
1943 $lb->delete(qw/0 end/);
1944 $lb->update;
1945 is($log[0], "x 0 1");
1946 with_fixed_font { like($log[1] ,qr/^x 0 0\.32258/) };
1947 is($log[2], "x 0 1");
1948
1949 @x = ();
1950 $lb->configure(qw/-xscrollcommand bogus/);
1951 $lb->insert(qw/0 foo/);
1952 $lb->update;
1953 like("@x" ,qr/Undefined subroutine &main::bogus.*horizontal scrolling command executed by listbox/s);
1954
1955 foreach ($mw->children) { $_->destroy }
1956
1957 SKIP: {
1958 skip("no -listvar in older Tks", 12)
1959 if !$listvar_impl;
1960
1961 {
1962 my @x;
1963 my $lb = $mw->$Listbox(-listvar => \@x);
1964 @x = qw(a b c d);
1965 is_deeply([$lb->get(0, "end")], [qw(a b c d)],
1966 "ListboxListVarProc");
1967 $lb->destroy;
1968 }
1969
1970 {
1971 my @x;
1972 @x = qw(a b c d);
1973 my $lb = $mw->$Listbox(-listvar => \@x);
1974 $lb->configure(-listvar => undef);
1975 @x = ();
1976 is_deeply(\@x, []);
1977 $lb->destroy;
1978 }
1979
1980 {
1981 my @x = qw(a b c d);
1982 my $lb = $mw->$Listbox(-listvar => \@x);
1983 push @x, qw(e f g);
1984 is(scalar(@x), 7);
1985 $lb->destroy;
1986 }
1987
1988 {
1989 my @x = qw(a b c d e f g);
1990 my $lb = $mw->$Listbox(-listvar => \@x);
1991 $lb->selection(qw(set end));
1992 @x = qw(a b c d);
1993 @x = (0..6);
1994 is($lb->curselection, undef,
1995 q{ListboxListVarProc, test selection after listvar mod});
1996 $lb->destroy;
1997 }
1998
1999 {
2000 my @x = qw(a b c d);
2001 my $lb = $mw->$Listbox(-listvar => \@x);
2002 $lb->selection(qw(set 3));
2003 push @x, qw(e f g);
2004 is_deeply([$lb->curselection], [3]);
2005 $lb->destroy;
2006 }
2007
2008 {
2009 my @x = qw(a b c d);
2010 my $lb = $mw->$Listbox(-listvar => \@x);
2011 $lb->selection(qw(set 0));
2012 splice @x, 0, 0, (1, 2, 3, 4); # not sure if this is the correct linsert translation
2013 is_deeply([$lb->curselection], [0]);
2014 $lb->destroy;
2015 }
2016
2017 {
2018 local $TODO = "Not yet implemented in Perl/Tk";
2019
2020 my @x = qw(a b c d);
2021 my $lb = $mw->$Listbox(-listvar => \@x);
2022 $lb->selection(qw(set 2));
2023 @x = qw(a b c);
2024 is_deeply([$lb->curselection], [2]);
2025 $lb->destroy;
2026 }
2027
2028 # test listbox-21.9 {ListboxListVarProc, test hscrollbar after listvar mod} {
2029 # catch {destroy $_lb}
2030 # catch {unset x}
2031 # set log {}
2032 # listbox $_lb -font $fixed -width 10 -xscrollcommand "record x" -listvar x
2033 # pack $_lb
2034 # update
2035 # lappend x "0000000000"
2036 # update
2037 # lappend x "00000000000000000000"
2038 # update
2039 # set log
2040 # } [list {x 0 1} {x 0 1} {x 0 0.5}]
2041 # test listbox-21.10 {ListboxListVarProc, test hscrollbar after listvar mod} {
2042 # catch {destroy $_lb}
2043 # catch {unset x}
2044 # set log {}
2045 # listbox $_lb -font $fixed -width 10 -xscrollcommand "record x" -listvar x
2046 # pack $_lb
2047 # update
2048 # lappend x "0000000000"
2049 # update
2050 # lappend x "00000000000000000000"
2051 # update
2052 # set x [list "0000000000"]
2053 # update
2054 # set log
2055 # } [list {x 0 1} {x 0 1} {x 0 0.5} {x 0 1}]
2056 # test listbox-21.11 {ListboxListVarProc, bad list} {
2057 # catch {destroy $_lb}
2058 # catch {unset x}
2059 # listbox $_lb -listvar x
2060 # set x [list a b c d]
2061 # catch {set x {this is a " bad list}} result
2062 # set result
2063 # } {can't set "x": invalid listvar value}
2064 # test listbox-21.12 {ListboxListVarProc, cleanup item attributes} {
2065 # catch {destroy $_lb}
2066 # set x [list a b c d e f g]
2067 # listbox $_lb -listvar x
2068 # $_lb itemconfigure end -fg red
2069 # set x [list a b c d]
2070 # set x [list 0 1 2 3 4 5 6]
2071 # $_lb itemcget end -fg
2072 # } {}
2073 # test listbox-21.12 {ListboxListVarProc, cleanup item attributes} {
2074 # catch {destroy $_lb}
2075 # set x [list a b c d e f g]
2076 # listbox $_lb -listvar x
2077 # $_lb itemconfigure end -fg red
2078 # set x [list a b c d]
2079 # set x [list 0 1 2 3 4 5 6]
2080 # $_lb itemcget end -fg
2081 # } {}
2082 # test listbox-21.13 {listbox item configurations and listvar based deletions} {
2083 # catch {destroy $_lb}
2084 # catch {unset x}
2085 # listbox $_lb -listvar x
2086 # $_lb insert end a b c
2087 # $_lb itemconfigure 1 -fg red
2088 # set x [list b c]
2089 # $_lb itemcget 1 -fg
2090 # } red
2091 # test listbox-21.14 {listbox item configurations and listvar based inserts} {
2092 # catch {destroy $_lb}
2093 # catch {unset x}
2094 # listbox $_lb -listvar x
2095 # $_lb insert end a b c
2096 # $_lb itemconfigure 0 -fg red
2097 # set x [list 1 2 3 4 a b c]
2098 # $_lb itemcget 0 -fg
2099 # } red
2100 # test listbox-21.15 {ListboxListVarProc, update vertical scrollbar} {
2101 # catch {destroy $_lb}
2102 # catch {unset x}
2103 # set log {}
2104 # listbox $_lb -listvar x -yscrollcommand "record y" -font fixed -height 3
2105 # pack $_lb
2106 # update
2107 # lappend x a b c d e f
2108 # update
2109 # set log
2110 # } [list {y 0 1} {y 0 0.5}]
2111 # test listbox-21.16 {ListboxListVarProc, update vertical scrollbar} {
2112 # catch {destroy $_lb}
2113 # catch {unset x}
2114 # listbox $_lb -listvar x -height 3
2115 # pack $_lb
2116 # update
2117 # set x [list 0 1 2 3 4 5]
2118 # $_lb yview scroll 3 units
2119 # update
2120 # set result {}
2121 # lappend result [$_lb yview]
2122 # set x [lreplace $x 3 3]
2123 # set x [lreplace $x 3 3]
2124 # set x [lreplace $x 3 3]
2125 # update
2126 # lappend result [$_lb yview]
2127 # set result
2128 # } [list {0.5 1} {0 1}]
2129 }
2130
2131 # UpdateHScrollbar
2132
2133 @log = ();
2134 $lb = $mw->Listbox(-font => $fixed, -width => 10, -xscrollcommand => ["record", "x"])->pack;
2135 $mw->update;
2136 $lb->insert("end", "0000000000");
2137 $mw->update;
2138 $lb->insert("end", "00000000000000000000");
2139 $mw->update;
2140 TODO_xscrollcommand_problem {
2141 is_deeply(\@log, ["x 0 1", "x 0 1", "x 0 0.5"]);
2142 };
2143
2144 SKIP: {
2145 skip("no itemconfigure in Tk800.x", 35)
2146 if $Tk::VERSION < 804;
2147
2148 {
2149 my $lb = $mw->$Listbox;
2150 eval { $lb->itemconfigure(0) };
2151 like($@, qr{item number "0" out of range},
2152 "ConfigureListboxItem error");
2153 $lb->destroy;
2154 }
2155
2156 {
2157 my $lb = $mw->$Listbox;
2158 $lb->insert(qw(end a b c d));
2159 is_deeply([$lb->itemconfigure(0)],
2160 [[qw(-background background Background), undef, undef],
2161 [qw(-bg -background)],
2162 [qw(-fg -foreground)],
2163 [qw(-foreground foreground Foreground), undef, undef],
2164 [qw(-selectbackground selectBackground Foreground), undef, undef],
2165 [qw(-selectforeground selectForeground Background), undef, undef],
2166 ], "itemconfigure options");
2167 $lb->destroy;
2168 }
2169
2170 {
2171 my $lb = $mw->$Listbox;
2172 $lb->insert(qw(end a b c d));
2173 is_deeply([$lb->itemconfigure(0, -background)],[qw(-background background Background),undef,undef],
2174 "itemconfigure with just one option"
2175 );
2176 $lb->destroy;
2177 }
2178
2179 {
2180 my $lb = $mw->$Listbox(Name => "lb");
2181 $lb->insert(qw(end a));
2182 eval { $lb->itemconfigure };
2183 like($@, qr{\Qwrong # args: should be ".lb itemconfigure index ?option? ?value? ?option value ...?"},
2184 q{ConfigureListboxItem, wrong num args});
2185 $lb->destroy;
2186 }
2187
2188 {
2189 my $lb = $mw->$Listbox(Name => "lb");
2190 my $i = 0;
2191 my @colors = qw(red orange yellow green blue darkblue violet);
2192 for my $color (@colors) {
2193 $lb->insert(end => $color);
2194 $lb->itemconfigure($i, -bg => $color);
2195 $i++;
2196 }
2197 $lb->pack;
2198 $lb->update;
2199 for my $i (0 .. 6) {
2200 is($lb->itemcget($i, -bg), $colors[$i],
2201 q{ConfigureListboxItem, multiple calls, } . $colors[$i]);
2202 }
2203 $lb->destroy;
2204 }
2205
2206 {
2207 my $lb = $mw->$Listbox;
2208 $lb->insert(qw(end a b c d));
2209
2210 no warnings 'qw';
2211
2212 foreach my $test
2213 (
2214 [qw(-background #ff0000 #ff0000 non-existent),
2215 qr{unknown color name "non-existent"}],
2216 [qw(-bg #ff0000 #ff0000 non-existent),
2217 qr{unknown color name "non-existent"}],
2218 [qw(-fg #110022 #110022 bogus),
2219 qr{unknown color name "bogus"}],
2220 [qw(-foreground #110022 #110022 bogus),
2221 qr{unknown color name "bogus"}],
2222 [qw(-selectbackground #110022 #110022 bogus),
2223 qr{unknown color name "bogus"}],
2224 [qw(-selectforeground #654321 #654321 bogus),
2225 qr{unknown color name "bogus"}],
2226 ) {
2227 my($name, $set_val, $get_val, $err_val, $err_msg) = @$test;
2228 $lb->itemconfigure(0, $name, $set_val);
2229 is($lb->itemcget(0, $name), $get_val, "itemconfigure $name");
2230 eval { $lb->itemconfigure(0, $name, $err_val) };
2231 like($@, $err_msg, "itemconfigure $name error ");
2232 is($lb->itemcget(0, $name), $get_val, "still same value after error");
2233 }
2234 $lb->destroy;
2235 }
2236
2237 {
2238 my $lb = $mw->$Listbox;
2239 $lb->insert(qw(end a b c d));
2240 is($lb->itemcget(0, -fg), undef, "itemcget with undef value");
2241 $lb->destroy;
2242 }
2243
2244 {
2245 my $lb = $mw->$Listbox;
2246 $lb->insert(qw(end a b c d));
2247 $lb->itemconfigure(0, -fg => "red");
2248 is($lb->itemcget(0, -fg), "red");
2249 $lb->destroy;
2250 }
2251
2252 {
2253 my $lb = $mw->$Listbox(Name => "lb");
2254 $lb->insert(qw(end a b c d));
2255 eval { $lb->itemcget(0) };
2256 like($@, qr{\Qwrong # args: should be ".lb itemcget index option"},
2257 "itemcget error");
2258 $lb->destroy;
2259 }
2260
2261 # No shortcuts in Perl/Tk, skipping shortcut test
2262
2263 # General item configuration issues
2264 {
2265 my $lb = $mw->$Listbox(Name => "lb");
2266 $lb->insert(qw(end a));
2267 $lb->itemconfigure(0, -fg => "red");
2268 $lb->delete(0, "end");
2269 $lb->insert("end", "a");
2270 is($lb->itemcget(0, -fg), undef,
2271 q{listbox item configurations and widget based deletions});
2272 $lb->destroy;
2273 }
2274
2275 {
2276 my $lb = $mw->$Listbox(Name => "lb");
2277 $lb->insert(end => a => b => "c");
2278 $lb->itemconfigure(0 => -fg => "red");
2279 $lb->insert(0, 1, 2, 3, 4);
2280 is($lb->itemcget(0, -fg), undef,
2281 q{listbox item configurations and widget based inserts});
2282 is($lb->itemcget(4, -fg), "red");
2283 $lb->destroy;
2284 }
2285 }
2286
2287 # Additional visual itemconfigure tests
2288 if ($visual) {
2289 skip("no itemconfigure in Tk800.x", 1) # XXX correct!
2290 if $Tk::VERSION < 804;
2291
2292 my $lb = $mw->$Listbox->pack;
2293 my $next;
2294 my $b = $mw->Button(-text => "Next",
2295 -command => sub { $next++ })->pack;
2296 $lb->insert("end", 1..10);
2297 $lb->itemconfigure(0, -background => "red");
2298 $lb->waitVariable(\$next);
2299 $lb->itemconfigure(1, -background => "green");
2300 $lb->waitVariable(\$next);
2301 $lb->itemconfigure(0, -foreground => "white");
2302 $lb->waitVariable(\$next);
2303 $lb->itemconfigure(1, -foreground => "red");
2304 $lb->waitVariable(\$next);
2305 $lb->selectionSet(2,3);
2306 $lb->waitVariable(\$next);
2307 $lb->itemconfigure(2, -selectforeground => "cyan");
2308 $lb->waitVariable(\$next);
2309 $lb->itemconfigure(3, -selectbackground => "orange");
2310 $lb->waitVariable(\$next);
2311 $lb->destroy;
2312 }
2313
2314 resetGridInfo();
2315
2316 sub record {
2317 push @log, join(" ", @_);
2318 }
2319
2320 sub getsize {
2321 my $w = shift;
2322 my $geom = $w->geometry;
2323 $geom =~ /(\d+x\d+)/;
2324 $1;
2325 }
2326
2327 sub resetGridInfo {
2328 # Some window managers, such as mwm, don't reset gridding information
2329 # unless the window is withdrawn and re-mapped. If this procedure
2330 # isn't invoked, the window manager will stay in gridded mode, which
2331 # can cause all sorts of problems. The "wm positionfrom" command is
2332 # needed so that the window manager doesn't ask the user to
2333 # manually position the window when it is re-mapped.
2334 #
2335 # On the other hand, calling these lines seem to cause strange
2336 # test failures with almost all window managers. The same lines of
2337 # code in Tcl/Tk seem also to be problematic. So run these lines only
2338 # for mwm
2339 if (eval {
2340 require Tk::Mwm;
2341 $mw->mwm('ismwmrunning');
2342 }) {
2343 $mw->withdraw;
2344 $mw->positionfrom('user');
2345 $mw->deiconify;
2346 }
2347 }
2348
2349 # Procedure that creates a second listbox for checking things related
2350 # to partially visible lines.
2351 sub mkPartial {
2352 eval {
2353 $partial_top->destroy
2354 if Tk::Exists($partial_top);
2355 };
2356 $partial_top = $mw->Toplevel;
2357 $partial_top->geometry('+0+0');
2358 $partial_lb = $partial_top->Listbox(-width => 30, -height => 5);
2359 $partial_lb->pack('-expand',1,'-fill','both');
2360 $partial_lb->insert('end','one','two','three','four','five','six','seven',
2361 'eight','nine','ten','eleven','twelve','thirteen',
2362 'fourteen','fifteen');
2363 $partial_top->update;
2364 my $geom = $partial_top->geometry;
2365 my($width, $height) = $geom =~ /(\d+)x(\d+)/;
2366 $partial_top->geometry($width . "x" . ($height-3));
2367 $partial_top->update;
2368 }
2369
2370 __END__
2371