From ec09292542874ee8290f460413afef0adf789039 Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Fri, 9 May 2014 12:20:32 +0200 Subject: Revert "Renaming image widget files to appropriate name" This reverts commit 82a3f8491f6ec3b6bbb161167fc435acbfacc3a1. --- src/gwin/gimage.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/gwin/image.h | 127 +++++++++++++++++++++++++++++++++++++ src/gwin/imagebox.c | 175 --------------------------------------------------- src/gwin/imagebox.h | 127 ------------------------------------- src/gwin/sys_defs.h | 2 +- src/gwin/sys_make.mk | 2 +- 6 files changed, 304 insertions(+), 304 deletions(-) create mode 100644 src/gwin/gimage.c create mode 100644 src/gwin/image.h delete mode 100644 src/gwin/imagebox.c delete mode 100644 src/gwin/imagebox.h (limited to 'src/gwin') diff --git a/src/gwin/gimage.c b/src/gwin/gimage.c new file mode 100644 index 00000000..44ba785a --- /dev/null +++ b/src/gwin/gimage.c @@ -0,0 +1,175 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +/** + * @file src/gwin/gimage.c + * @brief GWIN sub-system image code. + */ + +#include "gfx.h" + +#if GFX_USE_GWIN && GWIN_NEED_IMAGE + +#include "src/gwin/class_gwin.h" + +#define widget(gh) ((GImageObject *)gh) + +static void _destroy(GWindowObject *gh) { + if (gdispImageIsOpen(&widget(gh)->image)) + gdispImageClose(&widget(gh)->image); +} + +#if GWIN_NEED_IMAGE_ANIMATION + static void _redraw(GHandle gh); + + static void _timer(void *gh) { + // We need to re-test the visibility in case it has been made invisible since the last frame. + if ((((GHandle)gh)->flags & GWIN_FLG_VISIBLE)) + _redraw((GHandle)gh); + } +#endif + +static void _redraw(GHandle gh) { + coord_t x, y, w, h, dx, dy; + color_t bg; + #if GWIN_NEED_IMAGE_ANIMATION + delaytime_t delay; + #endif + + // The default display area + dx = 0; + dy = 0; + x = gh->x; + y = gh->y; + w = gh->width; + h = gh->height; + bg = gwinGetDefaultBgColor(); + + // If the image isn't open just clear the area + if (!gdispImageIsOpen(&widget(gh)->image)) { + gdispGFillArea(gh->display, x, y, w, h, bg); + return; + } + + // Center horizontally if the area is larger than the image + if (widget(gh)->image.width < w) { + w = widget(gh)->image.width; + dx = (gh->width-w)/2; + x += dx; + if (dx) + gdispGFillArea(gh->display, gh->x, y, dx, h, bg); + gdispGFillArea(gh->display, x+w, y, gh->width-dx-w, h, bg); + dx = 0; + } + + // Center image horizontally if the area is smaller than the image + else if (widget(gh)->image.width > w) { + dx = (widget(gh)->image.width - w)/2; + } + + // Center vertically if the area is larger than the image + if (widget(gh)->image.height < h) { + h = widget(gh)->image.height; + dy = (gh->height-h)/2; + y += dy; + if (dy) + gdispGFillArea(gh->display, x, gh->y, w, dy, bg); + gdispGFillArea(gh->display, x, y+h, w, gh->height-dy-h, bg); + dy = 0; + } + + // Center image vertically if the area is smaller than the image + else if (widget(gh)->image.height > h) { + dy = (widget(gh)->image.height - h)/2; + } + + // Reset the background color in case it has changed + gdispImageSetBgColor(&widget(gh)->image, bg); + + // Display the image + gdispGImageDraw(gh->display, &widget(gh)->image, x, y, w, h, dx, dy); + + #if GWIN_NEED_IMAGE_ANIMATION + // read the delay for the next frame + delay = gdispImageNext(&widget((GHandle)gh)->image); + + // Wait for that delay if required + switch(delay) { + case TIME_INFINITE: + // Everything is done + break; + case TIME_IMMEDIATE: + // We can't allow a continuous loop here as it would lock the system up so we delay for the minimum period + delay = 1; + // Fall through + default: + // Start the timer to draw the next frame of the animation + gtimerStart(&widget((GHandle)gh)->timer, _timer, (void*)gh, FALSE, delay); + break; + } + #endif +} + +static const gwinVMT imageVMT = { + "Image", // The class name + sizeof(GImageObject), // The object size + _destroy, // The destroy routine + _redraw, // The redraw routine + 0, // The after-clear routine +}; + +GHandle gwinGImageCreate(GDisplay *g, GImageObject *gobj, GWindowInit *pInit) { + if (!(gobj = (GImageObject *)_gwindowCreate(g, &gobj->g, pInit, &imageVMT, 0))) + return 0; + + // Ensure the gdispImageIsOpen() gives valid results + gobj->image.type = 0; + gobj->image.fns = 0; + + // Initialise the timer + #if GWIN_NEED_IMAGE_ANIMATION + gtimerInit(&gobj->timer); + #endif + + gwinSetVisible((GHandle)gobj, pInit->show); + + return (GHandle)gobj; +} + +bool_t gwinImageOpenGFile(GHandle gh, GFILE *f) { + // is it a valid handle? + if (gh->vmt != (gwinVMT *)&imageVMT) + return FALSE; + + if (gdispImageIsOpen(&widget(gh)->image)) + gdispImageClose(&widget(gh)->image); + + if ((gdispImageOpenGFile(&widget(gh)->image, f) & GDISP_IMAGE_ERR_UNRECOVERABLE)) + return FALSE; + + if ((gh->flags & GWIN_FLG_VISIBLE)) { + // Setting the clip here shouldn't be necessary if the redraw doesn't overdraw + // but we put it in for safety anyway + #if GDISP_NEED_CLIP + gdispGSetClip(gh->display, gh->x, gh->y, gh->width, gh->height); + #endif + _redraw(gh); + } + + return TRUE; +} + +gdispImageError gwinImageCache(GHandle gh) { + // is it a valid handle? + if (gh->vmt != (gwinVMT *)&imageVMT) + return GDISP_IMAGE_ERR_BADFORMAT; + + return gdispImageCache(&widget(gh)->image); +} + +#endif // GFX_USE_GWIN && GWIN_NEED_IMAGE +/** @} */ diff --git a/src/gwin/image.h b/src/gwin/image.h new file mode 100644 index 00000000..fdaabc92 --- /dev/null +++ b/src/gwin/image.h @@ -0,0 +1,127 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +/** + * @file src/gwin/image.h + * @brief GWIN image widget header file. + * + * @defgroup Image Image + * @ingroup GWIN + * + * @details GWIN allos it to create an image widget. The widget + * takes no user input. + * + * @pre GFX_USE_GDISP must be set to TRUE in your gfxconf.h + * @pre GFX_USE_GWIN must be set to TRUE in your gfxconf.h + * @pre GDISP_NEED_IMAGE must be set to TRUE in your gfxconf.h + * @pre GWIN_NEED_IMAGE must be set to TRUE in your gfxconf.h + * @pre At least one image type must be enabled in your gfxconf.h + * + * @{ + */ + +#ifndef _GWIN_IMAGE_H +#define _GWIN_IMAGE_H + +// This file is included within "gwin/gwin.h" + +// An image window +typedef struct GImageObject { + GWindowObject g; + gdispImage image; // The image itself + #if GWIN_NEED_IMAGE_ANIMATION + GTimer timer; // Timer used for animated images + #endif +} GImageObject; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Create an image widget. + * @details Display's a picture. + * @return NULL if there is no resultant drawing area, otherwise the widget handle. + * + * @param[in] g The GDisplay to display this window on + * @param[in] widget The image widget structure to initialise. If this is NULL, the structure is dynamically allocated. + * @param[in] pInit The initialization parameters to use. + * + * @note The default background color gets set to the current default one. + * @note An image window knows how to redraw. + * + * @api + */ +GHandle gwinGImageCreate(GDisplay *g, GImageObject *widget, GWindowInit *pInit); +#define gwinImageCreate(w, pInit) gwinGImageCreate(GDISP, w, pInit) + +/** + * @brief Opens the image using a GFILE + * @return TRUE if the image can be opened + * + * @param[in] gh The widget (must be an image widget) + * @param[in] f The open (for reading) GFILE to use + * + * @api + */ +bool_t gwinImageOpenGFile(GHandle gh, GFILE *f); + +/** + * @brief Opens the image using the specified filename + * @return TRUE if the open succeeds + * + * @param[in] gh The widget (must be an image widget) + * @param[in] filename The filename to open + * + * @api + */ +#define gwinImageOpenFile(gh, filename) gwinImageOpenGFile((gh), gfileOpen((filename), "rb")) + + /** + * @brief Sets the input routines that support reading the image from memory + * in RAM or flash. + * @pre GFILE_NEED_MEMFS must be TRUE + * @return TRUE if the IO open function succeeds + * + * @param[in] gh The widget (must be an image widget) + * @param[in] ptr A pointer to the image in RAM or Flash + * + * @api + */ +#define gwinImageOpenMemory(gh, ptr) gwinImageOpenGFile((gh), gfileOpenMemory((void *)(ptr), "rb")) + +/** + * @brief Sets the input routines that support reading the image from a BaseFileStream (eg. an SD-Card). + * @return TRUE if the IO open function succeeds + * @pre GFILE_NEED_CHIBIOSFS and GFX_USE_OS_CHIBIOS must be TRUE + * + * @param[in] gh The widget (must be an image widget) + * @param[in] streamPtr A pointer to the (open) BaseFileStream object. + * + * @api + */ +#define gwinImageOpenStream(gh, streamPtr) gwinImageOpenGFile((gh), gfileOpenBaseFIleStream((streamPtr), "rb")) + +/** + * @brief Cache the image. + * @details Decodes and caches the current frame into RAM. + * + * @param[in] gh The widget (must be an image widget) + * + * @return GDISP_IMAGE_ERR_OK (0) on success or an error code. + * + * @api + */ +gdispImageError gwinImageCache(GHandle gh); + +#ifdef __cplusplus +} +#endif + +#endif // _GWIN_IMAGE_H +/** @} */ + diff --git a/src/gwin/imagebox.c b/src/gwin/imagebox.c deleted file mode 100644 index 44ba785a..00000000 --- a/src/gwin/imagebox.c +++ /dev/null @@ -1,175 +0,0 @@ -/* - * This file is subject to the terms of the GFX License. If a copy of - * the license was not distributed with this file, you can obtain one at: - * - * http://ugfx.org/license.html - */ - -/** - * @file src/gwin/gimage.c - * @brief GWIN sub-system image code. - */ - -#include "gfx.h" - -#if GFX_USE_GWIN && GWIN_NEED_IMAGE - -#include "src/gwin/class_gwin.h" - -#define widget(gh) ((GImageObject *)gh) - -static void _destroy(GWindowObject *gh) { - if (gdispImageIsOpen(&widget(gh)->image)) - gdispImageClose(&widget(gh)->image); -} - -#if GWIN_NEED_IMAGE_ANIMATION - static void _redraw(GHandle gh); - - static void _timer(void *gh) { - // We need to re-test the visibility in case it has been made invisible since the last frame. - if ((((GHandle)gh)->flags & GWIN_FLG_VISIBLE)) - _redraw((GHandle)gh); - } -#endif - -static void _redraw(GHandle gh) { - coord_t x, y, w, h, dx, dy; - color_t bg; - #if GWIN_NEED_IMAGE_ANIMATION - delaytime_t delay; - #endif - - // The default display area - dx = 0; - dy = 0; - x = gh->x; - y = gh->y; - w = gh->width; - h = gh->height; - bg = gwinGetDefaultBgColor(); - - // If the image isn't open just clear the area - if (!gdispImageIsOpen(&widget(gh)->image)) { - gdispGFillArea(gh->display, x, y, w, h, bg); - return; - } - - // Center horizontally if the area is larger than the image - if (widget(gh)->image.width < w) { - w = widget(gh)->image.width; - dx = (gh->width-w)/2; - x += dx; - if (dx) - gdispGFillArea(gh->display, gh->x, y, dx, h, bg); - gdispGFillArea(gh->display, x+w, y, gh->width-dx-w, h, bg); - dx = 0; - } - - // Center image horizontally if the area is smaller than the image - else if (widget(gh)->image.width > w) { - dx = (widget(gh)->image.width - w)/2; - } - - // Center vertically if the area is larger than the image - if (widget(gh)->image.height < h) { - h = widget(gh)->image.height; - dy = (gh->height-h)/2; - y += dy; - if (dy) - gdispGFillArea(gh->display, x, gh->y, w, dy, bg); - gdispGFillArea(gh->display, x, y+h, w, gh->height-dy-h, bg); - dy = 0; - } - - // Center image vertically if the area is smaller than the image - else if (widget(gh)->image.height > h) { - dy = (widget(gh)->image.height - h)/2; - } - - // Reset the background color in case it has changed - gdispImageSetBgColor(&widget(gh)->image, bg); - - // Display the image - gdispGImageDraw(gh->display, &widget(gh)->image, x, y, w, h, dx, dy); - - #if GWIN_NEED_IMAGE_ANIMATION - // read the delay for the next frame - delay = gdispImageNext(&widget((GHandle)gh)->image); - - // Wait for that delay if required - switch(delay) { - case TIME_INFINITE: - // Everything is done - break; - case TIME_IMMEDIATE: - // We can't allow a continuous loop here as it would lock the system up so we delay for the minimum period - delay = 1; - // Fall through - default: - // Start the timer to draw the next frame of the animation - gtimerStart(&widget((GHandle)gh)->timer, _timer, (void*)gh, FALSE, delay); - break; - } - #endif -} - -static const gwinVMT imageVMT = { - "Image", // The class name - sizeof(GImageObject), // The object size - _destroy, // The destroy routine - _redraw, // The redraw routine - 0, // The after-clear routine -}; - -GHandle gwinGImageCreate(GDisplay *g, GImageObject *gobj, GWindowInit *pInit) { - if (!(gobj = (GImageObject *)_gwindowCreate(g, &gobj->g, pInit, &imageVMT, 0))) - return 0; - - // Ensure the gdispImageIsOpen() gives valid results - gobj->image.type = 0; - gobj->image.fns = 0; - - // Initialise the timer - #if GWIN_NEED_IMAGE_ANIMATION - gtimerInit(&gobj->timer); - #endif - - gwinSetVisible((GHandle)gobj, pInit->show); - - return (GHandle)gobj; -} - -bool_t gwinImageOpenGFile(GHandle gh, GFILE *f) { - // is it a valid handle? - if (gh->vmt != (gwinVMT *)&imageVMT) - return FALSE; - - if (gdispImageIsOpen(&widget(gh)->image)) - gdispImageClose(&widget(gh)->image); - - if ((gdispImageOpenGFile(&widget(gh)->image, f) & GDISP_IMAGE_ERR_UNRECOVERABLE)) - return FALSE; - - if ((gh->flags & GWIN_FLG_VISIBLE)) { - // Setting the clip here shouldn't be necessary if the redraw doesn't overdraw - // but we put it in for safety anyway - #if GDISP_NEED_CLIP - gdispGSetClip(gh->display, gh->x, gh->y, gh->width, gh->height); - #endif - _redraw(gh); - } - - return TRUE; -} - -gdispImageError gwinImageCache(GHandle gh) { - // is it a valid handle? - if (gh->vmt != (gwinVMT *)&imageVMT) - return GDISP_IMAGE_ERR_BADFORMAT; - - return gdispImageCache(&widget(gh)->image); -} - -#endif // GFX_USE_GWIN && GWIN_NEED_IMAGE -/** @} */ diff --git a/src/gwin/imagebox.h b/src/gwin/imagebox.h deleted file mode 100644 index fdaabc92..00000000 --- a/src/gwin/imagebox.h +++ /dev/null @@ -1,127 +0,0 @@ -/* - * This file is subject to the terms of the GFX License. If a copy of - * the license was not distributed with this file, you can obtain one at: - * - * http://ugfx.org/license.html - */ - -/** - * @file src/gwin/image.h - * @brief GWIN image widget header file. - * - * @defgroup Image Image - * @ingroup GWIN - * - * @details GWIN allos it to create an image widget. The widget - * takes no user input. - * - * @pre GFX_USE_GDISP must be set to TRUE in your gfxconf.h - * @pre GFX_USE_GWIN must be set to TRUE in your gfxconf.h - * @pre GDISP_NEED_IMAGE must be set to TRUE in your gfxconf.h - * @pre GWIN_NEED_IMAGE must be set to TRUE in your gfxconf.h - * @pre At least one image type must be enabled in your gfxconf.h - * - * @{ - */ - -#ifndef _GWIN_IMAGE_H -#define _GWIN_IMAGE_H - -// This file is included within "gwin/gwin.h" - -// An image window -typedef struct GImageObject { - GWindowObject g; - gdispImage image; // The image itself - #if GWIN_NEED_IMAGE_ANIMATION - GTimer timer; // Timer used for animated images - #endif -} GImageObject; - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Create an image widget. - * @details Display's a picture. - * @return NULL if there is no resultant drawing area, otherwise the widget handle. - * - * @param[in] g The GDisplay to display this window on - * @param[in] widget The image widget structure to initialise. If this is NULL, the structure is dynamically allocated. - * @param[in] pInit The initialization parameters to use. - * - * @note The default background color gets set to the current default one. - * @note An image window knows how to redraw. - * - * @api - */ -GHandle gwinGImageCreate(GDisplay *g, GImageObject *widget, GWindowInit *pInit); -#define gwinImageCreate(w, pInit) gwinGImageCreate(GDISP, w, pInit) - -/** - * @brief Opens the image using a GFILE - * @return TRUE if the image can be opened - * - * @param[in] gh The widget (must be an image widget) - * @param[in] f The open (for reading) GFILE to use - * - * @api - */ -bool_t gwinImageOpenGFile(GHandle gh, GFILE *f); - -/** - * @brief Opens the image using the specified filename - * @return TRUE if the open succeeds - * - * @param[in] gh The widget (must be an image widget) - * @param[in] filename The filename to open - * - * @api - */ -#define gwinImageOpenFile(gh, filename) gwinImageOpenGFile((gh), gfileOpen((filename), "rb")) - - /** - * @brief Sets the input routines that support reading the image from memory - * in RAM or flash. - * @pre GFILE_NEED_MEMFS must be TRUE - * @return TRUE if the IO open function succeeds - * - * @param[in] gh The widget (must be an image widget) - * @param[in] ptr A pointer to the image in RAM or Flash - * - * @api - */ -#define gwinImageOpenMemory(gh, ptr) gwinImageOpenGFile((gh), gfileOpenMemory((void *)(ptr), "rb")) - -/** - * @brief Sets the input routines that support reading the image from a BaseFileStream (eg. an SD-Card). - * @return TRUE if the IO open function succeeds - * @pre GFILE_NEED_CHIBIOSFS and GFX_USE_OS_CHIBIOS must be TRUE - * - * @param[in] gh The widget (must be an image widget) - * @param[in] streamPtr A pointer to the (open) BaseFileStream object. - * - * @api - */ -#define gwinImageOpenStream(gh, streamPtr) gwinImageOpenGFile((gh), gfileOpenBaseFIleStream((streamPtr), "rb")) - -/** - * @brief Cache the image. - * @details Decodes and caches the current frame into RAM. - * - * @param[in] gh The widget (must be an image widget) - * - * @return GDISP_IMAGE_ERR_OK (0) on success or an error code. - * - * @api - */ -gdispImageError gwinImageCache(GHandle gh); - -#ifdef __cplusplus -} -#endif - -#endif // _GWIN_IMAGE_H -/** @} */ - diff --git a/src/gwin/sys_defs.h b/src/gwin/sys_defs.h index 53ed016b..f4e1c2f7 100644 --- a/src/gwin/sys_defs.h +++ b/src/gwin/sys_defs.h @@ -874,7 +874,7 @@ extern "C" { #endif #if GWIN_NEED_IMAGE || defined(__DOXYGEN__) - #include "src/gwin/imagebox.h" + #include "src/gwin/image.h" #endif #endif /* GFX_USE_GWIN */ diff --git a/src/gwin/sys_make.mk b/src/gwin/sys_make.mk index 1e2404e6..4c670ea2 100644 --- a/src/gwin/sys_make.mk +++ b/src/gwin/sys_make.mk @@ -6,7 +6,7 @@ GFXSRC += $(GFXLIB)/src/gwin/gwin.c \ $(GFXLIB)/src/gwin/button.c \ $(GFXLIB)/src/gwin/slider.c \ $(GFXLIB)/src/gwin/checkbox.c \ - $(GFXLIB)/src/gwin/imagebox.c \ + $(GFXLIB)/src/gwin/gimage.c \ $(GFXLIB)/src/gwin/label.c \ $(GFXLIB)/src/gwin/radio.c \ $(GFXLIB)/src/gwin/list.c \ -- cgit v1.2.3