From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-74-118.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader02.eternal-september.org!aioe.org!Hx95GBhnJb0Xc8StPhH8AA.user.46.165.242.91.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: GtkAda callback and event Date: Fri, 10 Sep 2021 22:46:45 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <6133e791$0$6461$426a74cc@news.free.fr> <6134cb26$0$3697$426a74cc@news.free.fr> <6134db32$0$6461$426a74cc@news.free.fr> <6134dc71$0$3693$426a74cc@news.free.fr> <6134e03d$0$3372$426a74cc@news.free.fr> <61352d42$0$3749$426a74cc@news.free.fr> <944e2cf6-2e24-480e-b7f7-0e0e0f5082e7n@googlegroups.com> <6139be6f$0$12704$426a74cc@news.free.fr> <757da468-7b58-43c2-95e6-917b3212f7b2n@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Info: gioia.aioe.org; logging-data="55232"; posting-host="Hx95GBhnJb0Xc8StPhH8AA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 X-Notice: Filtered by postfilter v. 0.9.2 Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:62694 List-Id: 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