Difference between revisions of "LISTVIEW Spreadsheet (Sample)"

From SEGGER Wiki
Jump to: navigation, search
(Code)
(Code)
Line 24: Line 24:
   
 
<source lang="c">
 
<source lang="c">
  +
 
/*********************************************************************
 
/*********************************************************************
 
* SEGGER Microcontroller GmbH *
 
* SEGGER Microcontroller GmbH *
Line 63: Line 64:
 
**********************************************************************
 
**********************************************************************
 
*/
 
*/
  +
static U8 _EditActive;
  +
static int _Row, _Col;
  +
 
static const char * _acContent[][6] = {
 
static const char * _acContent[][6] = {
 
{ "1002", "Murphy", "Diane", "x5800", "President", "dmurphy@classicmodelcars.com" },
 
{ "1002", "Murphy", "Diane", "x5800", "President", "dmurphy@classicmodelcars.com" },
Line 82: Line 86:
 
*
 
*
 
**********************************************************************
 
**********************************************************************
  +
*/
  +
/*********************************************************************
  +
*
  +
* _SaveEdit
  +
*/
  +
static void _SaveEdit(void) {
  +
char acText[128];
  +
WM_HWIN hList;
  +
WM_HWIN hEdit;
  +
  +
//
  +
// If ENTER has been pressed, update the cell text
  +
//
  +
if (_EditActive) {
  +
_EditActive = 0;
  +
hList = WM_GetDialogItem(WM_HBKWIN, GUI_ID_LISTVIEW0);
  +
hEdit = WM_GetDialogItem(hList, GUI_ID_EDIT0);
  +
//
  +
// If ENTER has been pressed, update the cell text
  +
//
  +
EDIT_GetText(hEdit, acText, sizeof(acText));
  +
LISTVIEW_SetItemText(hList, _Col, _Row, acText);
  +
WM_SetFocus(hList);
  +
WM_DeleteWindow(hEdit);
  +
}
  +
}
  +
  +
/*********************************************************************
  +
*
  +
* _EscapeEdit
  +
*/
  +
static void _EscapeEdit(void) {
  +
WM_HWIN hList;
  +
WM_HWIN hEdit;
  +
  +
//
  +
// If ESC has been pressed, update the cell text
  +
//
  +
if (_EditActive) {
  +
_EditActive = 0;
  +
hList = WM_GetDialogItem(WM_HBKWIN, GUI_ID_LISTVIEW0);
  +
hEdit = WM_GetDialogItem(hList, GUI_ID_EDIT0);
  +
WM_SetFocus(hList);
  +
WM_DeleteWindow(hEdit);
  +
}
  +
}
  +
  +
/*********************************************************************
  +
*
  +
* _cbEdit
 
*/
 
*/
 
static void _cbEdit(WM_MESSAGE * pMsg) {
 
static void _cbEdit(WM_MESSAGE * pMsg) {
WM_KEY_INFO * pKey;
+
WM_KEY_INFO * pKey;
   
 
switch (pMsg->MsgId) {
 
switch (pMsg->MsgId) {
Line 93: Line 147:
 
pKey = (WM_KEY_INFO *)pMsg->Data.p;
 
pKey = (WM_KEY_INFO *)pMsg->Data.p;
 
if (pKey->PressedCnt == 0) {
 
if (pKey->PressedCnt == 0) {
if (pKey->Key == GUI_KEY_ENTER) {
+
switch (pKey->Key) {
  +
case GUI_KEY_ENTER:
WM_SetFocus(WM_GetParent(pMsg->hWin));
 
WM_DeleteWindow(pMsg->hWin);
+
_SaveEdit();
  +
return;
  +
case GUI_KEY_ESCAPE:
  +
_EscapeEdit();
 
return;
 
return;
 
}
 
}
Line 114: Line 171:
 
*/
 
*/
 
static void _cbListview(WM_MESSAGE * pMsg) {
 
static void _cbListview(WM_MESSAGE * pMsg) {
WM_KEY_INFO * pKey;
+
WM_KEY_INFO * pKey;
static int Row, Col;
+
char acText[128];
char acText[128];
+
GUI_RECT Rect;
GUI_RECT Rect;
+
WM_HWIN hEdit;
  +
int Id, NCode;
static WM_HWIN hEdit;
 
int Id, NCode;
+
int xPosEdit, yPosEdit;
int xPosEdit, yPosEdit;
+
int xSizeEdit, ySizeEdit;
int xSizeEdit, ySizeEdit;
 
   
 
switch(pMsg->MsgId) {
 
switch(pMsg->MsgId) {
Line 131: Line 187:
 
if (pKey->PressedCnt == 0) {
 
if (pKey->PressedCnt == 0) {
 
if (pKey->Key == GUI_KEY_ENTER) {
 
if (pKey->Key == GUI_KEY_ENTER) {
Row = LISTVIEW_GetSel(pMsg->hWin);
+
_Row = LISTVIEW_GetSel(pMsg->hWin);
Col = LISTVIEW_GetSelCol(pMsg->hWin);
+
_Col = LISTVIEW_GetSelCol(pMsg->hWin);
 
//
 
//
 
// Get selected cell text and cell rect
 
// Get selected cell text and cell rect
 
//
 
//
LISTVIEW_GetItemText(pMsg->hWin, Col, Row, acText, sizeof(acText));
+
LISTVIEW_GetItemText(pMsg->hWin, _Col, _Row, acText, sizeof(acText));
LISTVIEW_GetItemRect(pMsg->hWin, Col, Row, &Rect);
+
LISTVIEW_GetItemRect(pMsg->hWin, _Col, _Row, &Rect);
 
//
 
//
 
// Create EDIT above selected cell
 
// Create EDIT above selected cell
Line 149: Line 205:
 
WM_SetFocus(hEdit);
 
WM_SetFocus(hEdit);
 
WM_SetCallback(hEdit, _cbEdit);
 
WM_SetCallback(hEdit, _cbEdit);
  +
_EditActive = 1;
 
}
 
}
 
}
 
}
Line 155: Line 212:
 
//
 
//
 
LISTVIEW_Callback(pMsg);
 
LISTVIEW_Callback(pMsg);
  +
break;
  +
default:
  +
LISTVIEW_Callback(pMsg);
  +
}
  +
}
  +
  +
/*********************************************************************
  +
*
  +
* _cbBk
  +
*/
  +
static void _cbBk(WM_MESSAGE * pMsg) {
  +
int NCode, Id;
  +
  +
switch(pMsg->MsgId) {
  +
case WM_PAINT:
  +
GUI_SetBkColor(GUI_WHITE);
  +
GUI_Clear();
 
break;
 
break;
 
case WM_NOTIFY_PARENT:
 
case WM_NOTIFY_PARENT:
Line 160: Line 234:
 
NCode = pMsg->Data.v;
 
NCode = pMsg->Data.v;
 
switch (Id) {
 
switch (Id) {
case GUI_ID_EDIT0:
+
case GUI_ID_LISTVIEW0:
 
switch (NCode) {
 
switch (NCode) {
case WM_NOTIFICATION_ENTER_PRESSED:
+
case WM_NOTIFICATION_SEL_CHANGED:
//
+
_SaveEdit();
// 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;
 
break;
default:
 
//
 
// All other notifications are handled by the default callback
 
//
 
LISTVIEW_Callback(pMsg);
 
 
}
 
}
 
break;
 
break;
 
default:
 
default:
LISTVIEW_Callback(pMsg);
+
WM_DefaultProc(pMsg);
 
}
 
}
 
}
 
}
Line 201: Line 266:
 
//
 
//
 
GUI_Init();
 
GUI_Init();
  +
WM_SetCallback(WM_HBKWIN, _cbBk);
//
 
// Enable multi-buffering
 
//
 
WM_MULTIBUF_Enable(1);
 
 
//
 
//
 
// Create listview widget
 
// Create listview widget

Revision as of 16:16, 2 November 2021

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 - ( )
Wiki link   : https://wiki.segger.com/LISTVIEW_Spreadsheet_(Sample)
---------------------------END-OF-HEADER------------------------------
*/

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

/*********************************************************************
*
*       Static data
*
**********************************************************************
*/
static U8  _EditActive;
static int _Row, _Col;

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
*
**********************************************************************
*/
/*********************************************************************
*
*       _SaveEdit
*/
static void _SaveEdit(void) {
  char    acText[128];
  WM_HWIN hList;
  WM_HWIN hEdit;

  //
  // If ENTER has been pressed, update the cell text
  //
  if (_EditActive) {
    _EditActive = 0;
    hList       = WM_GetDialogItem(WM_HBKWIN, GUI_ID_LISTVIEW0);
    hEdit       = WM_GetDialogItem(hList,     GUI_ID_EDIT0);
    //
    // If ENTER has been pressed, update the cell text
    //
    EDIT_GetText(hEdit, acText, sizeof(acText));
    LISTVIEW_SetItemText(hList, _Col, _Row, acText);
    WM_SetFocus(hList);
    WM_DeleteWindow(hEdit);
  }
}

/*********************************************************************
*
*       _EscapeEdit
*/
static void _EscapeEdit(void) {
  WM_HWIN hList;
  WM_HWIN hEdit;

  //
  // If ESC has been pressed, update the cell text
  //
  if (_EditActive) {
    _EditActive = 0;
    hList       = WM_GetDialogItem(WM_HBKWIN, GUI_ID_LISTVIEW0);
    hEdit       = WM_GetDialogItem(hList,     GUI_ID_EDIT0);
    WM_SetFocus(hList);
    WM_DeleteWindow(hEdit);
  }
}

/*********************************************************************
*
*       _cbEdit
*/
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) {
      switch (pKey->Key) {
      case GUI_KEY_ENTER:
        _SaveEdit();
        return;
      case GUI_KEY_ESCAPE:
        _EscapeEdit();
        return;
      }
    }
    //
    // Handle key input normally
    //
    EDIT_Callback(pMsg);
    break;
  default:
    EDIT_Callback(pMsg);
  }
}

/*********************************************************************
*
*       _cbListview
*/
static void _cbListview(WM_MESSAGE * pMsg) {
  WM_KEY_INFO * pKey;
  char          acText[128];
  GUI_RECT      Rect;
  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);
        _EditActive = 1;
      }
    }
    //
    // Call default callback
    //
    LISTVIEW_Callback(pMsg);
    break;
  default:
    LISTVIEW_Callback(pMsg);
  }
}

/*********************************************************************
*
*       _cbBk
*/
static void _cbBk(WM_MESSAGE * pMsg) {
  int NCode, Id;

  switch(pMsg->MsgId) {
  case WM_PAINT:
    GUI_SetBkColor(GUI_WHITE);
    GUI_Clear();
    break;
  case WM_NOTIFY_PARENT:
    Id    = WM_GetId(pMsg->hWinSrc);
    NCode = pMsg->Data.v;
    switch (Id) {
    case GUI_ID_LISTVIEW0:
      switch (NCode) {
      case WM_NOTIFICATION_SEL_CHANGED:
        _SaveEdit();
        break;
      }
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

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

  //
  // Init GUI
  //
  GUI_Init();
  WM_SetCallback(WM_HBKWIN, _cbBk);
  //
  // 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 ****************************/