Fun with radio buttons



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.

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? Why is set_active() sending
the 'clicked' signal?

-----------------------------------------------------------------------

#!/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);

$btn1->signal_connect("clicked", \&clicked);
$btn2->signal_connect("clicked", \&clicked);

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

$win->show_all();

$btn2->set_active(1);

Gtk->main();
exit;

sub clicked {
  print "Button clicked\n";
}

-- 

Steve Fox
http://k-lug.org



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