File: CheckListBook.pm

package info (click to toggle)
libdemeter-perl 0.9.27%2Bds6-9
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 74,028 kB
  • sloc: perl: 73,233; python: 2,196; makefile: 1,999; ansic: 1,368; lisp: 454; sh: 74
file content (564 lines) | stat: -rw-r--r-- 14,903 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
package Demeter::UI::Wx::CheckListBook;

=for Copyright
 .
 Copyright (c) 2006-2019 Bruce Ravel (http://bruceravel.github.io/home).
 All rights reserved.
 .
 This file is free software; you can redistribute it and/or
 modify it under the same terms as Perl itself. See The Perl
 Artistic License.
 .
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut

use strict;
use warnings;

use Demeter::Constants qw($NUMBER);
use Demeter::UI::Wx::Colours;

use Wx qw( :everything );
use Wx::Event qw(EVT_LISTBOX EVT_LEFT_DOWN EVT_MIDDLE_DOWN EVT_RIGHT_DOWN EVT_CHECKLISTBOX
		 EVT_LEFT_DCLICK EVT_MOUSEWHEEL);

use base 'Wx::SplitterWindow';

use vars qw(@initial_page_callback);
@initial_page_callback = (sub{ print "callback 1\n" },
			  sub{ print "callback 2\n" },
			 );

## height, width, ratio
sub new {
  my ($class, $parent, $id, $position, $size, $initial) = @_;
  #print join(" ", $parent, $id, $position, $size), $/;
  $position = Wx::Size->new(@$position) if (ref($position) !~ m{Point});
  $size = Wx::Size->new(@$size) if (ref($size) !~ m{Size});
  my $self = $class->SUPER::new($parent, $id, $position, $size, wxSP_NOBORDER );
  my ($w, $h) = ($size->GetWidth, $size->GetHeight);

  if (($w <= 0) or ($h <= 0)) {
    ($w = 520) if ($w <= 0);
    ($h = 400) if ($h <= 0);
    $self -> SetSize($w, $h);
  };

  #$self->{LEFT} = Wx::Panel->new( $self, -1, wxDefaultPosition, Wx::Size->new(int($w/4),$h) );
  #my $box = Wx::BoxSizer->new( wxVERTICAL );
  #$self->{LEFT} -> SetSizerAndFit($box);

  $self->{LIST} = Wx::CheckListBox->new($self, -1, wxDefaultPosition, Wx::Size->new(int($w/3),$h), [ ], wxLB_SINGLE);
  $self->{LIST}->{datalist} = []; # see modifications to CheckListBox at end of this file....
  $self->{LIST} -> SetFont( Wx::Font->new( 8, wxDEFAULT, wxNORMAL, wxNORMAL, 0, "" ) );
  $self->{LIST}->{PARENT} = $self;
  EVT_LEFT_DOWN(   $self->{LIST},        sub{OnLeftDown(@_)}  );
  EVT_LEFT_DCLICK( $self->{LIST},        sub{OnLeftDclick(@_)});
  EVT_MIDDLE_DOWN( $self->{LIST},        sub{OnMiddleDown(@_)});
  EVT_RIGHT_DOWN(  $self->{LIST},        sub{OnRightDown(@_)} );
  EVT_LISTBOX(     $self, $self->{LIST}, sub{OnList(@_)}      );
  EVT_CHECKLISTBOX($self, $self->{LIST}, sub{OnCheck(@_)}     );
  EVT_MOUSEWHEEL(  $self->{LIST},        sub{OnWheel(@_)}     );

  #$box -> Add($self->{LIST}, 1, wxGROW|wxALL, 0);

  $self->{PAGE}  = Wx::Panel->new($self, -1, wxDefaultPosition, wxDefaultSize); #Wx::Size->new($w-int($w/4),$h));

  $self->SplitVertically($self->{LIST}, $self->{PAGE}, -int($w)-10);


  $self->{PAGEBOX} = Wx::BoxSizer->new( wxVERTICAL );
  $self->{PAGE} -> SetSizer($self->{PAGEBOX});

  $self->{W} = $w;
  $self->{H} = $h;
  $self->{CTRL}  = 0;
  $self->{SHIFT} = 0;
  $initial ||= Wx::Panel->new($self, -1, wxDefaultPosition, wxDefaultSize);
  $self->{initial} = $initial;
  $self->{initial} -> Reparent($self->{PAGE});
  $self->{PAGEBOX} -> Add($self->{initial}, 1, wxGROW|wxALL, 5);
  $self->{initial} -> Hide;
  $self->InitialPage;
  return $self;
};

sub InitialPage {
  my ($self) = @_;
  $self->{VIEW}->Hide if $self->{VIEW};
  $self->{LIST}->Clear;
  $self->{LIST}->AddData('Path list', $self->{initial});
  $self->{LIST}->Select(0);

  $self->{VIEW} = $self->{initial};
  $self->{VIEW} -> Show(1);

  #$self->{LIST}-> SetIndexedData(0, $self->{initial});
  $self->{LIST}-> Show;
};

sub do_callback {
  my ($self, $which) = @_;
  my $this = $self->{callbacks}->[$which];
  &$this;
};

sub set_initial_page_callback {
  my ($self, @callbacks) = @_;
  @initial_page_callback = @callbacks;
};

sub AddPage {
  my ($self, $page, $text, $select, $imageid, $position) = @_;
  my $end = (defined($position)) ? $position : $self->{LIST} -> GetCount;
  #print '>>>>>>', $text, $/;
  $self->{LIST} -> InsertData($text, $end, $page);
  #$self->{LIST} -> SetIndexedData($end, $page);
  $self->{LIST} -> Deselect($self->{LIST}->GetSelection);
  $self->{LIST} -> Select($end) if $select;

  $page->Reparent($self->{PAGE});
  $self->{initial} -> Hide;
  $self->{VIEW} -> Hide if ($self->{VIEW} and $self->{VIEW}->IsShown);

  $self->{VIEW}  = $page;
  $self->{VIEW} -> Show(1);
  $self->{PAGEBOX} -> Layout;
  return $self->{LIST} -> GetCount;
};

# sub MovePageAfter {
#   my ($self, $page_or_id, $pos) = @_;
#   my ($page, $id) = $self->page_and_id($page);
#   my $saved_page = $self->{LIST}-> GetClientData($id);

#   $self-> RemovePage($page);
#   $self->{LIST} -> InsertItems([$self->GetPageText($id)], $pos+1);
#   $self->{LIST} -> SetClientData($pos+1, $saved_page);
#   return $self->{LIST} -> GetCount;
# };

sub RemovePage {
  my ($self, $page) = @_;
  my ($obj, $id) = $self->page_and_id($page);
  return 0 if ($id == -1);
  my $new = ($id == 0) ? $id+1 : $id - 1;
  ($new = 0) if ($new >= $self->GetPageCount);
  ##print " ======== $id   $new\n";
  $self->{VIEW} -> Hide;
  $self->{LIST} -> GetIndexedData($new) -> Show;
  $self->{VIEW}  = ($self->{LIST}->IsEmpty) ? q{} : $self->{LIST}->GetIndexedData($new);
  $self->{LIST} -> Select($new);
  $self->{LIST} -> DeleteData($id);
  return 1;
};

sub DeletePage {
  my ($self, $page) = @_;
  my ($obj, $id) = $self->page_and_id($page);
  return 0 if ($id == -1);
  $self->RemovePage($id);
  $obj->Destroy if $obj !~ m{Panel}; # need to save initial page for later use
  ($self->{VIEW} = q{}) if ($self->{LIST}->IsEmpty);
  return 1;
};

sub Clear {
  my ($self, $page) = @_;
  foreach my $i (reverse(0 .. $self->GetPageCount-1)) {
    $self->DeletePage($i);
  };
};

## take a page object or a page id and return both
sub page_and_id {
  my ($self, $arg) = @_;
  my ($id, $obj) = (-1,-1);
  if ($arg =~ m{\A$NUMBER\z}) {
    $id = $arg;
    $obj = $self->{LIST}->GetIndexedData($arg)
  } else {
    foreach my $pos (0 .. $self->{LIST}->GetCount-1) {
      if ($arg eq $self->{LIST}->GetIndexedData($pos)) {
	$id = $pos;
	$obj = $arg;
	last;
      };
    };
  };
  return ($obj, $id);
};

sub DeleteAllPages {
  my ($self) = @_;
  $self->{LIST}->SetSelection(wxNOT_FOUND);
  $self->InitialPage;
};

sub GetCurrentPage {
  my ($self) = @_;
  return $self->{VIEW};
};

sub GetPage {
  my ($self, $pos) = @_;
  return $self->{LIST}->GetIndexedData($pos);
};

sub GetPageCount {
  my ($self) = @_;
  return $self->{LIST}->GetCount;
};

sub GetPageText {
  my ($self, $pos) = @_;
  return $self->{LIST}->GetString($pos);
};

sub GetSelection {
  my ($self) = @_;
  return $self->{LIST}->GetSelection;
};
sub SetSelection {
  my ($self, $pos) = @_;
  $self->{LIST} -> SetSelection($pos);
  ## plotzing here:
  ##print join("|", caller), $/;
  $self->{VIEW} -> Hide;
  $self->Refresh;
  $self->{LIST} -> GetIndexedData($pos) -> Show(1);
  $self->{VIEW} = $self->{LIST} -> GetIndexedData($pos);
  $self->{PAGEBOX} -> Layout;
};
{
  no warnings 'once';
  # alternate names
  *ChangeSelection = \ &SetSelection;
}

sub SetPageText {
  my ($self, $arg, $text) = @_;
  my ($obj, $id) = $self->page_and_id($arg);
  $self->{LIST}->SetString($id, $text);
};

#  HitTest
#  InsertPage

sub AdvanceSelection {
  my ($self, $dir) = @_;
  my $sel = $self->{LIST}->GetSelection;

  return if (($sel == 0) and (not $dir)); # already at top
  return if (($sel == $self->GetPageCount-1) and $dir); # already at bottom

  my $new = ($dir) ? $sel+1 : $sel-1;
  $self->SetSelection($new);
};

sub GetThemeBackgroundColour {
  return $wxBGC;
};

sub Check {
  my ($self, $pos, $value) = @_;
  my ($obj, $id) = $self->page_and_id($pos);
  $self->{LIST}->Check($id, $value);
  return $pos;
};
sub IsChecked {
  my ($self, $pos) = @_;
  my ($obj, $id) = $self->page_and_id($pos);
  return $self->{LIST}->IsChecked($id);
};

sub one {
  return 1;
};
sub noop {
  return 1;
};
{
  no warnings 'once';
  # alternate names
  *AssignImageList = \ &noop;
  *GetImageList	   = \ &noop;
  *SetImageList	   = \ &noop;
  *GetPageImage	   = \ &noop;
  *SetPageImage	   = \ &noop;
  *SetPadding	   = \ &noop;
  *SetPageSize	   = \ &noop;

  *GetRowCount	   = \ &one;
  *OnSelChange	   = \ &one;
}


sub OnLeftDown {
  my ($self, $event) = @_;
  if ($event->ControlDown) {
    #print "control left clicking\n";
    $self->{PARENT}->{CTRL} = 1;
    $self->{PARENT}->{NOW}  = $self->GetSelection;
  };
  if ($event->ShiftDown) {
    #print "shift left clicking\n";
    $self->{PARENT}->{SHIFT} = 1;
    $self->{PARENT}->{NOW}  = $self->GetSelection;
  };
  $event->Skip;
};
sub OnLeftDclick {
  my ($self, $event) = @_;
  #print "left double click\n";
  $self->GetParent->RenameSelection;
};

sub RenameSelection {
  my ($self) = @_;
  my $check_state = $self->{LIST}->IsChecked($self->{LIST}->GetSelection);
  my $oldname = $self->{LIST}->GetStringSelection;
  my $ted = Wx::TextEntryDialog->new( $self, "Enter the new name for \"$oldname\"", "Rename item", $oldname, wxOK|wxCANCEL, Wx::GetMousePosition);
  return if ($ted->ShowModal == wxID_CANCEL);
  my $newname = $ted->GetValue;
  return if ($newname =~ m{\A\s*\z});
  $self->{LIST}->SetString($self->{LIST}->GetSelection, $newname);
  my $page = $self->{LIST}->GetIndexedData($self->{LIST}->GetSelection);
  $page->Rename($newname) if $page->can('Rename');
  $self->{LIST}->Check($self->{LIST}->GetSelection, $check_state);
};

sub OnRightDown {
  my ($self, $event) = @_;
  #print "right clicking\n";
  ##$event->Skip;
}
sub OnMiddleDown {
  my ($self, $event) = @_;
  #print "middle clicking\n";
  ##$event->Skip;
}

sub OnList {
  my ($self, $event) = @_;
  my $sel = $event->GetSelection;
  return if ($sel == -1);
  my ($ctrl, $shift, $now) = ($self->{CTRL}, $self->{SHIFT}, $self->{NOW});
  $self->{CTRL}  =  0;
  $self->{SHIFT} =  0;
  $self->{NOW}   = -1;
  if ($ctrl) {
    my $onoff = ($self->{LIST}->IsChecked($sel)) ? 0 : 1;
    $self->{LIST}->Check($sel,$onoff);
    $self->{LIST}->Select($now);
  } elsif ($shift) {
    my ($i, $j) = sort {$a <=> $b} ($now, $sel);
    foreach my $pos ($i .. $j) {
      $self->{LIST}->Check($pos,1);
    };
    $self->{LIST}->Select($now);
  } else {
    $self->{VIEW} -> Hide;
    $self->{LIST} -> GetIndexedData($sel) -> Show;
    $self->{VIEW} = $self->{LIST} -> GetIndexedData($sel);
    $self->{PAGEBOX} -> Layout;
  };
};

sub OnCheck {
  my ($self, $event) = @_;
  my $sel = $event->GetSelection;
  #print $sel, $/;
  $event->Skip;
};

sub OnWheel {
  my ($self, $event) = @_;
  if ($event->GetWheelRotation < 0) { # scroll down, inrease selection
    $self->{PARENT}->AdvanceSelection(1);
  } else {			      # scroll up, decrease selection
    $self->{PARENT}->AdvanceSelection(0);
  };

  $event->Skip;
};
1;


=head1 NAME

Demeter::UI::Wx::CheckListBook - A CheckListBox-based notebook

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

Wx:CheckListBook is a class similar to Wx::Notebook but which uses a
Wx::CheckListBox to show the labels instead of tabs.

  $book = Demeter::UI::Wx::CheckListBook->new($parent, -1);

=head1 DESCRIPTION

This is a notebook which uses a Wx::CheckListBox to control the pages
of the notebook.  That is, the selected item in the CheckListBox has
its associated page displayed.  Each selectable item also has an
associated checkbox.  This allows an obvious chennel for simultaneous
single and multiple selection of items from the list.

Note that images associated with pages is not supported.  In fact,
some care is taken to make the list as compact as possible to allow
for maximum information display.

In the context of Artemis, this is used to display the set of paths
associated with a data set.

=head1 METHODS

=over 4

=item C<new>

The constructor.  It takes the same arguments as any other notebook
widget.

=item C<InitialPage>

This is called when the CheckListBook is initially created or whenever
its contents are deleted.  This makes a simple page intended to offer
instructions for how to begin using the CheckListBook.

=item C<AddPage>

Add a new page to the CheckListBook.  The arguments are the same as
for Wx::Notebook.

=item C<RemovePage>

Deletes the specified page, without deleting the associated window.
The argument is the same as for Wx::Notebook.

=item C<DeletePage>

Deletes the specified page, and the associated window.  The call to
this function generates the page changing events.  The argument is the
same as for Wx::Notebook.

=item C<Clear>

Same as C<DeleteAllPages> but without calling C<InitialPage>.

=item C<page_and_id>

Take a page object or a page id and return both as a two element list.

  ($obj, $id) = $book->page_and_id($page_or_id);

=item C<DeleteAllPages>

Delete all pages and their associated windows, then call
C<InitialPage>.

=item C<GetCurrentPage>

Return the page object currently displayed.

=item C<GetPage>

Return the page object at a given position.

  $page = $book->GetPage($position);

=item C<GetPageCount>

Return the number of items in the CheckListBook.  Note that position
indexing in 0-based, but this method returns the actual number of
items.

  $n = $book->GetPageCount;

=item C<GetPageText>

Return the label for a given position in the list.

  $label = $book->GetPageText;

=item C<GetSelection>

Returns the index (or list of indeces) of the selected item(s) or
wxNOT_FOUND if no item is selected.

=item C<SetSelection>

Sets the selection to the given item(s) or removes the selection
entirely if wxNOT_FOUND is given as the argument.

=item C<SetPageText>

Set the label for a given position to some given text.

  $book->SetPageText($position, $label);

=item C<AdvanceSelection>

Move the selection forward or backward for a true or false argument.

  $book->AdvanceSelection(1); # move selection forward by 1
  $book->AdvanceSelection(0); # move selection backward by 1

=item C<GetThemeBackgroundColour>

Unused

=item C<Check>

Toggle the checkbox for the given position.

  $book->Check($position, 1); # toggle it on
  $book->Check($position, 0); # toggle it off

=item C<IsChecked>

Returns true is the checkbox for the given position is toggled on.

=item C<RenameSelection>

Change the label at the given position interactively.

=back

=head1 DEPENDENCIES

L<Wx>, Wx::SplitterWindow, Wx::CheckListBox, and L<Const::Fast>

=head1 BUGS AND LIMITATIONS

Please report problems to the Ifeffit Mailing List
(L<http://cars9.uchicago.edu/mailman/listinfo/ifeffit/>)

Patches are welcome.

=head1 AUTHOR

Bruce Ravel (L<http://bruceravel.github.io/home>)

L<http://bruceravel.github.io/demeter/>

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2006-2019 Bruce Ravel (L<http://bruceravel.github.io/home>). All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlgpl>.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut