Anders Hörnfeldt
/
coniolib
Port of Conio.h
lib/ui.c@0:6c1bc9b3a347, 2010-04-19 (annotated)
- Committer:
- hornfeldt
- Date:
- Mon Apr 19 20:20:10 2010 +0000
- Revision:
- 0:6c1bc9b3a347
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hornfeldt | 0:6c1bc9b3a347 | 1 | /* |
hornfeldt | 0:6c1bc9b3a347 | 2 | ui.c |
hornfeldt | 0:6c1bc9b3a347 | 3 | User interface functions. |
hornfeldt | 0:6c1bc9b3a347 | 4 | Part of MicroVGA CONIO library / demo project |
hornfeldt | 0:6c1bc9b3a347 | 5 | Copyright (c) 2008-9 SECONS s.r.o., http://www.MicroVGA.com |
hornfeldt | 0:6c1bc9b3a347 | 6 | |
hornfeldt | 0:6c1bc9b3a347 | 7 | This program is free software: you can redistribute it and/or modify |
hornfeldt | 0:6c1bc9b3a347 | 8 | it under the terms of the GNU General Public License as published by |
hornfeldt | 0:6c1bc9b3a347 | 9 | the Free Software Foundation, either version 3 of the License, or |
hornfeldt | 0:6c1bc9b3a347 | 10 | (at your option) any later version. |
hornfeldt | 0:6c1bc9b3a347 | 11 | |
hornfeldt | 0:6c1bc9b3a347 | 12 | This program is distributed in the hope that it will be useful, |
hornfeldt | 0:6c1bc9b3a347 | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hornfeldt | 0:6c1bc9b3a347 | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hornfeldt | 0:6c1bc9b3a347 | 15 | GNU General Public License for more details. |
hornfeldt | 0:6c1bc9b3a347 | 16 | |
hornfeldt | 0:6c1bc9b3a347 | 17 | You should have received a copy of the GNU General Public License |
hornfeldt | 0:6c1bc9b3a347 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
hornfeldt | 0:6c1bc9b3a347 | 19 | */ |
hornfeldt | 0:6c1bc9b3a347 | 20 | |
hornfeldt | 0:6c1bc9b3a347 | 21 | #include "conio.h" |
hornfeldt | 0:6c1bc9b3a347 | 22 | #include "ui.h" |
hornfeldt | 0:6c1bc9b3a347 | 23 | #include "kbd.h" |
hornfeldt | 0:6c1bc9b3a347 | 24 | |
hornfeldt | 0:6c1bc9b3a347 | 25 | |
hornfeldt | 0:6c1bc9b3a347 | 26 | int runmenu(char x, char y, ROMDEF char *menu[], int defaultitem) |
hornfeldt | 0:6c1bc9b3a347 | 27 | { |
hornfeldt | 0:6c1bc9b3a347 | 28 | int keys[3], i,j, itemno; |
hornfeldt | 0:6c1bc9b3a347 | 29 | int nitems, width; |
hornfeldt | 0:6c1bc9b3a347 | 30 | ROMDEF char *s; |
hornfeldt | 0:6c1bc9b3a347 | 31 | |
hornfeldt | 0:6c1bc9b3a347 | 32 | itemno = defaultitem-1; |
hornfeldt | 0:6c1bc9b3a347 | 33 | width = 20; |
hornfeldt | 0:6c1bc9b3a347 | 34 | |
hornfeldt | 0:6c1bc9b3a347 | 35 | width = 10; |
hornfeldt | 0:6c1bc9b3a347 | 36 | nitems = 0; |
hornfeldt | 0:6c1bc9b3a347 | 37 | while (menu[nitems] != 0) { |
hornfeldt | 0:6c1bc9b3a347 | 38 | for (j=0;menu[nitems][j];j++); |
hornfeldt | 0:6c1bc9b3a347 | 39 | if (j>width) |
hornfeldt | 0:6c1bc9b3a347 | 40 | width = j; |
hornfeldt | 0:6c1bc9b3a347 | 41 | nitems++; |
hornfeldt | 0:6c1bc9b3a347 | 42 | } |
hornfeldt | 0:6c1bc9b3a347 | 43 | width+=2; |
hornfeldt | 0:6c1bc9b3a347 | 44 | |
hornfeldt | 0:6c1bc9b3a347 | 45 | if (itemno < 0 || itemno > nitems) |
hornfeldt | 0:6c1bc9b3a347 | 46 | itemno = 0; |
hornfeldt | 0:6c1bc9b3a347 | 47 | |
hornfeldt | 0:6c1bc9b3a347 | 48 | while (1) { |
hornfeldt | 0:6c1bc9b3a347 | 49 | |
hornfeldt | 0:6c1bc9b3a347 | 50 | |
hornfeldt | 0:6c1bc9b3a347 | 51 | cursoroff(); |
hornfeldt | 0:6c1bc9b3a347 | 52 | textattr(CYAN<<4 | BLACK); |
hornfeldt | 0:6c1bc9b3a347 | 53 | gotoxy(x,y); |
hornfeldt | 0:6c1bc9b3a347 | 54 | _putch(ACS_ULCORNER); |
hornfeldt | 0:6c1bc9b3a347 | 55 | for (i=0;i<width+2;i++) |
hornfeldt | 0:6c1bc9b3a347 | 56 | _putch(ACS_HLINE); |
hornfeldt | 0:6c1bc9b3a347 | 57 | _putch(ACS_URCORNER); |
hornfeldt | 0:6c1bc9b3a347 | 58 | |
hornfeldt | 0:6c1bc9b3a347 | 59 | for (i = 0;i<nitems;i++) { |
hornfeldt | 0:6c1bc9b3a347 | 60 | gotoxy(x,y+i+1); |
hornfeldt | 0:6c1bc9b3a347 | 61 | _putch(ACS_VLINE); |
hornfeldt | 0:6c1bc9b3a347 | 62 | _putch(' '); |
hornfeldt | 0:6c1bc9b3a347 | 63 | if (i == itemno) |
hornfeldt | 0:6c1bc9b3a347 | 64 | textattr(YELLOW); |
hornfeldt | 0:6c1bc9b3a347 | 65 | s = 0; |
hornfeldt | 0:6c1bc9b3a347 | 66 | for (j=0;j<width;j++) { |
hornfeldt | 0:6c1bc9b3a347 | 67 | if (s && *s) |
hornfeldt | 0:6c1bc9b3a347 | 68 | _putch(*s++); |
hornfeldt | 0:6c1bc9b3a347 | 69 | else _putch(' '); |
hornfeldt | 0:6c1bc9b3a347 | 70 | if (s == 0) |
hornfeldt | 0:6c1bc9b3a347 | 71 | s = (ROMDEF char *)menu[i]; |
hornfeldt | 0:6c1bc9b3a347 | 72 | } |
hornfeldt | 0:6c1bc9b3a347 | 73 | textattr(CYAN<<4 | BLACK); |
hornfeldt | 0:6c1bc9b3a347 | 74 | _putch(' '); |
hornfeldt | 0:6c1bc9b3a347 | 75 | _putch(ACS_VLINE); |
hornfeldt | 0:6c1bc9b3a347 | 76 | } |
hornfeldt | 0:6c1bc9b3a347 | 77 | |
hornfeldt | 0:6c1bc9b3a347 | 78 | gotoxy(x,y+nitems+1); |
hornfeldt | 0:6c1bc9b3a347 | 79 | _putch(ACS_LLCORNER); |
hornfeldt | 0:6c1bc9b3a347 | 80 | for (i=0;i<width+2;i++) |
hornfeldt | 0:6c1bc9b3a347 | 81 | _putch(ACS_HLINE); |
hornfeldt | 0:6c1bc9b3a347 | 82 | _putch(ACS_LRCORNER); |
hornfeldt | 0:6c1bc9b3a347 | 83 | |
hornfeldt | 0:6c1bc9b3a347 | 84 | |
hornfeldt | 0:6c1bc9b3a347 | 85 | while (!_kbhit()) ; |
hornfeldt | 0:6c1bc9b3a347 | 86 | |
hornfeldt | 0:6c1bc9b3a347 | 87 | if (_kbhit()) { |
hornfeldt | 0:6c1bc9b3a347 | 88 | keys[0] = keys[1]; |
hornfeldt | 0:6c1bc9b3a347 | 89 | keys[1] = keys[2]; |
hornfeldt | 0:6c1bc9b3a347 | 90 | keys[2] = _getch(); |
hornfeldt | 0:6c1bc9b3a347 | 91 | if (keys[0] == 0x1b && keys[1] == 0x5b) |
hornfeldt | 0:6c1bc9b3a347 | 92 | { |
hornfeldt | 0:6c1bc9b3a347 | 93 | switch (keys[2]) { |
hornfeldt | 0:6c1bc9b3a347 | 94 | case 'A': if (itemno>0) itemno--; else itemno = nitems-1; break; |
hornfeldt | 0:6c1bc9b3a347 | 95 | case 'B': itemno++; itemno %= nitems; break; |
hornfeldt | 0:6c1bc9b3a347 | 96 | } |
hornfeldt | 0:6c1bc9b3a347 | 97 | } |
hornfeldt | 0:6c1bc9b3a347 | 98 | else |
hornfeldt | 0:6c1bc9b3a347 | 99 | switch(keys[0]) { |
hornfeldt | 0:6c1bc9b3a347 | 100 | case KB_ENTER : cursoron(); return itemno+1; |
hornfeldt | 0:6c1bc9b3a347 | 101 | case KB_ESC: cursoron(); return 0; |
hornfeldt | 0:6c1bc9b3a347 | 102 | } |
hornfeldt | 0:6c1bc9b3a347 | 103 | } |
hornfeldt | 0:6c1bc9b3a347 | 104 | } |
hornfeldt | 0:6c1bc9b3a347 | 105 | } |
hornfeldt | 0:6c1bc9b3a347 | 106 | |
hornfeldt | 0:6c1bc9b3a347 | 107 | |
hornfeldt | 0:6c1bc9b3a347 | 108 | |
hornfeldt | 0:6c1bc9b3a347 | 109 | void drawfkeys(ROMDEF char *fkeys[]) |
hornfeldt | 0:6c1bc9b3a347 | 110 | { |
hornfeldt | 0:6c1bc9b3a347 | 111 | ROMDEF char *s; |
hornfeldt | 0:6c1bc9b3a347 | 112 | int i, j; |
hornfeldt | 0:6c1bc9b3a347 | 113 | |
hornfeldt | 0:6c1bc9b3a347 | 114 | gotoxy(1,25); |
hornfeldt | 0:6c1bc9b3a347 | 115 | for (i=0;i<10;i++) { |
hornfeldt | 0:6c1bc9b3a347 | 116 | textcolor(WHITE); |
hornfeldt | 0:6c1bc9b3a347 | 117 | textbackground(BLACK); |
hornfeldt | 0:6c1bc9b3a347 | 118 | if (i!= 0) |
hornfeldt | 0:6c1bc9b3a347 | 119 | _putch(' '); |
hornfeldt | 0:6c1bc9b3a347 | 120 | if (i== 9) { |
hornfeldt | 0:6c1bc9b3a347 | 121 | _putch('1'); |
hornfeldt | 0:6c1bc9b3a347 | 122 | _putch('0'); |
hornfeldt | 0:6c1bc9b3a347 | 123 | } else |
hornfeldt | 0:6c1bc9b3a347 | 124 | _putch((i%10)+'1'); |
hornfeldt | 0:6c1bc9b3a347 | 125 | textcolor(BLACK); |
hornfeldt | 0:6c1bc9b3a347 | 126 | textbackground(CYAN); |
hornfeldt | 0:6c1bc9b3a347 | 127 | |
hornfeldt | 0:6c1bc9b3a347 | 128 | s = fkeys[i] ? fkeys[i] : 0; |
hornfeldt | 0:6c1bc9b3a347 | 129 | for (j=0;j<6;j++) { |
hornfeldt | 0:6c1bc9b3a347 | 130 | if (s && *s) |
hornfeldt | 0:6c1bc9b3a347 | 131 | _putch(*s++); |
hornfeldt | 0:6c1bc9b3a347 | 132 | else _putch(' '); |
hornfeldt | 0:6c1bc9b3a347 | 133 | } |
hornfeldt | 0:6c1bc9b3a347 | 134 | } |
hornfeldt | 0:6c1bc9b3a347 | 135 | } |
hornfeldt | 0:6c1bc9b3a347 | 136 | |
hornfeldt | 0:6c1bc9b3a347 | 137 | void drawframe(int x, int y, int width, int height, int color) |
hornfeldt | 0:6c1bc9b3a347 | 138 | { |
hornfeldt | 0:6c1bc9b3a347 | 139 | int i,j; |
hornfeldt | 0:6c1bc9b3a347 | 140 | |
hornfeldt | 0:6c1bc9b3a347 | 141 | textattr(color); |
hornfeldt | 0:6c1bc9b3a347 | 142 | gotoxy(x,y); |
hornfeldt | 0:6c1bc9b3a347 | 143 | |
hornfeldt | 0:6c1bc9b3a347 | 144 | _putch(ACS_ULCORNER); |
hornfeldt | 0:6c1bc9b3a347 | 145 | for (i=0;i<width+2;i++) |
hornfeldt | 0:6c1bc9b3a347 | 146 | _putch(ACS_HLINE); |
hornfeldt | 0:6c1bc9b3a347 | 147 | _putch(ACS_URCORNER); |
hornfeldt | 0:6c1bc9b3a347 | 148 | |
hornfeldt | 0:6c1bc9b3a347 | 149 | for (i = 0;i<height;i++) { |
hornfeldt | 0:6c1bc9b3a347 | 150 | gotoxy(x,y+i+1); |
hornfeldt | 0:6c1bc9b3a347 | 151 | _putch(ACS_VLINE); |
hornfeldt | 0:6c1bc9b3a347 | 152 | _putch(' '); |
hornfeldt | 0:6c1bc9b3a347 | 153 | |
hornfeldt | 0:6c1bc9b3a347 | 154 | for (j=0;j<width;j++) { |
hornfeldt | 0:6c1bc9b3a347 | 155 | _putch(' '); |
hornfeldt | 0:6c1bc9b3a347 | 156 | } |
hornfeldt | 0:6c1bc9b3a347 | 157 | _putch(' '); |
hornfeldt | 0:6c1bc9b3a347 | 158 | _putch(ACS_VLINE); |
hornfeldt | 0:6c1bc9b3a347 | 159 | } |
hornfeldt | 0:6c1bc9b3a347 | 160 | |
hornfeldt | 0:6c1bc9b3a347 | 161 | gotoxy(x,y+height+1); |
hornfeldt | 0:6c1bc9b3a347 | 162 | _putch(ACS_LLCORNER); |
hornfeldt | 0:6c1bc9b3a347 | 163 | for (i=0;i<width+2;i++) |
hornfeldt | 0:6c1bc9b3a347 | 164 | _putch(ACS_HLINE); |
hornfeldt | 0:6c1bc9b3a347 | 165 | _putch(ACS_LRCORNER); |
hornfeldt | 0:6c1bc9b3a347 | 166 | } |