Difference between revisions of "BUTTON - Usage (Sample)"

From SEGGER Wiki
Jump to: navigation, search
(Created page with "{| class="wikitable" style="float:right; margin-left: 10px; background-color: #f9f9f9;" ! colspan="2" style="font-weight:bold; font-size:17px; font-family:Arial, Helvetica, sa...")
 
Line 1: Line 1:
 
{| class="wikitable" style="float:right; margin-left: 10px; background-color: #f9f9f9;"
 
{| class="wikitable" style="float:right; margin-left: 10px; background-color: #f9f9f9;"
! colspan="2" style="font-weight:bold; font-size:17px; font-family:Arial, Helvetica, sans-serif !important;; background-color:#ceceff;" | BUTTON_Custom.c
+
! colspan="2" style="font-weight:bold; font-size:17px; font-family:Arial, Helvetica, sans-serif !important;; background-color:#ceceff;" | BUTTON_Usage.c
 
|-
 
|-
| colspan="2" | [[File:BUTTON Custom.gif | frame ]]
+
| colspan="2" | [[File:BUTTON Usage.gif | frame ]]
 
|-
 
|-
 
| style="font-weight:bold; font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | File(s) required
 
| style="font-weight:bold; font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | File(s) required
 
| style="font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | <div style="font-family: monospace,monospace;">
 
| style="font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | <div style="font-family: monospace,monospace;">
* BUTTON_Custom.c
+
* BUTTON_Usage.c
 
</div>
 
</div>
 
|-
 
|-
Line 16: Line 16:
 
|-
 
|-
 
| style="font-weight:bold; font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | Download
 
| style="font-weight:bold; font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | Download
| style="font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | [https://wiki.segger.com/images/0/04/BUTTON_Custom.c BUTTON_Custom.c]
+
| style="font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | [https://wiki.segger.com/images/5/57/BUTTON_Usage.c BUTTON_Usage.c]
 
|}
 
|}
   
This sample demonstrates custom drawing of a BUTTON widget.
+
This sample demonstrates how to use a BUTTON widget.
   
 
== Code ==
 
== Code ==
Line 41: Line 41:
 
distributed in any way. We appreciate your understanding and fairness.
 
distributed in any way. We appreciate your understanding and fairness.
 
----------------------------------------------------------------------
 
----------------------------------------------------------------------
File : BUTTON_Custom.c
+
File : BUTTON_Usage.c
Purpose : Sample that demonstrates how the user can give a
+
Purpose : Sample that demonstrates the usage of a BUTTON widget.
BUTTON widget a custom look.
 
 
Requirements: WindowManager - (x)
 
Requirements: WindowManager - (x)
 
MemoryDevices - ( )
 
MemoryDevices - ( )
Line 54: Line 53:
   
 
#include "DIALOG.h"
 
#include "DIALOG.h"
  +
#include <stdio.h>
   
 
/*********************************************************************
 
/*********************************************************************
Line 61: Line 61:
 
**********************************************************************
 
**********************************************************************
 
*/
 
*/
  +
#define ID_BUTTON 1
   
 
/*********************************************************************
 
/*********************************************************************
Line 75: Line 76:
 
**********************************************************************
 
**********************************************************************
 
*/
 
*/
  +
static void _cbWin(WM_MESSAGE * pMsg) {
/*********************************************************************
 
  +
int Id, NCode;
*
 
  +
static int Clicked, Released;
* MainTask
 
  +
BUTTON_Handle hButton;
*/
 
  +
char acBuffer[64];
static void _cbButton(WM_MESSAGE * pMsg) {
 
GUI_RECT Rect;
 
   
 
switch(pMsg->MsgId) {
 
switch(pMsg->MsgId) {
  +
case WM_CREATE:
  +
//
  +
// Create a button as child of this window.
  +
//
  +
hButton = BUTTON_CreateEx(10, 10, 80, 20, pMsg->hWin, WM_CF_SHOW, 0, ID_BUTTON);
  +
BUTTON_SetText(hButton, "Click me");
  +
break;
 
case WM_PAINT:
 
case WM_PAINT:
  +
GUI_SetBkColor(GUI_WHITE);
  +
GUI_Clear();
  +
GUI_SetColor(GUI_BLACK);
  +
if(Clicked) {
  +
sprintf(acBuffer, "Button was clicked at: %d.", Clicked);
  +
GUI_DispStringAt(acBuffer, 10, 40);
  +
}
  +
if(Released) {
  +
sprintf(acBuffer, "Button was released at: %d.", Released);
  +
GUI_DispStringAt(acBuffer, 10, 60);
  +
}
  +
break;
  +
case WM_NOTIFY_PARENT:
  +
//
  +
// Since the button is a child of this window, reacts on the button
  +
// are sent to its parent window.
 
//
 
//
  +
Id = WM_GetId(pMsg->hWinSrc);
// Anything drawn here will be how the button looks.
 
  +
NCode = pMsg->Data.v;
 
//
 
//
  +
// Check if notification was sent from the button.
if(BUTTON_IsPressed(pMsg->hWin)) {
 
  +
//
GUI_SetColor(GUI_LIGHTRED);
 
} else {
+
switch(Id) {
  +
case ID_BUTTON:
GUI_SetColor(GUI_GRAY_AA);
 
  +
//
  +
// Check for the correct notification code.
  +
//
  +
switch(NCode) {
  +
case WM_NOTIFICATION_CLICKED:
  +
Clicked = GUI_GetTime();
  +
WM_InvalidateWindow(pMsg->hWin);
  +
break;
  +
case WM_NOTIFICATION_RELEASED:
  +
Released = GUI_GetTime();
  +
WM_InvalidateWindow(pMsg->hWin);
  +
break;
  +
}
  +
break;
 
}
 
}
WM_GetClientRect(&Rect);
 
Rect.x0 += 1;
 
Rect.x1 -= 1;
 
Rect.y0 += 1;
 
Rect.y1 -= 1;
 
GUI_FillRoundedRectEx(&Rect, 3);
 
GUI_SetColor(GUI_BLACK);
 
GUI_DrawRoundedRect(Rect.x0, Rect.y0, Rect.x1, Rect.y1, 3);
 
GUI_SetColor(GUI_WHITE);
 
GUI_SetTextMode(GUI_TM_TRANS);
 
GUI_DispStringInRect("Button", &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER);
 
 
break;
 
break;
 
default:
 
default:
  +
WM_DefaultProc(pMsg);
//
 
// Anything else apart from drawing is handled by the default callback.
 
//
 
BUTTON_Callback(pMsg);
 
 
}
 
}
 
}
 
}
Line 123: Line 147:
 
*/
 
*/
 
void MainTask(void) {
 
void MainTask(void) {
BUTTON_Handle hButton;
 
 
 
//
 
//
 
// Init emWin.
 
// Init emWin.
 
//
 
//
 
GUI_Init();
 
GUI_Init();
WM_SetDesktopColor(GUI_WHITE);
 
//
 
// Create BUTTON widget.
 
//
 
hButton = BUTTON_CreateEx(10, 10, 100, 30, WM_HBKWIN, WM_CF_SHOW | WM_CF_HASTRANS, 0, GUI_ID_BUTTON0);
 
 
//
 
//
// Overwrite callback of BUTTON.
+
// Create window.
 
//
 
//
  +
WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWin, 0);
WM_SetCallback(hButton, _cbButton);
 
   
 
while (1) {
 
while (1) {

Revision as of 09:29, 22 April 2020

BUTTON_Usage.c
BUTTON Usage.gif
File(s) required
  • BUTTON_Usage.c
Runs in simulation Yes
Runs on target Yes
Download BUTTON_Usage.c

This sample demonstrates how to use a BUTTON widget.

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only  be used  in accordance  with  a license  and should  not be  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : BUTTON_Usage.c
Purpose     : Sample that demonstrates the usage of a BUTTON widget.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"
#include <stdio.h>

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ID_BUTTON  1

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
static void _cbWin(WM_MESSAGE * pMsg) {
  int           Id, NCode;
  static int    Clicked, Released;
  BUTTON_Handle hButton;
  char          acBuffer[64];

  switch(pMsg->MsgId) {
  case WM_CREATE:
    //
    // Create a button as child of this window.
    //
    hButton = BUTTON_CreateEx(10, 10, 80, 20, pMsg->hWin, WM_CF_SHOW, 0, ID_BUTTON);
    BUTTON_SetText(hButton, "Click me");
    break;
  case WM_PAINT:
    GUI_SetBkColor(GUI_WHITE);
    GUI_Clear();
    GUI_SetColor(GUI_BLACK);
    if(Clicked) {
      sprintf(acBuffer, "Button was clicked at: %d.", Clicked);
      GUI_DispStringAt(acBuffer, 10, 40);
    }
    if(Released) {
      sprintf(acBuffer, "Button was released at: %d.", Released);
      GUI_DispStringAt(acBuffer, 10, 60);
    }
    break;
  case WM_NOTIFY_PARENT:
    //
    // Since the button is a child of this window, reacts on the button
    // are sent to its parent window.
    //
    Id    = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    //
    // Check if notification was sent from the button.
    //
    switch(Id) {
    case ID_BUTTON:
      //
      // Check for the correct notification code.
      //
      switch(NCode) {
      case WM_NOTIFICATION_CLICKED:
        Clicked = GUI_GetTime();
        WM_InvalidateWindow(pMsg->hWin);
        break;
      case WM_NOTIFICATION_RELEASED:
        Released = GUI_GetTime();
        WM_InvalidateWindow(pMsg->hWin);
        break;
      }
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  //
  // Init emWin.
  //
  GUI_Init();
  //
  // Create window.
  //
  WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWin, 0);

  while (1) {
    GUI_Delay(100);
  }
}

/*************************** End of file ****************************/