Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Accessing a custom widget's resources : (7 Items)
   
Accessing a custom widget's resources  
Hello,

I have been trying unsuccessfully to access a custom widget's resources. Any help would be greatly appreciated!

Details: 

A trending widget was created some time ago which has worked well in the past. This time, I tried to get the value of 
one of the resources of this custom-made widget. The resource was set from the resources tab of the control panel for 
the widget. Internally, this resource is a char *. 

At first, PtGetResources was returning NULL for the resource. To overcome the issue, I wrote my own getter function for 
the resource, and told my custom widget to use it. That also returned null. I started printing things from within the 
widget's code only to realize that the structure holding this resource is returning this resource as NULL. 

My guess is that I'm somehow trying to get this resource too early (I'm calling my callback in the widget's realize 
callback section.) 

Here's some code that might be of help: 

This is the custom widget's internal struct: 

typedef struct
{
   ....
    char                *idcode0;
    char                *idcode1;
    char                *idcode2;
    char                *idcode3;
    .... 
} CqTrendWidget_t;

t I altered the resource setting  to add my getter function: 
    static PtResourceRec_t resources[] =
    {
        RESOURCE1,     setter, getter,
            Pt_ARG_IS_STRING( CqTrendWidget_t, idcode0 ), 0,
    ....

This is my setter function: 

static int 
getter( PtWidget_t *widget, PtArg_t *argt )
{
	CqTrendWidget_t   *w = ( CqTrendWidget_t * ) widget;
	
	if (w->idcode0 == 0) fprintf(stderr, "NULL!\n");
	
	fprintf(stderr, "My resource =[%s]", w->idcode0); 
	fflush(stderr); 
	
	// The address of a pointer to a char  is in argt->value
		*(char **)argt->value = w->idcode0;
		
	return Pt_TRUE;
}


And in my callback function for my application (which I added as a realize callback for the widget), I try to retrieve 
the resource like so: 

    char * blah; 
    PtArg_t	args[1];
    PtSetArg(&args[0], RESOURCE1  , &blah, 0);
	
    fprintf(stderr, "HERE: [%s] \n", blah);
    fflush(stderr);

Results: 
blah is NULL (in my callback function)
 w->idcode0 is also NULL ( in the getter function) 
Re: Accessing a custom widget's resources  
Elena,

This code: 
{
   char * blah; 
    PtArg_t	args[1];
    PtSetArg(&args[0], RESOURCE1  , &blah, 0);
	
    fprintf(stderr, "HERE: [%s] \n", blah);
    fflush(stderr);
}
is missing a PtGetResources(). Is this a cut and paste issue?

Could you please post the code of the setter function?

Regards,
-Misha.
Re: Accessing a custom widget's resources  
Misha

Yes, that section is missing a call to PtGetResources. 

This is what that section is supposed to look like: 

sd_trend_config( PtWidget_t *widget, ApInfo_t *apinfo, PtCallbackInfo_t *cbinfo ) 
{
   char * blah; 
    PtArg_t	args[1];
    PtSetArg(&args[0], RESOURCE1  , &blah, 0);
    PtGetResources(widget, 1, &args);
    fprintf(stderr, "HERE: [%s] \n", blah);
    fflush(stderr);
}

The setter function doesn't set the resource that I'm trying to get. It sets a different resource to be the value passed
 in by PtSetResources. The resource that I am trying to get will not need to be altered while the application is running
. 

This resource that I'm having problems getting (RESOURCE1) is set to a value from within PhAB's control panel for that 
widget. 

Elena 
Re: Accessing a custom widget's resources  
I am confused. 
The resource you are trying to get is the idcode0.
You said that you don't set "it" in the setter function. In the setter you set some other resource. Who is setting/
modifing the idcode0?
I am guessing here... if you "remove" the setter function (replace it with a NULL in the resource declaration line), 
does your getter start to work?
Re: Accessing a custom widget's resources  
Misha,

in the header file of the custom widget the resource gets defined: 
#define RESOURCE1          Pt_RESOURCE( Pt_USER( 4 ), 2 )

Also defined there is the internal structure for the widget:

typedef struct
{
   ....
    char                *idcode0;
    char                *idcode1;
    char                *idcode2;
    char                *idcode3;
    .... 

And in the C file of the custom widget the resource gets "linked" to the internal structure with a resource record "
table": 

    static PtResourceRec_t resources[] =
    {
        RESOURCE1,     setter, getter,
            Pt_ARG_IS_STRING( CqTrendWidget_t, idcode0 ), 0,
    ....

From my understanding, the setter and getter functions are to allow me to extend the functionality of my custom widget 
when I call PtGetResources or PtSetResources. They don't have to be specified (marking them  0 in the resource records 
table. 

So I don't have to write a function to set the value I type in PhAB for RESOURCE1. That is done somewhat automatically 
when I linked RESOURCE1 with idcode0 in the resource table. 

Elena 
Re: Accessing a custom widget's resources  
The "Pt_ARG_IS_STRING( CqTrendWidget_t, idcode0 )" is only used if you have not specified a setter (for Set) /getter 
(for Get). In your case you need to set idcode0 and your other resource(s) in the setter function.
the code will look like this:
 free( w->idcode0 );
 w->idcode = strdup( <new string> );
 // check for errors -- ...
Re: Accessing a custom widget's resources  
Misha,

Thank you very much!

The setter was written incorrectly. 

Elena