comp.lang.ada
 help / color / mirror / Atom feed
* Ada/Motif problem...
@ 1996-08-27  0:00 Dale Stanbrough
  1996-08-27  0:00 ` Larry Kilgallen
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Dale Stanbrough @ 1996-08-27  0:00 UTC (permalink / raw)



Hi,

One of our students has run into problems writing a Motif routine.
The callback data is to be a string as follows..


Xt.Intrinsic.XtAddCallback(
                                 pb,
                                 Motif.xmstrdefs.XmNactivateCallback,
                                 hello_dialog_pkg.popup'access,
                                 "Hello World");
                                 

I've tried using unchecked_conversion from an access to all string
to XtPointer, but Gnat complains (quite rightly) about these pointer
types being of different sizes.

Any inspiration on how to pass Ada strings as callback data in Motif?

Here's the subprogram declaration...
                                 
procedure XtAddCallback(
                widget       : Xt.Intrinsic.Widget;
                callback_name: X.Strings.const_charp;
                callback     : XtCallbackProc;
                closure      : XtPointer);

Dale




^ permalink raw reply	[flat|nested] 13+ messages in thread
* Re: Ada/Motif problem
@ 1996-08-28  0:00 W. Wesley Groleau (Wes)
  0 siblings, 0 replies; 13+ messages in thread
From: W. Wesley Groleau (Wes) @ 1996-08-28  0:00 UTC (permalink / raw)



If the Motif libs were compiled with gcc then the following should work
with GNAT:

   HW : constant String := "Hello World" & ASCII.NUL;

   begin

      Xt.Intrinsic.XtAddCallback ( pb,
                                   Motif.et_cetera,
                                   hello.et_cetera'access,
                                   HW'Address             );

as long as you ensure that HW is ALWAYS in scope.

I've obviously never tried it with Ada-83, but that method for
sending a string to C works with several C compilers and Ada-83 compilers.

OOPS, I just realized you're trying to send it BACK to Ada.  Yes, you lose
bounds info, but you can sort of get them back as follows:

in callback:

   Local_HW : contant String := String_From_C ( the_thing_that_came_from_C );

where String_From_C is provided (with a different name) by most vendors,
but is basically

   Temp : String ( 1 .. some_arbitrary_limit );
   for Temp'Address use the_thing_that_came_from_C;

...

   for I in Temp'Range loop
      if Temp(I) = ASCII.NULL then
         return Temp ( Temp'First .. I-1 );
      end if;
   end loop;

---------------------------------------------------------------------------
W. Wesley Groleau (Wes)                                Office: 219-429-4923
Hughes Defense Communications (MS 10-40)                 Home: 219-471-7206
Fort Wayne,  IN   46808                  (Unix): wwgrol@pseserv3.fw.hac.com
---------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 13+ messages in thread
* Re: Ada/Motif problem...
@ 1996-08-28  0:00 G. Vincent Castellano
  0 siblings, 0 replies; 13+ messages in thread
From: G. Vincent Castellano @ 1996-08-28  0:00 UTC (permalink / raw)



> One of our students has run into problems writing a Motif routine.
> The callback data is to be a string as follows..
>
> Xt.Intrinsic.XtAddCallback(
>                                  pb,
>                                  Motif.xmstrdefs.XmNactivateCallback,
>                                  hello_dialog_pkg.popup'access,
>                                  "Hello World");

The specification of this routine is:

    procedure XtAddCallback(
                widget       : Xt.Intrinsic.Widget;
                callback_name: X.Strings.const_charp;
                callback     : XtCallbackProc;
                closure      : XtPointer);

The type XtPointer is not really a useful one, being a
private (access) type with no operations for setting it.
You are obliged to use Unchecked_Conversion to convert to
and from it.

A good choice in this case would be String_Access from the
package Ada.Strings.Unbounded.  (Any access type will
probably work, since access types are generally all the
same size.)

  function To_XtPointer is new Unchecked_Conversion(
              Source => Ada.Strings.Unbounded.String_Access,
              Target => Xt.Intrinsic.XtPointer );

  Hi_String:  constant Ada.Strings.Unbounded.String_Access
              := new String'("Hello World");

-- ...

  Xt.Intrinsic.XtAddCallback(    pb,
                                 Motif.xmstrdefs.XmNactivateCallback,
                                 hello_dialog_pkg.popup'access,
                                 To_XtPointer( Hi_String ) );

BUT:  be careful that the object you're specifying as callback
data is not local to a subprogram, else it will be garbage by
the time the callback is invoked.  Declare it in a package spec
or body to be safe.

Also consider whether callback data is really useful.  If it's
never going to change, you probably don't need it.  I prefer to
play it safe and design callbacks so that only integers are
passed around, which I can then use to index into a table.
 -----------------------------------------------------------------------
 -    G. Vincent Castellano, Sr. Software Engineer, OC Systems Inc     -
 -   gvc@ocsystems.com :: X/Ada WWW => http://www.ocsystems.com/xada   -
 -----------------------------------------------------------------------
 - "If virtual memory did not exist, it would                          -
 -   have become necessary for us to invent it."                       -
 -----------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~1996-09-02  0:00 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-08-27  0:00 Ada/Motif problem Dale Stanbrough
1996-08-27  0:00 ` Larry Kilgallen
1996-08-27  0:00 ` Dale Stanbrough
1996-08-27  0:00   ` Michael K Rohan
1996-08-28  0:00     ` Larry Kilgallen
1996-08-29  0:00       ` Paul Hussein
1996-08-28  0:00 ` Jon S Anthony
1996-08-28  0:00 ` David C. Hoos, Sr.
1996-08-29  0:00 ` Jon S Anthony
1996-08-29  0:00   ` Dale Stanbrough
1996-09-02  0:00     ` Sandy McPherson
  -- strict thread matches above, loose matches on Subject: below --
1996-08-28  0:00 W. Wesley Groleau (Wes)
1996-08-28  0:00 G. Vincent Castellano

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