Tk-ImageButton

 view release on metacpan or  search on metacpan

ImageButton.pm  view on Meta::CPAN

$Tk::ImageButton::VERSION = '1.0';

package ImageButton;

use strict;
use Tk;

require Tk::Label;
use base qw(Tk::Derived Tk::Label);

Construct Tk::Widget 'ImageButton';

sub ClassInit
{
	my ($class, $mw) = @_;
	$class->SUPER::ClassInit($mw);

	$mw->bind($class, '<Enter>', \&_IBEnter);		# Call _IBEnter when the mouse moves over the button.
	$mw->bind($class, '<Leave>', \&_IBLeave);		# Call _IBLeave when the mouse moves off the button.
	$mw->bind($class, '<ButtonPress-1>', \&_IBPress);	# Call _IBPress when the left mouse button is pressed over the button.
	$mw->bind($class, '<ButtonRelease-1>', \&_IBRelease);	# Call _IBRelease when the left mouse button is released.
}

sub Populate
{
	my ($widget, $args) = @_;

	# -imagedisplay  - Required - This option is used to define the image that will be used as the default image for the button.
	#                             This is also the fallback option should none of the other images be set.
	#                             Needs to be passed a Tk::Photo
	# -imageover     - Optional - This option is used to define the image that is used when the mouse pointer is over the button.
	#                             If this is not set then -imagedisplay will be used.
	#                             Needs to be passed a Tk::Photo
	# -imageclick    - Optional - This option is used to define the image that is used when the left mouse button is clicked
	#                             over the button. If this is not set then -imagedisplay will be set.
	#                             Needs to be passed a Tk::Photo
	# -imagedisabled - Optional - This option is used to define the image that will be used when the button is disabled.
	#                             If this is not set then -imagedisplay will be used.
	#                             Needs to be passed a Tk::Photo
	# -state         - Optional - This stores the state of the button, either 'normal' or 'disabled'. This defaults to 'normal'
	#                             which is why I've called it optional. Changing the state will automatically update the button.
	# -command       - Optional - When you set the command, when the mouse button is released then the command is run. If you're
	#                             going to be using a button, then you really should set a command!

	$widget->ConfigSpecs(
				-imagedisplay => [qw/METHOD imageDisplay ImageDisplay/, 0],
				-imageover => [qw/METHOD imageOver ImageOver/, 0],
				-imageclick => [qw/METHOD imageClick ImageClick/, 0],
				-imagedisabled => [qw/METHOD imageDisabled ImageDisabled/, 0],
				-state => [qw/METHOD state State/, 'normal'],
				-command => ["CALLBACK", "command", "Command", undef]
	);

	$widget->configure(-borderwidth => 0);
	$widget->SUPER::Populate;

	# This section sets up the variables we'll be using in this widget. They are pretty self-explanatory, each one corresponds
	# to an option (listed above). The 'image_*' vars store Tk::Photo data. The 'state' var stores the state of the button.

	$widget->{image_display} = 0;
	$widget->{image_click} = 0;
	$widget->{image_over} = 0;
	$widget->{image_disabled} = 0;
	$widget->{state} = 'normal';
}

# When the mouse pointer moves over the button, if the button is enabled and -imageover has been set then we change the
# visible image to that of the -imageover option. If -imageover isn't set or the button is disabled then we don't change
# the image from that of the default.



( run in 1.682 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )