Sample program using the list display functions of the AdvancedMenu library.

This sample program displays the following screen.

main()

list_items()

  • Register list items in the list resource.
  • Display the list.
  • Press F1 key to sort the list items.
  

show_item()

  • Displays the data of the specified list item.

Sample

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lib.h"
#include "AdvancedMenu.h"

// Prototypes
void list_items(void);
void show_item(LIST_HANDLE hList, int index);
int compare_name( const AL_ListItemData *a, const AL_ListItemData *b );
void loadSampleItems(LIST_HANDLE hList);
void releaseSampleItems(LIST_HANDLE hList);

///////////////////////////////////////////////////////////////////
// Color palettes
///////////////////////////////////////////////////////////////////
#define MENU_FORE_COLOR     RGB_WHITE
#define MENU_BACK_COLOR     0x427cbe    //Dull blue
#define MENU_BUTTON_COLOR   RGB_YELLOW
#define MENU_APPLY_COLOR    0xfcd9ff    //Color of Start icon (Light pink)
#define MENU_SELECT_COLOR   0x284a70    //Dark blue

#define FORE_COLOR          RGB_WHITTE
#define BACK_COLOR          0xf7f7f7    //Light gray
#define SELECT_COLOR        RGB_BLUE
#define DISABLE_COLOR       0xb0b0b0    //Gray
#define GIUDE_BUTTON        RGB_BLUE
#define APPLY_COLOR         RGB_MAGENTA
#define TITLE_TASK_COLOR    0x008000    //Dark Green

static const AM_ColorPalette CustomColor[] = {
    //ForeColor,        BackColor,        Select_ForeColor, Select_BackColor,  Control_ForeColor
    { MENU_FORE_COLOR,  MENU_BACK_COLOR,  RGB_WHITE,        MENU_SELECT_COLOR, MENU_BUTTON_COLOR }, //#0: Task menu base color
    { MENU_FORE_COLOR,  MENU_BACK_COLOR,  0,                0,                 MENU_APPLY_COLOR  }, //#1: Task menu apply color
    { RGB_BLACK,        BACK_COLOR,       RGB_WHITE,        SELECT_COLOR,      GIUDE_BUTTON      }, //#2: Base color
    { GIUDE_BUTTON,     BACK_COLOR,       0,                0,                 GIUDE_BUTTON      }, //#3: Guide color
    { RGB_WHITE,        TITLE_TASK_COLOR, 0,                0,                 0                 }, //#4: Title 0f tasks
    { APPLY_COLOR,      BACK_COLOR,       0,                0,                 APPLY_COLOR       }, //#5: Apply color
};
#define PX_MENU           0  // Task menu base color
#define PX_MENU_APPLY     1  // Task menu apply color
#define PX_BASE           2  // Base color
#define PX_GUIDE          3  // Guide color
#define PX_TITLE_TASK     4  // Title 0f tasks
#define PX_APPLY          5  // Apply color

///////////////////////////////////////////////////////////////////
// Main screen
///////////////////////////////////////////////////////////////////

// Options for the main screen
static const AM_Option MenuOption = {
    MENU_FORE_COLOR,                                // MenuForeColor
    DISABLE_COLOR,                                  // MenuDisabledForeColor
    MENU_BACK_COLOR,                                // MenuBackColor
    sizeof(CustomColor)/sizeof(AM_ColorPalette),      // NumberOfColorPalette
    (pAM_ColorPalette)CustomColor,                  // ColorPalettes
    0,                                              // OptionFlag
    HUGE_FONT,                                      // DefaultFont
    1                                               // DefaultLineSpacing
};

// Definition of the menu items of the main screen
static const AM_MenuItem MainMenuTable[] = {
//   itemID, y,           x, menuText,     Palette,       visible, enabled, selectable, showControl,   checked, font
    {0,      1,           1, "List demo.", PX_MENU,       true,    true,    false,      AM_NO_CONTROL},
    {0,      AM_PIX(129), 0, "Start",      PX_MENU_APPLY, true,    true,    false,      AM_SCAN_ICON,  false,   MEDIUM_FONT},
    {-1}
};

