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, WEIRD_PORT 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!cleanfeed1-a.proxad.net!nnrp1-1.free.fr!not-for-mail Newsgroups: comp.lang.ada References: <6133e791$0$6461$426a74cc@news.free.fr> From: DrPi <314@drpi.fr> Subject: Re: GtkAda callback and event Date: Sun, 5 Sep 2021 15:50:28 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.13.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: <6134cb26$0$3697$426a74cc@news.free.fr> Organization: Guest of ProXad - France NNTP-Posting-Date: 05 Sep 2021 15:50:30 CEST NNTP-Posting-Host: 82.65.30.55 X-Trace: 1630849830 news-3.free.fr 3697 82.65.30.55:59179 X-Complaints-To: abuse@proxad.net Xref: reader02.eternal-september.org comp.lang.ada:62640 List-Id: Le 05/09/2021 à 00:29, Dmitry A. Kazakov a écrit : > On 2021-09-04 23:39, DrPi wrote: > >> I use an event callback with user data. >> >> I first declare a package : >>     package Handler_Motion_Notify is new >> Gtk.Handlers.User_Return_Callback (Widget_Type => >> Gtk.Text_View.Gtk_Text_View_Record, >> >>     Return_Type => Boolean, >> >>     User_Type   => t_Debug_Panel); >> >> The function callback is declared like this : >>     function On_Motion_Notify (TextView : access >> Gtk.Text_View.Gtk_Text_View_Record'Class; >>                                DebugPanel : t_Debug_Panel) return >> Boolean; > > This is wrong. The motion-notify-event callback has the parameters: > > 1. Target > 2. Gdk.Event.Gdk_Event_Motion > 3. User data > >> The connection is done like this : >>        Handler_Motion_Notify.Connect (Widget => Panel.TextView, >>                                       Name   => >> Gtk.Widget.Signal_Motion_Notify_Event, >>                                       Cb     => On_Motion_Notify'Access, >>                                       User_Data => t_Debug_Panel(Panel)); >> >> This works correctly. But... I need to have access to the event in the >> callback function. > > Maybe it does not crash but it is incorrect. I suppose it doesn't crash because I don't use any callback parameter in the callback itself. Just a Put_Line() to check the callback is called. > >> How can I achieve this ? > > There is no shortcut Connect and Callback defined in > User_Return_Callback. Therefore you have to use a general-case callback > with the parameter list: > >    function On_Motion_Notify >             (  Object : access Gtk_Text_View_Record'Class; >                Params : Glib.Values.GValues; >                Data   : t_Debug_Panel >             )  return Boolean; > So I have to use a callback with Handler profile, not Simple_Handler profile. I missed that. > connected as: > >    Handler_Motion_Notify.Connect >    (  Panel.TextView, >       "motion-notify-event", -- = Signal_Motion_Notify_Event >       On_Motion_Notify'Access, >       t_Debug_Panel (Panel) >    ); > > The parameter is accessed using the function Nth or > Gtk.Argument.Unchecked_To_Gdk_Event_Motion, e.g. > >    Gtk.Argument.Unchecked_To_Gdk_Event_Motion (Params, 1) > With "Gtk.Arguments.Unchecked_To_Gdk_Event_Motion (Params, 1);" , I get an error : expected private type "C_GValues" defined at glib-values.ads:53 found private type "GValues" defined at glib-values.ads:48 > P.S. Since the target is practically never used in callbacks, you can > instantiate Gtk.Handlers.User_Return_Callback with GObject_Record. This > way you could use it with all types of widgets. That reduces the number > of instantiations. > Thanks for the tip.