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

« Back to documentation index

Show/hide line numbers WIDGET_EditWinmode.c Source File

WIDGET_EditWinmode.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_EditWinmode.c
00041 Purpose     : Demonstrates how to use a edit widget in 'windows' mode
00042 Requirements: WindowManager - (x)
00043               MemoryDevices - ( )
00044               AntiAliasing  - ( )
00045               VNC-Server    - ( )
00046               PNG-Library   - ( )
00047               TrueTypeFonts - ( )
00048 ----------------------------------------------------------------------
00049 */
00050 #include <stddef.h>
00051 #include "GUI.h"
00052 #include "WM.h"
00053 #include "DIALOG.h"
00054 
00055 /*********************************************************************
00056 *
00057 *       Defines
00058 *
00059 **********************************************************************
00060 */
00061 //
00062 // Recommended memory to run the sample with adequate performance
00063 //
00064 #define RECOMMENDED_MEMORY (1024L * 10)
00065 
00066 /*********************************************************************
00067 *
00068 *       Static data
00069 *
00070 **********************************************************************
00071 */
00072 static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
00073   { FRAMEWIN_CreateIndirect, "Edit winmode", 0,         90,  90, 140, 130, FRAMEWIN_CF_MOVEABLE},
00074   { EDIT_CreateIndirect,     NULL,     GUI_ID_EDIT0,    10,  10, 110,  20, 0, 15},
00075   { EDIT_CreateIndirect,     NULL,     GUI_ID_EDIT1,    10,  40, 110,  20, 0, 15},
00076   { BUTTON_CreateIndirect,   "Ok",     GUI_ID_OK,       10,  80,  50,  20 },
00077   { BUTTON_CreateIndirect,   "Cancel", GUI_ID_CANCEL,   70,  80,  50,  20 },
00078 };
00079 
00080 static WM_CALLBACK * _pEditCallback;
00081 
00082 static char * _apExplain[] = {
00083   "This sample shows how to use edit widgets with a",
00084   "user defined callback function and how to set a",
00085   "user defined AddKey function. It selects the",
00086   "contents of the edit field on receiving the focus",
00087   "and overwrites the contents if a key other than",
00088   "a cursor key is pressed.",
00089 };
00090 
00091 /*********************************************************************
00092 *
00093 *       Static code
00094 *
00095 **********************************************************************
00096 */
00097 /*********************************************************************
00098 *
00099 *       _cbEditAddKey
00100 *
00101 * Function description
00102 *   This function is called by the edit widget if a character should be added to the contents of the widget.
00103 */
00104 static void _cbEditAddKey(EDIT_Handle hObj, int Key) {
00105   char acBuffer[2] = {0};
00106 
00107   switch (Key) {
00108   case GUI_KEY_LEFT:
00109     EDIT_SetpfAddKeyEx(hObj, 0);                            // Clear user defined AddKey function
00110     EDIT_SetCursorAtChar(hObj, EDIT_GetNumChars(hObj) - 1); // Set cursor if GUI_KEY_LEFT has been pressed
00111     break;
00112   case GUI_KEY_RIGHT:
00113     EDIT_SetpfAddKeyEx(hObj, 0);                            // Clear user defined AddKey function
00114     EDIT_SetCursorAtChar(hObj, 1);                          // Set cursor if GUI_KEY_RIGHT has been pressed
00115     break;
00116   case GUI_KEY_UP:                                          // Do not react on GUI_KEY_UP and GUI_KEY_DOWN
00117   case GUI_KEY_DOWN:
00118     break;
00119   default:
00120     if (Key >= 0x20) {
00121       acBuffer[0] = (U8)Key;
00122       EDIT_SetpfAddKeyEx(hObj, 0);                          // Clear user defined AddKey function
00123       EDIT_SetText(hObj, acBuffer);                         // Overwrite contents of edit widget with pressed key
00124     }
00125   }
00126 }
00127 
00128 /*********************************************************************
00129 *
00130 *       _cbEdit
00131 *
00132 * Function description
00133 *   New callback function of edit widgets which should work in 'windows' mode.
00134 */
00135 static void _cbEdit(WM_MESSAGE * pMsg) {
00136   switch (pMsg->MsgId) {
00137   case WM_PID_STATE_CHANGED:
00138     if (((const WM_PID_STATE_CHANGED_INFO*)pMsg->Data.p)->State) {
00139       return;            // Do not call edit callback
00140     }
00141     break;
00142   case WM_TOUCH:
00143     if (pMsg->Data.p) {  // Something happened in our area (pressed or released)
00144       const GUI_PID_STATE* pState;
00145       pState = (const GUI_PID_STATE*)pMsg->Data.p;
00146       if (pState->Pressed) {
00147         if (WM_GetFocussedWindow() != pMsg->hWin) {
00148           WM_SetFocus(pMsg->hWin);
00149           return;        // Do not call edit callback
00150         }
00151       }
00152     }
00153     break;
00154   case WM_SET_FOCUS:
00155     if (pMsg->Data.v == 1) {
00156       EDIT_SetpfAddKeyEx(pMsg->hWin, _cbEditAddKey); // Set function pointer for a user defined AddKey function
00157       EDIT_SetSel(pMsg->hWin, 0, -1);                // Select the whole contents of the edit field
00158     }
00159   }
00160   if (_pEditCallback) {
00161     _pEditCallback(pMsg);
00162   }
00163 }
00164 
00165 /*********************************************************************
00166 *
00167 *       _cbDialog
00168 */
00169 static void _cbDialog(WM_MESSAGE * pMsg) {
00170   int     i;
00171   int     NCode;
00172   int     Id;
00173   WM_HWIN hDlg;
00174   WM_HWIN hItem;
00175 
00176   hDlg = pMsg->hWin;
00177   switch (pMsg->MsgId) {
00178   case WM_INIT_DIALOG:
00179     FRAMEWIN_SetFont(pMsg->hWin, &GUI_Font13_ASCII);
00180     FRAMEWIN_SetTextAlign(pMsg->hWin, GUI_TA_HCENTER);
00181     for (i = 0; i < 2; i++) {
00182       hItem = WM_GetDialogItem(hDlg, GUI_ID_EDIT0 + i);  // Get the handle of the edit widget
00183       if (!_pEditCallback) {
00184         _pEditCallback = WM_SetCallback(hItem, _cbEdit); // Overwrite callback function and remember original function
00185       } else {
00186         WM_SetCallback(hItem, _cbEdit);                  // Overwrite callback function
00187       }
00188       EDIT_SetText(hItem, "Hello world!");               // Fill widget with text
00189       EDIT_SetpfAddKeyEx(hItem, _cbEditAddKey);          // Set function pointer for a user defined AddKey function
00190       EDIT_SetSel(hItem, 0, -1);                         // Select the whole contents of the edit field
00191     }
00192     break;
00193   case WM_NOTIFY_PARENT:
00194     Id    = WM_GetId(pMsg->hWinSrc);      // Id of widget
00195     NCode = pMsg->Data.v;                 // Notification code
00196     switch (NCode) {
00197       case WM_NOTIFICATION_RELEASED:      // React only if released
00198         if (Id == GUI_ID_OK) {            // OK Button
00199           GUI_EndDialog(hDlg, 0);
00200         }
00201         if (Id == GUI_ID_CANCEL) {        // Cancel Button
00202           GUI_EndDialog(hDlg, 1);
00203         }
00204         break;
00205     }
00206     break;
00207   default:
00208     WM_DefaultProc(pMsg);
00209   }
00210 }
00211 
00212 /*********************************************************************
00213 *
00214 *       _cbDesktop
00215 *
00216 * Function description
00217 *   This routine handles the drawing of the desktop window.
00218 */
00219 static void _cbDesktop(WM_MESSAGE * pMsg) {
00220   unsigned i;
00221 
00222   switch (pMsg->MsgId) {
00223   case WM_PAINT:
00224     GUI_SetBkColor(GUI_RED);
00225     GUI_Clear();
00226     GUI_SetFont(&GUI_Font24_ASCII);
00227     GUI_DispStringHCenterAt("WIDGET_EditWinmode", 160, 5);
00228     GUI_DispNextLine();
00229     GUI_SetFont(GUI_DEFAULT_FONT);
00230     GUI_DispNextLine();
00231     for (i = 0; i < GUI_COUNTOF(_apExplain); i++) {
00232       GUI_DispStringHCenterAt(_apExplain[i], 160, GUI_GetDispPosY());
00233       GUI_DispNextLine();
00234     }
00235     break;
00236   }
00237 }
00238 
00239 /*********************************************************************
00240 *
00241 *       Public code
00242 *
00243 **********************************************************************
00244 */
00245 /*********************************************************************
00246 *
00247 *       MainTask
00248 */
00249 void MainTask(void) {
00250 
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, _cbDesktop);
00260   while(1) {
00261     GUI_ExecDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), &_cbDialog, 0, 0, 0);
00262     GUI_Delay(1000);
00263   }
00264 }
00265 
00266 /*************************** End of file ****************************/
00267