menus

sariputra
28 de Junio del 2004
Saludos. Busco un codigo que sea un menu con opciones, por el cual me pueda
mover mediante el tabulador y seleccionar con enter. Algo así como el menu del
programa pine (linux).
¿Alguien tiene a mano un ejemplo para ayudarme?
Muchas gracias.

noel solw
28 de Junio del 2004
Espero que este programa te sea util:

// program Menu.cpp

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <conio.h>

const int LEN = 44;
const int MEDIUM = LEN/2;
const int EXIT = -1;
const int ENTER = 'r';
const int ESC = '33';
const int FRA = LIGHTCYAN;
const int LGO = LIGHTGREEN;
const int RND = LIGHTMAGENTA;
const int MAN = LIGHTMAGENTA;
const int JUL = YELLOW;
const int SPR = YELLOW;
const int FIRST = 5;
const int SECOND = 43;
const int FROW = 2;

struct parameters
{
char *program,
*description;
int x,
y,
color;
};

const parameters p[LEN] = // ‰˜ˆŽ˜”„ Š˜’Ž

{{"Bmange", "BlankMange Curve", FIRST , FROW+0 , FRA}, // 1 //
{"Evolute", "Evolute Spiral", FIRST , FROW+1 , SPR}, // 2 //
{"Archim", "Spiral of Archimedes", FIRST , FROW+2 , SPR}, // 3 //
{"Log", "Logarithmic Spiral", FIRST , FROW+3 , SPR}, // 4 //
{"HFractal", "H Fractal", FIRST , FROW+4 , FRA}, // 5 //
{"Cantor", "Cantor's Comb", FIRST , FROW+5 , FRA}, // 6 //
{"BiTree", "Binary Tree - 90ø", FIRST , FROW+6 , FRA}, // 7 //
{"BTree", "Binary Tree - 45ø", FIRST , FROW+7 , FRA}, // 8 //
{"TerTree", "Ternary Tree", FIRST , FROW+8 , FRA}, // 9 //
{"Sierp", "Sierpinski's Sieve", FIRST , FROW+9, FRA}, // 10 //
{"Koch", "Koch Fractal", FIRST , FROW+10, FRA}, // 11 //
{"KochIsla", "Koch Island", FIRST , FROW+12, FRA}, // 12 //
{"KochIn", "Koch Inside Out", FIRST , FROW+13, FRA}, // 13 //
{"Mink", "Minkovski Sausage", FIRST , FROW+14, FRA}, // 14 //
{"MinkIsla", "Minkovski Island", FIRST , FROW+15, FRA}, // 15 //
{"Dragon", "Dragon Curve", FIRST , FROW+16, FRA}, // 16 //
{"Pascal", "Pascal's Triangle", FIRST , FROW+17, FRA}, // 17 //
{"Star", "Star Fractal", FIRST , FROW+18, FRA}, // 18 //
{"MSet", "Mandelbrot Set", FIRST , FROW+19, MAN}, // 19 //
{"Sinus", "Julia Sinus", FIRST , FROW+20, JUL}, // 20 //
{"Cosinus", "Julia Cosinus", FIRST, FROW+21, JUL}, // 21 //
{"Rabbit", "Julia Rabbit Fractal", FIRST, FROW+22, JUL}, // 22 //
{"Drago", "Julia Dragon", SECOND, FROW+0, JUL}, // 23 //
{"Cloud", "Julia Cloud Fractal", SECOND, FROW+1, JUL}, // 24 //
{"Torn", "Torn Square Curve", SECOND, FROW+2, LGO}, // 25 //
{"MandTree", "Mandelbrot Tree", SECOND, FROW+3, LGO}, // 26 //
{"Lung", "Lung Curve", SECOND, FROW+4, LGO}, // 27 //
{"Levy", "Levy's Fractal - 'C'", SECOND, FROW+5, LGO}, // 28 //
{"LevyTap", "Levy's Tapestry", SECOND, FROW+6, LGO}, // 29 //
{"IceSqr", "Ice Square Curve", SECOND, FROW+7, LGO}, // 30 //
{"SiFill", "Sierpinski's Filling", SECOND, FROW+8, LGO}, // 31 //
{"Hilbert", "Hilbert's Filling", SECOND, FROW+9, LGO}, // 32 //
{"Square", "Square Star Fractal", SECOND, FROW+10, LGO}, // 33 //
{"PytaTree", "Pythagoras Tree", SECOND, FROW+12, LGO}, // 34 //
{"LopSided", "Lopsided Tree", SECOND, FROW+13, LGO}, // 35 //
{"Alternat", "Alternating Tree", SECOND, FROW+14, LGO}, // 36 //
{"MathTree", "Mathematical Tree", SECOND, FROW+15, LGO}, // 37 //
{"RandTree", "Random Tree", SECOND, FROW+16, LGO}, // 38 //
{"TriTree", "Triangular Tree", SECOND, FROW+17, LGO}, // 39 //
{"LeafTree", "Leaf Like Tree", SECOND, FROW+18, LGO}, // 40 //
{"RndSierp", "Randomal Sierpinski", SECOND, FROW+19, RND}, // 41 //
{"Carpet", "Sierpinski's Carpet", SECOND, FROW+20, RND}, // 42 //
{"Bifur", "Bifurcation Diagram", SECOND, FROW+21, RND}, // 43 //
{"Forest", "Random Trees Forest", SECOND, FROW+22, RND}}; // 44 //


