comp.lang.ada
 help / color / mirror / Atom feed
* Using Motif/AXI XmStringTable in ADA
@ 1997-02-20  0:00 James Rice
  1997-02-21  0:00 ` Christopher Green
  1997-02-22  0:00 ` Nigel J. Tracey
  0 siblings, 2 replies; 3+ messages in thread
From: James Rice @ 1997-02-20  0:00 UTC (permalink / raw)



Sorry if this is a stupid question, or not phrased well, I am not very 
experienced with ADA or the AXI bindings.  I do know Motif though.

I am having a problem converting a Motif type to ADA, I am utilizing AXI Motif 
bindings.  I want to get all selected items from a Listbox widget.  Motif in C 
would return a StringTable, which is a pointer to some number of XmStrings, 
which I can traverse like an array, accessing the indivual strings.  With the 
bindings, I am returned a pointer, but how do I use it?  The relationship 
between pointers and arrays, is definitely not the same in ADA as it is in C.  
My question is, how do I get to the individual XmStrings in ADA.  Once I get a 
XmString, the AXI version of StringGetLtoR( ) will convert the XmString to an 
ADA String, so that is no problem.  

Thanks in advance for any help,
Jim.




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

* Re: Using Motif/AXI XmStringTable in ADA
  1997-02-20  0:00 Using Motif/AXI XmStringTable in ADA James Rice
@ 1997-02-21  0:00 ` Christopher Green
  1997-02-22  0:00 ` Nigel J. Tracey
  1 sibling, 0 replies; 3+ messages in thread
From: Christopher Green @ 1997-02-21  0:00 UTC (permalink / raw)



In article <1997Feb20.201801.29759@relay.nswc.navy.mil>,
James Rice <jamrice7@themall.net> wrote:
>Sorry if this is a stupid question, or not phrased well, I am not very 
>experienced with ADA or the AXI bindings.  I do know Motif though.
>
>I am having a problem converting a Motif type to ADA, I am utilizing AXI Motif 
>bindings.  I want to get all selected items from a Listbox widget.  Motif in C 
>would return a StringTable, which is a pointer to some number of XmStrings, 
>which I can traverse like an array, accessing the indivual strings.  With the 
>bindings, I am returned a pointer, but how do I use it?  The relationship 
>between pointers and arrays, is definitely not the same in ADA as it is in C.  
>My question is, how do I get to the individual XmStrings in ADA.  Once I get a 
>XmString, the AXI version of StringGetLtoR( ) will convert the XmString to an 
>ADA String, so that is no problem.  
>
>Thanks in advance for any help,
>Jim.

You are correct that there is not a straightforward conversion from the
AXI's type Xm.XmStringTable, which is equivalent to the C type XmString *,
to AXI's type Xm.XmStringList, which is an Ada array of Xm.XmString.

The easiest way to do this conversion is the following:

(1) Retrieve the values of both XmNselectedItems and XmNselectedItemCount.
    (in AXI, Xmdef.NselectedItems and Xmdef.NselectedItemCount).

(2) Map an Xm.XmStringList whose length is the value of XmNselectedItemCount
    onto the array pointed to by the value of XmNselectedItems.

(3) If you need the XmString values only in a local scope, you're done.
    Otherwise, copy the values to an array that exists in the proper scope.

Note: The value returned for the resource XmNselectedItems is the actual
compound strings, not a copy, so do not deallocate them.

Here is a callback that should do what you want:

with Xm;
with Xt;
package List_Callbacks is

  Global_Selected : Xm.XmStringList_Ptr;

  procedure Get_Selected (The_Widget  : in Xt.Widget; 
			  Client_Data : in Xt.Pointer; 
			  Call_Data   : in Xt.Pointer);
  pragma External (C, Get_Selected);

end List_Callbacks;

