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

« Back to documentation index

Show/hide line numbers WIDGET_Multiedit.c Source File

WIDGET_Multiedit.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_Multiedit.c
00041 Purpose     : Demonstrates the use of the MULTIEDIT widget.
00042 Requirements: WindowManager - (x)
00043               MemoryDevices - (x)
00044               AntiAliasing  - ( )
00045               VNC-Server    - ( )
00046               PNG-Library   - ( )
00047               TrueTypeFonts - ( ) 
00048 ----------------------------------------------------------------------
00049 */
00050 #include "GUI.h"
00051 #include "MULTIEDIT.h"
00052 #include "FRAMEWIN.h"
00053 #include "BUTTON.h"
00054 
00055 #include <string.h>
00056 
00057 /*********************************************************************
00058 *
00059 *       Defines
00060 *
00061 **********************************************************************
00062 */
00063 #define SPEED         1000
00064 
00065 #define MIN_SIZE_X    200
00066 #define MIN_SIZE_Y     80
00067 
00068 #define RESIZE_X      (1<<0)
00069 #define RESIZE_Y      (1<<1)
00070 #define REPOS_X       (1<<2)
00071 #define REPOS_Y       (1<<3)
00072 
00073 #define ID_NONEWRAP   (GUI_ID_USER + 0)
00074 #define ID_WORDWRAP   (GUI_ID_USER + 1)
00075 #define ID_CHARWRAP   (GUI_ID_USER + 2)
00076 #define ID_OVERWRITE  (GUI_ID_USER + 3)
00077 #define ID_READONLY   (GUI_ID_USER + 4)
00078 #define ID_PASSWORD   (GUI_ID_USER + 5)
00079 
00080 //
00081 // Recommended memory to run the sample with adequate performance
00082 //
00083 #define RECOMMENDED_MEMORY (1024L * 20)
00084 
00085 /*********************************************************************
00086 *
00087 *       static data
00088 *
00089 **********************************************************************
00090 */
00091 static WM_CALLBACK* _pcbFrameWin;
00092 static WM_CALLBACK* _pcbFrameWinClient;
00093 static WM_CALLBACK* _pcbMultiEdit;
00094 static WM_HWIN      _hMEdit, _hFrame, _hClient;
00095 static WM_HWIN      _hWrapButton;
00096 static int          _CaptureX;
00097 static int          _CaptureY;
00098 static int          _HasCaptured;
00099 static int          _ReadOnly;
00100 static int          _Overwrite;
00101 static int          _Password;
00102 static char         _acInfoText[100] = {0};
00103 
00104 /*********************************************************************
00105 *
00106 *       static code, helpers
00107 *
00108 **********************************************************************
00109 */
00110 /*********************************************************************
00111 *
00112 *       _ChangeInfoText
00113 */
00114 static int _ChangeInfoText(const char* pString, int Delay) {
00115   if (_hFrame) {
00116     GUI_Delay((int)(Delay * 0.75));
00117     strcpy(_acInfoText, pString);
00118     WM_InvalidateWindow(WM_HBKWIN);
00119   }
00120   if (_hFrame) {
00121     GUI_Delay(Delay);
00122     return 0;
00123   } 
00124   return 1;
00125 }
00126 
00127 /*********************************************************************
00128 *
00129 *       _SetCapture
00130 */
00131 static void _SetCapture(FRAMEWIN_Handle hWin, int x, int y, int Mode) {
00132   if ((_HasCaptured & REPOS_X) == 0) {
00133     _CaptureX = x;
00134   }
00135   if ((_HasCaptured & REPOS_Y) == 0) {
00136     _CaptureY = y;
00137   }
00138   if (!_HasCaptured) {
00139     WM_SetCapture(hWin, 1);
00140     _HasCaptured = Mode;
00141   }
00142 }
00143 
00144 /*********************************************************************
00145 *
00146 *       _ChangeWindowPosSize
00147 */
00148 static void _ChangeWindowPosSize(FRAMEWIN_Handle hWin, int* px, int* py) {
00149   int dx;
00150   int dy;
00151   GUI_RECT Rect;
00152 
00153   dx = 0;
00154   dy = 0;
00155   WM_GetClientRectEx(hWin, &Rect);
00156   //
00157   // Calculate new size of window
00158   //
00159   if (_HasCaptured & RESIZE_X) {
00160     dx = (_HasCaptured & REPOS_X) ? (_CaptureX - *px) : (*px - _CaptureX);
00161   }
00162   if (_HasCaptured & RESIZE_Y) {
00163     dy = (_HasCaptured & REPOS_Y) ? (_CaptureY - *py) : (*py - _CaptureY);
00164   }
00165   //
00166   // Check the minimal size of window
00167   //
00168   if ((Rect.x1 + dx + 1) < MIN_SIZE_X) {
00169     dx = MIN_SIZE_X - (Rect.x1 + 1);
00170     *px = _CaptureX;
00171   }
00172   if ((Rect.y1 + dy + 1) < MIN_SIZE_Y) {
00173     dy = MIN_SIZE_Y - (Rect.y1 + 1);
00174     *py = _CaptureY;
00175   }
00176   //
00177   // Set new window position
00178   //
00179   if (_HasCaptured & REPOS_X) {
00180     WM_MoveWindow(hWin, -dx, 0);
00181   }
00182   if (_HasCaptured & REPOS_Y) {
00183     WM_MoveWindow(hWin, 0, -dy);
00184   }
00185   //
00186   // Set new window size
00187   //
00188   WM_ResizeWindow(hWin, dx, dy);
00189 }
00190 
00191 /*********************************************************************
00192 *
00193 *       _OnTouch
00194 */
00195 static int _OnTouch(FRAMEWIN_Handle hWin, WM_MESSAGE* pMsg) {
00196   if (pMsg->Data.p) {  // Something happened in our area (pressed or released)
00197     GUI_PID_STATE* pState;
00198     pState = (GUI_PID_STATE*)pMsg->Data.p;
00199     if (pState->Pressed) {
00200       int x, y;
00201       x = pState->x;
00202       y = pState->y;
00203       if (WM_HasCaptured(hWin) == 0) {
00204         GUI_RECT Rect;
00205         int Mode = 0;
00206         int BorderSize = 4;
00207         WM_GetClientRectEx(hWin, &Rect);
00208         if (x > (Rect.x1 - BorderSize)) {
00209           Mode |= RESIZE_X;
00210         } else if (x < BorderSize) {
00211           Mode |= RESIZE_X | REPOS_X;
00212         }
00213         if (y > (Rect.y1 - BorderSize)) {
00214           Mode |= RESIZE_Y;
00215         } else if (y < BorderSize) {
00216           Mode |= RESIZE_Y | REPOS_Y;
00217         }
00218         if (Mode) {
00219           WM_SetFocus(hWin);
00220           WM_BringToTop(hWin);
00221           _SetCapture(hWin, x, y, Mode);
00222           return 1;
00223         }
00224       } else if (_HasCaptured) {
00225         _ChangeWindowPosSize(hWin, &x, &y);
00226         _SetCapture(hWin, x, y, 0);
00227         return 1;
00228       }
00229     }
00230   }
00231   _HasCaptured = 0;
00232   return 0;
00233 }
00234 
00235 /*********************************************************************
00236 *
00237 *       _CreateLButton
00238 */
00239 static WM_HWIN _CreateLButton(const char* pText, int x, int w, int h, WM_HWIN hParent, int Id) {
00240   WM_HWIN hButton;
00241   GUI_RECT Rect;
00242 
00243   WM_GetClientRectEx(hParent, &Rect);
00244   hButton = BUTTON_CreateEx(x, Rect.y1 - h + 1, w, h, hParent, WM_CF_SHOW | WM_CF_ANCHOR_BOTTOM, 0, Id);
00245   BUTTON_SetText(hButton, pText);
00246   BUTTON_SetFont(hButton, &GUI_Font8_ASCII);
00247   return hButton;
00248 }
00249 
00250 /*********************************************************************
00251 *
00252 *       _CreateRButton
00253 */
00254 static WM_HWIN _CreateRButton(const char* pText, int x, int w, int h, WM_HWIN hParent, int Id) {
00255   WM_HWIN hButton;
00256   GUI_RECT Rect;
00257 
00258   WM_GetClientRectEx(hParent, &Rect);
00259   hButton = BUTTON_CreateEx(Rect.x1 - x - w + 1, Rect.y1 - h + 1, w, h, hParent,
00260                             WM_CF_SHOW | WM_CF_ANCHOR_BOTTOM | WM_CF_ANCHOR_RIGHT, 0, Id);
00261   BUTTON_SetText(hButton, pText);
00262   BUTTON_SetFont(hButton, &GUI_Font8_ASCII);
00263   return hButton;
00264 }
00265 
00266 /*********************************************************************
00267 *
00268 *       _SetButtonState
00269 */
00270 static void _SetButtonState(WM_HWIN hButton, int State) {
00271   if (State) {
00272     BUTTON_SetTextColor(hButton, 0, GUI_MAKE_COLOR(0x0040F0));
00273     BUTTON_SetTextColor(hButton, 1, GUI_MAKE_COLOR(0x0040F0));
00274   } else {
00275     BUTTON_SetTextColor(hButton, 0, GUI_BLACK);
00276     BUTTON_SetTextColor(hButton, 1, GUI_BLACK);
00277   }
00278 }
00279 
00280 /*********************************************************************
00281 *
00282 *       static code, callbacks
00283 *
00284 **********************************************************************
00285 */
00286 /*********************************************************************
00287 *
00288 *       _cbBkWin
00289 */
00290 static void _cbBkWin(WM_MESSAGE* pMsg) {
00291   switch(pMsg->MsgId) {
00292   case WM_PAINT:
00293     GUI_SetBkColor(GUI_MAKE_COLOR(0x00A000));
00294     GUI_SetColor(GUI_WHITE);
00295     GUI_Clear();
00296     GUI_SetFont(&GUI_Font24_ASCII);
00297     GUI_DispStringHCenterAt("MULTIEDIT - Sample", 160, 5);
00298     GUI_SetFont(&GUI_Font8x16);
00299     GUI_DispStringAt(_acInfoText, 0, 30);
00300     break;
00301   default:
00302     WM_DefaultProc(pMsg);
00303   }
00304 }
00305 
00306 /*********************************************************************
00307 *
00308 *       _cbMultiEdit
00309 */
00310 static void _cbMultiEdit(WM_MESSAGE* pMsg) {
00311   if (pMsg->MsgId == WM_KEY) {
00312     if (((WM_KEY_INFO*)(pMsg->Data.p))->PressedCnt > 0) {
00313       int Key = ((WM_KEY_INFO*)(pMsg->Data.p))->Key;
00314       if (Key == GUI_KEY_INSERT) {
00315         WM_HWIN hWin;
00316         hWin = WM_GetDialogItem(WM_GetParent(pMsg->hWin), ID_OVERWRITE);
00317         _Overwrite ^= 1;
00318         _SetButtonState(hWin, _Overwrite);
00319       }
00320     }
00321   }
00322   if (_pcbMultiEdit) {
00323     (*_pcbMultiEdit)(pMsg);
00324   }
00325 }
00326 
00327 /*********************************************************************
00328 *
00329 *       _cbFrameWin
00330 */
00331 static void _cbFrameWin(WM_MESSAGE* pMsg) {
00332   switch(pMsg->MsgId) {
00333   case WM_TOUCH:
00334     if (FRAMEWIN_IsMinimized(pMsg->hWin) == 0) {
00335       if (_OnTouch(pMsg->hWin, pMsg)) {
00336         return;
00337       }
00338     }
00339     break;
00340   case WM_DELETE:
00341     _hFrame      = 0;
00342     _hClient     = 0;
00343     _hMEdit      = 0;
00344     _hWrapButton = 0;
00345     break;
00346   }
00347   if (_pcbFrameWin) {
00348     (*_pcbFrameWin)(pMsg);
00349   }
00350 }
00351 
00352 /*********************************************************************
00353 *
00354 *       _cbFrameWinClient
00355 */
00356 static void _cbFrameWinClient(WM_MESSAGE* pMsg) {
00357   switch(pMsg->MsgId) {
00358   case WM_NOTIFY_PARENT:
00359     if (pMsg->Data.v == WM_NOTIFICATION_RELEASED) {
00360       int Id = WM_GetId(pMsg->hWinSrc);
00361       if ((Id >= ID_NONEWRAP) && (Id <= ID_CHARWRAP)) {
00362         _SetButtonState(_hWrapButton, 0);
00363         if (Id == ID_NONEWRAP) {
00364           MULTIEDIT_SetWrapNone(_hMEdit);
00365         } else if (Id == ID_WORDWRAP) {
00366           MULTIEDIT_SetWrapWord(_hMEdit);
00367         } else if (Id == ID_CHARWRAP) {
00368           MULTIEDIT_SetWrapChar(_hMEdit);
00369         }
00370         _hWrapButton = pMsg->hWinSrc;
00371         _SetButtonState(_hWrapButton, 1);
00372       } else if (Id == ID_OVERWRITE) {
00373         _Overwrite ^= 1;
00374         MULTIEDIT_SetInsertMode(_hMEdit, 1 - _Overwrite);
00375         _SetButtonState(pMsg->hWinSrc, _Overwrite);
00376       } else if (Id == ID_READONLY) {
00377         _ReadOnly ^= 1;
00378         MULTIEDIT_SetReadOnly(_hMEdit, _ReadOnly);
00379         _SetButtonState(pMsg->hWinSrc, _ReadOnly);
00380       } else if (Id == ID_PASSWORD) {
00381         _Password ^= 1;
00382         MULTIEDIT_SetPasswordMode(_hMEdit, _Password);
00383         _SetButtonState(pMsg->hWinSrc, _Password);
00384       }
00385     }
00386     return;
00387   }
00388   if (_pcbFrameWinClient) {
00389     (*_pcbFrameWinClient)(pMsg);
00390   }
00391 }
00392 
00393 /*********************************************************************
00394 *
00395 *       _DemoMultiedit
00396 */
00397 static void _DemoMultiedit(void) {
00398   GUI_RECT Rect;
00399   int WinFlags;
00400 
00401   _hFrame    = 1;
00402   _Overwrite = 0;
00403   _ReadOnly  = 0;
00404   _Password  = 0;
00405   /* Create framewin */
00406   _ChangeInfoText("Create framewin", SPEED);
00407   _hFrame = FRAMEWIN_CreateEx(50, 80, 220, 120, WM_HBKWIN, WM_CF_SHOW, 0, 0, "Notepad", 0);
00408   _hClient = WM_GetClientWindow(_hFrame);
00409   _pcbFrameWin       = WM_SetCallback(_hFrame,  _cbFrameWin);
00410   _pcbFrameWinClient = WM_SetCallback(_hClient, _cbFrameWinClient);
00411   /* Set framewin properties */
00412   FRAMEWIN_SetMoveable(_hFrame, 1);
00413   FRAMEWIN_SetActive(_hFrame, 1);
00414   FRAMEWIN_SetTextAlign(_hFrame, GUI_TA_HCENTER | GUI_TA_VCENTER);
00415   FRAMEWIN_SetFont(_hFrame, &GUI_Font8x12_ASCII);
00416   FRAMEWIN_SetTitleHeight(_hFrame, 16);
00417   /* Add framewin buttons */
00418   if (_ChangeInfoText("Add framewin buttons", SPEED)) return;
00419   FRAMEWIN_AddCloseButton(_hFrame, FRAMEWIN_BUTTON_LEFT,  0);
00420   FRAMEWIN_AddMaxButton(_hFrame, FRAMEWIN_BUTTON_RIGHT, 0);
00421   FRAMEWIN_AddMinButton(_hFrame, FRAMEWIN_BUTTON_RIGHT, 1);
00422   WM_InvalidateWindow(_hFrame);
00423   /* Create buttons */
00424   if (_ChangeInfoText("Add option buttons", SPEED)) return;
00425   _hWrapButton = _CreateLButton("None",   0, 36, 16, _hClient, ID_NONEWRAP);
00426   _CreateLButton("Word",  37, 36, 16, _hClient, ID_WORDWRAP);
00427   _CreateLButton("Char",  74, 36, 16, _hClient, ID_CHARWRAP);
00428   _CreateRButton("PSW",   61, 30, 16, _hClient, ID_PASSWORD);
00429   _CreateRButton("OVR",   31, 30, 16, _hClient, ID_OVERWRITE);
00430   _CreateRButton("R/O",    0, 30, 16, _hClient, ID_READONLY);
00431   _SetButtonState(_hWrapButton, 1);
00432   /* Create multiedit */
00433   if (_ChangeInfoText("using\nMULTIEDIT_CreateEx", SPEED)) return;
00434   WinFlags = WM_CF_SHOW | WM_CF_ANCHOR_RIGHT | WM_CF_ANCHOR_LEFT | WM_CF_ANCHOR_TOP | WM_CF_ANCHOR_BOTTOM;
00435   WM_GetClientRectEx(_hClient, &Rect);
00436   _hMEdit = MULTIEDIT_CreateEx(0, 0, 0, Rect.y1 - 16 + 1, _hClient, WinFlags, MULTIEDIT_CF_INSERT, 0, 0, "");
00437   _pcbMultiEdit = WM_SetCallback(_hMEdit,  _cbMultiEdit);
00438   MULTIEDIT_SetAutoScrollH(_hMEdit, 1);
00439   MULTIEDIT_SetAutoScrollV(_hMEdit, 1);
00440   WM_SetFocus(_hMEdit);
00441   if (_ChangeInfoText("using\nMULTIEDIT_SetText", SPEED)) return;
00442   MULTIEDIT_SetText(_hMEdit, "This sample demonstrates the use of a multiedit widget!");
00443   if (_ChangeInfoText("using\nMULTIEDIT_SetFont", SPEED)) return;
00444   MULTIEDIT_SetFont(_hMEdit, &GUI_Font16_1);
00445   if (_ChangeInfoText("using\nMULTIEDIT_SetTextColor", SPEED)) return;
00446   MULTIEDIT_SetTextColor(_hMEdit, 0, GUI_MAKE_COLOR(0xE00000));
00447   if (_ChangeInfoText("using\nMULTIEDIT_SetBkColor", SPEED)) return;
00448   MULTIEDIT_SetBkColor(_hMEdit, 0, GUI_MAKE_COLOR(0xD0FFFF));
00449   if (_ChangeInfoText("using\nMULTIEDIT_SetWrapWord", SPEED)) return;
00450   MULTIEDIT_SetWrapWord(_hMEdit);
00451   _SetButtonState(_hWrapButton, 0);
00452   _hWrapButton = WM_GetDialogItem(_hClient, ID_WORDWRAP);
00453   _SetButtonState(_hWrapButton, 1);
00454   if (_ChangeInfoText("using\nMULTIEDIT_SetHBorder", SPEED)) return;
00455   MULTIEDIT_SetHBorder(_hMEdit, 3);
00456   if (_ChangeInfoText("using\nMULTIEDIT_SetPrompt", SPEED)) return;
00457   MULTIEDIT_SetPrompt(_hMEdit, "Type: ");
00458   if (_ChangeInfoText("Play with multiedit...", SPEED)) return;
00459   while (_hFrame) {
00460     GUI_Delay(100);
00461   }
00462 }
00463 
00464 /*********************************************************************
00465 *
00466 *       public code
00467 *
00468 **********************************************************************
00469 */
00470 /*********************************************************************
00471 *
00472 *       MainTask
00473 */
00474 void MainTask(void) {
00475   GUI_Init();
00476   //
00477   // Check if recommended memory for the sample is available
00478   //
00479   if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
00480     GUI_ErrorOut("Not enough memory available."); 
00481     return;
00482   }
00483   #if GUI_SUPPORT_MEMDEV
00484     WM_SetCreateFlags(WM_CF_MEMDEV);
00485   #endif
00486   GUI_CURSOR_Show();
00487   WM_SetCallback(WM_HBKWIN, _cbBkWin);
00488   while(1) {
00489     _DemoMultiedit();
00490     *_acInfoText = 0;
00491     WM_InvalidateWindow(WM_HBKWIN);
00492     GUI_Delay(SPEED);
00493   }
00494 }
00495 
00496 /*************************** End of file ****************************/