package Tk::KMenu; use strict; use warnings 'all'; use Tk; use Tk::Toplevel; sub new { my( $class, $top ) = @_; # pick parameters my $obj = { # create reference container => $top -> Toplevel( # create and save new toplevel window -bd => 1, -relief => 'raised', ), shadow => $top -> Toplevel( # create a shadow window -background => '#0f0f0f', # set the background to really dark grey ), menu => [], # save menu entries }; bless $obj, $class; # bless the reference $obj->{container} -> overrideredirect( 1 ); # delete menu border $obj->{shadow} -> overrideredirect( 1 ); # delete shadow border $obj->close; # don't show menu until ->show # if the menu loses focus, close it $obj->{container} -> bind( '' => sub { $obj->close; }, ); return $obj; # return object } # new sub add { my( $self, @pairs ) = @_; # pick parameters for( my $i = 0; $i < @pairs; $i += 2 ) { # iterate over @pairs # add a menu entry push( @{ $self->{menu} }, $self->{container} -> Button( -text => $pairs[$i], -anchor => 'w', -relief => 'flat', -bd => 1, -command => $pairs[$i+1], ) -> pack( -fill => 'x', ), ); } # foreach return $self; # return object } # add