Re: Making a subclass help



On Sat, 2004-06-12 at 07:52, Grahame White wrote:
but the one thing that has me stumped is getting the image to be
updated.

quite simple, really -- you're changing the filename in your object, but
not doing anything with it.  ;-)   see below.


#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
                undef, # default

passing undef here causes "uninitialized value" warnings for me.  zero
is probably a more sensible value.  same for INIT_INSTANCE.

                [qw/readable writable/] #flags
        ),
      Glib::ParamSpec->string (
          'image',
          'Image',
          'The image file to load into the button',

when i see image here, i think "the image object" not "the image file
name".  perhaps "image-file-name" would be a better name for the
property?

you may find that a more generic and useful widget would be one that
displays images from GdkPixbufs rather from files.  i don't have
blank.png, for example.  you can then layer the deals-with-filenames
stuff on top of that.  dealing with pixbufs can be a bit odd if you're
not used to it, though, so i'll skip it for now.

          "blank.png",
          [qw/readable writable/]
      ),
    ];


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

          # don't forget to tell the Gtk2::Image widget to display this
          # new file.  since we didn't save a reference to it, we have
          # to fetch it from the widget tree; it's our immediate child.
          $self->child->set_from_file ($self->{image});

    }
}


As I said above this mostly works in that, when used, a button is
created with an image in it. However when I use the set_image method
that I defined to say change the image to "ball.png" the image
variable in updated but I'm not sure how to then update the actual
image based on that.

you have to call Gtk2::Image->set_from_<whatevertypesource> ($newone);
this tells the Image widget to reload and update.  since you're using
filenames, that's set_from_filename() (parallel to new_from_file()).


I've been looking about for a way to do this. I've seen EventBox
mentioned a few times but from what I've read the EventBox has to be
inside the object that I want to affect, not something that I can do
with an Image.

erm, no, it's the other way around, you put things into EventBoxes.  the
EventBox is a container that has a GdkWindow, so it can receive events
(it receives base widget events, see the docs for more info).  some
widgets, notably Label and Image, don't have their own GdkWindows, so
they can't receive events.  so, if you want to handle a mouse click on
an Image or Label, you place the label inside an EventBox and connect
handlers to the events on the EventBox.

but the EventBox has nothing to do with getting an Image to update.  :-)


-- 
muppet <scott at asofyet dot org>




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