[gtkada] Using Item Factory with callback data
William C Brennan
compraxis at comcast.net
Sat Dec 7 07:06:11 CET 2002
At 12:34 PM +0100 12/6/02, Preben Randhol wrote:
>William C Brennan <compraxis at comcast.net> wrote on 05/12/2002 (08:38) :
> > Does anyone have a working example of creating a menu using an
> > item_factory that includes callback_data?
>
>Can you show us the code so we can see what you are doing? It is easier
>to help then.
Somehow, I knew you were going to ask for what I was doing. ;-)
Okay, I pruned down my source code to a fairly small compilable example.
To test, extract the following 3 files from this posting into a clean
directory. Compile & link with:
gnatmake tidbit `gtkada-config`
which creates the executable named "tidbit".
After runnning tidbit, I want the output of this program to contain
the value of Main_App_Window.CB_Data, namely "1234", but when I run,
I always get some other value.
If someone could tell me why, and how to get the value I want
(through the callback mechanism, of course) I would be most grateful.
Thanks!
-- Bill
>>>>>>>> start tidbit.adb <<<<<<<<<<<<<
with Main_App_Window;
with Gtk.Main;
procedure Tidbit is
begin
-- Set locale specific time and date format.
Gtk.Main.Set_Locale;
Gtk.Main.Init;
Main_App_Window.Create_And_Show;
Gtk.Main.Main;
end Tidbit;
>>>>>>>> end tidbit.adb <<<<<<<<<<<<<
>>>>>>>> start main_app_window.ads <<<<<<<<<<<<<
package Main_App_Window is
procedure Create_And_Show;
-- Creates the window ready to be shown.
end Main_App_Window;
>>>>>>>> end main_app_window.ads <<<<<<<<<<<<<
>>>>>>>> start main_app_window.adb <<<<<<<<<<<<<
with Glib;
with Gtk.Accel_Group;
with Gtk.Handlers;
with Gtk.Item_Factory;
with Gtk.Main;
with Gtk.Menu_Bar;
with Gtk.Widget;
with Gtk.Window;
with Text_IO;
Package body Main_App_Window is
-- **********************
-- *** Declarations ***
-- **********************
File_Menu_Item_Text: constant String := "File";
Table_Menu_Item_Text: constant String := "Table";
Quit_Menu_Item_Text: constant String := "Quit";
Dimensions_Menu_Item_Text: constant String := "Dimensions";
Units_Menu_Item_Text: constant String := "Units";
type CB_Data_Type is new Integer;
CB_Data: aliased CB_Data_Type;
package IF_Data_Pkg is new Gtk.Item_Factory.Data_Item( CB_Data_Type );
package H_Window_None_None is new Gtk.Handlers.Callback(
Widget_Type => Gtk.Window.Gtk_Window_Record
);
-- ****************************
-- *** Private procedures ***
-- ****************************
procedure Quit is
begin
Gtk.Main.Main_Quit;
end Quit;
procedure On_Main_Window_Destroyed(
Window: access Gtk.Window.Gtk_Window_Record'Class
) is
begin
Quit;
end On_Main_Window_Destroyed;
procedure On_Quit_Activated
( Callback_Data: in IF_Data_Pkg.Data_Type_Access;
Callback_Action: in Glib.Guint;
Widget: in IF_Data_Pkg.Limited_Widget ) is
begin
Quit;
end On_Quit_Activated;
procedure On_Dimensions_Activated
( Callback_Data: in IF_Data_Pkg.Data_Type_Access;
Callback_Action: in Glib.Guint;
Widget: in IF_Data_Pkg.Limited_Widget ) is
begin
Text_IO.Put_Line( "Dimensions activated." );
Text_IO.Put_Line( "Calback value =" &
CB_Data_Type'Image(Callback_Data.all) );
Text_IO.New_Line;
end On_Dimensions_Activated;
procedure On_Units_Activated
( Callback_Data: in IF_Data_Pkg.Data_Type_Access;
Callback_Action: in Glib.Guint;
Widget: in IF_Data_Pkg.Limited_Widget ) is
begin
Text_IO.Put_Line( "Units activated." );
Text_IO.Put_Line( "Calback value =" &
CB_Data_Type'Image(Callback_Data.all) );
Text_IO.New_Line;
end On_Units_Activated;
-- ***************************
-- *** Public procedures ***
-- ***************************
procedure Create_And_Show is
Entry_Array: constant Gtk.Item_Factory.Gtk_Item_Factory_Entry_Array :=
( IF_Data_Pkg.Gtk_New
("/_File", "", null, Gtk.Item_Factory.Branch, 0),
IF_Data_Pkg.Gtk_New
("/File/_Quit", "<Control>Q", On_Quit_Activated'Access,
Gtk.Item_Factory.Item, 0),
IF_Data_Pkg.Gtk_New
("/_Table", "", null, Gtk.Item_Factory.Branch, 0),
IF_Data_Pkg.Gtk_New
("/Table/_Dimensions", "", On_Dimensions_Activated'Access,
Gtk.Item_Factory.Item, 0),
IF_Data_Pkg.Gtk_New
("/Table/_Units", "", On_Units_Activated'Access,
Gtk.Item_Factory.Item, 0) );
My_Window: Gtk.Window.Gtk_Window;
Accel_Group: Gtk.Accel_Group.Gtk_Accel_Group;
Item_Factory: Gtk.Item_Factory.Gtk_Item_Factory;
Menu_Bar_Name: constant String := "<menu>";
Menu_Bar: Gtk.Widget.Gtk_Widget;
begin
CB_Data := 1234;
-- Create a toplevel Gtk window.
Gtk.Window.Gtk_New( My_Window );
Gtk.Window.Set_Border_Width( My_Window, 3 );
Gtk.Window.Set_Title( My_Window, "Tidbit" );
-- Connect the destroy signal to the finish-up procedure.
H_Window_None_None.Connect(
Widget => My_Window,
Name => "destroy",
Marsh => H_Window_None_None.To_Marshaller(
On_Main_Window_Destroyed'Access
)
);
-- Create the accelerator group.
Gtk.Accel_Group.Gtk_New( Accel_Group );
-- Create the Item Factory ready to build a menu bar.
Gtk.Item_Factory.Gtk_New
( Ifactory => Item_Factory,
Container_Type => Gtk.Menu_Bar.Get_Type,
Path => Menu_Bar_Name, -- name to use as reference later
Accel_Group => Accel_Group );
-- Create the menu bar and the entire hierarchy of menu choices.
IF_Data_Pkg.Create_Items
( Ifactory => Item_Factory,
Entries => Entry_Array,
Callback_Data => CB_Data'Access );
Gtk.Accel_Group.Attach( Accel_Group, My_Window );
-- Extract the reference to the created menu bar.
Menu_Bar := Gtk.Item_Factory.Get_Widget( Item_Factory, Menu_Bar_Name );
-- Add the menu bar to the top-level window.
Gtk.Window.Add( My_Window, Menu_Bar );
Gtk.Window.Show_All( My_Window );
end Create_And_Show;
end Main_App_Window;
>>>>>>>> start main_app_window.adb <<<<<<<<<<<<<
--
compraxis at comcast.net
Bill Brennan
Wayne, PA
More information about the gtkada
mailing list