[comp.windows.x.motif] XtFree'ing resources in the Toolkit set_values proc

Uucp@resq.fidonet.org (Uucp) (03/25/91)

From  uunet!osf.org!motif-talk-request
From: uunet!Morgan.COM!chuck (Chuck Ocheret)
To:   hpcvlx.cv.hp.com!ben
Date: Fri, 22 Mar 91 13:44:05 EST
Cc:   motif-talk@osf.org, samborn@sunrise.com

> I assume that you're subclassing Motif widgets.  In this case there are
> some shortcuts involving the as-yet-undocumented synthetic resource
> mechanism.

Well...?

Don't give us an official document.  But please spout something useful.

~chuck
+--------------------+             Chuck Ocheret             +---------------+
|chuck@fid.Morgan.COM|       Morgan Stanley & Co., Inc.      |(212) 703-4474 |
|    Duty now ...    |19th Floor, 1251 Avenue of the Americas|for the future.|
+--------------------+      New York, N.Y.  10020 USA        +---------------+
--  
Uucp - via FidoNet node 1:269/133
UUCP: uunet!resq!Uucp

Uucp@resq.fidonet.org (Uucp) (03/26/91)

From  uunet!osf.org!motif-talk-request
From: uunet!hpcvxben.cv.hp.com!ben (Benjamin Ellsworth)
To:   samborn@sunrise.com (Kevin Samborn) (Kevin Samborn)
Date: Mon, 25 Mar 91 11:49:09 PST
Cc:   motif-talk@osf.org


> ... I a hesitate to complicate my widgets by adding things like
> synthetic resources.

They are totally transparent to the application programmer.

> ... I believe that synthetic resources were the cause of all the
> trouble with freeing XmStrings. ...

That was someone's misunderstanding.  Synthetic resources were the
mechanism which were used to implement the behavior.  Synthetic
resources do not require any such behavior.

> However, if they really provide a shortcut, I (and probably other
> widget programmers) would be glad to have explicit details.  The
> amount of the bribe depends on the shortcut :-)

Here's a first cut at a description (copyright 1991 Benjamin Ellsworth).

Simply stated synthetic resources provide a mechanism whereby resources
may be caught at both set and get time.  It is done with the SetValues
and GetValuesHook procedures of the BaseClasses (Gadget, Primitive,
Manager, and VendorShell).  All base classes have a field for the
synthetic resource list and the number of resources in that list.

The archetypical use of synthetic resources is resolution independent
resources.  On SetValues the value is converted to pixels, and on the
GetValues the value is converted from pixels.  The benefit is that *all*
of the widget code may now run in pixels--no more clunky tests and
calls in the setvalues routines (compare 1.0 with 1.1).

To declare a synthetic resource you must provide the name of the
resource, the size of the resource, the offset of the resource in the
instance structure, and the routines to be invoked on importation and
exportation of that resource.

At class initialize time, the synthetic resources for a class and its
superclasses are coalesced much like regular resources (subclasses may
override superclass definitions, a single list per class, etc.).  This
list is attached to each class.

At setvalues time the BaseClass SetValues traverses the arglist
and checks to see if any of the args are synthetic resources.  If an
arg is a synthetic resource, the import routine is called.  If the
routine returns XmSYNTHETIC_LOAD, the converted value is loaded into
the widget instance structure.

At getvalues time the BaseClass GetValuesHook traverses the arglist
and checks to see if any of the args are synthetic resources.  If an
arg is a synthetic resource, the export routine is called.  The
arg.value is set by the routine to the appropriate value.

The import routine gets the "new" widget after the Xt wiget class
setvalues procedures have run, but before the Xm widget classes have
done anything, and the arglist entry.  The export routine gets a
current widget and the arglist entry.

With this background, we will now describe how to solve your particular
problem:

In the subclass instance record, keep an up to date copy of the pointer
(not the string, just the pointer).

In the subclass class definition, define XmNlabelString as a synthetic
resource with:

        - Name = XmNlabelString
        - Size = sizeof(XmString)
        - Offset = XtOffset(XmLabelWidget, label._label)
        - ExportProc = NULL
        - ImportProc = SomethingClever

In the procedure SomethingClever, which only gets invoked when someone
tries to change the label string resource, look at the label string.
If you don't like it, replace it with the saved pointer.  Since the
import proc will be run before the label class setvalues, the "old"
string will never be freed.

In this manner we save ourselves the structure copy.  The cost is four
bytes in the instance structure.


-----------------------------------------------------------------------
Benjamin Ellsworth      | ben@cv.hp.com                | INTERNET
Hewlett-Packard Company | {backbone}!hplabs!hp-pcd!ben | UUCP
1000 N.E. Circle        | (USA) (503) 750-4980         | FAX
Corvallis, OR 97330     | (USA) (503) 757-2000         | VOICE
-----------------------------------------------------------------------
                     All relevant disclaimers apply.
-----------------------------------------------------------------------

--  
Uucp - via FidoNet node 1:269/133
UUCP: uunet!resq!Uucp