added gwin enabled parameter and implemented button enable/disable functions

ugfx_release_2.6
Joel Bodenmann 2013-05-20 07:01:20 +02:00
parent 4d4a78f415
commit d4e0ce8b70
3 changed files with 50 additions and 1 deletions

View File

@ -63,7 +63,7 @@ typedef enum GButtonState_e {
GBTN_UP, GBTN_DOWN GBTN_UP, GBTN_DOWN
} GButtonState; } GButtonState;
typedef void (*GButtonDrawFunction)(GHandle gh, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param); typedef void (*GButtonDrawFunction)(GHandle gh, bool_t enabled, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param);
// A button window // A button window
typedef struct GButtonObject_t { typedef struct GButtonObject_t {
@ -145,6 +145,16 @@ void gwinSetButtonText(GHandle gh, const char *txt, bool_t useAlloc);
*/ */
void gwinButtonDraw(GHandle gh); void gwinButtonDraw(GHandle gh);
/**
* @brief Enable or disable a button
*
* @param[in] gh The window handle (must be a button window)
* @param[in] enabled Enable or disable the button
*
* @api
*/
void gwinButtonSetEnabled(GHandle gh, bool_t enabled);
/** /**
* @brief Set the callback routine to perform a custom button drawing. * @brief Set the callback routine to perform a custom button drawing.
* *
@ -156,6 +166,27 @@ void gwinButtonDraw(GHandle gh);
*/ */
void gwinSetButtonCustom(GHandle gh, GButtonDrawFunction fn, void *param); void gwinSetButtonCustom(GHandle gh, GButtonDrawFunction fn, void *param);
/**
* @brief Enable a button
*
* @api
*/
#define gwinEnableButton(gh) gwinButtonSetEnabled( ((GButtonObject *)(gh)), TRUE)
/**
* @brief Disable a button
*
* @api
*/
#define gwinDisableButton(gh) gwinButtonSetEnabled( ((GButtonObject *)(gh)), FALSE)
/**
* @brief Get the state of a button
*
* @param[in] gh The window handle (must be a button window)
*
* @api
*/
#define gwinGetButtonState(gh) (((GButtonObject *)(gh))->state) #define gwinGetButtonState(gh) (((GButtonObject *)(gh))->state)
/** /**

View File

@ -45,6 +45,7 @@ typedef struct GWindowObject_t {
coord_t x, y; // Screen relative position coord_t x, y; // Screen relative position
coord_t width, height; // Dimensions of this window coord_t width, height; // Dimensions of this window
color_t color, bgcolor; // Current drawing colors color_t color, bgcolor; // Current drawing colors
bool_t enabled; // Enabled/Disabled state
#if GDISP_NEED_TEXT #if GDISP_NEED_TEXT
font_t font; // Current font font_t font; // Current font
#endif #endif

View File

@ -55,6 +55,10 @@ static void gwinButtonCallback(void *param, GEvent *pe) {
#define pxe ((GEventToggle *)pe) #define pxe ((GEventToggle *)pe)
#define pbe ((GEventGWinButton *)pe) #define pbe ((GEventGWinButton *)pe)
// check if button is disabled
if (gh->enabled == false)
return;
switch (pe->type) { switch (pe->type) {
#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE #if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
case GEVENT_MOUSE: case GEVENT_MOUSE:
@ -135,6 +139,7 @@ static void gwinButtonCallback(void *param, GEvent *pe) {
GHandle gwinCreateButton(GButtonObject *gb, coord_t x, coord_t y, coord_t width, coord_t height, font_t font, GButtonType type) { GHandle gwinCreateButton(GButtonObject *gb, coord_t x, coord_t y, coord_t width, coord_t height, font_t font, GButtonType type) {
if (!(gb = (GButtonObject *)_gwinInit((GWindowObject *)gb, x, y, width, height, sizeof(GButtonObject)))) if (!(gb = (GButtonObject *)_gwinInit((GWindowObject *)gb, x, y, width, height, sizeof(GButtonObject))))
return 0; return 0;
gb->gwin.type = GW_BUTTON; gb->gwin.type = GW_BUTTON;
gb->fn = 0; gb->fn = 0;
gb->param = 0; gb->param = 0;
@ -145,6 +150,10 @@ GHandle gwinCreateButton(GButtonObject *gb, coord_t x, coord_t y, coord_t width,
gb->txt = ""; gb->txt = "";
geventListenerInit(&gb->listener); geventListenerInit(&gb->listener);
geventRegisterCallback(&gb->listener, gwinButtonCallback, gb); geventRegisterCallback(&gb->listener, gwinButtonCallback, gb);
// buttons are enabled by default
gb->gwin.enabled = true;
return (GHandle)gb; return (GHandle)gb;
} }
@ -225,6 +234,7 @@ void gwinButtonDraw(GHandle gh) {
#endif #endif
gbw->fn(gh, gbw->fn(gh,
gbw->gwin.enabled,
gbw->state == GBTN_DOWN, gbw->state == GBTN_DOWN,
gh->font && gbw->txt ? gbw->txt : "", gh->font && gbw->txt ? gbw->txt : "",
gbw->state == GBTN_DOWN ? &gbw->dn : &gbw->up, gbw->state == GBTN_DOWN ? &gbw->dn : &gbw->up,
@ -245,6 +255,13 @@ void gwinSetButtonCustom(GHandle gh, GButtonDrawFunction fn, void *param) {
#undef gbw #undef gbw
} }
void gwinButtonSetEnabled(GHandle gh, bool_t enabled) {
if (gh->type != GW_BUTTON)
return;
gh->enabled = enabled;
}
void gwinButtonDraw_3D(GHandle gh, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param) { void gwinButtonDraw_3D(GHandle gh, bool_t isdown, const char *txt, const GButtonDrawStyle *pstyle, void *param) {
(void) isdown; (void) isdown;
(void) param; (void) param;