Andrew Reed / Mbed OS CITY1082-i2c_master_wifi_mqtt
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WIDGET_ButtonRound.c Source File

WIDGET_ButtonRound.c

00001 /*********************************************************************
00002 *                SEGGER Microcontroller GmbH                         *
00003 *        Solutions for real time microcontroller applications        *
00004 **********************************************************************
00005 *                                                                    *
00006 *        (c) 1996 - 2018  SEGGER Microcontroller GmbH                *
00007 *                                                                    *
00008 *        Internet: www.segger.com    Support:  support@segger.com    *
00009 *                                                                    *
00010 **********************************************************************
00011 
00012 ** emWin V5.48 - Graphical user interface for embedded applications **
00013 All  Intellectual Property rights  in the Software belongs to  SEGGER.
00014 emWin is protected by  international copyright laws.  Knowledge of the
00015 source code may not be used to write a similar product.  This file may
00016 only be used in accordance with the following terms:
00017 
00018 The software  has been licensed to  Cypress Semiconductor Corporation,
00019 whose registered  office is situated  at 198 Champion Ct. San Jose, CA 
00020 95134 USA  solely for the  purposes of creating  libraries for Cypress
00021 PSoC3 and  PSoC5 processor-based devices,  sublicensed and distributed
00022 under  the  terms  and  conditions  of  the  Cypress  End User License
00023 Agreement.
00024 Full source code is available at: www.segger.com
00025 
00026 We appreciate your understanding and fairness.
00027 ----------------------------------------------------------------------
00028 Licensing information
00029 Licensor:                 SEGGER Microcontroller Systems LLC
00030 Licensed to:              Cypress Semiconductor Corp, 198 Champion Ct., San Jose, CA 95134, USA
00031 Licensed SEGGER software: emWin
00032 License number:           GUI-00319
00033 License model:            Services and License Agreement, signed June 10th, 2009
00034 Licensed platform:        Any Cypress platform (Initial targets are: PSoC3, PSoC5)
00035 ----------------------------------------------------------------------
00036 Support and Update Agreement (SUA)
00037 SUA period:               2009-06-12 - 2022-07-27
00038 Contact to extend SUA:    sales@segger.com
00039 ----------------------------------------------------------------------
00040 File        : WIDGET_ButtonRound.c
00041 Purpose     : Demonstrates how to create and use a round button
00042 Requirements: WindowManager - (x)
00043               MemoryDevices - (x)
00044               AntiAliasing  - ( )
00045               VNC-Server    - ( )
00046               PNG-Library   - ( )
00047               TrueTypeFonts - ( )
00048 ---------------------------END-OF-HEADER------------------------------
00049 */
00050 #include <stddef.h>
00051 #include <string.h>
00052 #include "WM.h"
00053 #include "FRAMEWIN.h"
00054 #include "BUTTON.h"
00055 
00056 /*********************************************************************
00057 *
00058 *       Defines
00059 *
00060 **********************************************************************
00061 */
00062 //
00063 // Recommended memory to run the sample with adequate performance
00064 //
00065 #define RECOMMENDED_MEMORY (1024L * 5)
00066 
00067 /*********************************************************************
00068 *
00069 *       Static data
00070 *
00071 **********************************************************************
00072 */
00073 static int _Color;
00074 static int _Font;
00075 static int _Pressed;
00076 
00077 static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
00078   { FRAMEWIN_CreateIndirect, "Round button sample", 0,      50,  60, 200, 120, FRAMEWIN_CF_MOVEABLE },
00079   { BUTTON_CreateIndirect,   "Button",   GUI_ID_BUTTON0,   100,  10,  80,  80 },
00080   { BUTTON_CreateIndirect,   "Callback", GUI_ID_BUTTON1,    10,  10,  60,  20 },
00081   { BUTTON_CreateIndirect,   "Font",     GUI_ID_BUTTON2,    10,  30,  60,  20 },
00082   { BUTTON_CreateIndirect,   "Color",    GUI_ID_BUTTON3,    10,  50,  60,  20 },
00083   { BUTTON_CreateIndirect,   "Cancel",   GUI_ID_CANCEL,     10,  70,  60,  20 }
00084 };
00085 
00086 /*********************************************************************
00087 *
00088 *       Static functions
00089 *
00090 **********************************************************************
00091 */
00092 /*********************************************************************
00093 *
00094 *       _OnPaint
00095 *
00096 * Function description
00097 *   Paints the owner drawn button
00098 */
00099 static void _OnPaint(BUTTON_Handle hObj) {
00100   int Index;
00101   char ac[50];
00102   GUI_RECT Rect;
00103 
00104   Index = (WIDGET_GetState(hObj) & BUTTON_STATE_PRESSED) ? 1 : 0;
00105   WM_GetClientRect(&Rect);
00106   //
00107   // Draw filled ellipse with button background color
00108   //
00109   GUI_SetColor(BUTTON_GetBkColor(hObj, Index));
00110   GUI_FillEllipse(Rect.x1 / 2, Rect.y1 / 2, Rect.x1 / 2, Rect.y1 / 2);
00111   //
00112   // Draw black shape
00113   //
00114   GUI_SetColor(GUI_BLACK);
00115   GUI_DrawEllipse(Rect.x1 / 2, Rect.y1 / 2, Rect.x1 / 2, Rect.y1 / 2);
00116   //
00117   // Draw button text with widget attributes
00118   //
00119   GUI_SetColor(BUTTON_GetTextColor(hObj, Index));
00120   GUI_SetBkColor(BUTTON_GetBkColor(hObj, Index));
00121   GUI_SetFont(BUTTON_GetFont(hObj));
00122   BUTTON_GetText(hObj, ac, sizeof(ac));
00123   if (_Pressed) {
00124     strcpy(ac + strlen(ac), "\npressed");
00125   }
00126   GUI_DispStringInRect(ac, &Rect, GUI_TA_HCENTER | GUI_TA_VCENTER);
00127 }
00128 
00129 /*********************************************************************
00130 *
00131 *       _cbButton
00132 *
00133 * Function description 
00134 *  1. Calls the owner draw function if the WM_PAINT message has been send
00135 *  2. Calls the original callback for further messages
00136 *  3. After processing the messages the function evaluates the pressed-state
00137 *     if the WM_TOUCH message has been send
00138 */
00139 static void _cbButton(WM_MESSAGE * pMsg) {
00140   switch (pMsg->MsgId) {
00141     case WM_PAINT:
00142       _OnPaint(pMsg->hWin);
00143       break;
00144     default:
00145       BUTTON_Callback(pMsg); // The original callback
00146       break;
00147   }
00148   if (pMsg->MsgId == WM_TOUCH) {
00149     if (BUTTON_IsPressed(pMsg->hWin)) {
00150       if (!_Pressed) {
00151         _Pressed = 1;
00152       }
00153     } else {
00154       _Pressed = 0;
00155     }
00156   }
00157 }
00158 
00159 /*********************************************************************
00160 *
00161 *       _cbDialog
00162 *
00163 * Function description
00164 *   Dialog callback routine
00165 */
00166 static void _cbDialog(WM_MESSAGE * pMsg) {
00167   int           NCode;
00168   int           Id;
00169   WM_HWIN       hDlg;
00170   BUTTON_Handle hButton;
00171 
00172   hDlg = pMsg->hWin;
00173   switch (pMsg->MsgId) {
00174     case WM_PAINT:
00175       WM_DefaultProc(pMsg); // Handle dialog items
00176       //
00177       // After drawing the dialog items add some user drawn items to the window
00178       //
00179       GUI_SetPenSize(10);
00180       GUI_SetColor(GUI_GREEN);
00181       GUI_DrawLine( 95,  5, 185, 95);
00182       GUI_SetColor(GUI_RED);
00183       GUI_DrawLine( 95, 95, 185,  5);
00184       break;
00185     case WM_INIT_DIALOG:
00186       hButton = WM_GetDialogItem(hDlg, GUI_ID_BUTTON0);
00187       WM_SetHasTrans(hButton);              // Set transparency flag for button
00188       break;
00189     case WM_KEY:
00190       switch (((WM_KEY_INFO *)(pMsg->Data.p))->Key) {
00191         case GUI_KEY_ESCAPE:
00192           GUI_EndDialog(hDlg, 1);
00193           break;
00194         case GUI_KEY_ENTER:
00195           GUI_EndDialog(hDlg, 0);
00196           break;
00197       }
00198       break;
00199     case WM_NOTIFY_PARENT:
00200       Id    = WM_GetId(pMsg->hWinSrc);      // Id of widget
00201       NCode = pMsg->Data.v;                 // Notification code
00202       switch (NCode) {
00203         case WM_NOTIFICATION_RELEASED:      // React only if released
00204           hButton = WM_GetDialogItem(hDlg, GUI_ID_BUTTON0);
00205           if (Id == GUI_ID_BUTTON1) {       // Toggle callback
00206             if (WM_GetCallback(hButton) == _cbButton) {
00207               WM_SetCallback(hButton, BUTTON_Callback);
00208             } else {
00209               WM_SetCallback(hButton, _cbButton);
00210             }
00211             WM_InvalidateWindow(hButton);
00212           }
00213           if (Id == GUI_ID_BUTTON2) {       // Toggle font
00214             if (_Font) {
00215               BUTTON_SetFont(hButton, &GUI_Font13_1);
00216             } else {
00217               BUTTON_SetFont(hButton, &GUI_Font8x16);
00218             }
00219             _Font ^= 1;
00220           }
00221           if (Id == GUI_ID_BUTTON3) {       // Toggle color
00222             if (_Color) {
00223               BUTTON_SetBkColor(hButton, 0, GUI_MAKE_COLOR(0xaaaaaa));
00224               BUTTON_SetBkColor(hButton, 1, GUI_WHITE);
00225               BUTTON_SetTextColor(hButton, 0, GUI_BLACK);
00226               BUTTON_SetTextColor(hButton, 1, GUI_BLACK);
00227             } else {
00228               BUTTON_SetBkColor(hButton, 0, GUI_BLUE);
00229               BUTTON_SetBkColor(hButton, 1, GUI_RED);
00230               BUTTON_SetTextColor(hButton, 0, GUI_WHITE);
00231               BUTTON_SetTextColor(hButton, 1, GUI_YELLOW);
00232             }
00233             _Color ^= 1;
00234           }
00235           if (Id == GUI_ID_OK) {            // OK Button
00236             GUI_EndDialog(hDlg, 0);
00237           }
00238           if (Id == GUI_ID_CANCEL) {        // Cancel Button
00239             GUI_EndDialog(hDlg, 1);
00240           }
00241           break;
00242       }
00243       break;
00244     default:
00245       WM_DefaultProc(pMsg);
00246   }
00247 }
00248 
00249 /*********************************************************************
00250 *
00251 *       Public code
00252 *
00253 **********************************************************************
00254 */
00255 /*********************************************************************
00256 *
00257 *       MainTask
00258 */
00259 void MainTask(void) {
00260   GUI_Init();
00261   //
00262   // Check if recommended memory for the sample is available
00263   //
00264   if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
00265     GUI_ErrorOut("Not enough memory available."); 
00266     return;
00267   }
00268   //
00269   // Use memory devices for all windows
00270   //
00271   #if GUI_SUPPORT_MEMDEV
00272     WM_SetCreateFlags(WM_CF_MEMDEV);
00273     WM_EnableMemdev(WM_HBKWIN);
00274   #endif
00275   WM_SetDesktopColor(GUI_GREEN);
00276   while(1) {
00277     _Font = _Color = 0;
00278     GUI_ExecDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), &_cbDialog, 0, 0, 0);
00279     GUI_Delay(1000);
00280   }
00281 }
00282 
00283 /*************************** End of file ****************************/