PushButton

From uGFX Wiki
Revision as of 05:47, 16 August 2014 by Inmarket (Talk | contribs) (Cancel feature)

Jump to: navigation, search

A push button is a stand-alone widget with a static size and a text where the text is drawn centered inside the button area. A push button only know the state pressed and unpressed.

Cancel feature

The button implementation allows it to cancel a button pressed event: When you push a button but release it outside of it's area, the action is canceled. However, this feature can become a problem with touchscreen systems which have a lot of noise on the output coordinates. When you have a small button, the chance that just one point is outside of the area of a small button is very high.

To fix this issue, there's the macro GWIN_BUTTON_LAZY_RELEASE available. When you set it to TRUE in your gfxconf.h, the cancel feature get's disabled.

Of course the real solution is to fix the noise. One of the things that may help is reducing your SPI frequency if your touch is connected via SPI. There is a demo program that allows you to fine-tune the parameters for your touch driver in tools/ginput_touch_driver_test. Compile and run it and each step will help you adjust one of the touch parameters. Unfortunately the parameters are not run-time changable. You need to run a test, alter the paremter, recompile and then run it again. Also don't try to do all the parameters at the same time as the setting of one can affect the setting of others - so do the tests one step at a time.

API Reference

The API reference for the push button widget can be found here.

Example

The following example shows how to use a push button widget:

#include "gfx.h"
 
static GListener gl;
static GHandle   ghButton1;
 
static void createWidgets(void) {
	GWidgetInit	wi;
 
	// Apply some default values for GWIN
	wi.customDraw = 0;
	wi.customParam = 0;
	wi.customStyle = 0;
	wi.g.show = TRUE;
 
	// Apply the button parameters	
	wi.g.width = 100;
	wi.g.height = 30;
	wi.g.y = 10;
	wi.g.x = 10;
	wi.text = "Push Button";
 
	// Create the actual button
	ghButton1 = gwinButtonCreate(NULL, &wi);
}
 
int main(void) {
	GEvent* pe;
 
	// Initialize the display
	gfxInit();
 
	// Set the widget defaults
	gwinSetDefaultFont(gdispOpenFont("UI2"));
	gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE);
	gdispClear(White);
 
	// Attach the mouse input
	gwinAttachMouse(0);
 
	// create the widget
	createWidgets();
 
	// We want to listen for widget events
	geventListenerInit(&gl);
	gwinAttachListener(&gl);
 
	while(1) {
		// Get an Event
		pe = geventEventWait(&gl, TIME_INFINITE);
 
		switch(pe->type) {
			case GEVENT_GWIN_BUTTON:
				if (((GEventGWinButton*)pe)->button == ghButton1) {
					// Our button has been pressed
					printf("Button clicked\r\n");
				}
				break;
 
			default:
				break;
		}
	}
 
	return 0;
}