Re: Assigning fixed variables ("data") via signal for items of a menu dynamically built



On Fri, 8 Aug 2008 03:17:19 -0700 (PDT)
Tom__G <tom guilleminot org> wrote:

> 
> Hi there,
> 
> For some reasons I must build a menu and sub-menus dynamically via a
> loop. My problem is how to "assign" a "fixed" value to the (g)pointer
> ("data") parameter in order to handle click after ?
> 
> Here is a bunch of my (Pascal) code :
>     For J:=1 to 3 do
>     begin
>       if J=1 then my_menu_subitems[J] :=
> gtk_menu_item_new_with_label('item 1');
>       if J=2 then my_menu_subitems[J] :=
> gtk_menu_item_new_with_label('item 2');
>       if J=3 then my_menu_subitems[J] :=
> gtk_menu_item_new_with_label('item 3');
>       gtk_menu_shell_append(GTK_MENU_SHELL(Common_Sub_Menu_For_Channels),
> my_menu_subitems[J]);
>       gtk_widget_show(my_menu_subitems[J]);    
>      
> g_signal_connect(G_OBJECT(my_menu_subitems[J]),'activate',G_CALLBACK(@Interface_Direct_Access_Actions),
> PChar(inttostr((J)))); 
>     ...
>     
> For instance I would like data=1 (ie PChar(inttostr((J)) ) was passed
> when clicked on 1st item, data=2 for 2nd etc...
> The problem is that the J value seems taken when clicking (and not at
> the assigning moment) so it is always the highest value (of the loop)
> which is sent (here 3).
> How to handle this please ?

IntToStr creates a temporary string, unless assigned, which you don't
do.
You must somewhere store the Strings and later free the memory.
Or simply use integers instead of strings/pchars:

g_signal_connect(G_OBJECT(my_menu_subitems[J]),'activate',G_CALLBACK(@Interface_Direct_Access_Actions),
 pointer(J)); 


Mattias



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