Moving the X11-specific code from "freeglut_menu.c" into its own file
[freeglut] / src / Common / freeglut_menu.c
1 /*\r
2  * freeglut_menu.c\r
3  *\r
4  * Pull-down menu creation and handling.\r
5  *\r
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.\r
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>\r
8  * Creation date: Thu Dec 16 1999\r
9  *\r
10  * Permission is hereby granted, free of charge, to any person obtaining a\r
11  * copy of this software and associated documentation files (the "Software"),\r
12  * to deal in the Software without restriction, including without limitation\r
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,\r
14  * and/or sell copies of the Software, and to permit persons to whom the\r
15  * Software is furnished to do so, subject to the following conditions:\r
16  *\r
17  * The above copyright notice and this permission notice shall be included\r
18  * in all copies or substantial portions of the Software.\r
19  *\r
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS\r
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL\r
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
26  */\r
27 \r
28 #define FREEGLUT_BUILDING_LIB\r
29 #include <GL/freeglut.h>\r
30 #include "freeglut_internal.h"\r
31 \r
32 /* -- DEFINITIONS ---------------------------------------------------------- */\r
33 \r
34 /*\r
35  * FREEGLUT_MENU_FONT can be any freeglut bitmapped font.\r
36  * (Stroked fonts would not be out of the question, but we'd need to alter\r
37  *  code, since GLUT (hence freeglut) does not quite unify stroked and\r
38  *  bitmapped font handling.)\r
39  * Old UNIX/X11 GLUT (BSD, UNIX, IRIX, LINUX, HPUX, ...) used a system\r
40  * font best approximated by an 18-pixel HELVETICA, I think.  MS-WINDOWS\r
41  * GLUT used something closest to the 8x13 fixed-width font.  (Old\r
42  * GLUT apparently uses host-system menus rather than building its own.\r
43  * freeglut is building its own menus from scratch.)\r
44  *\r
45  * FREEGLUT_MENU_HEIGHT gives the height of ONE menu box.  This should be\r
46  * the distances between two adjacent menu entries.  It should scale\r
47  * automatically with the font choice, so you needn't alter it---unless you\r
48  * use a stroked font.\r
49  *\r
50  * FREEGLUT_MENU_BORDER says how many pixels to allow around the edge of a\r
51  * menu.  (It also seems to be the same as the number of pixels used as\r
52  * a border around *items* to separate them from neighbors.  John says\r
53  * that that wasn't the original intent...if not, perhaps we need another\r
54  * symbolic constant, FREEGLUT_MENU_ITEM_BORDER, or such.)\r
55  */\r
56 #if TARGET_HOST_MS_WINDOWS\r
57 #define  FREEGLUT_MENU_FONT    GLUT_BITMAP_8_BY_13\r
58 #else\r
59 #define  FREEGLUT_MENU_FONT    GLUT_BITMAP_HELVETICA_18\r
60 #endif\r
61 \r
62 #define  FREEGLUT_MENU_HEIGHT  (glutBitmapHeight(FREEGLUT_MENU_FONT) + \\r
63                                 FREEGLUT_MENU_BORDER)\r
64 #define  FREEGLUT_MENU_BORDER   2\r
65 \r
66 \r
67 /*\r
68  * These variables are for rendering the freeglut menu items.\r
69  *\r
70  * The choices are fore- and background, with and without h for Highlighting.\r
71  * Old GLUT appeared to be system-dependant for its colors (sigh) so we are\r
72  * too.  These variables should be stuffed into global state and initialized\r
73  * via the glutInit*() system.\r
74  */\r
75 #if TARGET_HOST_MS_WINDOWS\r
76 static float menu_pen_fore  [4] = {0.0f,  0.0f,  0.0f,  1.0f};\r
77 static float menu_pen_back  [4] = {0.85f, 0.85f, 0.85f, 1.0f};\r
78 static float menu_pen_hfore [4] = {1.0f,  1.0f,  1.0f,  1.0f};\r
79 static float menu_pen_hback [4] = {0.15f, 0.15f, 0.45f, 1.0f};\r
80 #else\r
81 static float menu_pen_fore  [4] = {0.0f,  0.0f,  0.0f,  1.0f};\r
82 static float menu_pen_back  [4] = {0.70f, 0.70f, 0.70f, 1.0f};\r
83 static float menu_pen_hfore [4] = {0.0f,  0.0f,  0.0f,  1.0f};\r
84 static float menu_pen_hback [4] = {1.0f,  1.0f,  1.0f,  1.0f};\r
85 #endif\r
86 \r
87 \r
88 extern GLvoid fgPlatformGetGameModeVMaxExtent( SFG_Window* window, int* x, int* y );\r
89 \r
90 /* -- PRIVATE FUNCTIONS ---------------------------------------------------- */\r
91 \r
92 /*\r
93  * Private function to find a menu entry by index\r
94  */\r
95 static SFG_MenuEntry *fghFindMenuEntry( SFG_Menu* menu, int index )\r
96 {\r
97     SFG_MenuEntry *entry;\r
98     int i = 1;\r
99 \r
100     for( entry = (SFG_MenuEntry *)menu->Entries.First;\r
101          entry;\r
102          entry = (SFG_MenuEntry *)entry->Node.Next )\r
103     {\r
104         if( i == index )\r
105             break;\r
106         ++i;\r
107     }\r
108 \r
109     return entry;\r
110 }\r
111 \r
112 /*\r
113  * Deactivates a menu pointed by the function argument.\r
114  */\r
115 static void fghDeactivateSubMenu( SFG_MenuEntry *menuEntry )\r
116 {\r
117     SFG_MenuEntry *subMenuIter;\r
118     /* Hide the present menu's window */\r
119     fgSetWindow( menuEntry->SubMenu->Window );\r
120     glutHideWindow( );\r
121 \r
122     /* Forget about having that menu active anymore, now: */\r
123     menuEntry->SubMenu->Window->ActiveMenu = NULL;\r
124     menuEntry->SubMenu->IsActive = GL_FALSE;\r
125     menuEntry->SubMenu->ActiveEntry = NULL;\r
126 \r
127     /* Hide all submenu windows, and the root menu's window. */\r
128     for ( subMenuIter = (SFG_MenuEntry *)menuEntry->SubMenu->Entries.First;\r
129           subMenuIter;\r
130           subMenuIter = (SFG_MenuEntry *)subMenuIter->Node.Next )\r
131     {\r
132         subMenuIter->IsActive = GL_FALSE;\r
133 \r
134         /* Is that an active submenu by any case? */\r
135         if( subMenuIter->SubMenu )\r
136             fghDeactivateSubMenu( subMenuIter );\r
137     }\r
138 \r
139     fgSetWindow ( menuEntry->SubMenu->ParentWindow ) ;\r
140 }\r
141 \r
142 /*\r
143  * Private function to get the virtual maximum screen extent\r
144  */\r
145 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )\r
146 {\r
147     if( fgStructure.GameModeWindow )\r
148                 fgPlatformGetGameModeVMaxExtent ( window, x, y );\r
149     else\r
150     {\r
151         *x = fgDisplay.ScreenWidth;\r
152         *y = fgDisplay.ScreenHeight;\r
153     }\r
154 }\r
155 \r
156 /*\r
157  * Private function to check for the current menu/sub menu activity state\r
158  */\r
159 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )\r
160 {\r
161     SFG_MenuEntry* menuEntry;\r
162     int x, y;\r
163 \r
164     /* First of all check any of the active sub menus... */\r
165     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;\r
166          menuEntry;\r
167          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )\r
168     {\r
169         if( menuEntry->SubMenu && menuEntry->IsActive )\r
170         {\r
171             /*\r
172              * OK, have the sub-menu checked, too. If it returns GL_TRUE, it\r
173              * will mean that it caught the mouse cursor and we do not need\r
174              * to regenerate the activity list, and so our parents do...\r
175              */\r
176             GLboolean return_status;\r
177 \r
178             menuEntry->SubMenu->Window->State.MouseX =\r
179                 menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;\r
180             menuEntry->SubMenu->Window->State.MouseY =\r
181                 menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;\r
182             return_status = fghCheckMenuStatus( menuEntry->SubMenu );\r
183 \r
184             if ( return_status )\r
185                 return GL_TRUE;\r
186         }\r
187     }\r
188 \r
189     /* That much about our sub menus, let's get to checking the current menu: */\r
190     x = menu->Window->State.MouseX;\r
191     y = menu->Window->State.MouseY;\r
192 \r
193     /* Check if the mouse cursor is contained within the current menu box */\r
194     if( ( x >= FREEGLUT_MENU_BORDER ) &&\r
195         ( x < menu->Width  - FREEGLUT_MENU_BORDER ) &&\r
196         ( y >= FREEGLUT_MENU_BORDER ) &&\r
197         ( y < menu->Height - FREEGLUT_MENU_BORDER )  )\r
198     {\r
199         int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;\r
200 \r
201         /* The mouse cursor is somewhere over our box, check it out. */\r
202         menuEntry = fghFindMenuEntry( menu, menuID + 1 );\r
203         FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",\r
204                                       "fghCheckMenuStatus" );\r
205 \r
206         menuEntry->IsActive = GL_TRUE;\r
207         menuEntry->Ordinal = menuID;\r
208 \r
209         /*\r
210          * If this is not the same as the last active menu entry, deactivate\r
211          * the previous entry.  Specifically, if the previous active entry\r
212          * was a submenu then deactivate it.\r
213          */\r
214         if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )\r
215             if( menu->ActiveEntry->SubMenu )\r
216                 fghDeactivateSubMenu( menu->ActiveEntry );\r
217 \r
218         if( menuEntry != menu->ActiveEntry )\r
219         {\r
220             menu->Window->State.Redisplay = GL_TRUE;\r
221             if( menu->ActiveEntry )\r
222                 menu->ActiveEntry->IsActive = GL_FALSE;\r
223         }\r
224 \r
225         menu->ActiveEntry = menuEntry;\r
226         menu->IsActive = GL_TRUE;  /* XXX Do we need this? */\r
227 \r
228         /*\r
229          * OKi, we have marked that entry as active, but it would be also\r
230          * nice to have its contents updated, in case it's a sub menu.\r
231          * Also, ignore the return value of the check function:\r
232          */\r
233         if( menuEntry->SubMenu )\r
234         {\r
235             if ( ! menuEntry->SubMenu->IsActive )\r
236             {\r
237                 int max_x, max_y;\r
238                 SFG_Window *current_window = fgStructure.CurrentWindow;\r
239 \r
240                 /* Set up the initial menu position now... */\r
241                 menuEntry->SubMenu->IsActive = GL_TRUE;\r
242 \r
243                 /* Set up the initial submenu position now: */\r
244                 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);\r
245                 menuEntry->SubMenu->X = menu->X + menu->Width;\r
246                 menuEntry->SubMenu->Y = menu->Y +\r
247                     menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;\r
248 \r
249                 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )\r
250                     menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;\r
251 \r
252                 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )\r
253                 {\r
254                     menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -\r
255                                                FREEGLUT_MENU_HEIGHT -\r
256                                                2 * FREEGLUT_MENU_BORDER );\r
257                     if( menuEntry->SubMenu->Y < 0 )\r
258                         menuEntry->SubMenu->Y = 0;\r
259                 }\r
260 \r
261                 fgSetWindow( menuEntry->SubMenu->Window );\r
262                 glutPositionWindow( menuEntry->SubMenu->X,\r
263                                     menuEntry->SubMenu->Y );\r
264                 glutReshapeWindow( menuEntry->SubMenu->Width,\r
265                                    menuEntry->SubMenu->Height );\r
266                 glutPopWindow( );\r
267                 glutShowWindow( );\r
268                 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;\r
269                 fgSetWindow( current_window );\r
270                 menuEntry->SubMenu->Window->State.MouseX =\r
271                     x + menu->X - menuEntry->SubMenu->X;\r
272                 menuEntry->SubMenu->Window->State.MouseY =\r
273                     y + menu->Y - menuEntry->SubMenu->Y;\r
274                 fghCheckMenuStatus( menuEntry->SubMenu );\r
275             }\r
276 \r
277             /* Activate it because its parent entry is active */\r
278             menuEntry->SubMenu->IsActive = GL_TRUE;  /* XXX Do we need this? */\r
279         }\r
280 \r
281         /* Report back that we have caught the menu cursor */\r
282         return GL_TRUE;\r
283     }\r
284 \r
285     /* Looks like the menu cursor is somewhere else... */\r
286     if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&\r
287         ( !menu->ActiveEntry->SubMenu ||\r
288           !menu->ActiveEntry->SubMenu->IsActive ) )\r
289     {\r
290         menu->Window->State.Redisplay = GL_TRUE;\r
291         menu->ActiveEntry->IsActive = GL_FALSE;\r
292         menu->ActiveEntry = NULL;\r
293     }\r
294 \r
295     return GL_FALSE;\r
296 }\r
297 \r
298 /*\r
299  * Displays a menu box and all of its submenus (if they are active)\r
300  */\r
301 static void fghDisplayMenuBox( SFG_Menu* menu )\r
302 {\r
303     SFG_MenuEntry *menuEntry;\r
304     int i;\r
305     int border = FREEGLUT_MENU_BORDER;\r
306 \r
307     /*\r
308      * Have the menu box drawn first. The +- values are\r
309      * here just to make it more nice-looking...\r
310      */\r
311     /* a non-black dark version of the below. */\r
312     glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );\r
313     glBegin( GL_QUAD_STRIP );\r
314         glVertex2i( menu->Width         , 0                    );\r
315         glVertex2i( menu->Width - border,                border);\r
316         glVertex2i( 0                   , 0                    );\r
317         glVertex2i(               border,                border);\r
318         glVertex2i( 0                   , menu->Height         );\r
319         glVertex2i(               border, menu->Height - border);\r
320     glEnd( );\r
321 \r
322     /* a non-black dark version of the below. */\r
323     glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );\r
324     glBegin( GL_QUAD_STRIP );\r
325         glVertex2i( 0                   , menu->Height         );\r
326         glVertex2i(               border, menu->Height - border);\r
327         glVertex2i( menu->Width         , menu->Height         );\r
328         glVertex2i( menu->Width - border, menu->Height - border);\r
329         glVertex2i( menu->Width         , 0                    );\r
330         glVertex2i( menu->Width - border,                border);\r
331     glEnd( );\r
332 \r
333     glColor4fv( menu_pen_back );\r
334     glBegin( GL_QUADS );\r
335         glVertex2i(               border,                border);\r
336         glVertex2i( menu->Width - border,                border);\r
337         glVertex2i( menu->Width - border, menu->Height - border);\r
338         glVertex2i(               border, menu->Height - border);\r
339     glEnd( );\r
340 \r
341     /* Check if any of the submenus is currently active... */\r
342     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;\r
343          menuEntry;\r
344          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )\r
345     {\r
346         /* Has the menu been marked as active, maybe? */\r
347         if( menuEntry->IsActive )\r
348         {\r
349             /*\r
350              * That's truly right, and we need to have it highlighted.\r
351              * There is an assumption that mouse cursor didn't move\r
352              * since the last check of menu activity state:\r
353              */\r
354             int menuID = menuEntry->Ordinal;\r
355 \r
356             /* So have the highlight drawn... */\r
357             glColor4fv( menu_pen_hback );\r
358             glBegin( GL_QUADS );\r
359                 glVertex2i( border,\r
360                             (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );\r
361                 glVertex2i( menu->Width - border,\r
362                             (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );\r
363                 glVertex2i( menu->Width - border,\r
364                             (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );\r
365                 glVertex2i( border,\r
366                             (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );\r
367             glEnd( );\r
368         }\r
369     }\r
370 \r
371     /* Print the menu entries now... */\r
372 \r
373     glColor4fv( menu_pen_fore );\r
374 \r
375     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;\r
376          menuEntry;\r
377          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )\r
378     {\r
379         /* If the menu entry is active, set the color to white */\r
380         if( menuEntry->IsActive )\r
381             glColor4fv( menu_pen_hfore );\r
382 \r
383         /* Move the raster into position... */\r
384         /* Try to center the text - JCJ 31 July 2003*/\r
385         glRasterPos2i(\r
386             2 * border,\r
387             ( i + 1 )*FREEGLUT_MENU_HEIGHT -\r
388             ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )\r
389         );\r
390 \r
391         /* Have the label drawn, character after character: */\r
392         glutBitmapString( FREEGLUT_MENU_FONT,\r
393                           (unsigned char *)menuEntry->Text);\r
394 \r
395         /* If it's a submenu, draw a right arrow */\r
396         if( menuEntry->SubMenu )\r
397         {\r
398             int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );\r
399             int x_base = menu->Width - 2 - width;\r
400             int y_base = i*FREEGLUT_MENU_HEIGHT + border;\r
401             glBegin( GL_TRIANGLES );\r
402                 glVertex2i( x_base, y_base + 2*border);\r
403                 glVertex2i( menu->Width - 2, y_base +\r
404                             ( FREEGLUT_MENU_HEIGHT + border) / 2 );\r
405                 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );\r
406             glEnd( );\r
407         }\r
408 \r
409         /* If the menu entry is active, reset the color */\r
410         if( menuEntry->IsActive )\r
411             glColor4fv( menu_pen_fore );\r
412     }\r
413 }\r
414 \r
415 /*\r
416  * Private static function to set the parent window of a submenu and all\r
417  * of its submenus\r
418  */\r
419 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )\r
420 {\r
421     SFG_MenuEntry *menuEntry;\r
422 \r
423     menu->ParentWindow = window;\r
424 \r
425     for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;\r
426          menuEntry;\r
427          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )\r
428         if( menuEntry->SubMenu )\r
429             fghSetMenuParentWindow( window, menuEntry->SubMenu );\r
430 }\r
431 \r
432 /*\r
433  * Function to check for menu entry selection on menu deactivation\r
434  */\r
435 static void fghExecuteMenuCallback( SFG_Menu* menu )\r
436 {\r
437     SFG_MenuEntry *menuEntry;\r
438 \r
439     /* First of all check any of the active sub menus... */\r
440     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;\r
441          menuEntry;\r
442          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)\r
443     {\r
444         if( menuEntry->IsActive )\r
445         {\r
446             if( menuEntry->SubMenu )\r
447                 fghExecuteMenuCallback( menuEntry->SubMenu );\r
448             else\r
449                 if( menu->Callback )\r
450                 {\r
451                     SFG_Menu *save_menu = fgStructure.CurrentMenu;\r
452                     fgStructure.CurrentMenu = menu;\r
453                     menu->Callback( menuEntry->ID );\r
454                     fgStructure.CurrentMenu = save_menu;\r
455                 }\r
456 \r
457             return;\r
458         }\r
459     }\r
460 }\r
461 \r
462 \r
463 /*\r
464  * Displays the currently active menu for the current window\r
465  */\r
466 void fgDisplayMenu( void )\r
467 {\r
468     SFG_Window* window = fgStructure.CurrentWindow;\r
469     SFG_Menu* menu = NULL;\r
470 \r
471     FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",\r
472                                    "fgDisplayMenu" );\r
473 \r
474     /* Check if there is an active menu attached to this window... */\r
475     menu = window->ActiveMenu;\r
476     freeglut_return_if_fail( menu );\r
477 \r
478     fgSetWindow( menu->Window );\r
479 \r
480     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |\r
481                   GL_POLYGON_BIT );\r
482 \r
483     glDisable( GL_DEPTH_TEST );\r
484     glDisable( GL_TEXTURE_2D );\r
485     glDisable( GL_LIGHTING   );\r
486     glDisable( GL_CULL_FACE  );\r
487 \r
488     glMatrixMode( GL_PROJECTION );\r
489     glPushMatrix( );\r
490     glLoadIdentity( );\r
491     glOrtho(\r
492          0, glutGet( GLUT_WINDOW_WIDTH  ),\r
493          glutGet( GLUT_WINDOW_HEIGHT ), 0,\r
494         -1, 1\r
495     );\r
496 \r
497     glMatrixMode( GL_MODELVIEW );\r
498     glPushMatrix( );\r
499     glLoadIdentity( );\r
500 \r
501     fghDisplayMenuBox( menu );\r
502 \r
503     glPopAttrib( );\r
504 \r
505     glMatrixMode( GL_PROJECTION );\r
506     glPopMatrix( );\r
507     glMatrixMode( GL_MODELVIEW );\r
508     glPopMatrix( );\r
509 \r
510     glutSwapBuffers( );\r
511 \r
512     fgSetWindow ( window );\r
513 }\r
514 \r
515 /*\r
516  * Activates a menu pointed by the function argument\r
517  */\r
518 static void fghActivateMenu( SFG_Window* window, int button )\r
519 {\r
520     int max_x, max_y;\r
521 \r
522     /* We'll be referencing this menu a lot, so remember its address: */\r
523     SFG_Menu* menu = window->Menu[ button ];\r
524     SFG_Window* current_window = fgStructure.CurrentWindow;\r
525 \r
526     /* If the menu is already active in another window, deactivate it there */\r
527     if ( menu->ParentWindow )\r
528       menu->ParentWindow->ActiveMenu = NULL ;\r
529 \r
530     /* Mark the menu as active, so that it gets displayed: */\r
531     window->ActiveMenu = menu;\r
532     menu->IsActive = GL_TRUE;\r
533     fghSetMenuParentWindow ( window, menu );\r
534     fgState.ActiveMenus++;\r
535 \r
536     /* Set up the initial menu position now: */\r
537     fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);\r
538     fgSetWindow( window );\r
539     menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );\r
540     menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );\r
541 \r
542     if( menu->X + menu->Width > max_x )\r
543         menu->X -=menu->Width;\r
544 \r
545     if( menu->Y + menu->Height > max_y )\r
546     {\r
547         menu->Y -=menu->Height;\r
548         if( menu->Y < 0 )\r
549             menu->Y = 0;\r
550     }\r
551 \r
552     menu->Window->State.MouseX =\r
553         window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;\r
554     menu->Window->State.MouseY =\r
555         window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;\r
556 \r
557     fgSetWindow( menu->Window );\r
558     glutPositionWindow( menu->X, menu->Y );\r
559     glutReshapeWindow( menu->Width, menu->Height );\r
560     glutPopWindow( );\r
561     glutShowWindow( );\r
562     menu->Window->ActiveMenu = menu;\r
563     fghCheckMenuStatus( menu );\r
564     fgSetWindow( current_window );\r
565 }\r
566 \r
567 /*\r
568  * Update Highlight states of the menu\r
569  *\r
570  * Current mouse position is in menu->Window->State.MouseX/Y.\r
571  */\r
572 void fgUpdateMenuHighlight ( SFG_Menu *menu )\r
573 {\r
574     fghCheckMenuStatus( menu );\r
575 }\r
576 \r
577 /*\r
578  * Check whether an active menu absorbs a mouse click\r
579  */\r
580 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,\r
581                               int mouse_x, int mouse_y )\r
582 {\r
583     /*\r
584      * Near as I can tell, this is the menu behaviour:\r
585      *  - Down-click the menu button, menu not active:  activate\r
586      *    the menu with its upper left-hand corner at the mouse\r
587      *    location.\r
588      *  - Down-click any button outside the menu, menu active:\r
589      *    deactivate the menu\r
590      *  - Down-click any button inside the menu, menu active:\r
591      *    select the menu entry and deactivate the menu\r
592      *  - Up-click the menu button, menu not active:  nothing happens\r
593      *  - Up-click the menu button outside the menu, menu active:\r
594      *    nothing happens\r
595      *  - Up-click the menu button inside the menu, menu active:\r
596      *    select the menu entry and deactivate the menu\r
597      * Since menus can have submenus, we need to check this recursively.\r
598      */\r
599     if( window->ActiveMenu )\r
600     {\r
601         if( window == window->ActiveMenu->ParentWindow )\r
602         {\r
603             window->ActiveMenu->Window->State.MouseX =\r
604                                        mouse_x - window->ActiveMenu->X;\r
605             window->ActiveMenu->Window->State.MouseY =\r
606                                        mouse_y - window->ActiveMenu->Y;\r
607         }\r
608 \r
609         /* In the menu, invoke the callback and deactivate the menu */\r
610         if( fghCheckMenuStatus( window->ActiveMenu ) )\r
611         {\r
612             /*\r
613              * Save the current window and menu and set the current\r
614              * window to the window whose menu this is\r
615              */\r
616             SFG_Window *save_window = fgStructure.CurrentWindow;\r
617             SFG_Menu *save_menu = fgStructure.CurrentMenu;\r
618             SFG_Window *parent_window = window->ActiveMenu->ParentWindow;\r
619             fgSetWindow( parent_window );\r
620             fgStructure.CurrentMenu = window->ActiveMenu;\r
621 \r
622             /* Execute the menu callback */\r
623             fghExecuteMenuCallback( window->ActiveMenu );\r
624             fgDeactivateMenu( parent_window );\r
625 \r
626             /* Restore the current window and menu */\r
627             fgSetWindow( save_window );\r
628             fgStructure.CurrentMenu = save_menu;\r
629         }\r
630         else if( pressed )\r
631             /*\r
632              * Outside the menu, deactivate if it's a downclick\r
633              *\r
634              * XXX This isn't enough.  A downclick outside of\r
635              * XXX the interior of our freeglut windows should also\r
636              * XXX deactivate the menu.  This is more complicated.\r
637              */\r
638             fgDeactivateMenu( window->ActiveMenu->ParentWindow );\r
639 \r
640         /*\r
641          * XXX Why does an active menu require a redisplay at\r
642          * XXX this point?  If this can come out cleanly, then\r
643          * XXX it probably should do so; if not, a comment should\r
644          * XXX explain it.\r
645          */\r
646         if( ! window->IsMenu )\r
647             window->State.Redisplay = GL_TRUE;\r
648 \r
649         return GL_TRUE;\r
650     }\r
651 \r
652     /* No active menu, let's check whether we need to activate one. */\r
653     if( ( 0 <= button ) &&\r
654         ( FREEGLUT_MAX_MENUS > button ) &&\r
655         ( window->Menu[ button ] ) &&\r
656         pressed )\r
657     {\r
658         /* XXX Posting a requisite Redisplay seems bogus. */\r
659         window->State.Redisplay = GL_TRUE;\r
660         fghActivateMenu( window, button );\r
661         return GL_TRUE;\r
662     }\r
663 \r
664     return GL_FALSE;\r
665 }\r
666 \r
667 /*\r
668  * Deactivates a menu pointed by the function argument.\r
669  */\r
670 void fgDeactivateMenu( SFG_Window *window )\r
671 {\r
672     SFG_Window *parent_window = NULL;\r
673 \r
674     /* Check if there is an active menu attached to this window... */\r
675     SFG_Menu* menu = window->ActiveMenu;\r
676     SFG_MenuEntry *menuEntry;\r
677 \r
678     /* Did we find an active window? */\r
679     freeglut_return_if_fail( menu );\r
680 \r
681     parent_window = menu->ParentWindow;\r
682 \r
683     /* Hide the present menu's window */\r
684     fgSetWindow( menu->Window );\r
685     glutHideWindow( );\r
686 \r
687     /* Forget about having that menu active anymore, now: */\r
688     menu->Window->ActiveMenu = NULL;\r
689     menu->ParentWindow->ActiveMenu = NULL;\r
690     fghSetMenuParentWindow ( NULL, menu );\r
691     menu->IsActive = GL_FALSE;\r
692     menu->ActiveEntry = NULL;\r
693 \r
694     fgState.ActiveMenus--;\r
695 \r
696     /* Hide all submenu windows, and the root menu's window. */\r
697     for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;\r
698           menuEntry;\r
699           menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )\r
700     {\r
701         menuEntry->IsActive = GL_FALSE;\r
702 \r
703         /* Is that an active submenu by any case? */\r
704         if( menuEntry->SubMenu )\r
705             fghDeactivateSubMenu( menuEntry );\r
706     }\r
707 \r
708     fgSetWindow ( parent_window ) ;\r
709 }\r
710 \r
711 /*\r
712  * Recalculates current menu's box size\r
713  */\r
714 void fghCalculateMenuBoxSize( void )\r
715 {\r
716     SFG_MenuEntry* menuEntry;\r
717     int width = 0, height = 0;\r
718 \r
719     /* Make sure there is a current menu set */\r
720     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
721 \r
722     /* The menu's box size depends on the menu entries: */\r
723     for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;\r
724          menuEntry;\r
725          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )\r
726     {\r
727         /* Update the menu entry's width value */\r
728         menuEntry->Width = glutBitmapLength(\r
729             FREEGLUT_MENU_FONT,\r
730             (unsigned char *)menuEntry->Text\r
731         );\r
732 \r
733         /*\r
734          * If the entry is a submenu, then it needs to be wider to\r
735          * accomodate the arrow. JCJ 31 July 2003\r
736          */\r
737         if (menuEntry->SubMenu )\r
738             menuEntry->Width += glutBitmapLength(\r
739                 FREEGLUT_MENU_FONT,\r
740                 (unsigned char *)"_"\r
741             );\r
742 \r
743         /* Check if it's the biggest we've found */\r
744         if( menuEntry->Width > width )\r
745             width = menuEntry->Width;\r
746 \r
747         height += FREEGLUT_MENU_HEIGHT;\r
748     }\r
749 \r
750     /* Store the menu's box size now: */\r
751     fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;\r
752     fgStructure.CurrentMenu->Width  = width  + 4 * FREEGLUT_MENU_BORDER;\r
753 }\r
754 \r
755 \r
756 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */\r
757 \r
758 /*\r
759  * Creates a new menu object, adding it to the freeglut structure\r
760  */\r
761 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )\r
762 {\r
763     /* The menu object creation code resides in freeglut_structure.c */\r
764     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );\r
765     return fgCreateMenu( callback )->ID;\r
766 }\r
767 \r
768 /*\r
769  * Destroys a menu object, removing all references to it\r
770  */\r
771 void FGAPIENTRY glutDestroyMenu( int menuID )\r
772 {\r
773     SFG_Menu* menu;\r
774 \r
775     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );\r
776     menu = fgMenuByID( menuID );\r
777 \r
778     freeglut_return_if_fail( menu );\r
779 \r
780     /* The menu object destruction code resides in freeglut_structure.c */\r
781     fgDestroyMenu( menu );\r
782 }\r
783 \r
784 /*\r
785  * Returns the ID number of the currently active menu\r
786  */\r
787 int FGAPIENTRY glutGetMenu( void )\r
788 {\r
789     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );\r
790 \r
791     if( fgStructure.CurrentMenu )\r
792         return fgStructure.CurrentMenu->ID;\r
793 \r
794     return 0;\r
795 }\r
796 \r
797 /*\r
798  * Sets the current menu given its menu ID\r
799  */\r
800 void FGAPIENTRY glutSetMenu( int menuID )\r
801 {\r
802     SFG_Menu* menu;\r
803 \r
804     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );\r
805     menu = fgMenuByID( menuID );\r
806 \r
807     freeglut_return_if_fail( menu );\r
808 \r
809     fgStructure.CurrentMenu = menu;\r
810 }\r
811 \r
812 /*\r
813  * Adds a menu entry to the bottom of the current menu\r
814  */\r
815 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )\r
816 {\r
817     SFG_MenuEntry* menuEntry;\r
818     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );\r
819     menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );\r
820     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
821 \r
822     menuEntry->Text = strdup( label );\r
823     menuEntry->ID   = value;\r
824 \r
825     /* Have the new menu entry attached to the current menu */\r
826     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );\r
827 \r
828     fghCalculateMenuBoxSize( );\r
829 }\r
830 \r
831 /*\r
832  * Add a sub menu to the bottom of the current menu\r
833  */\r
834 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )\r
835 {\r
836     SFG_MenuEntry *menuEntry;\r
837     SFG_Menu *subMenu;\r
838 \r
839     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );\r
840     menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );\r
841     subMenu = fgMenuByID( subMenuID );\r
842 \r
843     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
844     freeglut_return_if_fail( subMenu );\r
845 \r
846     menuEntry->Text    = strdup( label );\r
847     menuEntry->SubMenu = subMenu;\r
848     menuEntry->ID      = -1;\r
849 \r
850     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );\r
851     fghCalculateMenuBoxSize( );\r
852 }\r
853 \r
854 /*\r
855  * Changes the specified menu item in the current menu into a menu entry\r
856  */\r
857 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )\r
858 {\r
859     SFG_MenuEntry* menuEntry = NULL;\r
860 \r
861     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );\r
862     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
863 \r
864     /* Get n-th menu entry in the current menu, starting from one: */\r
865     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );\r
866 \r
867     freeglut_return_if_fail( menuEntry );\r
868 \r
869     /* We want it to become a normal menu entry, so: */\r
870     if( menuEntry->Text )\r
871         free( menuEntry->Text );\r
872 \r
873     menuEntry->Text    = strdup( label );\r
874     menuEntry->ID      = value;\r
875     menuEntry->SubMenu = NULL;\r
876     fghCalculateMenuBoxSize( );\r
877 }\r
878 \r
879 /*\r
880  * Changes the specified menu item in the current menu into a sub-menu trigger.\r
881  */\r
882 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,\r
883                                      int subMenuID )\r
884 {\r
885     SFG_Menu*      subMenu;\r
886     SFG_MenuEntry* menuEntry;\r
887 \r
888     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );\r
889     subMenu = fgMenuByID( subMenuID );\r
890     menuEntry = NULL;\r
891 \r
892     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
893     freeglut_return_if_fail( subMenu );\r
894 \r
895     /* Get n-th menu entry in the current menu, starting from one: */\r
896     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );\r
897 \r
898     freeglut_return_if_fail( menuEntry );\r
899 \r
900     /* We want it to become a sub menu entry, so: */\r
901     if( menuEntry->Text )\r
902         free( menuEntry->Text );\r
903 \r
904     menuEntry->Text    = strdup( label );\r
905     menuEntry->SubMenu = subMenu;\r
906     menuEntry->ID      = -1;\r
907     fghCalculateMenuBoxSize( );\r
908 }\r
909 \r
910 /*\r
911  * Removes the specified menu item from the current menu\r
912  */\r
913 void FGAPIENTRY glutRemoveMenuItem( int item )\r
914 {\r
915     SFG_MenuEntry* menuEntry;\r
916 \r
917     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );\r
918     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
919 \r
920     /* Get n-th menu entry in the current menu, starting from one: */\r
921     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );\r
922 \r
923     freeglut_return_if_fail( menuEntry );\r
924 \r
925     fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );\r
926     if ( menuEntry->Text )\r
927       free( menuEntry->Text );\r
928 \r
929     free( menuEntry );\r
930     fghCalculateMenuBoxSize( );\r
931 }\r
932 \r
933 /*\r
934  * Attaches a menu to the current window\r
935  */\r
936 void FGAPIENTRY glutAttachMenu( int button )\r
937 {\r
938     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );\r
939 \r
940     freeglut_return_if_fail( fgStructure.CurrentWindow );\r
941     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
942 \r
943     freeglut_return_if_fail( button >= 0 );\r
944     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );\r
945 \r
946     fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;\r
947 }\r
948 \r
949 /*\r
950  * Detaches a menu from the current window\r
951  */\r
952 void FGAPIENTRY glutDetachMenu( int button )\r
953 {\r
954     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );\r
955 \r
956     freeglut_return_if_fail( fgStructure.CurrentWindow );\r
957     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
958 \r
959     freeglut_return_if_fail( button >= 0 );\r
960     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );\r
961 \r
962     fgStructure.CurrentWindow->Menu[ button ] = NULL;\r
963 }\r
964 \r
965 /*\r
966  * A.Donev: Set and retrieve the menu's user data\r
967  */\r
968 void* FGAPIENTRY glutGetMenuData( void )\r
969 {\r
970     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );\r
971     return fgStructure.CurrentMenu->UserData;\r
972 }\r
973 \r
974 void FGAPIENTRY glutSetMenuData(void* data)\r
975 {\r
976     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );\r
977     fgStructure.CurrentMenu->UserData=data;\r
978 }\r
979 \r
980 /*** END OF FILE ***/\r