Re: Making a subclass help



Hmm, another problem. This what I have so far, more or less what I had 
before plus some code to test the buttons with :

<code>

#!/usr/bin/perl -w

use strict;

package Mup::IButton;

use Gtk2;

#define a button with an index and an embedded image
use Glib::Object::Subclass
    Gtk2::Button::,
    signals => {
        show => \&on_show,
    },
    properties => [
        Glib::ParamSpec->int (
                'index', # name
                'Index', # nickname
                'The index number of the button', #blurb
                0, # min
                32767, # max
                0, # default
                [qw/readable writable/] #flags
        ),
        Glib::ParamSpec->string (
            'image',
            'Image',
            'The image file to load into the button',
            "",
            [qw/readable writable/]
        ),
    ];
    
sub INIT_INSTANCE{
        my $self = shift;
        $self->{index} = 0;
        $self->{image} = "";
        my $image = Gtk2::Image->new_from_file($self->{image});
        $self->add($image);
        $image->show;
}

sub on_show {
        my $self = shift;
        $self->set_image (image => $self->{image});
        $self->signal_chain_from_overridden;
}

sub set_index{
    my $self = shift;
    my %params = @_;
    $self->{index} = $params{index};
}

sub get_index{
    my $self = shift;
    return $self->{index};
}

sub set_image{
    my $self = shift;
    my %params = @_;
    if (-e $params{image}){
        $self->{image} = $params{image};
        $self->child->set_from_file($self->{image});
    }
}

sub get_image{
    my $self = shift;
    return $self->{image};
}

package main;

use Gtk2 '-init';
use Gtk2::SimpleMenu;

use constant TRUE => 1;
use constant FALSE => 0;

my @cells; #Game area array

my $blankcell = "blank.png"; #Image of empty cell

#create main window
my $window = Gtk2::Window->new;
$window->set_title('Solitaire');
$window->signal_connect(destroy => sub {Gtk2->main_quit;});
$window->set_border_width(3);

#create layout widget 1
my $vbox1 = Gtk2::VBox->new(FALSE, 1);
$window->add($vbox1);

#create the menu structure
my $menu_tree = [
    _File => {
        item_type => '<Branch>',
        children => [
            _Quit => {
                callback => sub { Gtk2->main_quit; },
                callback_action => 0,
                accelerator => '<ctrl>Q',
            },
        ]
    }
];

#build the menu
my $menu = Gtk2::SimpleMenu->new(menu_tree => $menu_tree);
$vbox1->pack_start($menu->{widget}, TRUE, TRUE, 0);

#create a 7x7 grid of buttons
for (my $i = 0; $i <= 6; $i++){
    my $hbox = Gtk2::HBox->new(FALSE, 1);
    $vbox1->pack_start($hbox, TRUE, TRUE, 0);
    for (my $j = 0; $j <= 6; $j++){
        my $cell = Mup::IButton->new(index => $i * 7 + $j, image => "blank.png");
        $cell->set_relief('half');
        $cell->signal_connect(clicked => sub { &clickcell($cell->get_index); });
        push (@cells, $cell);
        $hbox->pack_start($cell, TRUE, TRUE, 0);
    }
}


$window->show_all;

Gtk2->main;


sub closeapp{
    Gtk2->main_quit;
    return 0;
}

sub clickcell{
    my $clicked = @_;
    print $clicked . "\n";
    print $cells[$clicked]->get_image . "\n";
    $cells[$clicked]->set_image(image => "ball.png");
    return 0;
}

</code>

Basically what I'm trying to do is make an array of IButtons that, when 
clicked, updates the image of the button clicked. Not quite my final goal, 
but a start.

What _is_ happening, however, is that when I click a cell instead of 
passing the clicked IButton's index to clickcell() it's passing 1 instead. 
What's puzzling me in particular is that before adding the image property 
the correct IButton index was being passed to the function.

I'm also getting a similar bug with the image property, but I'm pretty sure 
that once the index problem is solved I'll be able to fix the image one 
quickly enough.

What I'm eventually going to make is a simple peg-solitaire game. It's just 
a test program that I start off making each time I try a different 
language. It's got simple rules, flexes the mind muscles a bit, gives a 
common example to compare against and also produces an 'artifact' that I 
can say "woo look what I built" about :)

Grahame



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]