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

« Back to documentation index

Show/hide line numbers WIDGET_Treeview.c Source File

WIDGET_Treeview.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_Treeview.c
00041 Purpose     : Demonstrates using the TREEVIEW widget
00042 Requirements: WindowManager - (x)
00043               MemoryDevices - (x)
00044               AntiAliasing  - ( )
00045               VNC-Server    - ( )
00046               PNG-Library   - ( )
00047               TrueTypeFonts - ( )
00048 ----------------------------------------------------------------------
00049 */
00050 
00051 #include "DIALOG.h"
00052 
00053 /*********************************************************************
00054 *
00055 *       Defines
00056 *
00057 **********************************************************************
00058 */
00059 #define NUM_CHILD_NODES 4
00060 #define NUM_CHILD_ITEMS 6
00061 #define TREEVIEW_DEPTH  7
00062 
00063 //
00064 // Recommended memory to run the sample with adequate performance
00065 //
00066 #define RECOMMENDED_MEMORY (1024L * 2700)
00067 
00068 /*********************************************************************
00069 *
00070 *       Static data
00071 *
00072 **********************************************************************
00073 */
00074 static int _NumNodes;
00075 static int _NumLeaves;
00076 
00077 /*********************************************************************
00078 *
00079 *       Static code
00080 *
00081 **********************************************************************
00082 */
00083 /*********************************************************************
00084 *
00085 *       _MakeNumberString
00086 */
00087 static void _MakeNumberString(char * p, U32 Number) {
00088   int v;
00089   int NumDecs;
00090   
00091   NumDecs = 0;
00092   *p      = '0';
00093   v       = Number;
00094   while (v) {
00095     NumDecs++;
00096     v /= 10;
00097   }
00098   p += NumDecs - 1;
00099   while (NumDecs--) {
00100     *p = '0' + Number % 10;
00101     Number /= 10;
00102     p--;
00103   }
00104 }
00105 
00106 /*********************************************************************
00107 *
00108 *       _GetLen
00109 */
00110 static int _GetLen(char * p) {
00111   int Len;
00112   
00113   for (Len = 0; *p++; Len++);
00114   return Len;
00115 }
00116 
00117 /*********************************************************************
00118 *
00119 *       _FillNode
00120 *
00121 * Function description
00122 *   Recursive filling of node
00123 *
00124 * Parameters:
00125 *   hTree     - obvious
00126 *   NumNodes  - Number of child nodes to be created at each node
00127 *   NumLeaves - Number of leaves to be created at each node
00128 *   MaxDepth  - Maximum depth (1.1.1.1.1.....)
00129 *   CurDepth  - Current depth
00130 *   acBuffer  - String to be used for TREEVIEW items
00131 *   p         - Pointer into to string to be used for numbering
00132 *
00133 * Return value:
00134 *  0 on success, 1 on error
00135 */
00136 static int _FillNode(WM_HWIN hTree, TREEVIEW_ITEM_Handle hNode, int NumNodes, int NumLeaves, int MaxDepth, int CurDepth, char * acBuffer, char * p) {
00137   TREEVIEW_ITEM_Handle hItem;
00138   int                  i;
00139   int                  Position;
00140 
00141   hItem    = 0;
00142   *(p + 1) = 0;
00143   *p       = '0' - 1;
00144   if (--CurDepth) {
00145     //
00146     // Create nodes
00147     //
00148     for (i = 0; i < NumNodes; i++) {
00149       (*p)++;
00150       Position = hItem ? TREEVIEW_INSERT_BELOW : TREEVIEW_INSERT_FIRST_CHILD;
00151       hItem    = TREEVIEW_ITEM_Create(1, acBuffer, 0);
00152       if (hItem == 0) {
00153         return 1; // Error
00154       }
00155       _NumNodes++;
00156       TREEVIEW_AttachItem(hTree, hItem, hNode, Position);
00157       hNode    = hItem;
00158       *(p + 1) = '.';
00159       p += 2;
00160       //
00161       // Recursive call of 'this' function for each node
00162       //
00163       _FillNode(hTree, hNode, NumNodes, NumLeaves, MaxDepth, CurDepth, acBuffer, p);
00164       p -= 2;
00165       *(p + 1) = 0;
00166     }
00167   }
00168   //
00169   // Create leaves
00170   //
00171   for (i = 0; i < NumLeaves; i++) {
00172     (*p)++;
00173     Position = hItem ? TREEVIEW_INSERT_BELOW : TREEVIEW_INSERT_FIRST_CHILD;
00174     hItem = TREEVIEW_ITEM_Create(0, acBuffer, 0);
00175     if (hItem == 0) {
00176       return 1; // Error
00177     }
00178     _NumLeaves++;
00179     TREEVIEW_AttachItem(hTree, hItem, hNode, Position);
00180     hNode = hItem;
00181   }
00182   return 0;
00183 }
00184 
00185 /*********************************************************************
00186 *
00187 *       _MakeNumberText
00188 *
00189 * Purpose:
00190 *   Create text widgets for showing the result
00191 */
00192 static void _MakeNumberText(WM_HWIN hParent, int xPos, int * pyPos, int xSize, int ySize, char * pText, U32 Value) {
00193   WM_HWIN hText;
00194   int     Len;
00195 
00196   Len = _GetLen(pText);
00197   _MakeNumberString(pText + Len, Value);
00198   hText = TEXT_CreateEx(xPos, *pyPos, xSize, ySize, hParent, WM_CF_SHOW, 0, GUI_ID_TEXT0, pText);
00199   *pyPos += WM_GetWindowSizeY(hText);
00200 }
00201 
00202 /*********************************************************************
00203 *
00204 *       Public code
00205 *
00206 **********************************************************************
00207 */
00208 /*********************************************************************
00209 *
00210 *       MainTask
00211 */
00212 void MainTask(void) {
00213   TREEVIEW_ITEM_Handle hNode;
00214   WM_HWIN              hTree;
00215   int                  xSize;
00216   int                  ySize;
00217   int                  yPos;
00218   int                  r;
00219   int                  TimeStart;
00220   int                  TimeUsed;
00221   int                  ySizeText;
00222   U32                  BytesFree;
00223   U32                  BytesUsed;
00224   char                 acBuffer[(TREEVIEW_DEPTH << 1) + 1];
00225   char                 acNumNodes[30]  = "Nodes:  ";
00226   char                 acNumLeaves[30] = "Leaves: ";
00227   char                 acNumTotal[30]  = "Total:  ";
00228   char                 acTimeUsed[30]  = "Time:   ";
00229   char                 acBytesUsed[30] = "Memory: ";
00230 
00231   //
00232   // Initialize emWin
00233   //
00234   WM_SetCreateFlags(WM_CF_MEMDEV);
00235   GUI_Init();
00236   //
00237   // Check if recommended memory for the sample is available
00238   //
00239   if (GUI_ALLOC_GetNumFreeBytes() < RECOMMENDED_MEMORY) {
00240     GUI_ErrorOut("Not enough memory available."); 
00241     return;
00242   }
00243   xSize = LCD_GetXSize();
00244   ySize = LCD_GetYSize();
00245   //
00246   // Set defaults for background and widgets
00247   //
00248   WM_SetDesktopColor(GUI_BLACK);
00249   SCROLLBAR_SetDefaultSkin(SCROLLBAR_SKIN_FLEX);
00250   SCROLLBAR_SetDefaultWidth(20);
00251   SCROLLBAR_SetThumbSizeMin(25);
00252   TEXT_SetDefaultFont(GUI_FONT_6X8);
00253   //
00254   // Draw info message before creating the widgets
00255   //
00256   GUI_DrawGradientV(0, 0, xSize - 1, ySize - 1, GUI_BLUE, GUI_BLACK);
00257   GUI_SetFont(GUI_FONT_20F_ASCII);
00258   GUI_DispStringHCenterAt("Filling TREEVIEW widget...", xSize >> 1, ySize / 3);
00259   GUI_X_Delay(1000);
00260   //
00261   // Create TREEVIEW
00262   //
00263   hTree = TREEVIEW_CreateEx(0, 0, xSize, ySize, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_TREEVIEW0);
00264   TREEVIEW_SetAutoScrollV(hTree, 1);
00265   TREEVIEW_SetSelMode(hTree, TREEVIEW_SELMODE_ROW);
00266   //
00267   // Fill TREEVIEW
00268   //
00269   hNode = TREEVIEW_InsertItem(hTree, TREEVIEW_ITEM_TYPE_NODE, 0, 0, "Tree");
00270   BytesFree = GUI_ALLOC_GetNumFreeBytes();
00271   TimeStart = GUI_GetTime();
00272   r = _FillNode(hTree, hNode, NUM_CHILD_NODES, NUM_CHILD_ITEMS, TREEVIEW_DEPTH, TREEVIEW_DEPTH, acBuffer, acBuffer);
00273   TimeUsed = GUI_GetTime() - TimeStart;
00274   BytesUsed = BytesFree - GUI_ALLOC_GetNumFreeBytes();
00275   if (r) {
00276     //
00277     // Error message
00278     //
00279     WM_DeleteWindow(hTree);
00280     GUI_MessageBox("Error", "Not enough memory available!", 0);
00281   } else {
00282     //
00283     // Show result
00284     //
00285     yPos = 20;
00286     ySizeText = GUI_GetYDistOfFont( TEXT_GetDefaultFont()) + 5;
00287     _MakeNumberText(hTree, xSize >> 1, &yPos, xSize >> 1, ySizeText, acNumNodes, _NumNodes);
00288     _MakeNumberText(hTree, xSize >> 1, &yPos, xSize >> 1, ySizeText, acNumLeaves, _NumLeaves);
00289     _MakeNumberText(hTree, xSize >> 1, &yPos, xSize >> 1, ySizeText, acNumTotal, _NumNodes + _NumLeaves);
00290     _MakeNumberText(hTree, xSize >> 1, &yPos, xSize >> 1, ySizeText, acTimeUsed, TimeUsed);
00291     _MakeNumberText(hTree, xSize >> 1, &yPos, xSize >> 1, ySizeText, acBytesUsed, BytesUsed);
00292     WM_SetFocus(hTree);
00293   }
00294   while (1) {
00295     GUI_Delay(100);
00296   }
00297 }
00298 
00299 /*************************** End of file ****************************/