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
|
package Gtk::ColorSelectButton;
=head1 NAME
Gtk::ColorSelectButton - Choose a color
=head1 SYNOPSIS
use Gtk;
use Gtk::ColorSelectButton;
...
init Gtk;
init Gtk::ColorSelectButton;
...
$color_button = Gtk::ColorSelectButton->new();
$hbox->pack_start($color_button, 1,1,0);
$color_button->show();
...
print $color_button->color; #shortcut for ->get('color')
...
$color_button->set( color => "10 40 250" ); # R G B
=head1 DESCRIPTION
Gtk::ColorSelectButton shows a button with a uniform color. Pressing
this buton pops up the color_selection dialog in which a new color
may be chosen. When the color selection dialog is closed, the chosen
color is reflected in the color of the button.
The gtk variable C<color> provides a way to access the chosen color.
=head1 AUTHOR
Dov Grobgeld <dov@imagic.weizmann.ac.il>, with modifications by
Kenneth Albanowski <kjahds@kjahds.com>.
=head1 COPYRIGHT
Copyright (c) 1998 Dov Grobgeld. All rights reserved. This program may
be redistributed and copied under the same licence as Perl itself.
=cut
use strict;
use vars qw($VERSION @ISA);
use Gtk;
$VERSION = "0.22";
@ISA = qw(Gtk::Button);
# Class defaults data
my @class_def_color = (255,175,0);
sub init {
my($class) = shift;
Gtk::Button->register_subtype($class);
}
sub GTK_CLASS_INIT {
my($class) = shift;
if (Gtk->major_version < 1 or (Gtk->major_version == 1 and Gtk->minor_version < 1)) {
add_arg_type $class "color", "string", 3; #R/W
} else {
add_arg_type $class "color", "GtkString", 3; #R/W
}
}
sub GTK_OBJECT_INIT {
my (@color) = @class_def_color;
my($color_button) = @_;
$color_button->{_color} ||= [@color];
my $preview = new Gtk::Preview -color;
signal_connect $color_button "size_allocate" => sub {
my($self,$allocation) = @_;
my($x,$y,$w,$h) = @$allocation;
$w -= 6;
$h -= 6;
$self->{_preview_width} = $w;
$self->{_preview_height} = $h;
$self->{_preview}->size($w,$h);
update_color $self;
};
$color_button->{_preview} = $preview;
$color_button->add($preview);
signal_connect $color_button "clicked" => \&cb_color_button;
$preview->show;
}
sub GTK_OBJECT_SET_ARG {
my($self,$arg,$id, $value) = @_;
$self->{_color} = [split(' ',$value)];
$self->update_color;
}
sub GTK_OBJECT_GET_ARG {
my($self,$arg,$id) = @_;
return join(' ',@{$self->{_color}});
}
sub new {
my $pkg = shift;
return new Gtk::Widget $pkg, @_;
}
sub update_color($) {
my($this) = shift;
return unless defined $this->{_preview} and defined $this->{_preview_width};
my($preview, $color) = ($this->{_preview}, $this->{_color});
my($width, $height) = ($this->{_preview_width}, $this->{_preview_height});
my($buf) = pack("C3", @$color) x $width;
for(my $i=0;$i<$height;$i++) {
$preview->draw_row($buf, 0, $i, $width);
}
$preview->draw(undef);
}
sub color_selection_ok {
my($widget, $dialog, $color_button) = @_;
my(@color) = $dialog->colorsel->get_color;
@{$color_button->{_color}} = map(int(255.99*$_),@color);
$color_button->update_color();
$dialog->destroy();
delete $color_button->{_cs_window};
}
sub cb_color_button {
my($color_button) = @_;
if (defined $color_button->{_cs_window}) {
if (!$color_button->{_cs_window}->mapped) {
$color_button->{_cs_window}->hide;
}
$color_button->{_cs_window}->show;
$color_button->{_cs_window}->window->raise;
return;
}
my $cs_window=new Gtk::ColorSelectionDialog("Color");
$cs_window->colorsel->set_color(map($_*1/255,@{$color_button->{_color}}));
$cs_window->show();
$cs_window->ok_button->signal_connect("clicked",
\&color_selection_ok,
$cs_window,
$color_button);
$cs_window->cancel_button->signal_connect("clicked",
sub { $cs_window->destroy; delete $color_button->{_cs_window} });
$color_button->{_cs_window} = $cs_window;
}
1;
|