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

« Back to documentation index

Show/hide line numbers WIDGET_PopupMenu.c Source File

WIDGET_PopupMenu.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_PopupMenu.c
00041 Purpose     : Shows how to create a popup menu
00042 Requirements: WindowManager - (x)
00043               MemoryDevices - ( )
00044               AntiAliasing  - ( )
00045               VNC-Server    - ( )
00046               PNG-Library   - ( )
00047               TrueTypeFonts - ( )
00048 ----------------------------------------------------------------------
00049 */
00050 
00051 #include "GUI.h"
00052 #include "MENU.h"
00053 
00054 /*********************************************************************
00055 *
00056 *       Defines
00057 *
00058 **********************************************************************
00059 */
00060 /* Define IDs for menu items */
00061 #define ID_MENU_NEW       (GUI_ID_USER +  0)
00062 #define ID_MENU_PASS      (GUI_ID_USER +  1)
00063 #define ID_MENU_EXIT      (GUI_ID_USER +  2)
00064 
00065 //
00066 // Recommended memory to run the sample with adequate performance
00067 //
00068 #define RECOMMENDED_MEMORY (1024L * 5)
00069 
00070 /*********************************************************************
00071 *
00072 *       Types
00073 *
00074 **********************************************************************
00075 */
00076 //
00077 // Structure for menu items
00078 //
00079 typedef struct {
00080   char * sText;
00081   U16 Id;
00082   U16 Flags;
00083 } MENU_ITEM;
00084 
00085 /*********************************************************************
00086 *
00087 *       Static data
00088 *
00089 **********************************************************************
00090 */
00091 //
00092 // Text for message box
00093 //
00094 static const char * _acMessage[] = {
00095   "New Game will be started...",
00096   "Passed...",
00097   "Application will be closed..."
00098 };
00099 //
00100 // Explanation of sample
00101 //
00102 static char * _apExplain[] = {
00103   "This sample shows how to use a popup menu.",
00104   "Please touch the display at any position...",
00105 };
00106 //
00107 // Array of menu items
00108 //
00109 static MENU_ITEM _aMenuItems[] = {
00110   {"New game", ID_MENU_NEW,  0},
00111   {"Pass",     ID_MENU_PASS, 0},
00112   {0,          0,            MENU_IF_SEPARATOR},
00113   {"Exit",     ID_MENU_EXIT, 0},
00114 };
00115 
00116 /*********************************************************************
00117 *
00118 *       Static code
00119 *
00120 **********************************************************************
00121 */
00122 /*********************************************************************
00123 *
00124 *       _AddMenuItem
00125 *
00126 * Function description
00127 *   Adds one menu item to the given menu
00128 */
00129 static void _AddMenuItem(MENU_Handle hMenu, MENU_Handle hSubmenu, const char* pText, U16 Id, U16 Flags) {
00130   MENU_ITEM_DATA Item;
00131 
00132   Item.pText    = pText;
00133   Item.hSubmenu = hSubmenu;
00134   Item.Flags    = Flags;
00135   Item.Id       = Id;
00136   MENU_AddItem(hMenu, &Item);
00137 }
00138 
00139 /*********************************************************************
00140 *
00141 *       _OpenPopup
00142 *
00143 * Function description
00144 *   The function opens a popup menu at the given position. It returns
00145 *   immediately after creation. On the first call it creates the menu.
00146 */
00147 static void _OpenPopup(WM_HWIN hParent, MENU_ITEM * pMenuItems, int NumItems, int x, int y) {
00148   static MENU_Handle hMenu;
00149   if (!hMenu) {
00150     int i;
00151 
00152     //
00153     // Create the popup window only one time
00154     //
00155     hMenu = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0);
00156     for (i = 0; i < NumItems; i++) {
00157       _AddMenuItem(hMenu, 0, pMenuItems[i].sText, pMenuItems[i].Id, pMenuItems[i].Flags);
00158     }
00159   }
00160   //
00161   // Open the popup menu. After opening the menu the function returns immediately.
00162   // After selecting a menu item or after touching the display outside the menu the 
00163   // popup menu will be closed, but not deleted.
00164   //
00165   MENU_Popup(hMenu, hParent, x, y, 0, 0, 0);
00166 }
00167 
00168 /*********************************************************************
00169 *
00170 *       _cbBkWin
00171 *
00172 * Function description
00173 *   Callback function of the background window. On receiving a
00174 *   WM_PID_STATE_CHANGED message it creates a popup menu.
00175 *   After selecting a menu item it shows a short message box.
00176 */
00177 static void _cbBkWin(WM_MESSAGE * pMsg) {
00178   unsigned       i;
00179   MENU_MSG_DATA* pData;
00180 
00181   switch (pMsg->MsgId) {
00182   case WM_PAINT:
00183     //
00184     // Paint the background window
00185     //
00186     GUI_SetBkColor(GUI_RED);
00187     GUI_Clear();
00188     GUI_SetFont(&GUI_Font24_ASCII);
00189     GUI_DispStringHCenterAt("WIDGET_PopupMenu - Sample", 160, 5);
00190     GUI_DispNextLine();
00191     GUI_SetFont(&GUI_Font13_ASCII);
00192     GUI_DispNextLine();
00193     for (i = 0; i < GUI_COUNTOF(_apExplain); i++) {
00194       GUI_DispStringHCenterAt(_apExplain[i], 160, GUI_GetDispPosY());
00195       GUI_DispNextLine();
00196     }
00197     break;
00198   case WM_MENU:
00199     //
00200     // Process the menu message
00201     //
00202     pData = (MENU_MSG_DATA*)pMsg->Data.p;
00203     switch (pData->MsgType) {
00204     case MENU_ON_ITEMSELECT:
00205       //
00206       // A menu item has been selected
00207       //
00208       switch (pData->ItemId) {
00209       case ID_MENU_NEW:
00210       case ID_MENU_PASS:
00211       case ID_MENU_EXIT:
00212         //
00213         // Short message box
00214         //
00215         GUI_MessageBox(_acMessage[pData->ItemId - GUI_ID_USER], "Message", GUI_MESSAGEBOX_CF_MODAL);
00216         break;
00217       }
00218       break;
00219     default:
00220       WM_DefaultProc(pMsg);
00221     }
00222     break;
00223   case WM_PID_STATE_CHANGED:
00224     //
00225     // Create a popup menu after touching the display
00226     //
00227     _OpenPopup(WM_HBKWIN, _aMenuItems, GUI_COUNTOF(_aMenuItems),
00228                ((WM_PID_STATE_CHANGED_INFO *)pMsg->Data.p)->x, 
00229                ((WM_PID_STATE_CHANGED_INFO *)pMsg->Data.p)->y);
00230     break;
00231   default:
00232     WM_DefaultProc(pMsg);
00233   }
00234 }
00235 
00236 /*********************************************************************
00237 *
00238 *       Public code
00239 *
00240 **********************************************************************
00241 */
00242 /*********************************************************************
00243 *
00244 *       MainTask
00245 *
00246 * Function description
00247 *   Sets only the callback function for the desktop window. The rest will
00248 *   be done within the callback function.
00249 */
00250 void MainTask(void) {
00251   GUI_Init();
00252   //
00253   // Check if recommended memory for the sample is available
00254   //
00255   if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
00256     GUI_ErrorOut("Not enough memory available."); 
00257     return;
00258   }
00259   WM_SetCallback(WM_HBKWIN, _cbBkWin);
00260   while (1) {
00261     GUI_Delay(100);
00262   }
00263 }
00264 
00265 /*************************** End of file ****************************/
00266