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=-2.9 required=3.0 tests=BAYES_00,NICE_REPLY_A autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader02.eternal-september.org!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!cleanfeed2-b.proxad.net!nnrp1-2.free.fr!not-for-mail Subject: Re: GtkAda callback and event Newsgroups: comp.lang.ada 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> From: DrPi <314@drpi.fr> Date: Fri, 10 Sep 2021 22:42:19 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Message-ID: <613bc32d$0$27421$426a74cc@news.free.fr> Organization: Guest of ProXad - France NNTP-Posting-Date: 10 Sep 2021 22:42:21 CEST NNTP-Posting-Host: 82.65.30.55 X-Trace: 1631306541 news-3.free.fr 27421 82.65.30.55:54274 X-Complaints-To: abuse@proxad.net Xref: reader02.eternal-september.org comp.lang.ada:62693 List-Id: Le 10/09/2021 à 08:56, Emmanuel Briot a écrit : >>> type My_Button is new Gtk.Whatever_Path.Button_Type with record >>> User_Data : User_Data_Type; >>> end record; > > This is indeed the recommended approach. In practice, most widgets have a single callback per event type (so one for motion_notify, one for click, and so on). All that's needed is the `Self` parameter which contains all relevant information. At least this was my experience based on the GPS source code, which is a relatively extensive GUI application. I don't remember the stats exactly, but there were just a few cases where this approach did not work. And this is why we implemented the higher-level approach in GtkAda, where there are no possible errors in the type of parameters for callbacks. > > There are a few cases where you want to share the same callback subprogram for multiple events, or multiple types of widgets for instance. In these cases, you might have to fallback to using the generics to connect, along with specifying a user data. As much as possible, I would recommend not using this approach if you can avoid it. > > I do not share Dmitry's distrust of generics, but for GUI applications I 100% agree that an OO approach works much better indeed. The performance cost is negligible in such contexts, and the flexibility is much needed. > >> 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. > That's very interesting. I didn't think I can extend the widget type to add my own parameters. Thanks all for your help. Nicolas