[Fwd: Re: Question about signal]
- From: Bob Caryl <bob fis-cal com>
- To: Gtkmm Mailing List <gtkmm-list gnome org>
- Subject: [Fwd: Re: Question about signal]
- Date: Thu, 06 Apr 2006 15:33:20 -0500
What follow is what I sent to Nietykalny in response to his email
requesting help with connecting signals in general. I cannot help it if
he infers that Gtk::Button::signal_clicked should be used in conjunction
with Gtk::CellRendereText.
-------- Original Message --------
Subject: Re: Question about signal
Date: Mon, 03 Apr 2006 09:43:09 -0500
From: Bob Caryl <bob fis-cal com>
Reply-To: bob fis-cal com
Organization: Fiscal Systems, Inc.
To: Nietykalny <sciemniacz poczta fm>
References: <20060330203627 A40C016009A poczta interia pl>
Hey Nietykalny,
Sorry for long response time. I had to go out of town on the 31st of
last month, so I missed your email.
Here is code that connects a Gtk::Button::signal_clicked signal to a
callback slot that needs to know which button was clicked. I store the
button pointers involved in a std::vector and the parameter I pass to
the callback slot is the index into that vector. The code is somewhat
lengthy, so here goes:
(I use Glade to design my GUI and libglademm to access the objects I
create; hence the calls to "xml->get_widget". You can pretty much
ignore that part and look at where I'm connecting my callback slot to
the Gtk::Button::signal_clicked signal. The following function sets up
my commandline buttons:
void Employee::setup_commandline(void)
{
Gtk::Button *button; // local var for initialize commandline buttons
Gtk::HButtonBox *hbbox;
xml->get_widget("hbuttonbox1",hbbox);
// instantiate the commandline buttons themselves
for(gint i = 0; i < NUMBER_OF_BUTTONS; i++)
{
xml->get_widget(button_captions[i],button);
// connect each
button to the same callback function
// passing the
counter variable "i" as its only parameter
button->signal_clicked().connect(sigc::bind<gint>(
sigc::mem_fun(*this, &Employee::on_button_clicked), i));
if(i >= BUTTON_APPLY) // segregate the last two buttons
to the right
hbbox->set_child_secondary(*button,TRUE);
buttons.push_back(button); // insert the button pointer into
the buttons array
}
}
Notice that I'm passing the variable "i" to the callback slot. I use
sigc::bind to do that.
Now, the callback slot code itself follows:
void Employee::on_button_clicked(gint i)
{
// the formal parameter "i" tells me which button has been
// clicked upon and I act accordingly. BUTTON_QUIT
// and the other values in the switch cases are an enum
// set up to match the index into the std::vector containing
// the pointers to the button objects.
switch(i)
{
case BUTTON_QUIT:
win->hide();
break;
case BUTTON_EXTEND:
if(!add_record())
break;
case BUTTON_EDIT:
if(!table_sel->get_selected())
{
ErrorDialog db(ErrorDialog::ERROR,"No record selected to
edit.");
db.run();
break;
}
set_widget_sensitivity(EDIT_STATE);
name_entry->grab_focus();
break;
case BUTTON_DELETE:
delete_record();
break;
case BUTTON_APPLY:
case BUTTON_CANCEL:
update_database(i);
set_widget_sensitivity(BROWSE_STATE);
break;
}
}
Now, the code snippet (the function it is in is rather long) that
follows shows a callback slot connected to
Gtk::CellRendererText::signal_edited:
jurisdiction_list = Gtk::ListStore::create(jurisdiction_columns);
j_tree->set_model(jurisdiction_list);
j_tree->append_column_editable(" Jurisdiction Name ",
jurisdiction_columns.m_name_text);
j_tree->get_column_cell_renderer(0)->property_cell_background() =
"lightgrey";
j_tree->append_column(" Rate ", jurisdiction_columns.m_rate);
// this line makes the second cell in the row editable
((Gtk::CellRendererText
*)j_tree->get_column_cell_renderer(1))->property_editable() = TRUE;
// this line actually connects the cell renderer signal edited
signal to my callback slot.
((Gtk::CellRendererText
*)j_tree->get_column_cell_renderer(1))->signal_edited().connect(sigc::mem_fun(*this,
&TaxTablewin::on_edited));
Ok... the callback slot itself follows. It is designed to make sure
that the value entered by the user is valid. It also adds the amount
entered into a variable that is displayed elsewhere in the GUI:
// the values passed to the callback slot are what the cell contained
before and after
// the edit occurred, respectively In this case, the user is entering a
sales tax
// rate.
void TaxTablewin::on_edited(const Glib::ustring &str1, const
Glib::ustring &str2)
{
double total;
gchar *output;
total = g_ascii_strtod((gchar *)str2.c_str(),NULL);
// if the value entered is out of range, or less than zero
// I display an error dialog and return; otherwise I process
// the data.
if(errno == ERANGE || total <= 0.00)
{
errno = ERANGE;
Gtk::MessageDialog
db(*this,g_strerror(errno),FALSE,Gtk::MESSAGE_ERROR,Gtk::BUTTONS_OK,TRUE);
Glib::RefPtr<Gdk::Pixbuf> fis_icon =
Gdk::Pixbuf::create_from_inline(-1,fisicon_inline,FALSE);
db.set_icon(fis_icon);
db.set_position(Gtk::WIN_POS_CENTER_ALWAYS);
db.run();
return;
}
Gtk::TreeModel::Row row = *(jurisdiction_sel->get_selected());
row[jurisdiction_columns.m_rate] = g_ascii_strtod((gchar
*)str2.c_str(),NULL);
t_jurisdiction_rates();
}
I hope this helps some. The gtkmm docs site has a section on signal
connections, but it is short and kinda vague, so I included my own code
as an example for you.
Cheers!
Bob Caryl
Nietykalny wrote:
Could I get a sample code how to do that. It is abstract for me, I've got problem with writing and using signals with parameters. If you would recomend some doc about gtkkmm except from gtkmm.org, it 'd be nice.
----------------------------------------------------------------------
Auto kontra pociag: efekt konfrontacji! > http://link.interia.pl/f1921
begin:vcard
fn:Robert Caryl
n:Caryl;Robert
org:Fiscal Systems, Inc.
adr:;;102 Commerce Circle;Madison;AL;35758;USA
email;internet:bob fis-cal com
title:Senior Software Design Engineer
tel;work:356-772-8920 X108
x-mozilla-html:TRUE
url:http://www.fis-cal.com
version:2.1
end:vcard
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]