TREEVIEW - Usage (Sample)

From SEGGER Wiki
Jump to: navigation, search
TREEVIEW_Usage.c
TREEVIEW Usage.gif
File(s) required
  • TREEVIEW_Usage.c
Runs in simulation Yes
Runs on target Yes
Download TREEVIEW_Usage.c

This sample demonstrates the usage of a TREEVIEW widget.

Demo

Code

/*********************************************************************
*                    SEGGER Microcontroller GmbH                     *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2020  SEGGER Microcontroller GmbH                *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V6.10 - Graphical user interface for embedded applications **
emWin is protected by international copyright laws.   Knowledge of the
source code may not be used to write a similar product.  This file may
only  be used  in accordance  with  a license  and should  not be  re-
distributed in any way. We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : TREEVIEW_Usage.c
Purpose     : Sample that demonstrates the usage of TREEVIEW widgets
              in emWin.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

#include "DIALOG.h"
#include <stdio.h>
#include <string.h>

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
/*********************************************************************
*
*       _cbTreeview
*/
static void _cbTreeview(WM_MESSAGE * pMsg) {
  WM_KEY_INFO * pInfo;

  switch(pMsg->MsgId) {
  case WM_KEY:
    //
    // When the TREEVIEW receives key input from the left or right arrow keys,
    // the node is either closed or opened, respectively.
    // We react on the key input and check the given key, so we can redraw the
    // TREEVIEW's parent window to keep the displayed item info up to date.
    //
    pInfo = (WM_KEY_INFO *)pMsg->Data.p;
    if (pInfo) {
      //
      // Check if the key matches the left or right arrow keys.
      //
      if (pInfo->Key == GUI_KEY_LEFT || pInfo->Key == GUI_KEY_RIGHT) {
        //
        // Redraw parent window if the keys match.
        //
        WM_InvalidateWindow(WM_GetParent(pMsg->hWin));
      }
    }
    //
    // We still want the key input to be processed normally, so we pass
    // the message to the TREEVIEW default callback.
    //
    TREEVIEW_Callback(pMsg);
    break;
  default:
    //
    // All other cases are handled by the default TREEVIEW callback.
    //
    TREEVIEW_Callback(pMsg);
  }
}

