Difference between revisions of "LISTVIEW - Usage (Sample)"

From SEGGER Wiki
Jump to: navigation, search
(Code)
Line 33: Line 33:
 
**********************************************************************
 
**********************************************************************
 
* *
 
* *
* (c) 1996 - 2020 SEGGER Microcontroller GmbH *
+
* (c) 1996 - 2022 SEGGER Microcontroller GmbH *
 
* *
 
* *
 
* Internet: www.segger.com Support: support@segger.com *
 
* Internet: www.segger.com Support: support@segger.com *
Line 39: Line 39:
 
**********************************************************************
 
**********************************************************************
   
** emWin V6.10 - Graphical user interface for embedded applications **
+
** emWin V6.24 - Graphical user interface for embedded applications **
 
emWin is protected by international copyright laws. Knowledge of the
 
emWin is protected by international copyright laws. Knowledge of the
 
source code may not be used to write a similar product. This file may
 
source code may not be used to write a similar product. This file may
Line 54: Line 54:
 
PNG-Library - ( )
 
PNG-Library - ( )
 
TrueTypeFonts - ( )
 
TrueTypeFonts - ( )
  +
Wiki link : https://wiki.segger.com/LISTVIEW_-_Usage_(Sample)
 
---------------------------END-OF-HEADER------------------------------
 
---------------------------END-OF-HEADER------------------------------
 
*/
 
*/
Line 60: Line 61:
 
#include <stdio.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <stdlib.h>
  +
  +
/*********************************************************************
  +
*
  +
* Defines
  +
*
  +
**********************************************************************
  +
*/
  +
#define ID_COLUMN_EMP_NO 0
  +
#define ID_COLUMN_LAST_NAME 1
  +
#define ID_COLUMN_FIRST_NAME 2
  +
#define ID_COLUMN_EXT 3
  +
#define ID_COLUMN_TITLE 4
  +
#define ID_COLUMN_EMAIL 5
   
 
/*********************************************************************
 
/*********************************************************************
Line 87: Line 101:
 
**********************************************************************
 
**********************************************************************
 
*/
 
*/
  +
/*********************************************************************
  +
*
  +
* _cbBk
  +
*/
  +
static void _cbBk(WM_MESSAGE * pMsg) {
  +
int Id, NCode;
  +
WM_HWIN hListview;
  +
int Sel;
  +
char acBuffer [128];
  +
char acFirstName[32];
  +
char acLastName [32];
  +
  +
switch (pMsg->MsgId) {
  +
case WM_PAINT:
  +
GUI_SetBkColor(GUI_WHITE);
  +
GUI_Clear();
  +
GUI_SetColor(GUI_BLACK);
  +
//
  +
// Display selected item
  +
//
  +
hListview = WM_GetDialogItem(WM_HBKWIN, GUI_ID_LISTVIEW0);
  +
Sel = LISTVIEW_GetSel(hListview);
  +
if (Sel != -1) {
  +
sprintf(acBuffer, "Selected row: %d", Sel);
  +
GUI_DispStringAt(acBuffer, 10, 140);
  +
LISTVIEW_GetItemText(hListview, ID_COLUMN_FIRST_NAME, Sel, acFirstName, sizeof(acFirstName));
  +
LISTVIEW_GetItemText(hListview, ID_COLUMN_LAST_NAME, Sel, acLastName, sizeof(acLastName));
  +
sprintf(acBuffer, "Selected employee name: %s %s", acFirstName, acLastName);
  +
GUI_DispStringAt(acBuffer, 10, 160);
  +
}
  +
//
  +
// No selection
  +
//
  +
else {
  +
sprintf(acBuffer, "Selected row: none.");
  +
GUI_DispStringAt(acBuffer, 10, 140);
  +
}
  +
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:
  +
//
  +
// Display new selection in this window when LISTVIEW selection has changed.
  +
//
  +
WM_InvalidateWindow(pMsg->hWin);
  +
break;
  +
}
  +
break;
  +
}
  +
break;
  +
default:
  +
WM_DefaultProc(pMsg);
  +
}
  +
}
  +
 
