1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/*
2008-12-25 : mises au normes C90
2007-05-06 : version initiale
*/
#include <conio2.h>
#include <stdio.h>
#include <string.h>
#define DBG 1
static int count (char const **as, size_t * p_len_max)
{
int n = 0;
size_t len_max = 0;
while (as[n] != NULL)
{
#if DBG
printf ("p=%p\n", as[n]);
#endif
{
size_t len = strlen (as[n]);
if (len > len_max)
{
len_max = len;
}
n++;
}
}
if (p_len_max != NULL)
{
*p_len_max = len_max;
}
return n;
}
static void display_nrm (int x, int y, char const **as, int sel,
size_t len_max)
{
gotoxy (x, y + sel);
textbackground (CYAN);
textcolor (BLACK);
cprintf (" %-*s ", len_max, as[sel]);
}
static void display_sel (int x, int y, char const **as, int sel,
size_t len_max)
{
gotoxy (x, y + sel);
textbackground (RED);
textcolor (WHITE);
cprintf (" %-*s ", len_max, as[sel]);
}
static void display (int x, int y, char const **as, int n, size_t len_max)
{
int i;
for (i = 0; i < n; i++)
{
display_nrm (x, y, as, i, len_max);
}
}
int menu_vert (int x, int y, char const **as)
{
int selection = 0;
size_t len_max;
int selection_max = count (as, &len_max);
int quitter = 0;
#if DBG
printf ("selection_max=%d len_max=%d\n", selection_max, len_max);
#endif
{
struct text_info ti;
gettextinfo (&ti);
display (x, y, as, selection_max, len_max);
display_sel (x, y, as, selection, len_max);
do
{
int c = getch ();
switch (c)
{
case 0:
/* certains caracteres etendus */
break;
case 224:
/* [Windows] d'autres caracteres etendus */
if (kbhit ())
{
int cx = getch ();
switch (cx)
{
case 72: /* UP */
display_nrm (x, y, as, selection, len_max);
if (selection > 0)
{
selection--;
}
else
{
selection = selection_max - 1;
}
display_sel (x, y, as, selection, len_max);
break;
case 80: /* DOWN */
display_nrm (x, y, as, selection, len_max);
if (selection < selection_max - 1)
{
selection++;
}
else
{
selection = 0;
}
display_sel (x, y, as, selection, len_max);
break;
default:
#if DBG
printf ("cx=%d\n", cx);
#else
;
#endif
}
}
break;
case 27:
/* abandon */
quitter = 1;
selection = -1;
break;
case 13:
/* validation */
quitter = 1;
break;
}
}
while (!quitter);
textattr (ti.normattr);
}
return selection + 1;
}
int main (void)
{
char const *as[] = {
"creer",
"modifier",
"supprimmer",
"quitter",
NULL,
};
int choix = menu_vert (1, 1, as);
printf ("choix = %d\n", choix);
return 0;
} |
Partager