2013-07-05 15:46:34 +00:00
|
|
|
/*
|
|
|
|
* 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:
|
|
|
|
*
|
2013-07-21 20:20:37 +00:00
|
|
|
* http://ugfx.org/license.html
|
2013-07-05 15:46:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-08-20 07:42:53 +00:00
|
|
|
* @file src/gwin/gwin_radio.c
|
2014-05-20 16:05:38 +00:00
|
|
|
* @brief GWIN sub-system radio button code
|
2013-07-05 15:46:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gfx.h"
|
|
|
|
|
|
|
|
#if GFX_USE_GWIN && GWIN_NEED_RADIO
|
|
|
|
|
2014-08-20 07:42:53 +00:00
|
|
|
#include "gwin_class.h"
|
2013-07-05 15:46:34 +00:00
|
|
|
|
2014-05-01 19:03:31 +00:00
|
|
|
#define GRADIO_TAB_CNR 8 // Diagonal corner on active tab
|
2014-05-01 06:44:11 +00:00
|
|
|
#define GRADIO_TOP_FADE 50 // (GRADIO_TOP_FADE/255)% fade to white for top of tab/button
|
|
|
|
#define GRADIO_BOTTOM_FADE 25 // (GRADIO_BOTTOM_FADE/255)% fade to black for bottom of tab/button
|
|
|
|
#define GRADIO_OUTLINE_FADE 128 // (GRADIO_OUTLINE_FADE/255)% fade to background for active tab edge
|
2014-05-01 03:57:44 +00:00
|
|
|
|
2013-07-05 15:46:34 +00:00
|
|
|
// Our pressed state
|
|
|
|
#define GRADIO_FLG_PRESSED (GWIN_FIRST_CONTROL_FLAG<<0)
|
|
|
|
|
|
|
|
// Send the button event
|
2013-07-07 09:40:37 +00:00
|
|
|
static void SendRadioEvent(GWidgetObject *gw) {
|
2013-07-05 15:46:34 +00:00
|
|
|
GSourceListener * psl;
|
|
|
|
GEvent * pe;
|
|
|
|
#define pbe ((GEventGWinRadio *)pe)
|
|
|
|
|
|
|
|
// Trigger a GWIN Button Event
|
|
|
|
psl = 0;
|
|
|
|
while ((psl = geventGetSourceListener(GWIDGET_SOURCE, psl))) {
|
|
|
|
if (!(pe = geventGetEventBuffer(psl)))
|
|
|
|
continue;
|
|
|
|
pbe->type = GEVENT_GWIN_RADIO;
|
2014-08-20 02:18:27 +00:00
|
|
|
pbe->gwin = (GHandle)gw;
|
2013-07-05 15:46:34 +00:00
|
|
|
pbe->group = ((GRadioObject *)gw)->group;
|
2014-07-15 06:38:13 +00:00
|
|
|
#if GWIN_WIDGET_TAGS
|
|
|
|
pbe->tag = gw->tag;
|
|
|
|
#endif
|
2013-07-05 15:46:34 +00:00
|
|
|
geventSendEvent(psl);
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef pbe
|
|
|
|
}
|
|
|
|
|
|
|
|
#if GINPUT_NEED_MOUSE
|
|
|
|
// A mouse down has occurred over the button
|
|
|
|
static void MouseDown(GWidgetObject *gw, coord_t x, coord_t y) {
|
|
|
|
(void) x; (void) y;
|
|
|
|
|
2013-07-07 09:40:37 +00:00
|
|
|
gwinRadioPress((GHandle)gw);
|
2013-07-05 15:46:34 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if GINPUT_NEED_TOGGLE
|
|
|
|
// A toggle on has occurred
|
|
|
|
static void ToggleOn(GWidgetObject *gw, uint16_t role) {
|
|
|
|
(void) role;
|
|
|
|
|
2013-07-07 09:40:37 +00:00
|
|
|
gwinRadioPress((GHandle)gw);
|
2013-07-05 15:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ToggleAssign(GWidgetObject *gw, uint16_t role, uint16_t instance) {
|
|
|
|
(void) role;
|
|
|
|
((GRadioObject *)gw)->toggle = instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t ToggleGet(GWidgetObject *gw, uint16_t role) {
|
|
|
|
(void) role;
|
|
|
|
return ((GRadioObject *)gw)->toggle;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// The radio button VMT table
|
|
|
|
static const gwidgetVMT radioVMT = {
|
|
|
|
{
|
|
|
|
"Radio", // The classname
|
|
|
|
sizeof(GRadioObject), // The object size
|
|
|
|
_gwidgetDestroy, // The destroy routine
|
|
|
|
_gwidgetRedraw, // The redraw routine
|
|
|
|
0, // The after-clear routine
|
|
|
|
},
|
|
|
|
gwinRadioDraw_Radio, // The default drawing routine
|
|
|
|
#if GINPUT_NEED_MOUSE
|
|
|
|
{
|
|
|
|
MouseDown, // Process mouse down events
|
|
|
|
0, // Process mouse up events (NOT USED)
|
|
|
|
0, // Process mouse move events (NOT USED)
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#if GINPUT_NEED_TOGGLE
|
|
|
|
{
|
|
|
|
1, // 1 toggle role
|
|
|
|
ToggleAssign, // Assign Toggles
|
|
|
|
ToggleGet, // Get Toggles
|
|
|
|
0, // Process toggle off events (NOT USED)
|
|
|
|
ToggleOn, // Process toggle on events
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#if GINPUT_NEED_DIAL
|
|
|
|
{
|
|
|
|
0, // No dial roles
|
|
|
|
0, // Assign Dials (NOT USED)
|
|
|
|
0, // Get Dials (NOT USED)
|
|
|
|
0, // Process dial move events (NOT USED)
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2013-10-24 08:36:11 +00:00
|
|
|
GHandle gwinGRadioCreate(GDisplay *g, GRadioObject *gw, const GWidgetInit *pInit, uint16_t group) {
|
|
|
|
if (!(gw = (GRadioObject *)_gwidgetCreate(g, &gw->w, pInit, &radioVMT)))
|
2013-07-05 15:46:34 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
#if GINPUT_NEED_TOGGLE
|
|
|
|
gw->toggle = GWIDGET_NO_INSTANCE;
|
|
|
|
#endif
|
|
|
|
gw->group = group;
|
|
|
|
gwinSetVisible((GHandle)gw, pInit->g.show);
|
|
|
|
return (GHandle)gw;
|
|
|
|
}
|
|
|
|
|
2013-07-07 09:40:37 +00:00
|
|
|
void gwinRadioPress(GHandle gh) {
|
2013-07-05 15:46:34 +00:00
|
|
|
GHandle gx;
|
|
|
|
|
|
|
|
if (gh->vmt != (gwinVMT *)&radioVMT || (gh->flags & GRADIO_FLG_PRESSED))
|
|
|
|
return;
|
|
|
|
|
2013-07-07 09:40:37 +00:00
|
|
|
if ((gx = gwinRadioGetActive(((GRadioObject *)gh)->group))) {
|
2013-07-05 15:46:34 +00:00
|
|
|
gx->flags &= ~GRADIO_FLG_PRESSED;
|
2014-05-21 03:02:00 +00:00
|
|
|
_gwinUpdate(gx);
|
2013-07-05 15:46:34 +00:00
|
|
|
}
|
|
|
|
gh->flags |= GRADIO_FLG_PRESSED;
|
2014-05-21 03:02:00 +00:00
|
|
|
_gwinUpdate(gh);
|
2013-07-07 09:40:37 +00:00
|
|
|
SendRadioEvent((GWidgetObject *)gh);
|
2013-07-05 15:46:34 +00:00
|
|
|
}
|
|
|
|
|
2013-07-07 09:40:37 +00:00
|
|
|
bool_t gwinRadioIsPressed(GHandle gh) {
|
2013-07-05 15:46:34 +00:00
|
|
|
if (gh->vmt != (gwinVMT *)&radioVMT)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return (gh->flags & GRADIO_FLG_PRESSED) ? TRUE : FALSE;
|
|
|
|
}
|
|
|
|
|
2013-07-07 09:40:37 +00:00
|
|
|
GHandle gwinRadioGetActive(uint16_t group) {
|
2014-05-09 15:11:30 +00:00
|
|
|
GHandle gh;
|
2013-07-05 15:46:34 +00:00
|
|
|
|
2014-05-09 15:11:30 +00:00
|
|
|
for(gh = gwinGetNextWindow(0); gh; gh = gwinGetNextWindow(gh)) {
|
2013-07-05 15:46:34 +00:00
|
|
|
if (gh->vmt == (gwinVMT *)&radioVMT && ((GRadioObject *)gh)->group == group && (gh->flags & GRADIO_FLG_PRESSED))
|
|
|
|
return gh;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------
|
|
|
|
* Custom Draw Routines
|
|
|
|
*----------------------------------------------------------*/
|
|
|
|
|
2013-07-07 09:40:37 +00:00
|
|
|
static const GColorSet *getDrawColors(GWidgetObject *gw) {
|
2014-05-09 15:11:30 +00:00
|
|
|
if (!(gw->g.flags & GWIN_FLG_SYSENABLED)) return &gw->pstyle->disabled;
|
2013-07-07 09:40:37 +00:00
|
|
|
if ((gw->g.flags & GRADIO_FLG_PRESSED)) return &gw->pstyle->pressed;
|
|
|
|
return &gw->pstyle->enabled;
|
2013-07-05 15:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void gwinRadioDraw_Radio(GWidgetObject *gw, void *param) {
|
2013-07-07 09:40:37 +00:00
|
|
|
#define gcw ((GRadioObject *)gw)
|
|
|
|
coord_t ld, df;
|
|
|
|
const GColorSet * pcol;
|
|
|
|
(void) param;
|
2013-07-05 15:46:34 +00:00
|
|
|
|
|
|
|
if (gw->g.vmt != (gwinVMT *)&radioVMT) return;
|
|
|
|
pcol = getDrawColors(gw);
|
|
|
|
|
|
|
|
ld = gw->g.width < gw->g.height ? gw->g.width : gw->g.height;
|
|
|
|
|
|
|
|
#if GDISP_NEED_CIRCLE
|
2013-07-07 09:40:37 +00:00
|
|
|
df = (ld-1)/2;
|
2013-10-24 08:36:11 +00:00
|
|
|
gdispGFillArea(gw->g.display, gw->g.x, gw->g.y, ld, ld, gw->pstyle->background);
|
|
|
|
gdispGDrawCircle(gw->g.display, gw->g.x+df, gw->g.y+df, df, pcol->edge);
|
2013-07-05 15:46:34 +00:00
|
|
|
|
|
|
|
if (gw->g.flags & GRADIO_FLG_PRESSED)
|
2013-10-24 08:36:11 +00:00
|
|
|
gdispGFillCircle(gw->g.display, gw->g.x+df, gw->g.y+df, df <= 2 ? 1 : (df-2), pcol->fill);
|
2013-07-05 15:46:34 +00:00
|
|
|
#else
|
2013-10-24 08:36:11 +00:00
|
|
|
gdispGFillArea(gw->g.display, gw->g.x+1, gw->g.y+1, ld, ld-2, gw->pstyle->background);
|
|
|
|
gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, ld, ld, pcol->edge);
|
2013-07-05 15:46:34 +00:00
|
|
|
|
|
|
|
df = ld < 4 ? 1 : 2;
|
|
|
|
if (gw->g.flags & GRADIO_FLG_PRESSED)
|
2013-10-24 08:36:11 +00:00
|
|
|
gdispGFillArea(gw->g.display, gw->g.x+df, gw->g.y+df, ld-2*df, ld-2*df, pcol->fill);
|
2013-07-05 15:46:34 +00:00
|
|
|
#endif
|
|
|
|
|
2013-10-24 08:36:11 +00:00
|
|
|
gdispGFillStringBox(gw->g.display, gw->g.x+ld+1, gw->g.y, gw->g.width-ld-1, gw->g.height, gw->text, gw->g.font, pcol->text, gw->pstyle->background, justifyLeft);
|
2013-07-05 15:46:34 +00:00
|
|
|
#undef gcw
|
|
|
|
}
|
|
|
|
|
2014-05-01 03:57:44 +00:00
|
|
|
#if GWIN_FLAT_STYLING
|
|
|
|
void gwinRadioDraw_Button(GWidgetObject *gw, void *param) {
|
|
|
|
const GColorSet * pcol;
|
|
|
|
(void) param;
|
2013-07-05 15:46:34 +00:00
|
|
|
|
2014-05-01 03:57:44 +00:00
|
|
|
if (gw->g.vmt != (gwinVMT *)&radioVMT) return;
|
|
|
|
pcol = getDrawColors(gw);
|
2013-07-05 15:46:34 +00:00
|
|
|
|
2013-10-24 08:36:11 +00:00
|
|
|
gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width-1, gw->g.height-1, gw->text, gw->g.font, pcol->text, pcol->fill, justifyCenter);
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-1, gw->g.y, gw->g.x+gw->g.width-1, gw->g.y+gw->g.height-1, pcol->edge);
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+gw->g.height-1, gw->g.x+gw->g.width-2, gw->g.y+gw->g.height-1, pcol->edge);
|
2013-07-07 09:40:37 +00:00
|
|
|
}
|
2014-05-01 03:57:44 +00:00
|
|
|
void gwinRadioDraw_Tab(GWidgetObject *gw, void *param) {
|
|
|
|
const GColorSet * pcol;
|
|
|
|
(void) param;
|
|
|
|
|
|
|
|
if (gw->g.vmt != (gwinVMT *)&radioVMT) return;
|
|
|
|
pcol = getDrawColors(gw);
|
|
|
|
|
|
|
|
if ((gw->g.flags & GRADIO_FLG_PRESSED)) {
|
|
|
|
gdispGDrawBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, pcol->edge);
|
|
|
|
gdispGFillStringBox(gw->g.display, gw->g.x+1, gw->g.y+1, gw->g.width-2, gw->g.height-1, gw->text, gw->g.font, pcol->text, pcol->fill, justifyCenter);
|
|
|
|
} else {
|
|
|
|
gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width-1, gw->g.height-1, gw->text, gw->g.font, pcol->text, pcol->fill, justifyCenter);
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-1, gw->g.y, gw->g.x+gw->g.width-1, gw->g.y+gw->g.height-1, pcol->edge);
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+gw->g.height-1, gw->g.x+gw->g.width-2, gw->g.y+gw->g.height-1, pcol->edge);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void gwinRadioDraw_Button(GWidgetObject *gw, void *param) {
|
|
|
|
const GColorSet * pcol;
|
|
|
|
fixed alpha;
|
|
|
|
fixed dalpha;
|
|
|
|
coord_t i;
|
|
|
|
color_t tcol, bcol;
|
|
|
|
(void) param;
|
|
|
|
|
|
|
|
if (gw->g.vmt != (gwinVMT *)&radioVMT) return;
|
|
|
|
pcol = getDrawColors(gw);
|
|
|
|
|
|
|
|
/* Fill the box blended from variants of the fill color */
|
2014-05-01 06:44:11 +00:00
|
|
|
tcol = gdispBlendColor(White, pcol->fill, GRADIO_TOP_FADE);
|
|
|
|
bcol = gdispBlendColor(Black, pcol->fill, GRADIO_BOTTOM_FADE);
|
2014-05-01 03:57:44 +00:00
|
|
|
dalpha = FIXED(255)/gw->g.height;
|
|
|
|
for(alpha = 0, i = 0; i < gw->g.height; i++, alpha += dalpha)
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+i, gw->g.x+gw->g.width-2, gw->g.y+i, gdispBlendColor(bcol, tcol, NONFIXED(alpha)));
|
|
|
|
|
|
|
|
gdispGDrawStringBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width-1, gw->g.height-1, gw->text, gw->g.font, pcol->text, justifyCenter);
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-1, gw->g.y, gw->g.x+gw->g.width-1, gw->g.y+gw->g.height-1, pcol->edge);
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+gw->g.height-1, gw->g.x+gw->g.width-2, gw->g.y+gw->g.height-1, pcol->edge);
|
|
|
|
}
|
|
|
|
void gwinRadioDraw_Tab(GWidgetObject *gw, void *param) {
|
|
|
|
const GColorSet * pcol;
|
|
|
|
fixed alpha;
|
|
|
|
fixed dalpha;
|
|
|
|
coord_t i;
|
|
|
|
color_t tcol, bcol;
|
|
|
|
(void) param;
|
|
|
|
|
|
|
|
if (gw->g.vmt != (gwinVMT *)&radioVMT) return;
|
|
|
|
pcol = getDrawColors(gw);
|
|
|
|
|
|
|
|
if ((gw->g.flags & GRADIO_FLG_PRESSED)) {
|
2014-05-01 06:44:11 +00:00
|
|
|
tcol = gdispBlendColor(pcol->edge, gw->pstyle->background, GRADIO_OUTLINE_FADE);
|
2014-05-01 03:57:44 +00:00
|
|
|
gdispGFillStringBox(gw->g.display, gw->g.x, gw->g.y, gw->g.width, gw->g.height, gw->text, gw->g.font, pcol->text, gw->g.bgcolor, justifyCenter);
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y, gw->g.x+gw->g.width-(GRADIO_TAB_CNR+1), gw->g.y, tcol);
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-(GRADIO_TAB_CNR+1), gw->g.y, gw->g.x+gw->g.width-1, gw->g.y+GRADIO_TAB_CNR, tcol);
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-1, gw->g.y+GRADIO_TAB_CNR, gw->g.x+gw->g.width-1, gw->g.y+gw->g.height-1, tcol);
|
|
|
|
} else {
|
|
|
|
/* Fill the box blended from variants of the fill color */
|
2014-05-01 06:44:11 +00:00
|
|
|
tcol = gdispBlendColor(White, pcol->fill, GRADIO_TOP_FADE);
|
|
|
|
bcol = gdispBlendColor(Black, pcol->fill, GRADIO_BOTTOM_FADE);
|
2014-05-01 03:57:44 +00:00
|
|
|
dalpha = FIXED(255)/gw->g.height;
|
|
|
|
for(alpha = 0, i = 0; i < gw->g.height; i++, alpha += dalpha)
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x, gw->g.y+i, gw->g.x+gw->g.width-2, gw->g.y+i, gdispBlendColor(bcol, tcol, NONFIXED(alpha)));
|
|
|
|
gdispGDrawLine(gw->g.display, gw->g.x+gw->g.width-1, gw->g.y, gw->g.x+gw->g.width-1, gw->g.y+gw->g.height-1, pcol->edge);
|
|
|
|
gdispGDrawStringBox(gw->g.display, gw->g.x+1, gw->g.y+1, gw->g.width-2, gw->g.height-2, gw->text, gw->g.font, pcol->text, justifyCenter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-07-05 15:46:34 +00:00
|
|
|
|
|
|
|
#endif /* GFX_USE_GWIN && GWIN_NEED_BUTTON */
|