Jump to content

gwinPrintfSetText


Recommended Posts

An other small contribution to ugfx. This function is a sprintf wrapper for GWidget objects. It automatically allocates the memory needed for the formatted text. Can be used to display values in labels, in custom draw routines which display the current slider value, etc....

I use this with ARM-GCC in C99 mode, no idea if this compiles on other platforms...

Example:

int val = 100;
gwinPrintfSetText(ghLabel1,"The value is: %d", val);

 

Code:

#include <stdarg.h>
#define gw		((GWidgetObject *)gh)
void gwinPrintfSetText(GHandle gh, char * fmt,...)
{
    if (!(gh->flags & GWIN_FLG_WIDGET))
		return;

	// Dispose of the old string
	if ((gh->flags & GWIN_FLG_ALLOCTXT)) {
		gh->flags &= ~GWIN_FLG_ALLOCTXT;
		if (gw->text) {
			gfxFree((void *)gw->text);
			gw->text = "";
		}
	}

	// Alloc the new text
	char *str;
    va_list va;
    va_start (va, fmt);

    int size = vsnprintf(NULL, 0, fmt, va); //determine size used by printf

    if ((str = gfxAlloc(size+1))) {
        gh->flags |= GWIN_FLG_ALLOCTXT;
        vsprintf(str, fmt, va);
    }
    gw->text = (const char *)str;
    va_end (va);

	_gwinUpdate(gh);
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...