void main(void)
{
    MENU_HANDLE hMenu;
    int event;

    hMenu = AM_CreateMenu(MainMenuTable, (const pAM_Option)&MenuOption);

    while(1){
        AM_ShowMenu(hMenu, AM_SELECT_NO_ID);
        while(1){
            event = AM_ExecMenu(hMenu);
            if (event == SCAN_KEY){
                // Open the list screen.
                list_items();
                break;
            }
        }
    }
}

///////////////////////////////////////////////////////////////////
// List screen
///////////////////////////////////////////////////////////////////

// Options for the list screen
static const AM_Option ListOption = {
    RGB_BLACK,                                      // MenuForeColor
    DISABLE_COLOR,                                  // MenuDisabledForeColor
    BACK_COLOR,                                     // MenuBackColor
    sizeof(CustomColor)/sizeof(AM_ColorPalette),      // NumberOfColorPalette
    (pAM_ColorPalette)CustomColor,                  // ColorPalettes
    AM_ALLOW_AUTOREPEAT,                            // OptionFlag
    MEDIUM_FONT,                                    // DefaultFont
    1                                               // DefaultLineSpacing
};

// Table of the menu items for the list scrteen
static const AM_MenuItem ListMenuTable[] = {
//   itemID, y,           x,          menuText,           Palette,       visible, enabled, selectable, showControl,   checked, font
    {0,      0,           0,          "   Code list    ", PX_TITLE_TASK, true,    true,    false,      AM_NO_CONTROL, false,   LARGE_FONT},
    {0,      AM_PIX(116), 0,          "Sort by name",     PX_GUIDE,      true,    true,    false,      AM_F1_ICON},
    {0,      AM_PIX(129), 0,          "Exit",             PX_GUIDE,      true,    true,    false,      AM_CLEAR_ICON},
    {0,      AM_PIX(129), AM_PIX(64), "Show",             PX_GUIDE,      true,    true,    false,      AM_ENT_ICON},
    {-1}
};


void list_items()
{
    LIST_HANDLE hList;
    int event;
    
    hList = AL_CreateList(ListMenuTable, (const pAM_Option)&ListOption, AM_PIX(18), 7);

    loadSampleItems(hList); //Using AL_AddItem function to register the list items to the list resource.

    AL_ShowList(hList, 0, 0);

    while(1){
        event = AL_ExecList(hList);
        if (event == CLEAR_KEY){
            //Exit
            break;
        }else if(event == F1_KEY){
            //Sort by name
            AL_SortListItem(hList, compare_name);
            AL_ShowList(hList, 0, 0);
        }else if(event == ENT_KEY){
            //Show
            show_item(hList, AL_GetSelectedIndex(hList));
            AL_RepaintList(hList);
        }
    }
    releaseSampleItems(hList);

    AL_ReleaseList(hList);
    return;
}

int compare_name( const AL_ListItemData *a, const AL_ListItemData *b )
{
    return strcmp((char *)(a->listItem.userParam2), (char *)(b->listItem.userParam2));
}

///////////////////////////////////////////////////////////////////
// Show item
///////////////////////////////////////////////////////////////////

// Menu ID
enum _SHOW_ITEM_ID {
    SHOW_ITEM_ID_CODE = 1,
    SHOW_ITEM_ID_NAME,
};

