Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

sys/curses/menu.c

Go to the documentation of this file.
00001 #include "crogue.h"
00002 
00003 static sint UI_Menu_Hotkey(char key, int numchoices, void (*getchoice)(int N, char *buf));
00004 
00005 /*
00006 UI_Menu_Pick parameters:
00007 pos: top-left corner of the window
00008 size: distance from top-left corner to bottom-left corner
00009 numchoices: number of entries on the menu
00010 getchoice: pointer to function taking int N and returning Nth entry on the list
00011 viewonly: zero for normal menu, non-zero for a scrolling text display with no selection
00012 
00013 Return codes:
00014 -2 Error
00015 -1 User aborted (no selection)
00016 [0, numchoices): user selection #
00017 */
00018 
00019 static int offset=0;
00020 static int persistence=0;
00021 
00022 //{{{
00023 void UI_Menu_Set_Persist(ushort p)
00024 {
00025     persistence = p;
00026 #ifdef IS_CALCULATOR
00027     selected = 0;
00028 #endif
00029     offset = 0;
00030 }
00031 //}}}
00032 //{{{
00033 static sint UI_Menu_Hotkey(char key, int numchoices, void (*getchoice)(int N, char *buf))
00034 {
00035     char buf[256];
00036     int i;
00037     
00038     for(i=0; i<numchoices; i++)
00039     {
00040         getchoice(i, buf);
00041         if(key == buf[0])
00042             return i;
00043     }
00044     return -1;
00045 }
00046 //}}}
00047 
00048 //{{{
00049 static void UI_Menu_Redraw( coord pos, coord size, int numchoices, void (*getchoice)(int N, char *buf), int scrollpos )
00050 {
00051     int i;
00052     char outbuf[256];
00053     char buf[256];
00054     
00055     // Draw border
00056     mvaddch(pos.y,          pos.x,          '+');
00057     mvaddch(pos.y,          pos.x+size.x-1, '+');
00058     mvaddch(pos.y+size.y-1, pos.x+size.x-1, '+');
00059     mvaddch(pos.y+size.y-1, pos.x,          '+');
00060     for(i=pos.x+1; i<pos.x+size.x-1; i++) {
00061         mvaddch(pos.y, i, '-');
00062         mvaddch(pos.y+size.y-1, i, '-');
00063     }
00064     for(i=pos.y+1; i<pos.y+size.y-1; i++) {
00065         mvaddch(i, pos.x, '|');
00066         mvaddch(i, pos.x+size.x-1, '|');
00067     }
00068     
00069     // Clear interior
00070     for(i=0; i<size.x-2; i++)
00071         outbuf[i] = ' ';
00072     outbuf[i] = '\0';
00073     for(i=pos.y+1; i<pos.y+size.y-1; i++)
00074         mvaddstr(i, pos.x+1, outbuf);
00075     
00076     // Draw contents
00077     for(i=0; i<size.y-2; i++) {
00078         if(i+scrollpos < numchoices) {
00079             getchoice(i+scrollpos, buf);
00080             if(buf[0]=='\n')
00081                 sprintf(outbuf, " %s", buf+1);
00082             else
00083                 sprintf(outbuf, " %c - %s", i+'a', buf);
00084             draw_string(outbuf, NULL, pos.x+1, i+pos.y+1, SCREEN_WIDTH,
00085                 FontHeight(), 0);
00086         }
00087     }
00088 }
00089 //}}}
00090 //{{{
00091 int UI_Menu_Pick( rect r, int numchoices, void (*getchoice)(int N, char *buf), int viewonly )
00092 {
00093     static int offset=0;
00094     int in;
00095     coord pos = r.topleft;
00096     coord size = r.extent;
00097     
00098     if(!persistence) {
00099         offset= 0;
00100     } else {
00101         if(offset>=numchoices)
00102             offset=0;
00103     }
00104     
00105     do
00106     {
00107         UI_Menu_Redraw(pos, size, numchoices, getchoice, offset);
00108         
00109         in = read_char();
00110         
00111         if(isdigit(in))
00112             return UI_Menu_Hotkey(in, numchoices, getchoice);
00113         else if(in==KEY_ESC)
00114             return -1;
00115         else if(in==' ' || in=='\n' || in=='>') {
00116             offset += r.extent.y - 2;;
00117             if(offset>=numchoices)
00118                 return -1;
00119         }
00120         else if(in=='<') {
00121             offset -= r.extent.y - 2;
00122             if(offset<0) offset=0;
00123         }
00124         else if(islower(in)) {
00125             if(offset+in-'a' < 0 || offset+in-'a' >= numchoices)
00126                 continue;
00127             return offset+in-'a';
00128         }
00129     }
00130     while(1);
00131 }
00132 //}}}
00133 

Generated on Thu May 20 13:12:12 2004 for CalcRogue by doxygen 1.3.6