From 9ac3c368b4de8e2f38bad56262c75d3310c8814b Mon Sep 17 00:00:00 2001 From: Andrew Hannam Date: Sun, 10 Jul 2016 10:42:21 +1000 Subject: Add gwinPrintg() and fix null pointer handling in sprintg() --- src/gwin/gwin_widget.c | 38 ++++++++++++++++++++++++++++++++++++++ src/gwin/gwin_widget.h | 18 ++++++++++++++++++ 2 files changed, 56 insertions(+) (limited to 'src/gwin') diff --git a/src/gwin/gwin_widget.c b/src/gwin/gwin_widget.c index 2965e7c6..483a63b3 100644 --- a/src/gwin/gwin_widget.c +++ b/src/gwin/gwin_widget.c @@ -530,6 +530,44 @@ void gwinSetText(GHandle gh, const char *text, bool_t useAlloc) { _gwinUpdate(gh); } +#if GFX_USE_GFILE && GFILE_NEED_PRINTG && GFILE_NEED_STRINGS + #include + + void gwinPrintg(GHandle gh, const char * fmt, ...) { + char *str; + va_list va; + int size; + + 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 + va_start (va, fmt); + + size = vsnprintg(0, 0, fmt, va) + 1; //determine the buffer size required + + if ((str = gfxAlloc(size))) { + gh->flags |= GWIN_FLG_ALLOCTXT; + vsnprintg(str, size, fmt, va); + gw->text = (const char *)str; + } else + gw->text = ""; + + va_end (va); + + _gwinUpdate(gh); + } +#endif + const char *gwinGetText(GHandle gh) { if (!(gh->flags & GWIN_FLG_WIDGET)) return 0; diff --git a/src/gwin/gwin_widget.h b/src/gwin/gwin_widget.h index ceba235b..ece98a06 100644 --- a/src/gwin/gwin_widget.h +++ b/src/gwin/gwin_widget.h @@ -231,6 +231,24 @@ void gwinSetText(GHandle gh, const char *text, bool_t useAlloc); */ const char *gwinGetText(GHandle gh); +#if (GFX_USE_GFILE && GFILE_NEED_PRINTG && GFILE_NEED_STRINGS) || defined(__DOXYGEN__) + /** + * @brief Set the text of a widget using a printf style format. + * @pre GFX_USE_GFILE, GFILE_NEED_PRINTG and GFILE_NEED_STRINGS must all be TRUE + * + * @param[in] gh The widget handle + * @param[in] fmt The format string using a printf/g syntax. See @p vsnprintg() + * @param[in] ... The printg paramters. + * + * @note The widget is automatically redrawn + * @note Non-widgets will ignore this call. + * @note The memory for the text is always allocated by this function. + * + * @api + */ + void gwinPrintg(GHandle gh, const char * fmt,...); +#endif + /** * @brief Check whether a handles is a widget handle or not * -- cgit v1.2.3