static const AM_MenuItem ShowItemMenuTable[] = {
//   itemID,           y,           x, menuText,           Palette,       visible, enabled, selectable, showControl,   checked, font
    {0,                0,           0, "     Detail     ", PX_TITLE_TASK, true,    true,    false,      AM_NO_CONTROL, false,   LARGE_FONT},
    {0,                2,           0, "Code:",            PX_BASE,       true,    true,    false,      AM_NO_CONTROL},
    {SHOW_ITEM_ID_CODE,3,           1, "",                 PX_BASE,       true,    true,    false,      AM_NO_CONTROL},
    {0,                5,           0, "Name:",            PX_BASE,       true,    true,    false,      AM_NO_CONTROL},
    {SHOW_ITEM_ID_NAME,6,           1, "",                 PX_BASE,       true,    true,    false,      AM_NO_CONTROL},
    {0,                AM_PIX(129), 0, "Close",            PX_GUIDE,      true,    true,    false,      AM_CLEAR_ICON},
    {-1}
};

void show_item(LIST_HANDLE hList, int index)
{
    MENU_HANDLE hMenu;
    int event;
    AL_ListItemData itemData;

    hMenu = AM_CreateMenu(ShowItemMenuTable, (const pAM_Option)&ListOption);
    AL_GetListItemData(hList, index, &itemData);
    AM_SetText(hMenu, SHOW_ITEM_ID_CODE, (char *)itemData.listItem.userParam1);
    AM_SetText(hMenu, SHOW_ITEM_ID_NAME, (char *)itemData.listItem.userParam2);

    AM_ShowMenu(hMenu, AM_SELECT_NO_ID);
    while(1){
        event = AM_ExecMenu(hMenu);
        if (event == CLEAR_KEY){
            break;
        }
    }
    AM_ReleaseMenu(hMenu);
}

//////////////////////////////////////////////////////////
//Sample data for the demo.
//////////////////////////////////////////////////////////
struct SampleData{
    char *code;
    char *name;
    char *description;
};

struct SampleData Samples[]  = {
    {"000000001001", "Apple"},
    {"000000001002", "Orange"},
    {"000000001003", "Pineapple"},
    {"000000001004", "Grapes"},
    {"000000001005", "Peach"},
    {"000000001006", "Mango"},
    {"000000001007", "Melon"},
    {"000000001008", "Apricot"},
    {"000000001009", "Strawberry"},
    {"000000001010", "Cherry"},
    {"000000001011", "Banana"},
    {"000000001012", "Grapefruit"},
    {NULL, NULL}
};

// Load sample data and add them to the list resource.
// Use userParam1 and userParam2 to save the pointer to allocated memory.
void loadSampleItems(LIST_HANDLE hList)
{
    int length;
    char *buffCode;
    char *buffName;
    AL_ListItem item;
    struct SampleData *p;
    char textBuff[21+1];

    p = Samples;
    while(p->code){
        sprintf(textBuff, "%-12.12s: %-7.7s", p->code, p->name); //Format list data in 21 digits for display data.
        item.listText = textBuff;
        item.paletteIndex = PX_BASE;
        item.showControl = AM_NO_CONTROL;

        //Allocate buffer for code
        length = strlen(p->code);
        buffCode = malloc(length+1);
        if (!buffCode){
            break;
        }
        strcpy(buffCode, p->code);
        item.userParam1 = (int)buffCode; //userParam1 for the code

        //Allocate buffer for name
        length = strlen(p->name);
        buffName= malloc(length+1);
        if (!buffName){
            free(buffCode);
            break;
        }
        strcpy(buffName, p->name);
        item.userParam2 = (int)buffName; //userParam2 for the name

        //Add list item
        if (!AL_AddListItem(hList, &item)){
            free(buffCode);
            free(buffName);
            break;
        }
        p++;
    }
}

void releaseSampleItems(LIST_HANDLE hList)
{
    int i;
    int numItems;
    AL_ListItemData itemData;

    numItems = AL_GetLines(hList);
    for (i = 0; i < numItems; i++){
        if(!AL_GetListItemData(hList, i, &itemData)){
            break;
        }
        free((void *)itemData.listItem.userParam1);
        free((void *)itemData.listItem.userParam2);
    }
}



Last updated: 2020/10/12