2
votes

In using GtkAda.Builder under GtkAda 20.1, I can't figure out how to capture event data.

with Gtk.Widget;                       use Gtk.Widget;
with Gtk.Main;                         use Gtk.Main;
with Gtkada.Builder;                   use Gtkada.Builder;
with Glib.Error;                       use Glib.Error;
with Callbacks;
with Glib;                             use type Glib.Guint;

with Ada.Text_IO;
procedure Main is

   Builder : Gtkada_Builder;
   Error   : aliased GError;
   Err_Num : Glib.Guint;

begin
   Init;

   Gtk_New (Builder);
   Err_Num := Add_From_File (Builder, "data/main_window.glade", Error'Access);

   if Err_Num = 0 then
      Ada.Text_IO.Put_Line ("Error : " & Get_Message (Error));
      Error_Free (Error);
      return;
   end if;

   Register_Handler
     (Builder      => Builder,
      Handler_Name => "on_main_window_destroy",
      Handler      => Callbacks.Main_Window_Destroy'Access);  -- Works fine
   Register_Handler
     (Builder => Builder,
      Handler_Name => "on_key_pressed",
      Handler      => Callbacks.Key_Pressed'Access);   -- but wait

   Show_All (Gtk_Widget(Get_Object (Builder, "main_window")));
   Main;

   Unref (Builder);
end Main;


package Callbacks is
   procedure Main_Window_Destroy
     (Object : access Gtkada_Builder_Record'Class);

   function Key_Pressed
     (Object : access Gtkada_Builder_Record'Class) 
      return Boolean;
      -- how do I get a Gdk.Event.Gdk_Event_Key from this????
end Callbacks;

As shown in the example, the GtkAda_Builder will only access two prototypes for callbacks, a procedure or a function, both of which take the Builder as an argument. How do I use this to examine the event that fired in the case of a key_press event? Or a draw event for that matter? There are no examples in testgtkada or anywhere that I've found which deal with this and I can't figure out how to do it. Is this just a toy class?

1

1 Answers

2
votes

If you want to get a data from keyboard event in GTKAda (and in GTK too), you have to connect function directly to the selected widget not by Glade calls (like Main_Window_Destroy). Glade allows only pass an additional data to the subprogram but there no way to get anything related to the event (like keyboard press/release, mouse movement or even new size of a widget after resizing).

For example, if you want to handle key pressed in Main_Window widget, code should looks that:

On_Key_Press_Event(Main_Window, My_Key_Pressed'Access);

And then callback show looks that:

function My_Key_Pressed
 (Self: access Gtk_Widget_Record'Class; Event: Gdk.Event.Gdk_Event_Key)
  return Boolean is
begin
   return True;
end My_Key_Pressed;

Edit: First paragraph, better clarification why code example from question doesn't work.