1 /***************************************************************************
2 menu.cpp -- menu management
3 -------------------
4 created : Fri Aug 13 22:23:19 CEST 1999
5 copyright : (C) 1999 by Eric Espie
6 email : torcs@free.fr
7 version : $Id: guimenu.cpp,v 1.2.2.2 2008/12/31 03:53:55 berniw Exp $
8 ***************************************************************************/
9
10 /***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
19 /** @file
20 GUI menu management.
21 @author <a href=mailto:torcs@free.fr>Eric Espie</a>
22 @version $Id: guimenu.cpp,v 1.2.2.2 2008/12/31 03:53:55 berniw Exp $
23 @ingroup gui
24 */
25
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #ifdef WIN32
30 #include <windows.h>
31 #endif
32 #include <GL/glut.h>
33
34 #include <tgfclient.h>
35 #include "gui.h"
36 #include "guimenu.h"
37
38 void
gfMenuInit(void)39 gfMenuInit(void)
40 {
41 }
42
43 /** Add the default menu keyboard callback to a screen.
44 The keys are:
45 <br><tt>Up Arrow .... </tt>Select Previous Entry
46 <br><tt>Down Arrow .. </tt>Select Next Entry
47 <br><tt>F1 .......... </tt>Help
48 <br><tt>Tab ......... </tt>Select Next Entry
49 <br><tt>Enter ....... </tt>Perform Action
50 <br><tt>Escape ...... </tt>Quit the menu
51 @ingroup gui
52 @param scr Screen Id
53 */
54 void
GfuiMenuDefaultKeysAdd(void * scr)55 GfuiMenuDefaultKeysAdd(void *scr)
56 {
57 GfuiAddKey(scr, 9, "Select Next Entry", NULL, gfuiSelectNext, NULL);
58 GfuiAddKey(scr, 13, "Perform Action", (void*)2, gfuiMouseAction, NULL);
59 GfuiAddSKey(scr, GLUT_KEY_UP, "Select Previous Entry", NULL, gfuiSelectPrev, NULL);
60 GfuiAddSKey(scr, GLUT_KEY_DOWN, "Select Next Entry", NULL, gfuiSelectNext, NULL);
61 GfuiAddSKey(scr, GLUT_KEY_PAGE_UP, "Select Previous Entry", NULL, gfuiSelectPrev, NULL);
62 GfuiAddSKey(scr, GLUT_KEY_PAGE_DOWN, "Select Next Entry", NULL, gfuiSelectNext, NULL);
63 GfuiAddSKey(scr, GLUT_KEY_F1, "Help", scr, GfuiHelpScreen, NULL);
64 GfuiAddSKey(scr, GLUT_KEY_F12, "Screen-Shot", NULL, GfuiScreenShot, NULL);
65
66 }
67
68 /** Create a new menu screen.
69 Set the title of the menu
70 Add the default keyboard callbacks to the menu.
71 @ingroup gui
72 @param title title of the screen
73 @return Handle of the menu
74 */
75 void *
GfuiMenuScreenCreate(const char * title)76 GfuiMenuScreenCreate(const char *title)
77 {
78 void *scr;
79
80 scr = GfuiScreenCreate();
81 GfuiTitleCreate(scr, title, strlen(title));
82
83 GfuiMenuDefaultKeysAdd(scr);
84
85 return scr;
86 }
87
88 static void
dispInfo(void * cbinfo)89 dispInfo(void *cbinfo)
90 {
91 GfuiVisibilitySet(((tMnuCallbackInfo*)cbinfo)->screen, ((tMnuCallbackInfo*)cbinfo)->labelId, 1);
92 }
93 static void
remInfo(void * cbinfo)94 remInfo(void *cbinfo)
95 {
96 GfuiVisibilitySet(((tMnuCallbackInfo*)cbinfo)->screen, ((tMnuCallbackInfo*)cbinfo)->labelId, 0);
97 }
98
99
100 /** Add a button to a menu screen.
101 @ingroup gui
102 @param scr Screen (menu) handle
103 @param text Text of the button
104 @param tip Text of the tip displayed when the button is focused
105 @param userdata Parameter of the Push function
106 @param onpush Callback when the button is pushed
107 @return Button Id
108 */
109 int
GfuiMenuButtonCreate(void * scr,const char * text,const char * tip,void * userdata,tfuiCallback onpush)110 GfuiMenuButtonCreate(void *scr, const char *text, const char *tip, void *userdata, tfuiCallback onpush)
111 {
112 tMnuCallbackInfo *cbinfo;
113 int xpos, ypos;
114 int nbItems = ((tGfuiScreen*)scr)->nbItems++;
115 int bId;
116
117 if (nbItems < 11) {
118 xpos = 320;
119 ypos = 380 - 30 * nbItems;
120 } else {
121 if (nbItems > 22) {
122 GfTrace("Too many items in that menu !!!\n");
123 return -1;
124 }
125 xpos = 380;
126 ypos = 380 - 30 * (nbItems - 11);
127 }
128
129 cbinfo = (tMnuCallbackInfo*)calloc(1, sizeof(tMnuCallbackInfo));
130 cbinfo->screen = scr;
131 cbinfo->labelId = GfuiTipCreate(scr, tip, strlen(tip));
132
133 GfuiVisibilitySet(scr, cbinfo->labelId, 0);
134
135 bId = GfuiButtonCreate(scr,
136 text,
137 GFUI_FONT_LARGE,
138 xpos, ypos, GFUI_BTNSZ, GFUI_ALIGN_HC_VB, 0,
139 userdata, onpush,
140 (void*)cbinfo, dispInfo,
141 remInfo);
142
143 return bId;
144 }
145
146 /** Add the "Back" or "Quit" button at the bottom of the menu screen.
147 @ingroup gui
148 @param scr Screen or Menu handle
149 @param text Text of the button
150 @param tip Text to display when the button is focused
151 @param userdata Parameter of the Push function
152 @param onpush Callback when the button is pushed
153 @return Button Id
154 */
155 int
GfuiMenuBackQuitButtonCreate(void * scr,const char * text,const char * tip,void * userdata,tfuiCallback onpush)156 GfuiMenuBackQuitButtonCreate(void *scr, const char *text, const char *tip, void *userdata, tfuiCallback onpush)
157 {
158 tMnuCallbackInfo *cbinfo;
159 int xpos, ypos;
160 int bId;
161
162 xpos = 320;
163 ypos = 40;
164
165 cbinfo = (tMnuCallbackInfo*)calloc(1, sizeof(tMnuCallbackInfo));
166 cbinfo->screen = scr;
167 cbinfo->labelId = GfuiTipCreate(scr, tip, strlen(tip));
168
169 GfuiVisibilitySet(scr, cbinfo->labelId, 0);
170
171 bId = GfuiButtonCreate(scr,
172 text,
173 GFUI_FONT_LARGE,
174 xpos, ypos, GFUI_BTNSZ, GFUI_ALIGN_HC_VB, 0,
175 userdata, onpush,
176 (void*)cbinfo, dispInfo,
177 remInfo);
178
179 GfuiAddKey(scr, (unsigned char)27, tip, userdata, onpush, NULL);
180
181 return bId;
182 }
183
184
185