Difference between revisions of "LISTVIEW Spreadsheet (Sample)"
(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...") |
(→Code) |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 16: | Line 16: | ||
|- |
|- |
||
| style="font-weight:bold; font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | Download |
| style="font-weight:bold; font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | Download |
||
− | | style="font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | [ |
+ | | style="font-size:12px; font-family:Arial, Helvetica, sans-serif !important;;" | [[Media:LISTVIEW_Spreadsheet.c | 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. |
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. |
||
+ | |||
+ | == Demo == |
||
+ | |||
+ | <html><iframe src="https://wiki.segger.com/demo/emwin/LISTVIEW_Spreadsheet/LISTVIEW_Spreadsheet.html" width="480" height="272" style="border: 1px #c0c0c0 solid;" marginwidth="0" marginheight="0" hspace="0" vspace="0" scrolling="no"> </iframe></html> |
||
== Code == |
== Code == |
||
Line 49: | Line 53: | ||
PNG-Library - ( ) |
PNG-Library - ( ) |
||
TrueTypeFonts - ( ) |
TrueTypeFonts - ( ) |
||
+ | Wiki link : https://wiki.segger.com/LISTVIEW_Spreadsheet_(Sample) |
||
---------------------------END-OF-HEADER------------------------------ |
---------------------------END-OF-HEADER------------------------------ |
||
*/ |
*/ |
||
Line 62: | Line 67: | ||
********************************************************************** |
********************************************************************** |
||
*/ |
*/ |
||
+ | 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 81: | Line 89: | ||
* |
* |
||
********************************************************************** |
********************************************************************** |
||
+ | */ |
||
+ | /********************************************************************* |
||
+ | * |
||
+ | * _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 |
+ | WM_KEY_INFO * pKey; |
switch (pMsg->MsgId) { |
switch (pMsg->MsgId) { |
||
Line 92: | Line 150: | ||
pKey = (WM_KEY_INFO *)pMsg->Data.p; |
pKey = (WM_KEY_INFO *)pMsg->Data.p; |
||
if (pKey->PressedCnt == 0) { |
if (pKey->PressedCnt == 0) { |
||
− | + | switch (pKey->Key) { |
|
+ | case GUI_KEY_ENTER: |
||
− | WM_SetFocus(WM_GetParent(pMsg->hWin)); |
||
− | + | _SaveEdit(); |
|
+ | return; |
||
+ | case GUI_KEY_ESCAPE: |
||
+ | _EscapeEdit(); |
||
return; |
return; |
||
} |
} |
||
Line 113: | Line 174: | ||
*/ |
*/ |
||
static void _cbListview(WM_MESSAGE * pMsg) { |
static void _cbListview(WM_MESSAGE * pMsg) { |
||
− | WM_KEY_INFO |
+ | WM_KEY_INFO * pKey; |
− | + | char acText[128]; |
|
− | + | GUI_RECT Rect; |
|
− | + | WM_HWIN hEdit; |
|
+ | int xPosEdit, yPosEdit; |
||
− | static WM_HWIN hEdit; |
||
− | int |
+ | int xSizeEdit, ySizeEdit; |
− | int xPosEdit, yPosEdit; |
||
− | int xSizeEdit, ySizeEdit; |
||
switch(pMsg->MsgId) { |
switch(pMsg->MsgId) { |
||
Line 130: | Line 189: | ||
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); |
|
− | + | _Col = LISTVIEW_GetSelCol(pMsg->hWin); |
|
// |
// |
||
// Get selected cell text and cell rect |
// Get selected cell text and cell rect |
||
// |
// |
||
− | LISTVIEW_GetItemText(pMsg->hWin, |
+ | LISTVIEW_GetItemText(pMsg->hWin, _Col, _Row, acText, sizeof(acText)); |
− | LISTVIEW_GetItemRect(pMsg->hWin, |
+ | LISTVIEW_GetItemRect(pMsg->hWin, _Col, _Row, &Rect); |
// |
// |
||
// Create EDIT above selected cell |
// Create EDIT above selected cell |
||
Line 148: | Line 207: | ||
WM_SetFocus(hEdit); |
WM_SetFocus(hEdit); |
||
WM_SetCallback(hEdit, _cbEdit); |
WM_SetCallback(hEdit, _cbEdit); |
||
+ | _EditActive = 1; |
||
} |
} |
||
} |
} |
||
Line 154: | Line 214: | ||
// |
// |
||
LISTVIEW_Callback(pMsg); |
LISTVIEW_Callback(pMsg); |
||
+ | break; |
||
+ | default: |
||
+ | LISTVIEW_Callback(pMsg); |
||
+ | } |
||
+ | } |
||
+ | |||
+ | /********************************************************************* |
||
+ | * |
||
+ | * _cbBk |
||
+ | */ |
||
+ | static void _cbBk(WM_MESSAGE * pMsg) { |
||
+ | int NCode, Id; |
||
+ | WM_HWIN hEdit; |
||
+ | GUI_RECT Rect; |
||
+ | |||
+ | switch(pMsg->MsgId) { |
||
+ | case WM_PAINT: |
||
+ | GUI_SetBkColor(GUI_WHITE); |
||
+ | GUI_Clear(); |
||
break; |
break; |
||
case WM_NOTIFY_PARENT: |
case WM_NOTIFY_PARENT: |
||
Line 159: | Line 238: | ||
NCode = pMsg->Data.v; |
NCode = pMsg->Data.v; |
||
switch (Id) { |
switch (Id) { |
||
− | case |
+ | case GUI_ID_LISTVIEW0: |
switch (NCode) { |
switch (NCode) { |
||
− | case |
+ | case WM_NOTIFICATION_SEL_CHANGED: |
+ | _SaveEdit(); |
||
+ | break; |
||
+ | case WM_NOTIFICATION_SCROLL_CHANGED: |
||
// |
// |
||
− | // |
+ | // Move EDIT when scroll position of LISTVIEW has changed. |
// |
// |
||
− | + | LISTVIEW_GetItemRect(pMsg->hWinSrc, _Col, _Row, &Rect); |
|
− | + | hEdit = WM_GetDialogItem(pMsg->hWinSrc, GUI_ID_EDIT0); |
|
+ | WM_MoveTo(hEdit, Rect.x0, Rect.y0); |
||
break; |
break; |
||
} |
} |
||
Line 173: | Line 256: | ||
break; |
break; |
||
default: |
default: |
||
− | + | WM_DefaultProc(pMsg); |
|
} |
} |
||
} |
} |
||
Line 195: | Line 278: | ||
// |
// |
||
GUI_Init(); |
GUI_Init(); |
||
+ | WM_SetCallback(WM_HBKWIN, _cbBk); |
||
− | // |
||
− | // Enable multi-buffering |
||
− | // |
||
− | WM_MULTIBUF_Enable(1); |
||
// |
// |
||
// Create listview widget |
// Create listview widget |
Latest revision as of 11:14, 22 March 2022
LISTVIEW_Spreadsheet.c | |
---|---|
File(s) required |
|
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.
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 : 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 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;
WM_HWIN hEdit;
GUI_RECT Rect;
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;
case WM_NOTIFICATION_SCROLL_CHANGED:
//
// Move EDIT when scroll position of LISTVIEW has changed.
//
LISTVIEW_GetItemRect(pMsg->hWinSrc, _Col, _Row, &Rect);
hEdit = WM_GetDialogItem(pMsg->hWinSrc, GUI_ID_EDIT0);
WM_MoveTo(hEdit, Rect.x0, Rect.y0);
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 ****************************/