2 # This file is part of Language::Befunge::Debugger.
3 # Copyright (c) 2007 Jerome Quelin, all rights reserved.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the same terms as Perl itself.
9 package Language
::Befunge
::Debugger
::Breakpoints
;
14 use List
::MoreUtils qw
[ firstidx
];
16 use Tk
; # should come before POE
28 # my $id = Language::Befunge::Debugger::Breakpoints->spawn( %opts );
30 # create a new debugger gui for a befunge script. refer to the embedded
31 # pod for an explanation of the supported options.
34 my ($class, %opts) = @_;
36 my $session = POE
::Session
->create(
38 _start
=> \
&_on_start
,
39 _stop
=> sub { print "ouch!\n" },
41 breakpoint_add
=> \
&_do_breakpoint_add
,
42 visibility_toggle
=> \
&_do_visibility_toggle
,
45 _b_breakpoint_remove
=> \
&_on_b_breakpoint_remove
,
57 # breakpoint_add( $brkpt );
59 # Add $brkpt to the list of breakpoints.
61 sub _do_breakpoint_add
{
62 my ($h, $brkpt) = @_[HEAP
, ARG0
];
64 my @elems = $h->{list
}->get(0, 'end');
67 $h->{list
}->delete(0, 'end');
68 $h->{list
}->insert(0, sort @elems);
69 $h->{list
}->selectionSet( firstidx
{ $_ eq $brkpt } $h->{list
}->get(0, 'end') );
70 $h->{but_remove
}->configure(-state => 'normal' );
75 # visibility_toggle();
77 # Request window to be hidden / shown depending on its previous state.
79 sub _do_visibility_toggle
{
82 my $method = $h->{mw
}->state eq 'normal' ?
'withdraw' : 'deiconify';
91 # _on_start( \%opts );
93 # session initialization. %opts is received from spawn();
96 my ($k, $h, $from, $s, $opts) = @_[KERNEL
, HEAP
, SENDER
, SESSION
, ARG0
];
100 my $top = $opts->{parent
}->Toplevel(-title
=> 'Breakpoints');
102 $h->{list
} = $top->Listbox->pack;
103 $h->{but_remove
} = $top->Button(
105 -state => 'disabled',
107 -command
=> $s->postback('_b_breakpoint_remove')
108 )->pack(-side
=>'left',-fill
=>'x',-expand
=>1);
112 -command
=> $s->postback('visibility_toggle')
113 )->pack(-side
=>'left',-fill
=>'x',-expand
=>1);
116 $top->protocol( WM_DELETE_WINDOW
=> $s->postback('visibility_toggle') );
117 $top->bind( '<F8>', $s->postback('visibility_toggle') );
120 $top->update; # force redraw
121 $top->resizable(0,0);
122 my ($maxw,$maxh) = $top->geometry =~ /^(\d+)x(\d+)/;
123 $top->maxsize($maxw,$maxh); # bug in resizable: minsize in effet but not maxsize
127 $h->{parent_session
} = $from->ID;
128 # initial breakpoint?
129 $k->yield('breakpoint_add', $opts->{breakpoint
}) if exists $opts->{breakpoint
};
137 # _b_breakpoint_remove();
139 # called when the user wants to remove a breakpoint.
141 sub _on_b_breakpoint_remove
{
142 my ($k, $h) = @_[KERNEL
, HEAP
];
143 my ($idx) = $h->{list
}->curselection;
144 return unless defined $idx;
145 my $brkpt = $h->{list
}->get($idx);
146 $h->{list
}->delete($idx);
147 $k->post( $h->{parent_session
}, 'breakpoint_remove', $brkpt );
149 $h->{but_remove
}->configure(-state=>'disabled') if $h->{list
}->index('end') == 0;
160 Language::Befunge::Debugger::Breakpoints - a window listing breakpoints
166 my $id = Language::Befunge::Debugger::Breakpoints->spawn(%opts);
167 $kernel->post( $id, 'visibility_toggle' );
173 LBD::Breakpoints implements a POE session, creating a Tk window listing
174 the breakpoints set in a debugger session. The window can be hidden at
181 =head2 my $id = Language::Befunge::Debugger::Breakpoints->spawn( %opts );
183 Create a window listing breakpoints, and return the associated POE
184 session ID. One can pass the following options:
190 A Tk window that will be the parent of the toplevel window created. This
191 parameter is mandatory.
194 =item breakpoint => $brkpt
196 An optional breakpoint to be added during session creation.
204 The newly created POE session accepts the following events:
209 =item breakpoint_add( $brkpt )
211 Add a breakpoint in the list of breakpoints.
214 =item visibility_toggle()
216 Request the window to be hidden or restaured, depending on its previous
217 state. Note that closing the window is actually interpreted as hiding
227 L<Language::Befunge::Debugger>.
233 Jerome Quelin, C<< <jquelin at cpan.org> >>
237 =head1 COPYRIGHT & LICENSE
239 Copyright (c) 2007 Jerome Quelin, all rights reserved.
241 This program is free software; you can redistribute it and/or modify
242 it under the same terms as Perl itself.