RE: Linking button handler to data structure



Following is an example of what I've done.. I have a module that
basically ties a hash (ok, tied scalars) for a configuration system.
This is stripped out of a larger module I'm building. A sample also
follows: (written from memory.. not tested..) Getting the value then
is as easy as de-referencing "$config->{'Item1'}" or
'print Dumper $config'.

Hope this helps,
SMF 8)

Shawn Ferris
Senior DBA
Time Warner Telecom

--- Sample script
#!/usr/bin/perl

use Data::Dumper;

use Gtk;
use GUI::Config;

Gtk->init();

my $w=Gtk::Window->new;
   $w->show;

my $hbox=Gtk::HBox->new();
   $hbox->show;
   $w->add($hbox);

my $config=GUI::Config->new;
   $hbox->pack_start(
     $config->new_check_button('Item1'),
     0,0,0,
   );
  
   $hbox->pack_start(
     $config->new_check_button('Item2'),
     0,0,0,
   );

   $hbox->pack_start(
     $config->new_check_button('Item3'),
     0,0,0,
   );
  
   $hbox->pack_start(
     $config->new_check_button('Item4'),
     0,0,0,
   );

my $button=Gtk::Button->new_with_label('Dump Config');
   $hbox->pack_start($button,0,0,0);
   $button->signal_connect('button_press_event' => sub {
       my($button,$config)= _;
       print Dumper $config;
     },
     $config,
   );

Gtk->main;

------- Module below
package GUI::Config;

use strict;
use Gtk;

use Data::Dumper;

sub new {
  my($class)= _;

  my $self={};

  bless $self, ref $class || $class;
}

sub new_check_button {
  my($self,$option)= _;

  my $button=Gtk::CheckButton->new_with_label(ucfirst $option);
     $button->show;
     $button->set_active($self->{$option}||0);

  my $scalar;
  tie $scalar, 'CBTie', $button;

  $self->{$option}=\$scalar;

  $button;
}

1;

package CBTie;

sub TIESCALAR {
  my($class,$widget)= _;

  my $self={
     'widget' => $widget,
  };
  bless $self, ref $class || $class;
}

sub FETCH {
  my($self)= _;
  $self->{widget}->get_active
}

sub STORE {
  my($self,$value)= _;
  $self->{widget}->set_active($value);
}

1;

Message: 1
Date: Fri, 27 Dec 2002 16:18:04 -0800 (PST)
From: Tom Jennings <tomj wps com>
To: gtk-perl-list gnome org
Subject: Linking button handler to data structure

I'm an experienced programmer having trouble with my first real
Gtk/Perl program.

I've built a scrollable list of CheckButtons, each with the name
of some datum from an external source:

      [ ] Item1
      [ ] Item2
      ...
      [ ] ItemN

I want all buttons to have the same handler, and to determine
which button was click by having the handler get the label text,
eg. "Item1" ... "ItemN".

I'm stumped -- how do I reference the Label text that I know is
attached with the CheckButton widget here?

Any hints appreciated.

tomj

The content contained in this electronic message is not intended to
constitute formation of a contract binding TWTC.  TWTC will be
contractually bound only upon execution, by an authorized officer, of
a contract including agreed terms and conditions or by express
application of its tariffs.

This message is intended only for the use of the individual or entity
to which it is addressed. If the reader of this message is not the
intended recipient, or the employee or agent responsible for
delivering the message to the intended recipient, you are hereby
notified that any dissemination, distribution or copying of this
message is strictly prohibited. If you have received this
communication in error, please notify us immediately by replying to
the sender of this E-Mail or by telephone.



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