C Language Development Kit/Snippets/Menu/ShowTextMenu

From OptiWiki

Jump to: navigation, search
Description Shows a text style menu that adjusts its layout depending on the terminal type and the fontsize.
Syntax
void ShowTextMenu( int *nIndex, const char *txtHeader, stxtMenu *menuItems, int nMax, int nOptions );
Arguments
int *nIndex

Holds the index number of the currently selected menu item.

const char *txtHeader

Contains the text of the header above the menu. stxtMenu *menuItems menuItems is a pointer to a structure that holds information about a menu item that is displayed in the menu. Besides the line of text of the menu item, this structure also contains a pointer to the function that needs to be executed when the menu item is selected.

typedef struct
{
    char text;                // the displayed line of text of the menu item
    void (*funcitem)(void);   // pointer to the function that is executed on selection.
                              // the function must have the "void function( void )" format
                              // if the function pointer is set to NULL, then this
}stxtMenu;                    // menu function returns on selection of this item.
int nMax

The number of menuItems that need to be displayed.

int nOptions

nOptions can be one of the following 2 values:

MENU_FIXED Scrolling is possible between the first and last menu item.
MENU_LOOP Scrolling will loop when the first and last menu item are exceeded
Returns None
Remarks An additional menu line will become available if the txtHeader is filled with an empty string (NULL).

Filling a menuItems[].funcitem with NULL causes the ShowTextMenu() function to return (exit) when this menu item is selected. (Especially usefull for terminals that have no CLR_KEY)

Important note when using the PHLxxxx:
Do not declare constant strings directly inside a structure, because the Microtec compiler will place such constant strings in RAM-memory instead of in ROM-memory. This means the string data will be lost as soon as the battery is empty or removed.

Therefore DO NOT declare strings inside a constant structure like in the following example:

const stxtMenu mnuSetFont[] = {
    (char*)"Function 1", function1,    // This implementation causes "Function 1" to be
    (char*)"Function 2", function2     // placed in RAM-memory by the Microtec compiler
};                                     // (no matter what cast you use)!

Use the following implementation instead:

const char mnuTextFnc1[] = "Function 1";
const char mnuTextFnc2[] = "Function 2";
 
const stxtMenu mnuSetFont[] = {
    (char*)mnuTextFnc1, function1,     // This implementation causes "Function 1" to be
    (char*)mnuTextFnc2, function2      // placed in ROM-memory by the Microtec compiler, 
};                                     // so it can't be lost
Example
#include <stdio.h>
#include <stdlib.h>
 
#include "lib.h"
#include "menu.h"     //Holds the menu functions
#include "input.h"    //Holds the input functions
 
const char mnuTextTiny[] = "Set tiny font";
const char mnuTextSmall[] = "Set small font";
const char mnuTextMedium[] = "Set medium font";
const char mnuTextLarge[] = "Set large font";
 
void SetLargeFont( void )
{
    setfont( LARGE_FONT, NULL );
    putchar('\f');
    printf("Large font set\nPress a key");
    WaitForKey();
}
 
void SetMediumFont( void )
{
    putchar('\f');
#if !(PHL|PHL1000)
    if( setfont( MEDIUM_FONT, NULL ) == OK )
        printf("Medium font set\nPress a key");
    else
#endif
        printf("Cannot set\nmedium font\nPress a key");
    WaitForKey();
}
 
void SetSmallFont( void )
{
    setfont( SMALL_FONT, NULL );
    putchar('\f');
    printf("Small font set\nPress a key");
    WaitForKey();
}
 
void SetTinyFont( void )
{
    putchar('\f');
#if !(PHL|PHL1000)
    if( setfont( TINY_FONT, NULL ) == OK )
        printf("Tiny font set\nPress a key");
    else
#endif
        printf("Cannot set\ntiny font\nPress a key");
    WaitForKey();
}
 
// place the menu in the const section 
const stxtMenu mnuSetFont[] = {
    (char*)mnuTextTiny, 	SetTinyFont,
    (char*)mnuTextSmall, 	SetSmallFont,
    (char*)mnuTextMedium,	SetMediumFont,
    (char*)mnuTextLarge,	SetLargeFont
};
 
void main( void )
{
    int nIndex = 0;
 
    for(;;)
    {
        ShowTextMenu( &nIndex,
                      "Font selection",
                      (stxtMenu*)mnuSetFont,
                      sizeof(mnuSetFont) / sizeof(stxtMenu),
                      MENU_FIXED );
 
        if( nIndex == -1 )	// CLR_KEY was pressed
        {
            printf("\fCLR key is\n pressed\n\nPress a key");
            WaitForKey();
            nIndex = 0; // reset selection to the first item
        }
    }
}


Views
Personal tools