/*********************************************************************
 
/*********************************************************************
 
*
 
*
Line 98: Line 171:
 
ps[0] = (const char *)p0 + 1; // Move pointer by 1 to ignore the 'x'
 
ps[0] = (const char *)p0 + 1; // Move pointer by 1 to ignore the 'x'
 
ps[1] = (const char *)p1 + 1;
 
ps[1] = (const char *)p1 + 1;
 
 
//
 
//
 
// Parse to int
 
// Parse to int
Line 108: Line 180:
 
}
 
}
 
}
 
}
  +
//
 
  +
// Return comparison result
  +
//
 
if (v[0] > v[1]) {
 
if (v[0] > v[1]) {
 
return -1;
 
return -1;
Line 140: Line 214:
 
//
 
//
 
WM_MULTIBUF_Enable(1);
 
WM_MULTIBUF_Enable(1);
  +
//
  +
// Set callback to background window to receive LISTVIEW notifications
  +
//
  +
WM_SetCallback(WM_HBKWIN, _cbBk);
 
//
 
//
 
// Create listview widget
 
// Create listview widget
 
//
 
//
hListview = LISTVIEW_CreateEx(0, 0, LCD_GetXSize(), LCD_GetYSize(), WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_LISTVIEW0);
+
hListview = LISTVIEW_CreateEx(10, 10, 250, 120, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_LISTVIEW0);
 
//
 
//
 
// Add columns to listview
 
// Add columns to listview
Line 173: Line 251:
 
LISTVIEW_EnableSort(hListview);
 
LISTVIEW_EnableSort(hListview);
 
//
 
//
// Enable scrollbar if needed
+
// Enable motion scrolling
 
//
 
//
  +
LISTVIEW_EnableMotion(hListview, LISTVIEW_CF_MOTION_H | LISTVIEW_CF_MOTION_V);
LISTVIEW_SetAutoScrollH(hListview, 1);
 
LISTVIEW_SetAutoScrollV(hListview, 1);
 
   
 
while (1) {
 
while (1) {

Revision as of 11:11, 22 March 2022

LISTVIEW_Usage.c
LISTVIEW Usage.png
File(s) required
  • LISTVIEW_Usage.c
Runs in simulation Yes
Runs on target Yes
Download LISTVIEW_Usage.c

This sample demonstrates the usage of a LISTVIEW widget.

Demo

Code

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

** emWin V6.24 - 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_Usage.c
Purpose     : Sample that demonstrates the usage of LISTVIEW widgets
              in emWin.
Requirements: WindowManager - (x)
              MemoryDevices - ( )
              AntiAliasing  - ( )
              VNC-Server    - ( )
              PNG-Library   - ( )
              TrueTypeFonts - ( )
Wiki link   : https://wiki.segger.com/LISTVIEW_-_Usage_(Sample)
---------------------------END-OF-HEADER------------------------------
*/

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

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ID_COLUMN_EMP_NO        0
#define ID_COLUMN_LAST_NAME     1
#define ID_COLUMN_FIRST_NAME    2
#define ID_COLUMN_EXT           3
#define ID_COLUMN_TITLE         4
#define ID_COLUMN_EMAIL         5

/*********************************************************************
*
*       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
*
**********************************************************************
*/
/*********************************************************************
*
*       _cbBk
*/
static void _cbBk(WM_MESSAGE * pMsg) {
  int     Id, NCode;
  WM_HWIN hListview;
  int     Sel;
  char    acBuffer   [128];
  char    acFirstName[32];
  char    acLastName [32];

  switch (pMsg->MsgId) {
  case WM_PAINT:
    GUI_SetBkColor(GUI_WHITE);
    GUI_Clear();
    GUI_SetColor(GUI_BLACK);
    //
    // Display selected item
    //
    hListview = WM_GetDialogItem(WM_HBKWIN, GUI_ID_LISTVIEW0);
    Sel       = LISTVIEW_GetSel(hListview);
    if (Sel != -1) {
      sprintf(acBuffer, "Selected row: %d", Sel);
      GUI_DispStringAt(acBuffer, 10, 140);
      LISTVIEW_GetItemText(hListview, ID_COLUMN_FIRST_NAME, Sel, acFirstName, sizeof(acFirstName));
      LISTVIEW_GetItemText(hListview, ID_COLUMN_LAST_NAME,  Sel, acLastName,  sizeof(acLastName));
      sprintf(acBuffer, "Selected employee name: %s %s", acFirstName, acLastName);
      GUI_DispStringAt(acBuffer, 10, 160);
    }
    //
    // No selection
    //
    else {
      sprintf(acBuffer, "Selected row: none.");
      GUI_DispStringAt(acBuffer, 10, 140);
    }
    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:
        //
        // Display new selection in this window when LISTVIEW selection has changed.
        //
        WM_InvalidateWindow(pMsg->hWin);
        break;
      }
      break;
    }
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

/*********************************************************************
*
*       _CompareExtension
*/
static int _CompareExtension(const void * p0, const void * p1) {
  const char * ps[2];
  int          v[2];
  int          i;

  ps[0] = (const char *)p0 + 1; // Move pointer by 1 to ignore the 'x'
  ps[1] = (const char *)p1 + 1;
  //
  // Parse to int
  //
  for (i = 0; i < 2; i++) {
    v[i] = atoi(ps[i]);
    if (v[i] == 0) {
      return 0;
    }
  }
  //
  // Return comparison result
  //
  if (v[0] > v[1]) {
    return -1;
  } else if (v[0] < v[1]) {
    return 1;
  } else {
    return 0;
  }
}

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

  //
  // Init GUI
  //
  GUI_Init();
  //
  // Enable multi-buffering
  //
  WM_MULTIBUF_Enable(1);
  //
  // Set callback to background window to receive LISTVIEW notifications
  // 
  WM_SetCallback(WM_HBKWIN, _cbBk);
  //
  // Create listview widget
  //
  hListview = LISTVIEW_CreateEx(10, 10, 250, 120, WM_HBKWIN, WM_CF_SHOW, 0, GUI_ID_LISTVIEW0);
  //
  // 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, _CompareExtension);
  LISTVIEW_SetCompareFunc(hListview, 4, LISTVIEW_CompareText);
  LISTVIEW_SetCompareFunc(hListview, 5, LISTVIEW_CompareText);
  //
  // Enable sorting
  //
  LISTVIEW_EnableSort(hListview);
  //
  // Enable motion scrolling
  //
  LISTVIEW_EnableMotion(hListview, LISTVIEW_CF_MOTION_H | LISTVIEW_CF_MOTION_V);

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

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