90846db4ba41665a8572ada2ed51c6e18eded7a3
[reactos.git] / freeldr / freeldr / menu.c
1 /*
2 * FreeLoader
3 * Copyright (C) 1999, 2000 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "freeldr.h"
21 #include "stdlib.h"
22 #include "tui.h"
23 #include "menu.h"
24 #include "options.h"
25
26 static int nOSListBoxLeft;
27 static int nOSListBoxRight;
28 static int nOSListBoxTop;
29 static int nOSListBoxBottom;
30
31 static int nOSSelected = 0; // Currently selected OS (zero based)
32
33 int RunMenu(void)
34 {
35 int key;
36 int second;
37 BOOL bDone = FALSE;
38
39 if (nTimeOut > 0)
40 nTimeOut++; // Increment the timeout since 0 doesn't count for a second
41
42 // Initialise the menu
43 InitMenu();
44
45 // Update the menu
46 DrawMenu();
47
48 second = getsecond();
49
50 // Loop
51 do
52 {
53 // Check for a keypress
54 if (kbhit())
55 {
56 // Cancel the timeout
57 if (nTimeOut != -1)
58 {
59 nTimeOut = -1;
60 DrawMenu();
61 }
62
63 // Get the key
64 key = getch();
65
66 // Is it extended?
67 if (key == 0)
68 key = getch(); // Yes - so get the extended key
69
70 // Process the key
71 switch (key)
72 {
73 case KEY_UP:
74 if (nOSSelected)
75 {
76 nOSSelected--;
77
78 // Update the menu
79 DrawMenu();
80 }
81 break;
82 case KEY_DOWN:
83 if (nOSSelected < (nNumOS-1))
84 {
85 nOSSelected++;
86
87 // Update the menu
88 DrawMenu();
89 }
90 break;
91 case KEY_ENTER:
92 bDone = TRUE;
93 break;
94 case KEY_F8:
95 DoOptionsMenu();
96 DrawBackdrop();
97 DrawMenu();
98 break;
99 }
100 }
101
102 // Update the date & time
103 UpdateDateTime();
104
105 if (nTimeOut > 0)
106 {
107 if (getsecond() != second)
108 {
109 second = getsecond();
110 nTimeOut--;
111
112 // Update the menu
113 DrawMenu();
114 }
115 }
116
117 if (nTimeOut == 0)
118 bDone = TRUE;
119 }
120 while (!bDone);
121
122 return nOSSelected;
123 }
124
125 void InitMenu(void)
126 {
127 int i;
128 int height = 1; // Allow room for top & bottom borders
129 int width = 0;
130
131 for(i=0; i<nNumOS; i++)
132 {
133 height++;
134 if(strlen(OSList[i].name) > width)
135 width = strlen(OSList[i].name);
136 }
137 width += 18; // Allow room for left & right borders, plus 8 spaces on each side
138
139 // Calculate the OS list box area
140 nOSListBoxLeft = (nScreenWidth - width) / 2;
141 nOSListBoxRight = nOSListBoxLeft + width;
142 nOSListBoxTop = (nScreenHeight - height) / 2 + 1;
143 nOSListBoxBottom = nOSListBoxTop + height;
144 }
145
146 void DrawMenu(void)
147 {
148 int i, j;
149 char text[260];
150 char temp[260];
151 int space, space_left, space_right;
152
153 // Update the status bar
154 DrawStatusText(" Use \x18\x19 to select, ENTER to boot. Press F8 for advanced options.");
155
156 DrawBox(nOSListBoxLeft, nOSListBoxTop, nOSListBoxRight, nOSListBoxBottom, D_VERT, D_HORZ, TRUE, TRUE, ATTR(cMenuFgColor, cMenuBgColor));
157
158 for(i=0; i<nNumOS; i++)
159 {
160 space = (nOSListBoxRight - nOSListBoxLeft - 2) - strlen(OSList[i].name);
161 space_left = (space / 2) + 1;
162 space_right = (space - space_left) + 1;
163
164 text[0] = '\0';
165 for(j=0; j<space_left; j++)
166 strcat(text, " ");
167 strcat(text, OSList[i].name);
168 for(j=0; j<space_right; j++)
169 strcat(text, " ");
170
171 if(i == nOSSelected)
172 {
173 DrawText(nOSListBoxLeft+1, nOSListBoxTop+1+i, text, ATTR(cSelectedTextColor, cSelectedTextBgColor));
174 }
175 else
176 {
177 DrawText(nOSListBoxLeft+1, nOSListBoxTop+1+i, text, ATTR(cTextColor, cMenuBgColor));
178 }
179 }
180
181 if (nTimeOut >= 0)
182 {
183 strcpy(text, "[ Time Remaining: ");
184 itoa(nTimeOut, temp, 10);
185 strcat(text, temp);
186 strcat(text, " ]");
187
188 DrawText(nOSListBoxRight - strlen(text) - 1, nOSListBoxBottom, text, ATTR(cMenuFgColor, cMenuBgColor));
189 }
190 }