<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.ugfx.io/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=King2</id>
		<title>uGFX Wiki - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.ugfx.io/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=King2"/>
		<link rel="alternate" type="text/html" href="https://wiki.ugfx.io/index.php/Special:Contributions/King2"/>
		<updated>2026-05-12T22:39:01Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>https://wiki.ugfx.io/index.php?title=Creating_a_custom_rendering_routine&amp;diff=1500</id>
		<title>Creating a custom rendering routine</title>
		<link rel="alternate" type="text/html" href="https://wiki.ugfx.io/index.php?title=Creating_a_custom_rendering_routine&amp;diff=1500"/>
				<updated>2016-02-18T01:15:42Z</updated>
		
		<summary type="html">&lt;p&gt;King2: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Every [[Widgets|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.&lt;br /&gt;
&lt;br /&gt;
== The interface ==&lt;br /&gt;
Every widget provides a function to submit a custom rendering function:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void gwinSetCustomDraw(GHandle gh, CustomWidgetDrawFunction fn, void*param);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The ''CustomWidgetDrawFunction'' is a typedef'ed function pointer:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=c&amp;gt;&lt;br /&gt;
typedef void (*CustomWidgetDrawFunction)(struct GWidgetObject* gw, void* param);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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'''.&lt;br /&gt;
&lt;br /&gt;
'''''Note:''' The pointer to the custom rendering routine can also be passed through the initialization struct (the ''customDraw'' field).&lt;br /&gt;
&lt;br /&gt;
'''''Note:''' Do never use the &amp;lt;code&amp;gt;gwinDrawXxx()&amp;lt;/code&amp;gt; calls inside a rendering routine as this would lock the widget again. Use &amp;lt;code&amp;gt;gdispDrawXxx()&amp;lt;/code&amp;gt; instead ''&lt;br /&gt;
&lt;br /&gt;
== Widget information ==&lt;br /&gt;
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!&lt;br /&gt;
&lt;br /&gt;
'''''Important:''' Do never use the &amp;lt;code&amp;gt;gwinDrawXxx()&amp;lt;/code&amp;gt; calls inside a rendering routine as this would lock the widget again. Use &amp;lt;code&amp;gt;gdispDrawXxx()&amp;lt;/code&amp;gt; instead.''&lt;br /&gt;
&lt;br /&gt;
To retrieve all the widget specific information the ''GWidgetObject'' struct needs to be casted to the actual widget type object.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
We are now going to show on a real world example how a custom rendering routine can be implemented. We will write a custom rendering routine for the button widget that will allow to display an icon on the left side of the text. We will keep it as simple as possible: We won't do color blending for gradients and other things that would make this look nicer in order to focus on the things that are actually important. The expected result can be seen in the image on the right.&lt;br /&gt;
[[File:Buttons custom rendering.png|thumbnail|The result expected result. The ''Settings'' button on the right side is being pressed]]&lt;br /&gt;
&lt;br /&gt;
=== Implementation ===&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
void myButtonRendering(GWidgetObject* gw, void* param)&lt;br /&gt;
{&lt;br /&gt;
	const GColorSet* colors;&lt;br /&gt;
&lt;br /&gt;
	// Get the icon/image&lt;br /&gt;
	gdispImage* icon = (gdispImage*)param;&lt;br /&gt;
	if (!icon) {&lt;br /&gt;
		return;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Get the appropriate color pallete from the widget style&lt;br /&gt;
	if (!gwinGetEnabled((GHandle)gw))&lt;br /&gt;
		colors = &amp;amp;gw-&amp;gt;pstyle-&amp;gt;disabled;&lt;br /&gt;
	else if ((gw-&amp;gt;g.flags &amp;amp; GBUTTON_FLG_PRESSED))&lt;br /&gt;
		colors = &amp;amp;gw-&amp;gt;pstyle-&amp;gt;pressed;&lt;br /&gt;
	else&lt;br /&gt;
		colors = &amp;amp;gw-&amp;gt;pstyle-&amp;gt;enabled;&lt;br /&gt;
&lt;br /&gt;
	// Draw the basic rectangle with border&lt;br /&gt;
	gdispGFillArea(gw-&amp;gt;g.display, gw-&amp;gt;g.x+1, gw-&amp;gt;g.y+1, gw-&amp;gt;g.width-2, gw-&amp;gt;g.height-2, colors-&amp;gt;fill);&lt;br /&gt;
	gdispGDrawBox(gw-&amp;gt;g.display, gw-&amp;gt;g.x, gw-&amp;gt;g.y, gw-&amp;gt;g.width, gw-&amp;gt;g.height, colors-&amp;gt;edge);&lt;br /&gt;
&lt;br /&gt;
	// Draw the string. Use StringBox() for proper justification and word-wrapping support&lt;br /&gt;
	gdispGDrawStringBox(gw-&amp;gt;g.display, gw-&amp;gt;g.x+gw-&amp;gt;g.height, gw-&amp;gt;g.y, gw-&amp;gt;g.width-gw-&amp;gt;g.height, gw-&amp;gt;g.height, gw-&amp;gt;text, gw-&amp;gt;g.font, colors-&amp;gt;text, justifyLeft);&lt;br /&gt;
&lt;br /&gt;
	// Draw the image&lt;br /&gt;
	gdispGImageDraw(gw-&amp;gt;g.display, icon, gw-&amp;gt;g.x+(gw-&amp;gt;g.height-icon-&amp;gt;width)/2, gw-&amp;gt;g.y+(gw-&amp;gt;g.height-icon-&amp;gt;height)/2, icon-&amp;gt;width, icon-&amp;gt;height, 0, 0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Usage ===&lt;br /&gt;
The following program will show how the same rendering routine can be applied to two buttons that are using two different icons:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;gfx.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
GHandle ghButton1;&lt;br /&gt;
GHandle ghButton2;&lt;br /&gt;
gdispImage iconWrench;&lt;br /&gt;
gdispImage iconUgfx;&lt;br /&gt;
font_t font1;&lt;br /&gt;
font_t font2;&lt;br /&gt;
&lt;br /&gt;
void myButtonRendering(GWidgetObject* gw, void* param)&lt;br /&gt;
{&lt;br /&gt;
	const GColorSet* colors;&lt;br /&gt;
&lt;br /&gt;
	// Get the icon/image&lt;br /&gt;
	gdispImage* icon = (gdispImage*)param;&lt;br /&gt;
	if (!icon) {&lt;br /&gt;
		return;&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	// Get the appropriate color pallete from the widget style&lt;br /&gt;
	if (!gwinGetEnabled((GHandle)gw))&lt;br /&gt;
		colors = &amp;amp;gw-&amp;gt;pstyle-&amp;gt;disabled;&lt;br /&gt;
	else if ((gw-&amp;gt;g.flags &amp;amp; GBUTTON_FLG_PRESSED))&lt;br /&gt;
		colors = &amp;amp;gw-&amp;gt;pstyle-&amp;gt;pressed;&lt;br /&gt;
	else&lt;br /&gt;
		colors = &amp;amp;gw-&amp;gt;pstyle-&amp;gt;enabled;&lt;br /&gt;
&lt;br /&gt;
	// Draw the basic rectangle with border&lt;br /&gt;
	gdispGFillArea(gw-&amp;gt;g.display, gw-&amp;gt;g.x+1, gw-&amp;gt;g.y+1, gw-&amp;gt;g.width-2, gw-&amp;gt;g.height-2, colors-&amp;gt;fill);&lt;br /&gt;
	gdispGDrawBox(gw-&amp;gt;g.display, gw-&amp;gt;g.x, gw-&amp;gt;g.y, gw-&amp;gt;g.width, gw-&amp;gt;g.height, colors-&amp;gt;edge);&lt;br /&gt;
&lt;br /&gt;
	// Draw the string. Use StringBox() for proper justification and word-wrapping support&lt;br /&gt;
	gdispGDrawStringBox(gw-&amp;gt;g.display, gw-&amp;gt;g.x+gw-&amp;gt;g.height, gw-&amp;gt;g.y, gw-&amp;gt;g.width-gw-&amp;gt;g.height, gw-&amp;gt;g.height, gw-&amp;gt;text, gw-&amp;gt;g.font, colors-&amp;gt;text, justifyLeft);&lt;br /&gt;
&lt;br /&gt;
	// Draw the image&lt;br /&gt;
	gdispGImageDraw(gw-&amp;gt;g.display, icon, gw-&amp;gt;g.x+(gw-&amp;gt;g.height-icon-&amp;gt;width)/2, gw-&amp;gt;g.y+(gw-&amp;gt;g.height-icon-&amp;gt;height)/2, icon-&amp;gt;width, icon-&amp;gt;height, 0, 0);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void createWidgets(void) {&lt;br /&gt;
	GWidgetInit	wi;&lt;br /&gt;
&lt;br /&gt;
	// Prepare the images&lt;br /&gt;
	gdispImageOpenFile(&amp;amp;iconWrench, &amp;quot;settings.gif&amp;quot;);&lt;br /&gt;
	gdispImageOpenFile(&amp;amp;iconUgfx, &amp;quot;padlock.gif&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
	// Apply some default values for GWIN&lt;br /&gt;
	gwinWidgetClearInit(&amp;amp;wi);&lt;br /&gt;
	wi.g.show = TRUE;&lt;br /&gt;
&lt;br /&gt;
	// Create a regular button&lt;br /&gt;
	wi.g.width = 180;&lt;br /&gt;
	wi.g.height = 52;&lt;br /&gt;
	wi.g.y = 10;&lt;br /&gt;
	wi.g.x = 10;&lt;br /&gt;
	wi.customParam = &amp;amp;iconWrench;&lt;br /&gt;
	wi.customDraw = myButtonRendering;&lt;br /&gt;
	wi.text = &amp;quot;Setting&amp;quot;;&lt;br /&gt;
	ghButton1 = gwinButtonCreate(0, &amp;amp;wi);&lt;br /&gt;
&lt;br /&gt;
	// Create a regular button&lt;br /&gt;
	wi.g.width = 180;&lt;br /&gt;
	wi.g.height = 52;&lt;br /&gt;
	wi.g.y = 70;&lt;br /&gt;
	wi.g.x = 10;&lt;br /&gt;
	wi.customParam = &amp;amp;iconUgfx;&lt;br /&gt;
	wi.customDraw = myButtonRendering;&lt;br /&gt;
	wi.text = &amp;quot;Lock&amp;quot;;&lt;br /&gt;
	ghButton2 = gwinButtonCreate(0, &amp;amp;wi);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
	gfxInit();&lt;br /&gt;
&lt;br /&gt;
	gwinSetDefaultFont(gdispOpenFont(&amp;quot;*&amp;quot;));&lt;br /&gt;
	gwinSetDefaultStyle(&amp;amp;WhiteWidgetStyle, FALSE);&lt;br /&gt;
	gdispClear(White);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	createWidgets();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
	while(1) {&lt;br /&gt;
		gfxSleepMilliseconds(250);&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>King2</name></author>	</entry>

	</feed>