Difference between revisions of "PushButton"

From uGFX Wiki
Jump to: navigation, search
(Custom Draw Routines)
(Update example code)
 
Line 87: Line 87:
 
gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE);
 
gwinSetDefaultStyle(&WhiteWidgetStyle, FALSE);
 
gdispClear(White);
 
gdispClear(White);
 
// Attach the mouse input
 
gwinAttachMouse(0);
 
 
   
 
   
 
// create the widget
 
// create the widget

Latest revision as of 13:51, 9 September 2024

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 can have the state pressed or unpressed.

API Reference

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

Custom Draw Routines

The button widget predefines a number of custom draw routines. They can be set using gwinSetCustomDraw() or by specifying the custom draw routine in the GWidgetInit structure during creation. Some require certain features to be turned on in your gfxconf.h file. The predefined custom draw routines are:

Custom Draw Routine Requires Description
gwinButtonDraw_Normal The standard button look
gwinButtonDraw_Rounded GDISP_NEED_ARC A button with rounded corners
gwinButtonDraw_Ellipse GDISP_NEED_ELLIPSE An elliptical button
gwinButtonDraw_ArrowUp GDISP_NEED_CONVEX_POLYGON An up arrow
gwinButtonDraw_ArrowDown GDISP_NEED_CONVEX_POLYGON An down arrow
gwinButtonDraw_ArrowLeft GDISP_NEED_CONVEX_POLYGON An left arrow
gwinButtonDraw_ArrowRight GDISP_NEED_CONVEX_POLYGON An right arrow
gwinButtonDraw_Image GDISP_NEED_IMAGE An image button. The param must be a gdispImage pointer. See the API documentation for details.

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.

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
	gwinWidgetClearInit(&wi);
	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);
 
	// 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)->gwin == ghButton1) {
					// Our button has been pressed
					printf("Button clicked\r\n");
				}
				break;
 
			default:
				break;
		}
	}
 
	return 0;
}