Getting String from widget

Hi,
I’ve used QNX4 for over 2 years, but I’ve never done any Photon coding. I
was recently trying to change some Photon code and was having a little
trouble with it. I was trying to get the text out of a label widget, and it
would either be garbled, or would crash the program.

The code I was trying is as follows:

PtArg_t arg;
char* textptr;

PtSetArg( &arg, Pt_ARG_TEXT_STRING, textptr, sizeof(textptr) );
PtGetResources( textwidget, 1, &arg );

where textwidget is the PtWidget * that was made into a PtLabel (with
PtCreateWidget).

Now, I’m not sure if textptr needs to be an array (like char textptr[32]),
or just a regular pointer (which would point directly to the label widgets
text data?). Any ideas? I will keep fooling with it and hopefully I’ll
get something useful soon.

TIA,
Ron

Ron Cococcia <cococr@cs.rpi.edu> wrote:

Hi,
I’ve used QNX4 for over 2 years, but I’ve never done any Photon coding. I
was recently trying to change some Photon code and was having a little
trouble with it. I was trying to get the text out of a label widget, and it
would either be garbled, or would crash the program.

The code I was trying is as follows:

PtArg_t arg;
char* textptr;

PtSetArg( &arg, Pt_ARG_TEXT_STRING, textptr, sizeof(textptr) );

you need “&textptr”, not “textptr” - when getting resources,
you pass the address of a pointer to the type you want.

PtGetResources( textwidget, 1, &arg );

where textwidget is the PtWidget * that was made into a PtLabel (with
PtCreateWidget).

Now, I’m not sure if textptr needs to be an array (like char textptr[32]),
or just a regular pointer (which would point directly to the label widgets
text data?).

Just the regular pointer. Pt_ARG_TEXT_STRING is a string type
resource.

Any ideas? I will keep fooling with it and hopefully I’ll
get something useful soon.

Here’s a snippet of sample code taken from our introductory
Photon course that does much the same thing (and shows setting
a string-type resource, too):

********************** begins *************************

extern int my_counter;


int
getNTestCounterCB (PtWidget_t *widget, ApInfo_t *apinfo,
PtCallbackInfo_t *cbinfo)
{
PtArg_t args[1];
char *new_value_str;
int new_value;

/* eliminate ‘unreferenced’ warnings */
widget = widget, apinfo = apinfo, cbinfo = cbinfo;

/* Get the new value from the text widget. */
PtSetArg (&args[0], Pt_ARG_TEXT_STRING,
&new_value_str, 0);
PtGetResources (ABW_ctr_string, 1, args);

/* Convert the string to an integer. If the conversion
fails, just redisplay the old value of the counter. */
if (sscanf (new_value_str, “%d”, &new_value) == 1)
my_counter = new_value;
displayCounter();

return (Pt_CONTINUE);
}


void displayCounter (void)
{
char buf[35];
PtArg_t args[1];

/* Display the current value of the counter. */
itoa (my_counter, buf, 10);
PtSetArg (&args[0], Pt_ARG_TEXT_STRING, buf, 0);
PtSetResources (ABW_ctr_string, 1, args);
}

*********************** ends **************************


I hope this gives you some help.


Norbert Black
QSSL Training Services

I’ll try some of this, thanks a ton!

Ron


“Norbert Black” <nblack@qnx.com> wrote in message
news:9urrc3$bm9$1@nntp.qnx.com

Ron Cococcia <> cococr@cs.rpi.edu> > wrote:
Hi,
I’ve used QNX4 for over 2 years, but I’ve never done any Photon coding.
I
was recently trying to change some Photon code and was having a little
trouble with it. I was trying to get the text out of a label widget,
and it
would either be garbled, or would crash the program.

The code I was trying is as follows:

PtArg_t arg;
char* textptr;

PtSetArg( &arg, Pt_ARG_TEXT_STRING, textptr, sizeof(textptr) );

you need “&textptr”, not “textptr” - when getting resources,
you pass the address of a pointer to the type you want.

PtGetResources( textwidget, 1, &arg );

where textwidget is the PtWidget * that was made into a PtLabel (with
PtCreateWidget).

Now, I’m not sure if textptr needs to be an array (like char
textptr[32]),
or just a regular pointer (which would point directly to the label
widgets
text data?).

Just the regular pointer. Pt_ARG_TEXT_STRING is a string type
resource.

Any ideas? I will keep fooling with it and hopefully I’ll
get something useful soon.


Here’s a snippet of sample code taken from our introductory
Photon course that does much the same thing (and shows setting
a string-type resource, too):

********************** begins *************************

extern int my_counter;


int
getNTestCounterCB (PtWidget_t *widget, ApInfo_t *apinfo,
PtCallbackInfo_t *cbinfo)
{
PtArg_t args[1];
char *new_value_str;
int new_value;

/* eliminate ‘unreferenced’ warnings */
widget = widget, apinfo = apinfo, cbinfo = cbinfo;

/* Get the new value from the text widget. */
PtSetArg (&args[0], Pt_ARG_TEXT_STRING,
&new_value_str, 0);
PtGetResources (ABW_ctr_string, 1, args);

/* Convert the string to an integer. If the conversion
fails, just redisplay the old value of the counter. */
if (sscanf (new_value_str, “%d”, &new_value) == 1)
my_counter = new_value;
displayCounter();

return (Pt_CONTINUE);
}


void displayCounter (void)
{
char buf[35];
PtArg_t args[1];

/* Display the current value of the counter. */
itoa (my_counter, buf, 10);
PtSetArg (&args[0], Pt_ARG_TEXT_STRING, buf, 0);
PtSetResources (ABW_ctr_string, 1, args);
}

*********************** ends **************************


I hope this gives you some help.


Norbert Black
QSSL Training Services

Ron Cococcia <request@qnx.com> wrote:
: I’ll try some of this, thanks a ton!

You should probably also read the Manipulating Resources in Application Code
chapter of the Photon Programmer’s Guide.


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