Difference between revisions of "Creating a custom rendering routine"

From uGFX Wiki
Jump to: navigation, search
(Widget information)
(Widget information)
Line 20: Line 20:
 
== Widget information ==
 
== Widget information ==
 
In order to render the widget you need some information about it (eg. position, size, font, ...). All these information can be fetched from the ''GWidgetObject'' struct which is passed as a parameter to the custom rendering function. This is the only time where you're not only allowed to but even obligated to directly access the struct members directly instead of using the equivalent GWIN API calls to retrieve these information. Do never access struct members directly outside of the custom rendering routine!
 
In order to render the widget you need some information about it (eg. position, size, font, ...). All these information can be fetched from the ''GWidgetObject'' struct which is passed as a parameter to the custom rendering function. This is the only time where you're not only allowed to but even obligated to directly access the struct members directly instead of using the equivalent GWIN API calls to retrieve these information. Do never access struct members directly outside of the custom rendering routine!
 +
 +
To retrieve all the widget specific information the ''GWidgetObject'' struct needs to be casted to the actual widget type object. However, it is strongly recommended to verify that the received ''GWidgetObject'' is of the required widget type by looking at the VMT. The following code is an example for a [[Slider]] widget:
 +
<syntaxhighlight lang="c">
 +
// Make sure that we received a proper struct
 +
if (gw->g.vmt != (gwinVMT *)&sliderVMT)
 +
return;
 +
 +
// Cast it so we can access slider specific information (eg. the value)
 +
GSliderObject* s = (GSliderObject*)gw;
 +
</syntaxhighlight>
  
 
'''''Important:''' Do never use the <code>gwinDrawXxx()</code> calls inside a rendering routine as this would lock the widget again. Use <code>gdispDrawXxx()</code> instead ''
 
'''''Important:''' Do never use the <code>gwinDrawXxx()</code> calls inside a rendering routine as this would lock the widget again. Use <code>gdispDrawXxx()</code> instead ''

Revision as of 18:27, 27 June 2015

Every widget comes with a custom render interface. The default style in which a widget is drawn is very basic and minimalistic in order to make it run on the even lowest performance systems smoothly. However, the custom render interface allows you to submit your own rendering routines. This does not only provide a very flexible way to render a widget matching to your systems performance, but it gives you also the possibility to render a widget matching your applications style.

The interface

Every widget provides a function to submit a custom rendering function:

void gwinSetCustomDraw(GHandle gh, CustomWidgetDrawFunction fn, void*param);

The CustomWidgetDrawFunction is a typedef'ed function pointer:

typedef void (*CustomWidgetDrawFunction)(struct GWidgetObject* gw, void* param);

The param parameter can be used to pass a custom parameter such as an image pointer in case of you're rendering routine needs to draw an image. However, in most of the cases, this parameter will be NULL.

Note: The pointer to the custom rendering routine can also be passed through the initialization struct (the customDraw field).

Note: Do never use the gwinDrawXxx() calls inside a rendering routine as this would lock the widget again. Use gdispDrawXxx() instead

Widget information

In order to render the widget you need some information about it (eg. position, size, font, ...). All these information can be fetched from the GWidgetObject struct which is passed as a parameter to the custom rendering function. This is the only time where you're not only allowed to but even obligated to directly access the struct members directly instead of using the equivalent GWIN API calls to retrieve these information. Do never access struct members directly outside of the custom rendering routine!

To retrieve all the widget specific information the GWidgetObject struct needs to be casted to the actual widget type object. However, it is strongly recommended to verify that the received GWidgetObject is of the required widget type by looking at the VMT. The following code is an example for a Slider widget:

// Make sure that we received a proper struct
if (gw->g.vmt != (gwinVMT *)&sliderVMT)
	return;
 
// Cast it so we can access slider specific information (eg. the value)
GSliderObject* s = (GSliderObject*)gw;

Important: Do never use the gwinDrawXxx() calls inside a rendering routine as this would lock the widget again. Use gdispDrawXxx() instead

Examples

ToDo