LISTVIEW Spreadsheet (Sample)

From SEGGER Wiki
Revision as of 09:58, 28 July 2021 by Florian (talk | contribs) (Created page with "{| class="wikitable" style="float:right; margin-left: 10px; background-color: #f9f9f9;" ! colspan="2" style="font-weight:bold; font-size:17px; font-family:Arial, Helvetica, sa...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
LISTVIEW_Spreadsheet.c
LISTVIEW with editable cells.
File(s) required
  • LISTVIEW_Spreadsheet.c
Runs in simulation Yes
Runs on target Yes
Download LISTVIEW_Spreadsheet.c

This sample demonstrates a LISTVIEW widget in single-cell mode with cells that can be edited, such as in a spreadsheet. To edit a cell, press ENTER and to save the edited cell text, press ENTER again.

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        : LISTVIEW_Spreadsheet.c
Purpose     : LISTVIEW sample with cells that can be edited.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
---------------------------END-OF-HEADER------------------------------
*/

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

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
static const char * _acContent[][6] = {
  { "1002", "Murphy",    "Diane",    "x5800", "President",            "dmurphy@classicmodelcars.com"    },
  { "1056", "Patterson", "Mary",     "x4611", "VP Sales",             "mpatterso@classicmodelcars.com"  },
  { "1076", "Firrelli",  "Jeff",     "x9273", "VP Marketing",         "jfirrelli@classicmodelcars.com"  },
  { "1088", "Patterson", "William",  "x4871", "Sales Manager (APAC)", "wpatterson@classicmodelcars.com" },
  { "1102", "Bondur",    "Gerard",   "x5408", "Sale Manager (EMEA)",  "gbondur@classicmodelcars.com"    },
  { "1143", "Bow",       "Anthony",  "x5428", "Sales Manager (NA)",   "abow@classicmodelcars.com"       },
  { "1165", "Jennings",  "Leslie",   "x3291", "Sales Rep",            "ljennings@classicmodelcars.com"  },
  { "1166", "Thompson",  "Leslie",   "x4065", "Sales Rep",            "lthompson@classicmodelcars.com"  },
  { "1188", "Firrelli",  "Julie",    "x2173", "Sales Rep",            "jfirrelli@classicmodelcars.com"  },
  { "1216", "Patterson", "Steve",    "x4334", "Sales Rep",            "spatterson@classicmodelcars.com" },
  { "1286", "Tseng",     "Foon Yue", "x2248", "Sales Rep",            "ftseng@classicmodelcars.com"     },
};

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/
static void _cbEdit(WM_MESSAGE * pMsg) {
  WM_KEY_INFO  * pKey;

  switch (pMsg->MsgId) {
  case WM_KEY:
    //
    // Delete EDIT widget when ENTER has been released.
    //
    pKey = (WM_KEY_INFO *)pMsg->Data.p;
    if (pKey->PressedCnt == 0) {
      if (pKey->Key == GUI_KEY_ENTER) {
        WM_SetFocus(WM_GetParent(pMsg->hWin));
        WM_DeleteWindow(pMsg->hWin);
        return;
      }
    }
    //
    // Handle key input normally
    //
    EDIT_Callback(pMsg);
    break;
  default:
    EDIT_Callback(pMsg);
  }
}

/*********************************************************************
*
*       _cbListview
*/
static void _cbListview(WM_MESSAGE * pMsg) {
  WM_KEY_INFO  * pKey;
  static int     Row, Col;
  char           acText[128];
  GUI_RECT       Rect;
  static WM_HWIN hEdit;
  int            Id, NCode;
  int            xPosEdit, yPosEdit;
  int            xSizeEdit, ySizeEdit;

  switch(pMsg->MsgId) {
  case WM_KEY:
    //
    // Handle editing of cell text with ENTER key
    //
    pKey = (WM_KEY_INFO *)pMsg->Data.p;
    if (pKey->PressedCnt == 0) {
      if (pKey->Key == GUI_KEY_ENTER) {
        Row = LISTVIEW_GetSel(pMsg->hWin);
        Col = LISTVIEW_GetSelCol(pMsg->hWin);
        //
        // Get selected cell text and cell rect
        //
        LISTVIEW_GetItemText(pMsg->hWin, Col, Row, acText, sizeof(acText));
        LISTVIEW_GetItemRect(pMsg->hWin, Col, Row, &Rect);
        //
        // Create EDIT above selected cell
        //
        xPosEdit  = Rect.x0;
        yPosEdit  = Rect.y0;
        xSizeEdit = Rect.x1 - Rect.x0 + 1;
        ySizeEdit = Rect.y1 - Rect.y0 + 1;
        hEdit = EDIT_CreateEx(xPosEdit, yPosEdit, xSizeEdit, ySizeEdit, pMsg->hWin, WM_CF_SHOW, 0, GUI_ID_EDIT0, sizeof(acText));
        EDIT_SetText(hEdit, acText);
        WM_SetFocus(hEdit);
        WM_SetCallback(hEdit, _cbEdit);
      }
    }
    //
    // Call default callback
    //
    LISTVIEW_Callback(pMsg);
    break;
  case WM_NOTIFY_PARENT:
    Id    = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch (Id) {
    case GUI_ID_EDIT0:
      switch (NCode) {
      case WM_NOTIFICATION_ENTER_PRESSED:
        //
        // If ENTER has been pressed, update the cell text
        //
        EDIT_GetText(hEdit, acText, sizeof(acText));
        LISTVIEW_SetItemText(pMsg->hWin, Col, Row, acText);
        break;
      }
      break;
    }
    break;
  default:
    LISTVIEW_Callback(pMsg);
  }
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       MainTask
*/
void MainTask(void) {
  LISTVIEW_Handle hListview;
  int             i;

  //
  // Init GUI
  //
  GUI_Init();
  //
  // Enable multi-buffering
  //
  WM_MULTIBUF_Enable(1);
  //
  // Create listview widget
  //
  hListview = LISTVIEW_CreateEx(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_LISTVIEW0);
  LISTVIEW_EnableCellSelect(hListview, 1);
  //
  // Add columns to listview
  //
  LISTVIEW_AddColumn(hListview, 45,  "Emp. Nr.",   GUI_TA_LEFT);
  LISTVIEW_AddColumn(hListview, 60,  "Last Name",  GUI_TA_LEFT);
  LISTVIEW_AddColumn(hListview, 60,  "First Name", GUI_TA_LEFT);
  LISTVIEW_AddColumn(hListview, 40,  "Ext.",       GUI_TA_LEFT);
  LISTVIEW_AddColumn(hListview, 110, "Title",      GUI_TA_LEFT);
  LISTVIEW_AddColumn(hListview, 180, "Email",      GUI_TA_LEFT);
  //
  // Add rows to listview
  //
  for (i = 0; i < GUI_COUNTOF(_acContent); i++) {
    LISTVIEW_AddRow(hListview, _acContent[i]);
  }
  //
  // Set compare functions
  //
  LISTVIEW_SetCompareFunc(hListview, 0, LISTVIEW_CompareDec);
  LISTVIEW_SetCompareFunc(hListview, 1, LISTVIEW_CompareText);
  LISTVIEW_SetCompareFunc(hListview, 2, LISTVIEW_CompareText);
  LISTVIEW_SetCompareFunc(hListview, 3, LISTVIEW_CompareText);
  LISTVIEW_SetCompareFunc(hListview, 4, LISTVIEW_CompareText);
  LISTVIEW_SetCompareFunc(hListview, 5, LISTVIEW_CompareText);
  //
  // Enable sorting
  //
  LISTVIEW_EnableSort(hListview);
  //
  // Enable scrollbar if needed
  //
  LISTVIEW_SetAutoScrollH(hListview, 1);
  LISTVIEW_SetAutoScrollV(hListview, 1);
  //
  // Set custom callback
  //
  WM_SetCallback(hListview, _cbListview);

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

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