comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: GtkAda callback and event
Date: Fri, 10 Sep 2021 22:46:45 +0200	[thread overview]
Message-ID: <shgg7j$1lu0$1@gioia.aioe.org> (raw)
In-Reply-To: c9395f4d-e1d3-4c33-80f1-ff86c0cdf7fan@googlegroups.com

On 2021-09-10 08:56, Emmanuel Briot wrote:

>> User data is a property of the event not of the button. E.g. consider a
>> button to save file. The user data would be some Ada object responsible
>> for dealing with the file. What about other buttons? What about
>> life-times of buttons? User data usually have a longer life span etc.
> 
> Presumably you would have one callback procedure to deal with the file, another one to exit the application, another one to ask the user's name and whatever else. For all of these, you do not need user data and the high-level callbacks provided by GtkAda are much easier to use.
> 
> One contrived example where user data might be used is for instance: we have one callback responsible for moving the cursor in an editor, and the user data is used to pass UP, DOWN, LEFT or RIGHT to indicate which direction to move. But even in such a case, I would argue it is better to have four small callbacks that call the same shared procedure, and still use the high-level callbacks of GtkAda.

These are rather unrealistic examples. For a real example take an 
application with a tree view. There are lots of events you would need to 
process by the in widget derived from Get_Tree_View.

Almost none of them will be emitted by the tree view!

1. The tree view refers/contain other objects like selection. These 
objects emit events, not the tree view.

2. The buttons, menus etc having effect on the tree view, e.g. a button 
to collapse the tree view. These will emit events for the tree view 
unrelated to it from the GTK point of view.

My design is to instantiate Gtk.Handlers.User_Return_Callback (and 
parameterless handler) with GObject_Record and the user data set to the 
tree view. Then I use this handler everywhere without caring what object 
emits the event. The emitter is irrelevant in 90% of cases.

An OO alternative to Gtk.Handlers.User_Callback would be for example, 
taking Button for simplicity:

    type Gtk_Button_Record is new Gtk_Bin_Record with null record;
    type Gtk_Button is access all Gtk_Button_Record'Class;

    ...

    type Button_Void_Handler is limited interface;
    procedure Handle
              (  Handler : in out Button_Void_Handler ;
                 Button  : access Gtk_Button_Record'Class
              )  is abstract;

    procedure On_Clicked
              (  Self    : not null access Gtk_Button_Record;
                 Handler : not null access Button_Void_Handler'Class;
                 After   : Boolean := False
              );
    procedure On_Enter
              (  Self    : not null access Gtk_Button_Record;
                 Handler : not null access Button_Void_Handler'Class;
                 After   : Boolean := False
              );
    ...

This is type-safe and can be generated the way On_Clicked with 
Cb_Gtk_Button_Void is generated.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

  parent reply	other threads:[~2021-09-10 20:46 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-04 21:39 GtkAda callback and event DrPi
2021-09-04 22:29 ` Dmitry A. Kazakov
2021-09-05 13:50   ` DrPi
2021-09-05 14:48     ` Dmitry A. Kazakov
2021-09-05 14:58       ` DrPi
2021-09-05 15:04         ` DrPi
2021-09-05 15:12           ` Dmitry A. Kazakov
2021-09-05 15:20             ` DrPi
2021-09-05 15:53               ` Dmitry A. Kazakov
2021-09-05 20:49                 ` DrPi
2021-09-09  6:56                   ` Emmanuel Briot
2021-09-09  7:57                     ` DrPi
2021-09-09 18:41                       ` Jere
2021-09-09 19:58                         ` Dmitry A. Kazakov
2021-09-09 21:01                           ` Jere
2021-09-10  6:38                             ` Dmitry A. Kazakov
2021-09-10  6:56                               ` Emmanuel Briot
2021-09-10 20:42                                 ` DrPi
2021-09-10 20:58                                   ` Dmitry A. Kazakov
2021-09-11  7:38                                     ` Emmanuel Briot
2021-09-11 15:24                                     ` DrPi
2021-09-11 15:56                                       ` Dmitry A. Kazakov
2021-09-12  7:08                                       ` AdaMagica
2021-09-12  8:52                                         ` Dmitry A. Kazakov
2021-09-12 13:00                                           ` DrPi
2021-09-12 13:57                                             ` Dmitry A. Kazakov
2021-09-10 20:46                                 ` Dmitry A. Kazakov [this message]
2021-09-11  7:36                                   ` Emmanuel Briot
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox