comp.lang.ada
 help / color / mirror / Atom feed
From: Preben Randhol <randhol+news@pvv.org>
Subject: Re: Creating tempfile takes too long: GNAT or Windows bug? (& GtkAda)
Date: Tue, 15 Oct 2002 10:08:42 +0000 (UTC)
Date: 2002-10-15T10:08:42+00:00	[thread overview]
Message-ID: <slrnaqnq8q.r6.randhol+news@kiuk0152.chembio.ntnu.no> (raw)
In-Reply-To: 3DAB4A59.6060208@cogeco.ca

Warren W. Gay VE3WWG wrote:
> 
> It was 1.x of some sort. Here is a snippit that bugs me:
> 
>     procedure On_Clist2_Select_Row
>       (Object : access Gtk_Clist_Record'Class;
>        Params : Gtk.Arguments.Gtk_Args)
>     is
>        Row_X : Gint := To_Gint (Params, 1);
> --    Rows  : Gint := To_Gint (Params, 2);
> --    Evt   : Gdk_Event := To_Event (Params, 3);
>     begin
>        ...
> 
> The Params argument is of type Gtk.Arguments.Gtk_Args. To get the
> real args, I have to use a bunch of To_G*() conversion calls. If
> the API changes at some later date, this now incorrect code will not
> be caught at compile time. This bugs me because, as a GUI, this
> error may not be noticed for a long time -- in fact, it is likely
> the user will find it instead of me. ;-)

Yes I agree with you there. Perhaps this is what you look for:

--------------------------------------------------------------------------
--  This small examples show how you can create your own marshallers when
--  you want to create a handler for a signal that has no predefined
--  marshaller.
--
--  NOTE: you should already be quite familiar with how handlers and
--  marshallers work in GtkAda before trying to understand this example.
--
--  Most of the time, in this case, you should simply create a general
--  handler such as defined in the generic packages in Gtk.Handlers, and
--  as demonstrated in the first example below.
--  In that case, the conversion from C's System.Address types to your own
--  types is done directly in your handler.
--
--  In other cases, for instance when you reuse the same signal multiple
--  times, it might be cleaner to define a marshaller, so that your handlers
--  can have typed parameters. This is the second example demonstrated
--  below.
--
--  This example interfaces to a signal defined in the gtktipsquery.h in the
--  gtk+ distribution. The signal is called "widget_entered", and is called to
--  display a tooltip every time the pointer enteres a new widget
--  The general form for handlers, as defined in the C package is:
--
--    void  (*widget_entered) (GtkTipsQuery   *tips_query,
--                             GtkWidget      *widget,
--                             const gchar    *tip_text,
--                             const gchar    *tip_private);
--
--  There is no predefined marshaller for it in gtk-marshallers.ads

with Gtk.Arguments;
with Gtk.Handlers;
with Gtk.Marshallers;
with Gtk.Tips_Query; use Gtk.Tips_Query;
with Gtk.Widget;     use Gtk.Widget;

package Own_Marshaller is

   package Tips_Handlers is new Gtk.Handlers.Callback
     (Widget_Type => Gtk.Tips_Query.Gtk_Tips_Query_Record);

   --------------------
   -- First solution --
   --------------------

   --  First and simpler solution: use the general form for handlers, as
   --  defined in gtk-handlers.
   --  This does not require anything special, and the conversion of
   --  arguments will have to be done in the handler itself.
   --
   --  Example of use:
   --     see general_tips.adb

   procedure My_General_Tips (Tips   : access Gtk_Tips_Query_Record'Class;
                              Params : Gtk.Arguments.Gtk_Args);


   ---------------------
   -- Second solution --
   ---------------------

   --  If you want your handlers to directly have the
   --  correct number of parameters (and correctly typed), you have to
   --  provide your own marshallers. The subprogram below is the correct
   --  form for the handler, and the package implements the marshaller.
   --
   --  This solution is more work, but on the other hand is easier to reuse
   --  when the package below has been written.
   --
   --  Example of use:
   --     see specific_tips.adb

   procedure My_Specific_Tips (Tips   : access Gtk_Tips_Query_Record'Class;
                               Widget : access Gtk_Widget_Record'Class;
                               Text   : String;
                               Tips_P : String);

   package My_Tips_Marshaller_Pkg is
      type Handler is access
        procedure (Tips   : access Gtk_Tips_Query_Record'Class;
                   Widget : access Gtk_Widget_Record'Class;
                   Text   : String;
                   Tips_P : String);
      procedure My_Tips_Marshaller (Tips   : access Gtk_Tips_Query_Record'Class;
                                    Params : Gtk.Arguments.Gtk_Args;
                                    Cb     : Gtk.Marshallers.General_Handler);
      function To_Marshaller (Cb : Handler)
                             return Tips_Handlers.Marshallers.Marshaller;
   end My_Tips_Marshaller_Pkg;

end Own_Marshaller;

with Gtk.Marshallers;  use Gtk.Marshallers;
with Unchecked_Conversion;

package body Own_Marshaller is

   ---------------------
   -- My_General_Tips --
   ---------------------

   procedure My_General_Tips (Tips   : access Gtk_Tips_Query_Record'Class;
                              Params : Gtk.Arguments.Gtk_Args)
   is
      --  Have to do the conversion by hand
      Widget : Gtk_Widget := Gtk_Widget (Gtk.Arguments.To_Object (Params, 1));
      Text   : String     := Gtk.Arguments.To_String (Params, 2);
      Tips_P : String     := Gtk.Arguments.To_String (Params, 3);

   begin
      null;  --  Whatever you want here
   end My_General_Tips;


   ----------------------
   -- My_Specific_Tips --
   ----------------------

   procedure My_Specific_Tips (Tips   : access Gtk_Tips_Query_Record'Class;
                               Widget : access Gtk_Widget_Record'Class;
                               Text   : String;
                               Tips_P : String)
   is
   begin
      null;  --  Whatever you want here
   end My_Specific_Tips;

   ----------------------------
   -- My_Tips_Marshaller_Pkg --
   ----------------------------

   package body My_Tips_Marshaller_Pkg is

      function To_Handler is new Unchecked_Conversion
        (Gtk.Marshallers.General_Handler, Handler);
      function To_General_Handler is new Unchecked_Conversion
        (Handler, Gtk.Marshallers.General_Handler);

      ------------------------
      -- My_Tips_Marshaller --
      ------------------------

      procedure My_Tips_Marshaller (Tips   : access Gtk_Tips_Query_Record'Class;
                                    Params : Gtk.Arguments.Gtk_Args;
                                    Cb     : Gtk.Marshallers.General_Handler)
      is
         My_Cb : Handler := To_Handler (Cb);
      begin
         --  Basically, we do the same thing as in My_General_Tips above
         My_Cb (Tips,
                Gtk_Widget (Gtk.Arguments.To_Object (Params, 1)),
                Gtk.Arguments.To_String (Params, 2),
                Gtk.Arguments.To_String (Params, 3));
      end My_Tips_Marshaller;

      -------------------
      -- To_Marshaller --
      -------------------

      function To_Marshaller (Cb : Handler)
                             return Tips_Handlers.Marshallers.Marshaller
      is
      begin
         return (Func  => To_General_Handler (Cb),
                 Proxy => My_Tips_Marshaller'Access);
      end To_Marshaller;

   end My_Tips_Marshaller_Pkg;

end Own_Marshaller;

-----------------------------------------------------------------------------

It is taken from the examples in the document that comes with the
library.

> I don't think much of using Gint as a data type, but maybe I am
> being fussy. I'd prefer to user more strongly typed data types that
> match the parameter or argument's purpose, than any general purpose
> argument. If you must, Gint as a base type might be ok.

No I use Integer or Natural etc and I do Gint (variable) if needed in
function call.

Preben
-- 
Ada95 is good for you.
http://libre.act-europe.fr/Software_Matters/02-C_pitfalls.pdf



  reply	other threads:[~2002-10-15 10:08 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-09 11:58 Creating tempfile takes too long: GNAT or Windows bug? Mário Amado Alves
2002-10-09 14:15 ` BREHMER Philippe,
2002-10-09 16:43 ` tmoran
2002-10-09 17:14 ` Warren W. Gay VE3WWG
2002-10-09 18:40   ` Preben Randhol
2002-10-10 12:06     ` Marin David Condic
2002-10-10 12:29       ` Preben Randhol
2002-10-10 17:29         ` Creating tempfile takes too long: GNAT or Windows bug? (& GtkAda) Warren W. Gay VE3WWG
2002-10-10 19:43           ` Stephen Leake
2002-10-11 10:59             ` Preben Randhol
2002-10-11 10:46           ` Preben Randhol
2002-10-14 22:51             ` Warren W. Gay VE3WWG
2002-10-15 10:08               ` Preben Randhol [this message]
2002-10-11 12:00           ` Marin David Condic
2002-10-11 11:38         ` Creating tempfile takes too long: GNAT or Windows bug? Marin David Condic
2002-10-11 13:57           ` Preben Randhol
2002-10-12 13:14             ` Marin David Condic
2002-10-12 15:01               ` Preben Randhol
2002-10-10 19:44       ` Jeffrey Carter
2002-10-14 12:42         ` Simon Clubley
2002-10-10 14:02   ` Creating tempfile takes too long: GNAT or Windows bug? Solved Mário Amado Alves
2002-10-09 18:56 ` Creating tempfile takes too long: GNAT or Windows bug? Pascal Obry
2002-10-12  4:45 ` James Ross
replies disabled

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