Re: Adding signals dynamically



gtk_signal_connect (GTK_OBJECT (CC_GLBL->host_files),
"activate", GTK_SIGNAL_FUNC (edit_this_file),  label);

This is part of a routine that scans the contents of a directory and
appends the file names into a menu. I am trying to attach a signal to
each menu item so that when the user selects the menu item the function
edit_this_file gets called and opens that specific menu item. Does
anyone have any idea if this will actually be possible? i.e create
signals dianamically and how can the signal get the right menu item
passed to the callback.

What you mean by adding signals dynamically?
If I understand correctly what you want, you just have to:

set up a loop to dynamically create your menu_items,
(for example reading from a list containing the file
pointers)

for each file get the filename, which you need for the menu_item label,

connect each menu_item with the SAME callback function 
but with a different data argument, it could be the file pointer 
for example. Then it will be the responsability of the
callback to do the work according to the data received.
This way your signal is created in a totally static way.

use something like this simplistic example:

while (list != NULL)
{
menu_item = gtk_menu_item_new_with_label (filename);
gtk_menu_append (GTK_MENU (menu), menu_item);
gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
                    GTK_SIGNAL_FUNC (handler_function), fp);
list = list->next;
}

If you need the data argument to pass some other structure 
then you can use set_data/get_data functions to pass your info
for each specific menu_item.

You can use pointers for functions in signal_connect functions,
but I don't think you even need this.

Carlos




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