/*********************************************************************
*
*       _cbWin
*/
static void _cbWin(WM_MESSAGE * pMsg) {
  static WM_HWIN       hTree;
  TREEVIEW_ITEM_Handle hItem;
  TREEVIEW_ITEM_Handle hLastNode;
  TREEVIEW_ITEM_Handle hLastChild;
  int                  i;
  char                 acBuffer[64];
  char                 acName[32];
  int                  NCode, Id;
  TREEVIEW_ITEM_INFO   Info;

  switch(pMsg->MsgId) {
  case WM_CREATE:
    //
    // Create TREEVIEW handle
    // With these flags set row-selection is available and the treeview is created with a vertical scrollbar
    //
    hTree = TREEVIEW_CreateEx(0, 0, 130, LCD_GetYSize(), pMsg->hWin, WM_CF_SHOW, TREEVIEW_CF_ROWSELECT | TREEVIEW_CF_AUTOSCROLLBAR_V, GUI_ID_TREEVIEW0);
    //
    // Change appearance of treeview
    //
    TREEVIEW_SetBkColor(hTree, TREEVIEW_CI_UNSEL, GUI_GRAY_AA);
    TREEVIEW_SetBkColor(hTree, TREEVIEW_CI_SEL, GUI_BLUE);
    TREEVIEW_SetFont(hTree, &GUI_Font16_1);
    //
    // Create first node
    //
    sprintf(acBuffer, "Node 1");
    hItem = TREEVIEW_ITEM_Create(TREEVIEW_ITEM_TYPE_NODE, acBuffer, 0);
    // The last two parameters should be 0 when a node is added to an empty tree
    TREEVIEW_AttachItem(hTree, hItem, 0, 0);
    hLastNode = hItem;
    //
    // Add a leaf to the node
    // -> Leafs are nodes that do not have any children
    //
    sprintf(acBuffer, "Leaf 1");
    hItem = TREEVIEW_ITEM_Create(TREEVIEW_ITEM_TYPE_LEAF, acBuffer, 0);
    TREEVIEW_AttachItem(hTree, hItem, hLastNode, TREEVIEW_INSERT_FIRST_CHILD);
    //
    // Create some more nodes with children
    //
    for (i = 2; i <= 5; i++) {
      sprintf(acBuffer, "Node %d", i);
      hItem = TREEVIEW_ITEM_Create(TREEVIEW_ITEM_TYPE_NODE, acBuffer, 0);
      TREEVIEW_AttachItem(hTree, hItem, hLastNode, TREEVIEW_INSERT_BELOW);
      hLastNode = hItem;
      //
      // Create a child node
      //
      sprintf(acBuffer, "Child node %d", i);
      hItem = TREEVIEW_ITEM_Create(TREEVIEW_ITEM_TYPE_NODE, acBuffer, 0);
      TREEVIEW_AttachItem(hTree, hItem, hLastNode, TREEVIEW_INSERT_FIRST_CHILD);
      hLastChild = hItem;
      //
      // Create a leaf for each child node
      //
      sprintf(acBuffer, "Leaf %d", i);
      hItem = TREEVIEW_ITEM_Create(TREEVIEW_ITEM_TYPE_LEAF, acBuffer, 0);
      TREEVIEW_AttachItem(hTree, hItem, hLastChild, TREEVIEW_INSERT_FIRST_CHILD);
    }
    //
    // Fully expand a node
    //
    TREEVIEW_ITEM_ExpandAll(hLastNode);
    //
    // Set custom callback to TREEVIEW.
    // This is not a necessity. In this example though, we react on key input to the TREEVIEW.
    // When the widget receives key input, the parent should be redrawn, so that the displayed
    // item info is still accurate.
    //
    WM_SetCallback(hTree, _cbTreeview);
    break;
  case WM_PAINT:
    GUI_SetBkColor(GUI_BLACK);
    GUI_Clear();
    //
    // Display name of currently selected item.
    //
    GUI_SetFont(&GUI_Font16_1);
    sprintf(acBuffer, "Currently selected item: ");
    hItem = TREEVIEW_GetSel(hTree);
    if(hItem) {
      TREEVIEW_ITEM_GetText(hItem, acName, sizeof(acName));
      strcat(acBuffer, acName);
    } else {
      strcat(acBuffer, "none.");
    }
    GUI_DispStringAt(acBuffer, 150, 10);
    //
    // Display information about currently selected item
    //
    GUI_SetFont(&GUI_Font13B_1);
    TREEVIEW_ITEM_GetInfo(hItem, &Info);
    //
    sprintf(acBuffer, "Item is a ");
    if (Info.IsNode) {
      strcat(acBuffer, "node.");
    } else {
      strcat(acBuffer, "leaf.");
    }
    GUI_DispStringAt(acBuffer, 150, 30);
    //
    if(Info.IsExpanded) {
      sprintf(acBuffer, "Item is expanded.");
    } else {
      sprintf(acBuffer, "Item is closed.");
    }
    GUI_DispStringAt(acBuffer, 150, 45);
    //
    if (Info.HasLines) {
      sprintf(acBuffer, "Joining lines are visible.");
    } else {
      sprintf(acBuffer, "Joining lines are not visible.");
    }
    GUI_DispStringAt(acBuffer, 150, 60);
    //
    if(Info.HasRowSelect) {
      sprintf(acBuffer, "Row selection is active.");
    } else {
      sprintf(acBuffer, "Row selection is not active.");
    }
    GUI_DispStringAt(acBuffer, 150, 75);
    //
    sprintf(acBuffer, "Indentation level: %d", Info.Level);
    GUI_DispStringAt(acBuffer, 150, 90);
    break;
  case WM_NOTIFY_PARENT:
    Id    = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch(Id) {
    case GUI_ID_TREEVIEW0:
      switch (NCode) {
      case WM_NOTIFICATION_SEL_CHANGED:
        //
        // When the selection in the TREEVIEW has changed, redraw the parent window to display the change.
        //
        WM_InvalidateWindow(pMsg->hWin);
        break;
      }
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  //
  // Init emWin.
  //
  GUI_Init();
  //
  // Create parent window.
  //
  WM_CreateWindowAsChild(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, _cbWin, 0);

  while (1) {
    GUI_Delay(100);
  }
}

/*************************** End of file ****************************/