with Xmdef;
package body List_Callbacks is

  -- This rename lets us write "Selected_Items /= Xt.XtNULL" below.
  function "=" (Left, Right : in Xt.Pointer) return Boolean renames Xt."=";

  procedure Get_Selected (The_Widget  : in Xt.Widget;
			  Client_Data : in Xt.Pointer;
			  Call_Data   : in Xt.Pointer) is

    -- Args is a buffer for the ArgList we build up in order to retrieve
    -- XmNselectedItems and XmNselectedItemCount using Xt.GetValues.
    -- We initialize Selected_Items and Item_Count, even though we are
    -- about to fill them in, because Xt.GetValues will do nothing if
    -- it fails (this is the behavior of XtGetValues() in C Xt).
    Args           : Xt.ArgList (1 .. 2);
    Selected_Items : Xm.XmStringTable := Xt.XtNULL;
    Item_Count     : Xm.Int           := 0;

  begin

    -- Fill in Args with the resource names and pointers to the variables
    -- that will receive the resource values.  Xt.GetValues will fill in
    -- the variables via the pointers.
    Xt.SetArg (Args (1), Xmdef.NselectedItems,     Selected_Items'Address);
    Xt.SetArg (Args (2), Xmdef.NselectedItemCount, Item_Count'Address);
    Xt.GetValues (The_Widget, Args, 2);

    -- Make sure we actually retrieved something before proceeding.
    -- If Xt.GetValues did nothing, Selected_Items will still contain
    -- Xt.XtNULL (and mapping an array onto the null address will break).
    if Selected_Items /= Xt.XtNULL then
      declare

	-- Here is the key point.  We map an Ada array of the correct size
	-- onto the address of the C array.
	The_XmString_List : Xm.XmStringList (1 .. Item_Count);
	for The_XmString_List use at Selected_Items;

      begin

	-- Now we can do what we want with the Ada array of Xm.XmString.
	-- This will save a copy of the array onto the heap.
	-- (N.B. The Xm.XmString objects are still the original objects
	-- returned from the XmList widget.  The array itself may be
	-- deallocated when we are done with it, but the Xm.XmStrings
	-- in the array must not be deallocated.)
	Global_Selected := new Xm.XmStringList'(The_XmString_List);
      end;

    -- If nothing was returned by Xt.GetValues (for example, because the
    -- callback was applied to a widget that was not an XmList), then
    -- set the global variable to NULL to indicate the failure.
    -- (Raising an exception from a callback is ineffective, because the
    -- caller is C code in Xt, and nothing will handle the exception.)
    else
      Global_Selected := NULL;
    end if;

  end Get_Selected;

end List_Callbacks;

I do development, maintenance, and tech support for the AXI bindings and am
always ready to answer technical questions on them at "cgreen@atc.com".

Chris Green                                  Email cgreen@atc.com
Advanced Technology Center                   Phone (714) 583-9119
22982 Mill Creek Drive                                   ext. 220
Laguna Hills, CA 92653                       Fax   (714) 583-9213




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

* Re: Using Motif/AXI XmStringTable in ADA
  1997-02-20  0:00 Using Motif/AXI XmStringTable in ADA James Rice
  1997-02-21  0:00 ` Christopher Green
@ 1997-02-22  0:00 ` Nigel J. Tracey
  1 sibling, 0 replies; 3+ messages in thread
From: Nigel J. Tracey @ 1997-02-22  0:00 UTC (permalink / raw)
  To: James Rice


In article <1997Feb20.201801.29759@relay.nswc.navy.mil>,
	jamrice7@themall.net (James Rice) writes:
>Sorry if this is a stupid question, or not phrased well, I am not very 
>experienced with ADA or the AXI bindings.  I do know Motif though.
>
>I am having a problem converting a Motif type to ADA, I am utilizing AXI Motif 
>bindings.  I want to get all selected items from a Listbox widget.  Motif in C 
>would return a StringTable, which is a pointer to some number of XmStrings, 
>which I can traverse like an array, accessing the indivual strings.  With the 
>bindings, I am returned a pointer, but how do I use it?  The relationship 
>between pointers and arrays, is definitely not the same in ADA as it is in C.  
>My question is, how do I get to the individual XmStrings in ADA.  Once I get a 
>XmString, the AXI version of StringGetLtoR( ) will convert the XmString to an 
>ADA String, so that is no problem.  
>
>Thanks in advance for any help,
>Jim.

You can use the generic Interfaces.C.Pointers package to convert
the pointer to an array. The function Interfaces.C.Pointers.Value
will return an array which has one element for each of the XmStrings.

I have done this for XmNchildren resources something like below

with Interfaces.C.Pointers

..

   type Widget_Array_Type is array (Integer range <>) of
      aliased Xt.Intrinsic.Widget;

    function To_Widget is new Ada.Unchecked_Conversion
       (Integer_Access, Xt.Intrinsic.Widget);

   package Widget_List_Package is new Interfaces.C.Pointers
      (Integer, Xt.Intrinsic.Widget, Widget_Array_Type, To_Widget (null));

   Num_Children          : aliased Integer;
   Children              : aliased Xt.Intrinsic.WidgetList;

begin

      Xt.Intrinsic.XtVaGetValues
        (GUI_Widgets.Test,
         Stdarg.Empty &
           XmNnumChildren & To_Integer (Num_Children'Unchecked_Access) &
           XmNchildren & To_Integer (Children'Unchecked_Access) &
         null);

      declare
         Widget_List : Widget_Array_Type (1 .. Num_Children);
         Pointer     : Widget_List_Package.Pointer :=
           Widget_List_Package.Pointer (Children);
         Set_State   : aliased Boolean;
      begin
         Widget_List := Widget_List_Package.Value
                          (Pointer,
                           Interfaces.C.Ptrdiff_t (Num_Children));
         for I in Widget_List'range loop
            Xt.Intrinsic.XtVaGetValues
              (Widget_List (I),
               Stdarg.Empty &
                 XmNset & To_Integer (Set_State'Unchecked_Access) &
               null);
            if Set_State then
               Selected_Test_Type := Execute_Modules.Test_Type'Val (I - 1);
               exit;
            end if;
         end loop;
      end;
..

Hope that helps some,

Nigel

--------------------
Nigel J. Tracey MEng, Research Associate (High Integrity Systems Group).
Department of Computer Science,   Office: X/D016
University of York,               Tel   : [0|+44]1904 432769
York, England.                    E-Mail: njt@minster.york.ac.uk
YO1 5DD.                          URL   : http://sv1pc161.cs.york.ac.uk/~njt





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

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

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-02-20  0:00 Using Motif/AXI XmStringTable in ADA James Rice
1997-02-21  0:00 ` Christopher Green
1997-02-22  0:00 ` Nigel J. Tracey

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