2013-06-02 04:14:49 +00:00
|
|
|
/*
|
|
|
|
* This file is subject to the terms of the GFX License, v1.0. If a copy of
|
|
|
|
* the license was not distributed with this file, you can obtain one at:
|
|
|
|
*
|
|
|
|
* http://chibios-gfx.com/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file src/gwin/checkbox.c
|
|
|
|
* @brief GWIN sub-system button code.
|
|
|
|
*
|
|
|
|
* @defgroup Checkbox Checkbox
|
|
|
|
* @ingroup GWIN
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gfx.h"
|
|
|
|
|
|
|
|
#if (GFX_USE_GWIN && GWIN_NEED_CHECKBOX) || defined(__DOXYGEN__)
|
|
|
|
|
2013-06-02 14:15:46 +00:00
|
|
|
static const GCheckboxColor defaultColors = {
|
|
|
|
Grey, // border
|
|
|
|
Grey, // selected
|
|
|
|
Black // background
|
2013-06-02 04:14:49 +00:00
|
|
|
};
|
|
|
|
|
2013-06-02 14:15:46 +00:00
|
|
|
/* default style drawing routine */
|
2013-06-02 16:59:51 +00:00
|
|
|
static void gwinCheckboxDrawDefaultStyle(GHandle gh, bool_t enabled, bool_t isChecked, void* param) {
|
2013-06-02 14:15:46 +00:00
|
|
|
#define gcw ((GCheckboxObject *)gh)
|
|
|
|
|
|
|
|
(void) enabled;
|
|
|
|
(void) param;
|
|
|
|
|
|
|
|
gdispDrawBox(gh->x, gh->y, gh->width, gh->height, gcw->colors->border);
|
|
|
|
|
2013-06-02 16:59:51 +00:00
|
|
|
if (isChecked)
|
2013-06-02 14:15:46 +00:00
|
|
|
gdispFillArea(gh->x+2, gh->y+2, gh->width-4, gh->height-4, gcw->colors->checked);
|
|
|
|
else
|
|
|
|
gdispFillArea(gh->x+2, gh->y+2, gh->width-4, gh->height-4, gcw->colors->bg);
|
|
|
|
|
|
|
|
#undef gcw
|
|
|
|
}
|
2013-06-02 04:14:49 +00:00
|
|
|
|
|
|
|
/* process an event callback */
|
|
|
|
static void gwinCheckboxCallback(void *param, GEvent *pe) {
|
|
|
|
GSourceListener *psl;
|
|
|
|
#define gh ((GHandle)param)
|
|
|
|
#define gbw ((GCheckboxObject *)param)
|
|
|
|
#define gsh ((GSourceHandle)param)
|
|
|
|
#define pme ((GEventMouse *)pe)
|
|
|
|
#define pte ((GEventTouch *)pe)
|
|
|
|
#define pxe ((GEventToggle *)pe)
|
|
|
|
#define pbe ((GEventGWinCheckbox *)pe)
|
|
|
|
|
|
|
|
/* check if checkbox is disabled */
|
|
|
|
if (!gh->enabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (pe->type) {
|
|
|
|
#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
|
|
|
|
case GEVENT_MOUSE:
|
|
|
|
case GEVENT_TOUCH:
|
|
|
|
|
|
|
|
// Ignore anything other than the primary mouse button going up or down
|
|
|
|
if (!((pme->current_buttons ^ pme->last_buttons) & GINPUT_MOUSE_BTN_LEFT))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((pme->current_buttons & GINPUT_MOUSE_BTN_LEFT)
|
|
|
|
&& pme->x >= gbw->gwin.x && pme->x < gbw->gwin.x + gbw->gwin.width
|
|
|
|
&& pme->y >= gbw->gwin.y && pme->y < gbw->gwin.y + gbw->gwin.height) {
|
|
|
|
|
2013-06-02 16:59:51 +00:00
|
|
|
gbw->isChecked = !gbw->isChecked;
|
2013-06-02 04:14:49 +00:00
|
|
|
|
|
|
|
gwinCheckboxDraw((GHandle)param);
|
2013-06-03 10:23:12 +00:00
|
|
|
break;
|
2013-06-02 04:14:49 +00:00
|
|
|
}
|
2013-06-03 10:23:12 +00:00
|
|
|
return;
|
2013-06-02 04:14:49 +00:00
|
|
|
#endif /* GFX_USE_GINPUT && GINPUT_NEED_MOUSE */
|
|
|
|
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-02 16:43:19 +00:00
|
|
|
// Trigger a GWIN checkbox event
|
|
|
|
psl = 0;
|
|
|
|
while ((psl = geventGetSourceListener(gsh, psl))) {
|
|
|
|
if (!(pe = geventGetEventBuffer(psl)))
|
|
|
|
continue;
|
|
|
|
pbe->type = GEVENT_GWIN_CHECKBOX;
|
|
|
|
pbe->checkbox = gh;
|
2013-06-02 16:59:51 +00:00
|
|
|
pbe->isChecked = gbw->isChecked;
|
2013-06-02 16:43:19 +00:00
|
|
|
geventSendEvent(psl);
|
|
|
|
}
|
|
|
|
|
2013-06-02 04:14:49 +00:00
|
|
|
#undef gh
|
|
|
|
#undef pbe
|
|
|
|
#undef pme
|
|
|
|
#undef pte
|
|
|
|
#undef pxe
|
|
|
|
#undef gsh
|
|
|
|
#undef gbw
|
|
|
|
}
|
|
|
|
|
|
|
|
GHandle gwinCheckboxCreate(GCheckboxObject *gb, coord_t x, coord_t y, coord_t width, coord_t height) {
|
|
|
|
if (!(gb = (GCheckboxObject *)_gwinInit((GWindowObject *)gb, x, y, width, height, sizeof(GCheckboxObject))))
|
|
|
|
return 0;
|
|
|
|
|
2013-06-02 14:15:46 +00:00
|
|
|
gb->gwin.type = GW_CHECKBOX; // create a window of the type checkbox
|
|
|
|
gb->fn = gwinCheckboxDrawDefaultStyle; // set the default style drawing routine
|
|
|
|
gb->colors = &defaultColors; // asign the default colors
|
|
|
|
gb->param = 0; // some safe value here
|
2013-06-02 16:59:51 +00:00
|
|
|
gb->isChecked = GCHBX_UNCHECKED; // checkbox is currently unchecked
|
2013-06-02 14:15:46 +00:00
|
|
|
gb->gwin.enabled = TRUE; // checkboxes are enabled by default
|
2013-06-02 04:14:49 +00:00
|
|
|
|
|
|
|
geventListenerInit(&gb->listener);
|
|
|
|
geventRegisterCallback(&gb->listener, gwinCheckboxCallback, gb);
|
|
|
|
|
|
|
|
// checkboxes are enabled by default
|
|
|
|
gb->gwin.enabled = TRUE;
|
|
|
|
|
|
|
|
return (GHandle)gb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gwinCheckboxSetEnabled(GHandle gh, bool_t enabled) {
|
|
|
|
if (gh->type != GW_CHECKBOX)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gh->enabled = enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gwinCheckboxDraw(GHandle gh) {
|
|
|
|
#define gcw ((GCheckboxObject *)gh)
|
|
|
|
|
|
|
|
if (gh->type != GW_CHECKBOX)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#if GDISP_NEED_CLIP
|
|
|
|
gdispSetClip(gh->x, gh->y, gh->width, gh->height);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
gcw->fn(gh,
|
|
|
|
gcw->gwin.enabled,
|
2013-06-02 16:59:51 +00:00
|
|
|
gcw->isChecked,
|
2013-06-02 04:14:49 +00:00
|
|
|
gcw->param);
|
|
|
|
|
|
|
|
#undef gcw
|
|
|
|
}
|
|
|
|
|
|
|
|
#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE
|
|
|
|
bool_t gwinCheckboxAttachMouse(GHandle gh, uint16_t instance) {
|
|
|
|
GSourceHandle gsh;
|
|
|
|
|
|
|
|
if (gh->type != GW_CHECKBOX || !(gsh = ginputGetMouse(instance)))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return geventAttachSource(&((GCheckboxObject *)gh)->listener, gsh, GLISTEN_MOUSEMETA);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-06-02 14:15:46 +00:00
|
|
|
void gwinCheckboxSetColors(GHandle gh, color_t border, color_t checked, color_t bg) {
|
|
|
|
#define gcw ((GCheckboxObject *)gh)
|
|
|
|
|
|
|
|
if (gh->type != GW_CHECKBOX)
|
|
|
|
return;
|
|
|
|
|
|
|
|
gcw->colors->border = border;
|
|
|
|
gcw->colors->checked = checked,
|
|
|
|
gcw->colors->bg = bg;
|
|
|
|
|
|
|
|
#undef gcw
|
|
|
|
}
|
|
|
|
|
2013-06-02 04:14:49 +00:00
|
|
|
#endif /* (GFX_USE_GWIN && GWIN_NEED_CHECKBOX) */
|
|
|
|
/** @} */
|
|
|
|
|