void WriteLn(int i, int sw)
{
gotoxy(p[i].x, p[i].y);
if (sw)
{
textcolor(LIGHTRED);
textbackground(BLACK);
cprintf("%2d", i+1);
textcolor(p[i].color);
cprintf("%10s", p[i].program);
textcolor(LIGHTGRAY);
cprintf("%22s", p[i].description);
}
else
{
textcolor(WHITE);
textbackground(RED);
cprintf("%2d", i+1);
cprintf("%10s", p[i].program);
cprintf("%22s", p[i].description);
}
}

void DisplayMenu()
{
clrscr();
textbackground(LIGHTGRAY);
for (int i = 0; i < 2000; i++)
putch(' ');
textcolor(BLACK);
gotoxy(5,1);
cputs("Fractal's Programs Menu:");
gotoxy(5,25);
cputs("select with arrows keys and press enter -- press 'Esc' to exit");
for (i = 0; i < LEN; i++)
WriteLn(i, 1);
}

int GetChoice(int k)
{
char key = '*';
int now = k,
old = k;

WriteLn(old, 0);
for(;(key != ENTER) && (key != ESC);)
{

key = getch();
switch(key)
{
case ESC : now = EXIT; // escape
break;
case '6' : // arrow rigth
case 'M' : if (old < MEDIUM)
now = old + MEDIUM;
else
now = old - MEDIUM;
break;
case '2' : // arrow down
case 'P' : if (old == MEDIUM-1)
now = 0;
else if (old == LEN-1)
now = MEDIUM;
else
now = old + 1;
break;
case '4' : // arrow left
case 'K' : if (old < MEDIUM)
now = old + MEDIUM;
else
now = old - MEDIUM;
break;
case '8' : // arrow up
case 'H' : if (old == 0)
now = MEDIUM-1;
else if (old == MEDIUM)
now = LEN-1;
else
now = old - 1;
break;
}
if ((old != now) && (now != EXIT))
{
WriteLn(old, 1);
WriteLn(now, 0);
old = now;
}
}
return now;
}

void main()
{
int choice = 0;
_setcursortype(_NOCURSOR);
for(;choice != EXIT;)
{
DisplayMenu();
choice = GetChoice(choice);
if (choice != EXIT)
{
textcolor(WHITE);
textbackground(BLACK);
clrscr();
printf("elegiste el programa %s.cpp n", p[choice].program);
getch();
}
}
textcolor(WHITE);
textbackground(BLACK);
_setcursortype(_NORMALCURSOR);
clrscr();
gotoxy(20,10);
cputs("end of Menu Program - goob bye ! ! !nnn");
}

sariputra
28 de Junio del 2004
Muchisimas gracias. El Dev C++ me da algunos errores. En el c++ builder 6, no me da errores, pero tengo problemas con la ejecución desde c++ builder de codigos.c ( se cierran al ejecutarlos y ademas no genera el ejecutable(no me aclaro)).
En linux he tratado, pero me da error la liberia conio.h , he estado leyendo y creo q no existe esa liberia para linux (corrigeme si me equivoco) . Muchas gracias por tu ayuda

juan esteban ospina
28 de Junio del 2004
Hola, saludos.
Yo tengo un código, que tiene un menú, pero no utiliza la tecla tab sino las flechas cursor.
Si te interesa escríbeme.