2
* Mesa 3-D graphics library
4
* Copyright (C) 1995-2006 Brian Paul
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
16
* You should have received a copy of the GNU Library General Public
17
* License along with this library; if not, write to the Free
18
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
* Library for glut using mesa fbdev driver
24
* Written by Sean D'Epagnier (c) 2006
37
#define MENU_FONT_WIDTH 9
38
#define MENU_FONT_HEIGHT 15
39
#define MENU_FONT GLUT_BITMAP_9_BY_15
40
#define SUBMENU_OFFSET 20
42
struct GlutMenu *Menus;
46
static double MenuProjection[16];
48
static int AttachedMenus[3];
49
static int NumMenus = 1;
50
static int SelectedMenu;
52
void InitializeMenus(void)
54
glPushAttrib(GL_TRANSFORM_BIT);
55
glMatrixMode(GL_PROJECTION);
58
gluOrtho2D(0.0, VarInfo.xres, VarInfo.yres, 0.0);
59
glGetDoublev(GL_PROJECTION_MATRIX, MenuProjection);
69
for(i = 1; i<NumMenus; i++) {
70
for(j = 0; j<Menus[i].NumItems; j++)
71
free(Menus[i].Items[j].name);
78
int TryMenu(int button, int pressed)
80
if(ActiveMenu && !pressed) {
87
if(AttachedMenus[button] && pressed) {
88
ActiveMenu = AttachedMenus[button];
96
static int DrawMenu(int menu, int x, int *y)
101
for(i=0; i < Menus[menu].NumItems; i++) {
102
char *s = Menus[menu].Items[i].name;
104
if(MouseY >= *y && MouseY < *y + MENU_FONT_HEIGHT &&
105
MouseX >= x && MouseX < x + Menus[menu].width) {
109
Menus[menu].selected = i;
114
*y += MENU_FONT_HEIGHT;
115
glRasterPos2i(x, *y);
117
glutBitmapCharacter(MENU_FONT, *s);
119
if(Menus[menu].selected == i)
120
if(Menus[menu].Items[i].submenu)
121
if(DrawMenu(Menus[menu].Items[i].submenu, x
122
+ SUBMENU_OFFSET, y)) {
124
Menus[menu].selected = -1;
138
x = Menus[ActiveMenu].x;
139
y = Menus[ActiveMenu].y;
141
/* save old settings */
142
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT
143
| GL_ENABLE_BIT | GL_VIEWPORT_BIT);
145
glMatrixMode(GL_MODELVIEW);
149
glMatrixMode(GL_PROJECTION);
151
glLoadMatrixd(MenuProjection);
152
glViewport(0, 0, VarInfo.xres, VarInfo.yres);
154
glDisable(GL_DEPTH_TEST);
155
glDisable(GL_ALPHA_TEST);
156
glDisable(GL_LIGHTING);
158
glDisable(GL_TEXTURE_2D);
159
glEnable(GL_COLOR_LOGIC_OP);
160
glLogicOp(GL_AND_REVERSE);
162
if(DrawMenu(ActiveMenu, x, &y))
163
Menus[ActiveMenu].selected = -1;
165
/* restore settings */
167
glMatrixMode(GL_MODELVIEW);
176
MenuStatusFunc(GLUT_MENU_IN_USE, MouseX, MouseY);
178
MenuStateFunc(GLUT_MENU_IN_USE);
179
Menus[ActiveMenu].x = MouseX-Menus[ActiveMenu].width/2;
181
if(Menus[ActiveMenu].x < 0)
182
Menus[ActiveMenu].x = 0;
183
if(Menus[ActiveMenu].x + Menus[ActiveMenu].width >= VarInfo.xres)
184
Menus[ActiveMenu].x = VarInfo.xres - Menus[ActiveMenu].width - 1;
186
Menus[ActiveMenu].y = MouseY-Menus[ActiveMenu].NumItems*MENU_FONT_HEIGHT/2;
187
Menus[ActiveMenu].selected = -1;
193
MenuStatusFunc(GLUT_MENU_NOT_IN_USE, MouseX, MouseY);
195
MenuStateFunc(GLUT_MENU_NOT_IN_USE);
196
if(SelectedMenu > 0) {
197
int selected = Menus[SelectedMenu].selected;
199
if(Menus[SelectedMenu].Items[selected].submenu == 0)
200
Menus[SelectedMenu].func(Menus[SelectedMenu].Items
206
/* glut menu functions */
208
int glutCreateMenu(void (*func)(int value))
210
CurrentMenu = NumMenus;
212
Menus = realloc(Menus, sizeof(*Menus) * NumMenus);
213
Menus[CurrentMenu].NumItems = 0;
214
Menus[CurrentMenu].Items = NULL;
215
Menus[CurrentMenu].func = func;
216
Menus[CurrentMenu].width = 0;
220
void glutSetMenu(int menu)
225
int glutGetMenu(void)
230
void glutDestroyMenu(int menu)
232
if(menu == CurrentMenu)
236
static void NameMenuEntry(int entry, const char *name)
238
int cm = CurrentMenu;
239
if(!(Menus[cm].Items[entry-1].name = realloc(Menus[cm].Items[entry-1].name,
240
strlen(name) + 1))) {
241
sprintf(exiterror, "realloc failed in NameMenuEntry\n");
244
strcpy(Menus[cm].Items[entry-1].name, name);
245
if(strlen(name) * MENU_FONT_WIDTH > Menus[cm].width)
246
Menus[cm].width = strlen(name) * MENU_FONT_WIDTH;
249
static int AddMenuItem(const char *name)
251
int cm = CurrentMenu;
252
int item = Menus[cm].NumItems++;
253
if(!(Menus[cm].Items = realloc(Menus[cm].Items,
254
Menus[cm].NumItems * sizeof(*Menus[0].Items)))) {
255
sprintf(exiterror, "realloc failed in AddMenuItem\n");
258
Menus[cm].Items[item].name = NULL;
259
NameMenuEntry(item+1, name);
263
void glutAddMenuEntry(const char *name, int value)
265
int item = AddMenuItem(name);
266
Menus[CurrentMenu].Items[item].value = value;
267
Menus[CurrentMenu].Items[item].submenu = 0;
270
void glutAddSubMenu(const char *name, int menu)
272
int item = AddMenuItem(name);
273
if(menu == CurrentMenu) {
274
sprintf(exiterror, "Recursive menus not supported\n");
277
Menus[CurrentMenu].Items[item].submenu = menu;
280
void glutChangeToMenuEntry(int entry, const char *name, int value)
282
NameMenuEntry(entry, name);
283
Menus[CurrentMenu].Items[entry-1].value = value;
284
Menus[CurrentMenu].Items[entry-1].submenu = 0;
287
void glutChangeToSubMenu(int entry, const char *name, int menu)
289
NameMenuEntry(entry, name);
290
Menus[CurrentMenu].Items[entry-1].submenu = menu;
293
void glutRemoveMenuItem(int entry)
295
memmove(Menus[CurrentMenu].Items + entry - 1,
296
Menus[CurrentMenu].Items + entry,
297
sizeof(*Menus[0].Items) * (Menus[CurrentMenu].NumItems - entry));
298
Menus[CurrentMenu].NumItems--;
301
void glutAttachMenu(int button)
303
AttachedMenus[button] = CurrentMenu;
306
void glutDetachMenu(int button)
308
AttachedMenus[button] = 0;