[gtkada] GtkAda builder example

Brian Drummond brian at shapes.demon.co.uk
Sat Sep 8 19:17:57 CEST 2012


I realised, having made the create_builder example from testgtk work
standalone, that it doesn't use the interface I had been intending to
try!

There are two ways of interfacing to the GTK builder. The TestGTK
example uses the low level Gtk.Builder binding. However there is a
higher level binding, Gtkada.Builder, which is supposed to simplify the
process of connecting signal handlers.

I have put together a tiny example using this interface, but I am not
really happy with it, as will be evident from the comments.

Handlers are in their own package at library level. If they are local to
the main procedure, they run into accessibility problems
testglade.adb:136:51: subprogram must not be deeper than access type

I have not included the XML file, as it is huge. 

I hope this example is helpful, and any comments on where I went wrong
are welcome.

- Brian

------ testglade.adb ----------------------------------------------

with Gtk.Widget;
with Ada.Text_IO;		use Ada.Text_IO;
with Handlers;			use Handlers;


procedure TestGlade is
   Builder : Gtkada_Builder;
   Error   : Glib.Error.GError;
   GladeFileName : constant String := "Test1.glade";

-- Handlers look like 
--
--   type Builder_Handler is access procedure
--     (Builder : access Gtkada_Builder_Record'Class);
--
--   type Object_Handler is access procedure
--     (User_Data : access GObject_Record'Class);
--
-- and types returning Boolean
--
-- Having only Gtkada_Builder_Record'Class and GObject_Record'Class
-- seems restrictive. I failed to find any way to register and use 
-- the following:
--    procedure Destroy(Object : access Gtk_Widget_Record'Class);
-- eventually resorting to Gtk.Builder.Get_Widget in the procedure body.
-- I must be missing something here...
--
--     Steps below are described in Gtkada_builder.ads
--     See also Gtk_builder.ads
--
--     Step 1: create a Builder and add the XML data, 

begin
   Gtk.Main.Init;
   Gtk_New (Builder);
   Error := Add_From_File (Builder, GladeFileName);
   if Error /= null then
      Put_Line ("Error in " & GladeFileName & " : " 
                & Glib.Error.Get_Message (Error));
      Glib.Error.Error_Free (Error);
   end if;

--     Step 2: add calls to "Register_Handler" to associate your
--     handlers with your callbacks.

   register_handler(Builder, "on_quit_activate",  File_Quit'access);
   register_handler(Builder, "On_Connect",        On_Clicked'access);
   register_handler(Builder, "On_Disconnect",     On_Clicked'access);
   register_handler(Builder, "On_Help",           on_help'access);
   register_handler(Builder, "On_First_Clicked",  On_Clicked'access);
   register_handler(Builder, "On_Second_Clicked", On_Clicked'access);
   register_handler(Builder, "On_Third_Clicked",  On_Clicked'access);
   register_handler(Builder, "On_Destroy",        Destroy'access);

--     Step 3: call Do_Connect. Once to connect all registered handlers

   Do_Connect (Builder);

--    Step 3.5, not mentioned in gtkada_builder.ads...
--  Find our main window, then display it and all of its children.
   Gtk.Widget.Show_All (Get_Widget (Builder, "window1"));
   Gtk.Main.Main;

--     Step 4: when the application terminates or all Windows 
--             described through
--             your builder should be closed, call Unref to free memory
--             associated with the Builder.
--     Unref is part of what other package, used how exactly?

end TestGlade;

------ handlers.ads -----------------------------------------------

with Gtkada.Builder; use Gtkada.Builder;

package Handlers is

   procedure on_help
     (Object : access Gtkada_Builder_Record'Class);
   procedure On_Clicked
     (Object : access Gtkada_Builder_Record'Class);
   procedure Destroy (Object : access Gtkada_Builder_Record'Class);
   procedure File_Quit (Object : access Gtkada_Builder_Record'Class);

end Handlers;

------ handlers.adb -----------------------------------------------

with Ada.Text_IO;		use Ada.Text_IO;
with Gtk.Main;
with Gtk.Widget;

package body Handlers is

   procedure on_help
     (Object : access Gtkada_Builder_Record'Class)
   is
      pragma Unreferenced (Object);
   begin
      Put_Line (" =^..^=   HALP!");
   end on_help;

   procedure On_Clicked
     (Object : access Gtkada_Builder_Record'Class)
   is
      pragma Unreferenced (Object);
   begin
      Put_Line ("Button pressed");
   end On_Clicked;

   procedure Destroy (Object : access Gtkada_Builder_Record'Class) is
   begin
      Put_Line ("Destroy_Clicked");
      Gtk.Main.Main_Quit;
   end Destroy;

   procedure File_Quit (Object : access Gtkada_Builder_Record'Class) is
   begin
      Gtk.Widget.Destroy (Get_Widget(Object, "window1"));
   end File_Quit;

end Handlers;

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








More information about the gtkada mailing list