Menu
This document describes the changes you must make to add a menu to your project.
Changes to myio.h
/*** MENU.C ***/ typedef struct _tagMENUITEM { char *Selection, *Description; int HotKey, ID; int (*Func2Call)(int); struct _tagMENUITEM *Menu2Call; } MENUITEM; void DrawMenu( MENUITEM MenuList[], int Row, int Col, int RegColor, int HiColor, int Item ); void ClearMenu( MENUITEM MenuList[], int Row, int Col, int Back ); int Menu( MENUITEM MenuList[], int Row, int Col, int RegColor, int HiColor, int Back, int AllowEsc );
MENU.C
The following represents the MENU.C module, it should be added
to your poroject:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <dos.h> #include "myio.h" void DrawMenu( MENUITEM MenuList[], int Row, int Col, int RegColor, int HiColor, int Item ) { unsigned int i, Width, Height; for( Width=Height=0;MenuList[Height].Selection; Height++ ) Width=max(Width, strlen(MenuList[Height].Selection) ); Box( Row, Col, Height+1, Width+3, RegColor ); for(i=0; MenuList[i].Selection; i++ ) { Locate( Row+i+1, Col+2 ); CPrintStr(MenuList[i].Selection, i!=(unsigned)Item?RegColor:HiColor ); } } void ClearMenu( MENUITEM MenuList[], int Row, int Col, int Back ) { unsigned int Width, Height; for( Width=Height=0;MenuList[Height].Selection; Height++ ) Width=max(Width, strlen(MenuList[Height].Selection) ); ClearBox( Row, Col, Height+1, Width+3, Back ); } int Menu( MENUITEM MenuList[], int Row, int Col, int RegColor, int HiColor, int Back, int AllowEsc ) { int Ch, CurItem=0, LastItem, i; for(LastItem=0; MenuList[LastItem].Selection; LastItem++) ; DrawMenu( MenuList, Row, Col, RegColor, HiColor, CurItem ); StatusMsg( MenuList[CurItem].Description ); HideCursor(); while ( 1 ) { if( (Ch=GetKey())==ESC && AllowEsc ) break; if( Ch==ENTER ) { if( MenuList[CurItem].Func2Call!=NULL ) { if( (*MenuList[CurItem].Func2Call)(MenuList[CurItem].ID)==0 ) break; DrawMenu( MenuList, Row, Col, RegColor, HiColor, CurItem ); StatusMsg( MenuList[CurItem].Description ); } else if( MenuList[CurItem].Menu2Call!=NULL ) { Menu( MenuList[CurItem].Menu2Call, Row+CurItem+1, Col+1, RegColor, HiColor, Back, 1 ); DrawMenu( MenuList, Row, Col, RegColor, HiColor, CurItem ); StatusMsg( MenuList[CurItem].Description ); } else break; HideCursor(); } else { Locate( Row+CurItem+1, Col+2 ); CPrintStr(MenuList[CurItem].Selection, RegColor ); HideCursor(); if( (Ch==CURUP && CurItem-- == 0) || Ch==END ) CurItem=LastItem-1; else if( (Ch==CURDN && ++CurItem == LastItem) || Ch == HOME ) CurItem=0; else { if( Ch>='a' && Ch<='z' ) Ch=toupper(Ch); for(i=0; MenuList[i].Selection && MenuList[i].HotKey!=Ch; i++) ; if( MenuList[i].Selection ) CurItem=i; } Locate( Row+CurItem+1, Col+2 ); CPrintStr(MenuList[CurItem].Selection, HiColor ); StatusMsg( MenuList[CurItem].Description ); HideCursor(); } } ClearMenu( MenuList, Row, Col, Back ); return( Ch ); }