Problem with PtToggleButton

I’m trying to create a dialog where the user will be able to select one or
more options
via toggle buttons and also set or clear all toggle buttons by pressing a
Set/Clear All
button. We’ve been using the following code to read and manipulate the
visible
state of toggle buttons:

/***************************************************************************


READ_OFF_ON


*******/
int read_off_on( PtWidget_t *widget)
{
PtArg_t arg[1];
ushort_t *flags_ptr;


PtSetArg (&arg, Pt_ARG_FLAGS, &flags_ptr, 0);
PtGetResources (widget, 1, &arg);
if ( (*flags_ptr & Pt_SET) != 0)
return ON;
else
return OFF;
}

/***************************************************************************


WRITE_OFF_ON


*******/
void write_off_on( int value, PtWidget_t *widget)
{
PtArg_t arg[1];
ushort_t *flags_ptr;

PtSetArg (&arg, Pt_ARG_FLAGS, &flags_ptr, 0);
PtGetResources (widget, 1, &arg);
if ( value == 0)
*flags_ptr &= ~Pt_SET;
else
*flags_ptr |= Pt_SET;
}


This approach worked for apps built with PhAB 1.11 and 1.12, but the
write_off_on() function doesn’t seem to work for the code I’m trying to
implement using PhAB 1.13.

Any suggestions, insights, etc., will be appreciated.

TIA,

Dan Garber
dgarber@webcraft.com

Dan Garber <dgarber@webcraft.com> wrote:
: I’m trying to create a dialog where the user will be able to select one or
: more options
: via toggle buttons and also set or clear all toggle buttons by pressing a
: Set/Clear All
: button. We’ve been using the following code to read and manipulate the
: visible
: state of toggle buttons:

: /***************************************************************************
: ********
: READ_OFF_ON
: ****************************************************************************
: *******/
: int read_off_on( PtWidget_t *widget)
: {
: PtArg_t arg[1];
: ushort_t *flags_ptr;


: PtSetArg (&arg, Pt_ARG_FLAGS, &flags_ptr, 0);
: PtGetResources (widget, 1, &arg);
: if ( (*flags_ptr & Pt_SET) != 0)
: return ON;
: else
: return OFF;
: }

: /***************************************************************************
: ********
: WRITE_OFF_ON
: ****************************************************************************
: *******/
: void write_off_on( int value, PtWidget_t *widget)
: {
: PtArg_t arg[1];
: ushort_t *flags_ptr;

: PtSetArg (&arg, Pt_ARG_FLAGS, &flags_ptr, 0);
: PtGetResources (widget, 1, &arg);
: if ( value == 0)
: *flags_ptr &= ~Pt_SET;
: else
: *flags_ptr |= Pt_SET;
: }


: This approach worked for apps built with PhAB 1.11 and 1.12, but the
: write_off_on() function doesn’t seem to work for the code I’m trying to
: implement using PhAB 1.13.

You appear to be depending on the fact the point you are returned by
PtGetResources is actually pointing to the internal data structure. You
then manipulate it and it just happens to work. I think you were depending
on undocumented behaviour which apparently has changed in later versions.

I would write your function as follows:
void write_off_on( int value, PtWidget_t *widget)
{
PtArg_t arg[1];
long flags = 0; // my docs show flags as a long

if ( value )
flags |= Pt_SET;
PtSetArg (&arg, Pt_ARG_FLAGS, flags, Pt_SET);
PtSetResources (widget, 1, &arg);
}


This has not been tested - I am just writing it off the top of my head.

Rick…

Rick Duff Internet: rick@astra.mb.ca
Astra Network QUICS: rgduff
QNX Consulting and Custom Programming URL: http://www.astra.mb.ca
+1 (204) 987-7475 Fax: +1 (204) 987-7479

rick@astra.mb.ca wrote:
: You appear to be depending on the fact the point you are returned by
: PtGetResources is actually pointing to the internal data structure. You
: then manipulate it and it just happens to work. I think you were depending
: on undocumented behaviour which apparently has changed in later versions.

And, in fact, the docs tell you not to do that:

PtGetResources() returns pointers directly into the widget’s
internal memory. Don’t attempt to modify the resources directly
using these pointers. Such a modification won’t have the
desired effect and will likely corrupt the widget’s behavior.
Never free these pointers either - this will certainly result in a
memory violation or some other fault.

Using const pointers will help avoid these problems.


Steve Reid stever@qnx.com
TechPubs (Technical Publications)
QNX Software Systems

Rick-

Thanks, it looks like it works :slight_smile: (Ahh, the joys of legacy code)

Dan

You appear to be depending on the fact the point you are returned by
PtGetResources is actually pointing to the internal data structure. You
then manipulate it and it just happens to work. I think you were depending
on undocumented behaviour which apparently has changed in later versions.

I would write your function as follows:
void write_off_on( int value, PtWidget_t *widget)
{
PtArg_t arg[1];
long flags = 0; // my docs show flags as a long

if ( value )
flags |= Pt_SET;
PtSetArg (&arg, Pt_ARG_FLAGS, flags, Pt_SET);
PtSetResources (widget, 1, &arg);
}


This has not been tested - I am just writing it off the top of my head.

Rick…

Rick Duff Internet: rick@astra.mb.ca
Astra Network QUICS: rgduff
QNX Consulting and Custom Programming URL: http://www.astra.mb.ca
+1 (204) 987-7475 Fax: +1 (204) 987-7479

Rick-

Thanks, it looks like it works :slight_smile: (Ahh, the joys of legacy code)

Dan

You appear to be depending on the fact the point you are returned by
PtGetResources is actually pointing to the internal data structure. You
then manipulate it and it just happens to work. I think you were depending
on undocumented behaviour which apparently has changed in later versions.

I would write your function as follows:
void write_off_on( int value, PtWidget_t *widget)
{
PtArg_t arg[1];
long flags = 0; // my docs show flags as a long

if ( value )
flags |= Pt_SET;
PtSetArg (&arg, Pt_ARG_FLAGS, flags, Pt_SET);
PtSetResources (widget, 1, &arg);
}


This has not been tested - I am just writing it off the top of my head.

Rick…

Rick Duff Internet: rick@astra.mb.ca
Astra Network QUICS: rgduff
QNX Consulting and Custom Programming URL: http://www.astra.mb.ca
+1 (204) 987-7475 Fax: +1 (204) 987-7479