Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
WIDGET_Menu.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_Menu.c 00041 Purpose : Shows how to work with menu widget 00042 Requirements: WindowManager - (x) 00043 MemoryDevices - (x) 00044 AntiAliasing - ( ) 00045 VNC-Server - ( ) 00046 PNG-Library - ( ) 00047 TrueTypeFonts - ( ) 00048 ---------------------------------------------------------------------- 00049 */ 00050 00051 #include <stdio.h> 00052 #include "GUI.h" 00053 #include "DIALOG.h" 00054 #include "MENU.h" 00055 #include "MESSAGEBOX.h" 00056 00057 /********************************************************************* 00058 * 00059 * Defines 00060 * 00061 ********************************************************************** 00062 */ 00063 #define ID_MENU (GUI_ID_USER + 0) 00064 #define ID_MENU_FILE_NEW (GUI_ID_USER + 1) 00065 #define ID_MENU_FILE_OPEN (GUI_ID_USER + 2) 00066 #define ID_MENU_FILE_CLOSE (GUI_ID_USER + 3) 00067 #define ID_MENU_FILE_EXIT (GUI_ID_USER + 4) 00068 #define ID_MENU_FILE_RECENT (GUI_ID_USER + 5) 00069 #define ID_MENU_RECENT_0 (GUI_ID_USER + 6) 00070 #define ID_MENU_RECENT_1 (GUI_ID_USER + 7) 00071 #define ID_MENU_RECENT_2 (GUI_ID_USER + 8) 00072 #define ID_MENU_RECENT_3 (GUI_ID_USER + 9) 00073 #define ID_MENU_EDIT_UNDO (GUI_ID_USER + 10) 00074 #define ID_MENU_EDIT_REDO (GUI_ID_USER + 11) 00075 #define ID_MENU_EDIT_COPY (GUI_ID_USER + 12) 00076 #define ID_MENU_EDIT_PASTE (GUI_ID_USER + 13) 00077 #define ID_MENU_EDIT_DELETE (GUI_ID_USER + 14) 00078 #define ID_MENU_HELP_ABOUT (GUI_ID_USER + 15) 00079 00080 // 00081 // Recommended memory to run the sample with adequate performance 00082 // 00083 #define RECOMMENDED_MEMORY (1024L * 10) 00084 00085 /********************************************************************* 00086 * 00087 * Static data 00088 * 00089 ********************************************************************** 00090 */ 00091 static WM_HWIN _hMenu; 00092 static WM_HWIN _hText; 00093 static WM_HWIN _hFrame; 00094 00095 static char * _paDescription[] = { 00096 "Creates a new file", 00097 "Opens an existing file", 00098 "Closes the file", 00099 "Quits the application", 00100 " ", 00101 "Opens file 1", 00102 "Opens file 2", 00103 "Opens file 3", 00104 "Opens file 4", 00105 "Undoes the last action", 00106 "Redoes the previously undone action", 00107 "Copies to clipboard", 00108 "Inserts contents of clipboard", 00109 "Deletes the selection", 00110 "Displays program information" 00111 }; 00112 00113 /********************************************************************* 00114 * 00115 * Static code 00116 * 00117 ********************************************************************** 00118 */ 00119 /********************************************************************* 00120 * 00121 * _AddMenuItem 00122 */ 00123 static void _AddMenuItem(MENU_Handle hMenu, MENU_Handle hSubmenu, const char* pText, U16 Id, U16 Flags) { 00124 MENU_ITEM_DATA Item; 00125 00126 Item.pText = pText; 00127 Item.hSubmenu = hSubmenu; 00128 Item.Flags = Flags; 00129 Item.Id = Id; 00130 MENU_AddItem(hMenu, &Item); 00131 } 00132 00133 /********************************************************************* 00134 * 00135 * _CreateMenu 00136 * 00137 * Function description 00138 * Creates the menu widget 00139 */ 00140 static WM_HWIN _CreateMenu(WM_HWIN hParent) { 00141 MENU_Handle hMenu; 00142 MENU_Handle hMenuFile; 00143 MENU_Handle hMenuEdit; 00144 MENU_Handle hMenuHelp; 00145 MENU_Handle hMenuRecent; 00146 00147 // 00148 // Create main menu 00149 // 00150 hMenu = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_HORIZONTAL, ID_MENU); 00151 // 00152 // Create sub menus 00153 // 00154 hMenuFile = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0); 00155 hMenuEdit = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0); 00156 hMenuHelp = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0); 00157 hMenuRecent = MENU_CreateEx(0, 0, 0, 0, WM_UNATTACHED, 0, MENU_CF_VERTICAL, 0); 00158 // 00159 // Add menu items to menu 'Recent' 00160 // 00161 _AddMenuItem(hMenuRecent, 0, "File 1", ID_MENU_RECENT_0, 0); 00162 _AddMenuItem(hMenuRecent, 0, "File 2", ID_MENU_RECENT_1, 0); 00163 _AddMenuItem(hMenuRecent, 0, "File 3", ID_MENU_RECENT_2, 0); 00164 _AddMenuItem(hMenuRecent, 0, "File 4", ID_MENU_RECENT_3, 0); 00165 // 00166 // Add menu items to menu 'File' 00167 // 00168 _AddMenuItem(hMenuFile, 0, "New", ID_MENU_FILE_NEW, 0); 00169 _AddMenuItem(hMenuFile, 0, "Open", ID_MENU_FILE_OPEN, 0); 00170 _AddMenuItem(hMenuFile, 0, "Close", ID_MENU_FILE_CLOSE, MENU_IF_DISABLED); 00171 _AddMenuItem(hMenuFile, 0, 0, 0, MENU_IF_SEPARATOR); 00172 _AddMenuItem(hMenuFile, hMenuRecent, "Files...", ID_MENU_FILE_RECENT, 0); 00173 _AddMenuItem(hMenuFile, 0, 0, 0, MENU_IF_SEPARATOR); 00174 _AddMenuItem(hMenuFile, 0, "Exit", ID_MENU_FILE_EXIT, 0); 00175 // 00176 // Add menu items to menu 'Edit' 00177 // 00178 _AddMenuItem(hMenuEdit, 0, "Undo", ID_MENU_EDIT_UNDO, 0); 00179 _AddMenuItem(hMenuEdit, 0, "Redo", ID_MENU_EDIT_REDO, 0); 00180 _AddMenuItem(hMenuEdit, 0, 0, 0, MENU_IF_SEPARATOR); 00181 _AddMenuItem(hMenuEdit, 0, "Copy", ID_MENU_EDIT_COPY, 0); 00182 _AddMenuItem(hMenuEdit, 0, "Paste", ID_MENU_EDIT_PASTE, 0); 00183 _AddMenuItem(hMenuEdit, 0, "Delete", ID_MENU_EDIT_DELETE, 0); 00184 // 00185 // Add menu items to menu 'Help' 00186 // 00187 _AddMenuItem(hMenuHelp, 0, "About", ID_MENU_HELP_ABOUT, 0); 00188 // 00189 // Add menu items to main menu 00190 // 00191 _AddMenuItem(hMenu, hMenuFile, "File", 0, 0); 00192 _AddMenuItem(hMenu, hMenuEdit, "Edit", 0, 0); 00193 _AddMenuItem(hMenu, hMenuHelp, "Help", 0, 0); 00194 // 00195 // Attach menu to parent window 00196 // 00197 FRAMEWIN_AddMenu(hParent, hMenu); 00198 return hMenu; 00199 } 00200 00201 /********************************************************************* 00202 * 00203 * _DrawGradientV 00204 * 00205 * Function description 00206 * Draws a vertical gradient 00207 */ 00208 static void _DrawGradientV(int x0, int y0, int x1, int y1, GUI_COLOR Color0, GUI_COLOR Color1) { 00209 int r0; 00210 int g0; 00211 int b0; 00212 int r1; 00213 int g1; 00214 int b1; 00215 int y; 00216 int ySize; 00217 int r; 00218 int g; 00219 U32 a; 00220 I32 b; 00221 int Diff; 00222 00223 ySize = y1 - y0 + 1; 00224 a = 00225 r0 = (Color0 >> 0) & 0x000000ff; 00226 g0 = (Color0 >> 8) & 0x000000ff; 00227 b0 = (Color0 >> 16) & 0x000000ff; 00228 r1 = (Color1 >> 0) & 0x000000ff; 00229 g1 = (Color1 >> 8) & 0x000000ff; 00230 b1 = (Color1 >> 16) & 0x000000ff; 00231 for (y = y0; y <= y1; y++) { 00232 GUI_COLOR Color; 00233 Diff = y - y0; 00234 r = r0 + (r1 - r0) * Diff / ySize; 00235 g = g0 + (g1 - g0) * Diff / ySize; 00236 b = b0 + (b1 - b0) * Diff / ySize; 00237 Color = r | (g << 8) | (b << 16); 00238 #if (GUI_USE_ARGB == 1) 00239 Color |= 0xFF000000; 00240 #endif 00241 GUI_SetColor(Color); 00242 GUI_DrawHLine(y, x0, x1); 00243 } 00244 } 00245 00246 /********************************************************************* 00247 * 00248 * _MessageBox 00249 * 00250 * Function description 00251 * Creates and executes a modal message box 00252 */ 00253 static void _MessageBox(const char * pText, const char * pCaption) { 00254 WM_HWIN hBox; 00255 00256 hBox = MESSAGEBOX_Create(pText, pCaption, GUI_MESSAGEBOX_CF_MODAL | GUI_MESSAGEBOX_CF_MOVEABLE); 00257 WM_SetStayOnTop(hBox, 1); 00258 WM_BringToTop(hBox); 00259 GUI_ExecCreatedDialog(hBox); 00260 WM_SetFocus(_hMenu); 00261 MENU_SetSel(_hMenu, -1); 00262 } 00263 00264 /********************************************************************* 00265 * 00266 * _cbClient 00267 * 00268 * Function description 00269 * Callback routine of client window which 'owns' the menu widget 00270 * and the status bar 00271 */ 00272 static void _cbClient(WM_MESSAGE * pMsg) { 00273 char acBuffer[50]; 00274 int Index; 00275 int xSize; 00276 int ySize; 00277 int xPos; 00278 int yPos; 00279 WM_HWIN hWin; 00280 WM_HWIN hClient; 00281 MENU_MSG_DATA * pData; 00282 MENU_ITEM_DATA Data; 00283 00284 hWin = pMsg->hWin; 00285 hClient = WM_GetClientWindow(hWin); 00286 xSize = WM_GetWindowSizeX(hClient); 00287 ySize = WM_GetWindowSizeY(hClient); 00288 switch (pMsg->MsgId) { 00289 case WM_SIZE: 00290 // 00291 // Adjusts text widget on changinmg the size 00292 // 00293 xPos = WM_GetWindowOrgX(hClient); 00294 yPos = WM_GetWindowOrgY(hClient); 00295 WM_SetWindowPos(_hText, xPos + 4, yPos + ySize - 10, xSize, 10); 00296 TEXT_SetText(_hText, "Ready"); 00297 break; 00298 case WM_PAINT: 00299 // 00300 // Draws the background of the client window 00301 // 00302 _DrawGradientV(0, 0, xSize - 1, ySize - 12 - 1, GUI_WHITE, GUI_LIGHTBLUE); 00303 GUI_SetColor(GUI_LIGHTGRAY); 00304 GUI_FillRect(0, ySize - 12, xSize - 1, ySize - 1); 00305 GUI_SetTextMode(GUI_TM_TRANS); 00306 GUI_SetColor(GUI_BLACK); 00307 GUI_SetFont(&GUI_Font24B_ASCII); 00308 GUI_DispStringHCenterAt( 00309 "MENU widget sample" 00310 , xSize / 2, 40); 00311 GUI_SetFont(&GUI_Font16B_ASCII); 00312 GUI_DispStringHCenterAt( 00313 "The sample shows how to use the MENU\n" 00314 "widget. Use the keyboard or the pointer\n" 00315 "input device for playing with the widget.\n" 00316 "On highlighting a menu item the status\n" 00317 "bar shows a small description. On selecting\n" 00318 "a menu item a message box will be shown." 00319 , xSize / 2, 70); 00320 break; 00321 case WM_MENU: 00322 pData = (MENU_MSG_DATA*)pMsg->Data.p; 00323 switch (pData->MsgType) { 00324 case MENU_ON_ITEMPRESSED: 00325 // 00326 // This message is send regardless of the item state is disabled or not 00327 // 00328 MENU_GetItem(pMsg->hWinSrc, pData->ItemId, &Data); 00329 if (Data.Flags & MENU_IF_DISABLED) { 00330 _MessageBox("The pressed item was disabled", "Message"); 00331 } 00332 break; 00333 case MENU_ON_ITEMACTIVATE: 00334 // 00335 // This message is send on highlighting a menu item 00336 // 00337 Index = pData->ItemId - ID_MENU_FILE_NEW; 00338 if (Index >= 0) { 00339 TEXT_SetText(_hText, _paDescription[pData->ItemId - ID_MENU_FILE_NEW]); 00340 } else { 00341 TEXT_SetText(_hText, "Ready"); 00342 } 00343 break; 00344 case MENU_ON_ITEMSELECT: 00345 // 00346 // This message is send only if an enabled item has been selected 00347 // 00348 sprintf(acBuffer, "ID of the selected\nitem is 0x%X", pData->ItemId); 00349 _MessageBox(acBuffer, "Message"); 00350 break; 00351 } 00352 } 00353 WM_DefaultProc(pMsg); 00354 } 00355 00356 /********************************************************************* 00357 * 00358 * Public code 00359 * 00360 ********************************************************************** 00361 */ 00362 /********************************************************************* 00363 * 00364 * MainTask 00365 */ 00366 void MainTask(void) { 00367 int xSize; 00368 int ySize; 00369 WM_HWIN hClient; 00370 00371 WM_SetCreateFlags(WM_CF_MEMDEV); 00372 GUI_Init(); 00373 // 00374 // Check if recommended memory for the sample is available 00375 // 00376 if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) { 00377 GUI_ErrorOut("Not enough memory available."); 00378 return; 00379 } 00380 WM_SetDesktopColor(GUI_BLACK); 00381 MENU_SetDefaultEffect(&WIDGET_Effect_3D1L); 00382 // 00383 // Create frame window 00384 // 00385 _hFrame = FRAMEWIN_CreateEx(10, 10, 300, 220, WM_HBKWIN, WM_CF_SHOW, FRAMEWIN_CF_MOVEABLE, 0, "Application with menu", _cbClient); 00386 FRAMEWIN_SetFont(_hFrame, &GUI_Font13_ASCII); 00387 FRAMEWIN_AddMaxButton(_hFrame, FRAMEWIN_BUTTON_RIGHT, 0); 00388 FRAMEWIN_AddMinButton(_hFrame, FRAMEWIN_BUTTON_RIGHT, 2); 00389 // 00390 // Create menu widget 00391 // 00392 _hMenu = _CreateMenu(_hFrame); 00393 // 00394 // Create text widget as simple status bar 00395 // 00396 hClient = WM_GetClientWindow(_hFrame); 00397 xSize = WM_GetWindowSizeX(hClient); 00398 ySize = WM_GetWindowSizeY(hClient); 00399 _hText = TEXT_CreateEx(4, ySize - 10, xSize, 10, hClient, WM_CF_SHOW, 0, GUI_ID_TEXT0, "Ready"); 00400 TEXT_SetFont(_hText, &GUI_Font8_ASCII); 00401 // 00402 // Set keyboard focus to menu widget 00403 // 00404 WM_SetFocus(_hMenu); 00405 MENU_SetSel(_hMenu, -1); 00406 while (1) { 00407 GUI_Delay(100); 00408 } 00409 } 00410 00411 /*************************** End of file ****************************/
Generated on Thu Jul 14 2022 12:58:43 by
