Re: Fun with radio buttons



Steve Fox <drfickle k-lug org> writes:

Gtk 1.x issue here:

I'm having problems with radio buttons. I want my application's
preference window to set them to the stored values when it is created. I
made sure to connect the radio buttons' callbacks to the 'clicked'
signal (rather than 'toggled') so that they were only called when a user
made a mouse or keyboard action (or so I thought).

I'm using set_active() to set the selected radio button to the stored
preference. set_active() sends the 'toggled' signal as it should, but it
seems to be also sending the 'clicked' signal as well (as evidenced by
the code below).

Since I have implemented a configuration saving system like GConf where
things are saved and activated immediately, this causes unnecessary file
saves when set_active() is called.

I suppose it's impossible to signal_connect after set_active? You
wouldn't ask if it was the case :)..

Currently we are working around this by comparing the current state to
the stored preference, and only saving if they differ.

Does anyone know of a more elegant solution?

I don't think it's more elegant because it's much longer code :)
but I'd do something of the like:

-=-=---=-=---=-=---=-=---=-=--
#!/usr/bin/perl -w
use strict;
use Gtk;

init Gtk;

my $win = new Gtk::Window("toplevel");
$win->signal_connect( "delete_event", sub { Gtk->exit( 0 ); } );
my $vbox = new Gtk::VBox;
my $btn1 = new Gtk::RadioButton("Button1");
my $btn2 = new Gtk::RadioButton("Button2", $btn1);

my $sig_handler_btn1 = $btn1->signal_connect(clicked => sub { print "clicked btn 1\n" } );
my $sig_handler_btn2 = $btn2->signal_connect(clicked => sub { print "clicked btn 2\n" } );

$win->add($vbox);
$vbox->pack_start_defaults($btn1);
$vbox->pack_start_defaults($btn2);

$win->show_all();

$btn2->signal_handler_block($sig_handler_btn2);
$btn2->set_active(1);
$btn2->signal_handler_unblock($sig_handler_btn2);

Gtk->main();
-=-=---=-=---=-=---=-=---=-=--

Why is set_active() sending the 'clicked' signal?

Probably that it solves more problems that it creates.

-- 
Guillaume Cottenceau - http://people.mandrakesoft.com/~gc/



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