Moving platform-specific fields in "SFG_Display" into a substructure
[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 #if TARGET_HOST_POSIX_X11\r
146 GLvoid fgPlatformGetGameModeVMaxExtent( SFG_Window* window, int* x, int* y )\r
147 {\r
148     int wx, wy;\r
149     Window w;\r
150 \r
151     XTranslateCoordinates(\r
152         fgDisplay.pDisplay.Display,\r
153         window->Window.Handle,\r
154         fgDisplay.pDisplay.RootWindow,\r
155         0, 0, &wx, &wy, &w);\r
156 \r
157     *x = fgState.GameModeSize.X + wx;\r
158     *y = fgState.GameModeSize.Y + wy;\r
159 }\r
160 #endif\r
161 \r
162 \r
163 static GLvoid fghGetVMaxExtent( SFG_Window* window, int* x, int* y )\r
164 {\r
165     if( fgStructure.GameModeWindow )\r
166                 fgPlatformGetGameModeVMaxExtent ( window, x, y );\r
167     else\r
168     {\r
169         *x = fgDisplay.ScreenWidth;\r
170         *y = fgDisplay.ScreenHeight;\r
171     }\r
172 }\r
173 \r
174 /*\r
175  * Private function to check for the current menu/sub menu activity state\r
176  */\r
177 static GLboolean fghCheckMenuStatus( SFG_Menu* menu )\r
178 {\r
179     SFG_MenuEntry* menuEntry;\r
180     int x, y;\r
181 \r
182     /* First of all check any of the active sub menus... */\r
183     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;\r
184          menuEntry;\r
185          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )\r
186     {\r
187         if( menuEntry->SubMenu && menuEntry->IsActive )\r
188         {\r
189             /*\r
190              * OK, have the sub-menu checked, too. If it returns GL_TRUE, it\r
191              * will mean that it caught the mouse cursor and we do not need\r
192              * to regenerate the activity list, and so our parents do...\r
193              */\r
194             GLboolean return_status;\r
195 \r
196             menuEntry->SubMenu->Window->State.MouseX =\r
197                 menu->Window->State.MouseX + menu->X - menuEntry->SubMenu->X;\r
198             menuEntry->SubMenu->Window->State.MouseY =\r
199                 menu->Window->State.MouseY + menu->Y - menuEntry->SubMenu->Y;\r
200             return_status = fghCheckMenuStatus( menuEntry->SubMenu );\r
201 \r
202             if ( return_status )\r
203                 return GL_TRUE;\r
204         }\r
205     }\r
206 \r
207     /* That much about our sub menus, let's get to checking the current menu: */\r
208     x = menu->Window->State.MouseX;\r
209     y = menu->Window->State.MouseY;\r
210 \r
211     /* Check if the mouse cursor is contained within the current menu box */\r
212     if( ( x >= FREEGLUT_MENU_BORDER ) &&\r
213         ( x < menu->Width  - FREEGLUT_MENU_BORDER ) &&\r
214         ( y >= FREEGLUT_MENU_BORDER ) &&\r
215         ( y < menu->Height - FREEGLUT_MENU_BORDER )  )\r
216     {\r
217         int menuID = ( y - FREEGLUT_MENU_BORDER ) / FREEGLUT_MENU_HEIGHT;\r
218 \r
219         /* The mouse cursor is somewhere over our box, check it out. */\r
220         menuEntry = fghFindMenuEntry( menu, menuID + 1 );\r
221         FREEGLUT_INTERNAL_ERROR_EXIT( menuEntry, "Cannot find menu entry",\r
222                                       "fghCheckMenuStatus" );\r
223 \r
224         menuEntry->IsActive = GL_TRUE;\r
225         menuEntry->Ordinal = menuID;\r
226 \r
227         /*\r
228          * If this is not the same as the last active menu entry, deactivate\r
229          * the previous entry.  Specifically, if the previous active entry\r
230          * was a submenu then deactivate it.\r
231          */\r
232         if( menu->ActiveEntry && ( menuEntry != menu->ActiveEntry ) )\r
233             if( menu->ActiveEntry->SubMenu )\r
234                 fghDeactivateSubMenu( menu->ActiveEntry );\r
235 \r
236         if( menuEntry != menu->ActiveEntry )\r
237         {\r
238             menu->Window->State.Redisplay = GL_TRUE;\r
239             if( menu->ActiveEntry )\r
240                 menu->ActiveEntry->IsActive = GL_FALSE;\r
241         }\r
242 \r
243         menu->ActiveEntry = menuEntry;\r
244         menu->IsActive = GL_TRUE;  /* XXX Do we need this? */\r
245 \r
246         /*\r
247          * OKi, we have marked that entry as active, but it would be also\r
248          * nice to have its contents updated, in case it's a sub menu.\r
249          * Also, ignore the return value of the check function:\r
250          */\r
251         if( menuEntry->SubMenu )\r
252         {\r
253             if ( ! menuEntry->SubMenu->IsActive )\r
254             {\r
255                 int max_x, max_y;\r
256                 SFG_Window *current_window = fgStructure.CurrentWindow;\r
257 \r
258                 /* Set up the initial menu position now... */\r
259                 menuEntry->SubMenu->IsActive = GL_TRUE;\r
260 \r
261                 /* Set up the initial submenu position now: */\r
262                 fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);\r
263                 menuEntry->SubMenu->X = menu->X + menu->Width;\r
264                 menuEntry->SubMenu->Y = menu->Y +\r
265                     menuEntry->Ordinal * FREEGLUT_MENU_HEIGHT;\r
266 \r
267                 if( menuEntry->SubMenu->X + menuEntry->SubMenu->Width > max_x )\r
268                     menuEntry->SubMenu->X = menu->X - menuEntry->SubMenu->Width;\r
269 \r
270                 if( menuEntry->SubMenu->Y + menuEntry->SubMenu->Height > max_y )\r
271                 {\r
272                     menuEntry->SubMenu->Y -= ( menuEntry->SubMenu->Height -\r
273                                                FREEGLUT_MENU_HEIGHT -\r
274                                                2 * FREEGLUT_MENU_BORDER );\r
275                     if( menuEntry->SubMenu->Y < 0 )\r
276                         menuEntry->SubMenu->Y = 0;\r
277                 }\r
278 \r
279                 fgSetWindow( menuEntry->SubMenu->Window );\r
280                 glutPositionWindow( menuEntry->SubMenu->X,\r
281                                     menuEntry->SubMenu->Y );\r
282                 glutReshapeWindow( menuEntry->SubMenu->Width,\r
283                                    menuEntry->SubMenu->Height );\r
284                 glutPopWindow( );\r
285                 glutShowWindow( );\r
286                 menuEntry->SubMenu->Window->ActiveMenu = menuEntry->SubMenu;\r
287                 fgSetWindow( current_window );\r
288                 menuEntry->SubMenu->Window->State.MouseX =\r
289                     x + menu->X - menuEntry->SubMenu->X;\r
290                 menuEntry->SubMenu->Window->State.MouseY =\r
291                     y + menu->Y - menuEntry->SubMenu->Y;\r
292                 fghCheckMenuStatus( menuEntry->SubMenu );\r
293             }\r
294 \r
295             /* Activate it because its parent entry is active */\r
296             menuEntry->SubMenu->IsActive = GL_TRUE;  /* XXX Do we need this? */\r
297         }\r
298 \r
299         /* Report back that we have caught the menu cursor */\r
300         return GL_TRUE;\r
301     }\r
302 \r
303     /* Looks like the menu cursor is somewhere else... */\r
304     if( menu->ActiveEntry && menu->ActiveEntry->IsActive &&\r
305         ( !menu->ActiveEntry->SubMenu ||\r
306           !menu->ActiveEntry->SubMenu->IsActive ) )\r
307     {\r
308         menu->Window->State.Redisplay = GL_TRUE;\r
309         menu->ActiveEntry->IsActive = GL_FALSE;\r
310         menu->ActiveEntry = NULL;\r
311     }\r
312 \r
313     return GL_FALSE;\r
314 }\r
315 \r
316 /*\r
317  * Displays a menu box and all of its submenus (if they are active)\r
318  */\r
319 static void fghDisplayMenuBox( SFG_Menu* menu )\r
320 {\r
321     SFG_MenuEntry *menuEntry;\r
322     int i;\r
323     int border = FREEGLUT_MENU_BORDER;\r
324 \r
325     /*\r
326      * Have the menu box drawn first. The +- values are\r
327      * here just to make it more nice-looking...\r
328      */\r
329     /* a non-black dark version of the below. */\r
330     glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );\r
331     glBegin( GL_QUAD_STRIP );\r
332         glVertex2i( menu->Width         , 0                    );\r
333         glVertex2i( menu->Width - border,                border);\r
334         glVertex2i( 0                   , 0                    );\r
335         glVertex2i(               border,                border);\r
336         glVertex2i( 0                   , menu->Height         );\r
337         glVertex2i(               border, menu->Height - border);\r
338     glEnd( );\r
339 \r
340     /* a non-black dark version of the below. */\r
341     glColor4f( 0.5f, 0.5f, 0.5f, 1.0f );\r
342     glBegin( GL_QUAD_STRIP );\r
343         glVertex2i( 0                   , menu->Height         );\r
344         glVertex2i(               border, menu->Height - border);\r
345         glVertex2i( menu->Width         , menu->Height         );\r
346         glVertex2i( menu->Width - border, menu->Height - border);\r
347         glVertex2i( menu->Width         , 0                    );\r
348         glVertex2i( menu->Width - border,                border);\r
349     glEnd( );\r
350 \r
351     glColor4fv( menu_pen_back );\r
352     glBegin( GL_QUADS );\r
353         glVertex2i(               border,                border);\r
354         glVertex2i( menu->Width - border,                border);\r
355         glVertex2i( menu->Width - border, menu->Height - border);\r
356         glVertex2i(               border, menu->Height - border);\r
357     glEnd( );\r
358 \r
359     /* Check if any of the submenus is currently active... */\r
360     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;\r
361          menuEntry;\r
362          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next )\r
363     {\r
364         /* Has the menu been marked as active, maybe? */\r
365         if( menuEntry->IsActive )\r
366         {\r
367             /*\r
368              * That's truly right, and we need to have it highlighted.\r
369              * There is an assumption that mouse cursor didn't move\r
370              * since the last check of menu activity state:\r
371              */\r
372             int menuID = menuEntry->Ordinal;\r
373 \r
374             /* So have the highlight drawn... */\r
375             glColor4fv( menu_pen_hback );\r
376             glBegin( GL_QUADS );\r
377                 glVertex2i( border,\r
378                             (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );\r
379                 glVertex2i( menu->Width - border,\r
380                             (menuID + 0)*FREEGLUT_MENU_HEIGHT + border );\r
381                 glVertex2i( menu->Width - border,\r
382                             (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );\r
383                 glVertex2i( border,\r
384                             (menuID + 1)*FREEGLUT_MENU_HEIGHT + border );\r
385             glEnd( );\r
386         }\r
387     }\r
388 \r
389     /* Print the menu entries now... */\r
390 \r
391     glColor4fv( menu_pen_fore );\r
392 \r
393     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First, i = 0;\r
394          menuEntry;\r
395          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next, ++i )\r
396     {\r
397         /* If the menu entry is active, set the color to white */\r
398         if( menuEntry->IsActive )\r
399             glColor4fv( menu_pen_hfore );\r
400 \r
401         /* Move the raster into position... */\r
402         /* Try to center the text - JCJ 31 July 2003*/\r
403         glRasterPos2i(\r
404             2 * border,\r
405             ( i + 1 )*FREEGLUT_MENU_HEIGHT -\r
406             ( int )( FREEGLUT_MENU_HEIGHT*0.3 - border )\r
407         );\r
408 \r
409         /* Have the label drawn, character after character: */\r
410         glutBitmapString( FREEGLUT_MENU_FONT,\r
411                           (unsigned char *)menuEntry->Text);\r
412 \r
413         /* If it's a submenu, draw a right arrow */\r
414         if( menuEntry->SubMenu )\r
415         {\r
416             int width = glutBitmapWidth( FREEGLUT_MENU_FONT, '_' );\r
417             int x_base = menu->Width - 2 - width;\r
418             int y_base = i*FREEGLUT_MENU_HEIGHT + border;\r
419             glBegin( GL_TRIANGLES );\r
420                 glVertex2i( x_base, y_base + 2*border);\r
421                 glVertex2i( menu->Width - 2, y_base +\r
422                             ( FREEGLUT_MENU_HEIGHT + border) / 2 );\r
423                 glVertex2i( x_base, y_base + FREEGLUT_MENU_HEIGHT - border );\r
424             glEnd( );\r
425         }\r
426 \r
427         /* If the menu entry is active, reset the color */\r
428         if( menuEntry->IsActive )\r
429             glColor4fv( menu_pen_fore );\r
430     }\r
431 }\r
432 \r
433 /*\r
434  * Private static function to set the parent window of a submenu and all\r
435  * of its submenus\r
436  */\r
437 static void fghSetMenuParentWindow( SFG_Window *window, SFG_Menu *menu )\r
438 {\r
439     SFG_MenuEntry *menuEntry;\r
440 \r
441     menu->ParentWindow = window;\r
442 \r
443     for( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;\r
444          menuEntry;\r
445          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )\r
446         if( menuEntry->SubMenu )\r
447             fghSetMenuParentWindow( window, menuEntry->SubMenu );\r
448 }\r
449 \r
450 /*\r
451  * Function to check for menu entry selection on menu deactivation\r
452  */\r
453 static void fghExecuteMenuCallback( SFG_Menu* menu )\r
454 {\r
455     SFG_MenuEntry *menuEntry;\r
456 \r
457     /* First of all check any of the active sub menus... */\r
458     for( menuEntry = (SFG_MenuEntry *)menu->Entries.First;\r
459          menuEntry;\r
460          menuEntry = (SFG_MenuEntry *)menuEntry->Node.Next)\r
461     {\r
462         if( menuEntry->IsActive )\r
463         {\r
464             if( menuEntry->SubMenu )\r
465                 fghExecuteMenuCallback( menuEntry->SubMenu );\r
466             else\r
467                 if( menu->Callback )\r
468                 {\r
469                     SFG_Menu *save_menu = fgStructure.CurrentMenu;\r
470                     fgStructure.CurrentMenu = menu;\r
471                     menu->Callback( menuEntry->ID );\r
472                     fgStructure.CurrentMenu = save_menu;\r
473                 }\r
474 \r
475             return;\r
476         }\r
477     }\r
478 }\r
479 \r
480 \r
481 /*\r
482  * Displays the currently active menu for the current window\r
483  */\r
484 void fgDisplayMenu( void )\r
485 {\r
486     SFG_Window* window = fgStructure.CurrentWindow;\r
487     SFG_Menu* menu = NULL;\r
488 \r
489     FREEGLUT_INTERNAL_ERROR_EXIT ( fgStructure.CurrentWindow, "Displaying menu in nonexistent window",\r
490                                    "fgDisplayMenu" );\r
491 \r
492     /* Check if there is an active menu attached to this window... */\r
493     menu = window->ActiveMenu;\r
494     freeglut_return_if_fail( menu );\r
495 \r
496     fgSetWindow( menu->Window );\r
497 \r
498     glPushAttrib( GL_DEPTH_BUFFER_BIT | GL_TEXTURE_BIT | GL_LIGHTING_BIT |\r
499                   GL_POLYGON_BIT );\r
500 \r
501     glDisable( GL_DEPTH_TEST );\r
502     glDisable( GL_TEXTURE_2D );\r
503     glDisable( GL_LIGHTING   );\r
504     glDisable( GL_CULL_FACE  );\r
505 \r
506     glMatrixMode( GL_PROJECTION );\r
507     glPushMatrix( );\r
508     glLoadIdentity( );\r
509     glOrtho(\r
510          0, glutGet( GLUT_WINDOW_WIDTH  ),\r
511          glutGet( GLUT_WINDOW_HEIGHT ), 0,\r
512         -1, 1\r
513     );\r
514 \r
515     glMatrixMode( GL_MODELVIEW );\r
516     glPushMatrix( );\r
517     glLoadIdentity( );\r
518 \r
519     fghDisplayMenuBox( menu );\r
520 \r
521     glPopAttrib( );\r
522 \r
523     glMatrixMode( GL_PROJECTION );\r
524     glPopMatrix( );\r
525     glMatrixMode( GL_MODELVIEW );\r
526     glPopMatrix( );\r
527 \r
528     glutSwapBuffers( );\r
529 \r
530     fgSetWindow ( window );\r
531 }\r
532 \r
533 /*\r
534  * Activates a menu pointed by the function argument\r
535  */\r
536 static void fghActivateMenu( SFG_Window* window, int button )\r
537 {\r
538     int max_x, max_y;\r
539 \r
540     /* We'll be referencing this menu a lot, so remember its address: */\r
541     SFG_Menu* menu = window->Menu[ button ];\r
542     SFG_Window* current_window = fgStructure.CurrentWindow;\r
543 \r
544     /* If the menu is already active in another window, deactivate it there */\r
545     if ( menu->ParentWindow )\r
546       menu->ParentWindow->ActiveMenu = NULL ;\r
547 \r
548     /* Mark the menu as active, so that it gets displayed: */\r
549     window->ActiveMenu = menu;\r
550     menu->IsActive = GL_TRUE;\r
551     fghSetMenuParentWindow ( window, menu );\r
552     fgState.ActiveMenus++;\r
553 \r
554     /* Set up the initial menu position now: */\r
555     fghGetVMaxExtent(menu->ParentWindow, &max_x, &max_y);\r
556     fgSetWindow( window );\r
557     menu->X = window->State.MouseX + glutGet( GLUT_WINDOW_X );\r
558     menu->Y = window->State.MouseY + glutGet( GLUT_WINDOW_Y );\r
559 \r
560     if( menu->X + menu->Width > max_x )\r
561         menu->X -=menu->Width;\r
562 \r
563     if( menu->Y + menu->Height > max_y )\r
564     {\r
565         menu->Y -=menu->Height;\r
566         if( menu->Y < 0 )\r
567             menu->Y = 0;\r
568     }\r
569 \r
570     menu->Window->State.MouseX =\r
571         window->State.MouseX + glutGet( GLUT_WINDOW_X ) - menu->X;\r
572     menu->Window->State.MouseY =\r
573         window->State.MouseY + glutGet( GLUT_WINDOW_Y ) - menu->Y;\r
574 \r
575     fgSetWindow( menu->Window );\r
576     glutPositionWindow( menu->X, menu->Y );\r
577     glutReshapeWindow( menu->Width, menu->Height );\r
578     glutPopWindow( );\r
579     glutShowWindow( );\r
580     menu->Window->ActiveMenu = menu;\r
581     fghCheckMenuStatus( menu );\r
582     fgSetWindow( current_window );\r
583 }\r
584 \r
585 /*\r
586  * Update Highlight states of the menu\r
587  *\r
588  * Current mouse position is in menu->Window->State.MouseX/Y.\r
589  */\r
590 void fgUpdateMenuHighlight ( SFG_Menu *menu )\r
591 {\r
592     fghCheckMenuStatus( menu );\r
593 }\r
594 \r
595 /*\r
596  * Check whether an active menu absorbs a mouse click\r
597  */\r
598 GLboolean fgCheckActiveMenu ( SFG_Window *window, int button, GLboolean pressed,\r
599                               int mouse_x, int mouse_y )\r
600 {\r
601     /*\r
602      * Near as I can tell, this is the menu behaviour:\r
603      *  - Down-click the menu button, menu not active:  activate\r
604      *    the menu with its upper left-hand corner at the mouse\r
605      *    location.\r
606      *  - Down-click any button outside the menu, menu active:\r
607      *    deactivate the menu\r
608      *  - Down-click any button inside the menu, menu active:\r
609      *    select the menu entry and deactivate the menu\r
610      *  - Up-click the menu button, menu not active:  nothing happens\r
611      *  - Up-click the menu button outside the menu, menu active:\r
612      *    nothing happens\r
613      *  - Up-click the menu button inside the menu, menu active:\r
614      *    select the menu entry and deactivate the menu\r
615      * Since menus can have submenus, we need to check this recursively.\r
616      */\r
617     if( window->ActiveMenu )\r
618     {\r
619         if( window == window->ActiveMenu->ParentWindow )\r
620         {\r
621             window->ActiveMenu->Window->State.MouseX =\r
622                                        mouse_x - window->ActiveMenu->X;\r
623             window->ActiveMenu->Window->State.MouseY =\r
624                                        mouse_y - window->ActiveMenu->Y;\r
625         }\r
626 \r
627         /* In the menu, invoke the callback and deactivate the menu */\r
628         if( fghCheckMenuStatus( window->ActiveMenu ) )\r
629         {\r
630             /*\r
631              * Save the current window and menu and set the current\r
632              * window to the window whose menu this is\r
633              */\r
634             SFG_Window *save_window = fgStructure.CurrentWindow;\r
635             SFG_Menu *save_menu = fgStructure.CurrentMenu;\r
636             SFG_Window *parent_window = window->ActiveMenu->ParentWindow;\r
637             fgSetWindow( parent_window );\r
638             fgStructure.CurrentMenu = window->ActiveMenu;\r
639 \r
640             /* Execute the menu callback */\r
641             fghExecuteMenuCallback( window->ActiveMenu );\r
642             fgDeactivateMenu( parent_window );\r
643 \r
644             /* Restore the current window and menu */\r
645             fgSetWindow( save_window );\r
646             fgStructure.CurrentMenu = save_menu;\r
647         }\r
648         else if( pressed )\r
649             /*\r
650              * Outside the menu, deactivate if it's a downclick\r
651              *\r
652              * XXX This isn't enough.  A downclick outside of\r
653              * XXX the interior of our freeglut windows should also\r
654              * XXX deactivate the menu.  This is more complicated.\r
655              */\r
656             fgDeactivateMenu( window->ActiveMenu->ParentWindow );\r
657 \r
658         /*\r
659          * XXX Why does an active menu require a redisplay at\r
660          * XXX this point?  If this can come out cleanly, then\r
661          * XXX it probably should do so; if not, a comment should\r
662          * XXX explain it.\r
663          */\r
664         if( ! window->IsMenu )\r
665             window->State.Redisplay = GL_TRUE;\r
666 \r
667         return GL_TRUE;\r
668     }\r
669 \r
670     /* No active menu, let's check whether we need to activate one. */\r
671     if( ( 0 <= button ) &&\r
672         ( FREEGLUT_MAX_MENUS > button ) &&\r
673         ( window->Menu[ button ] ) &&\r
674         pressed )\r
675     {\r
676         /* XXX Posting a requisite Redisplay seems bogus. */\r
677         window->State.Redisplay = GL_TRUE;\r
678         fghActivateMenu( window, button );\r
679         return GL_TRUE;\r
680     }\r
681 \r
682     return GL_FALSE;\r
683 }\r
684 \r
685 /*\r
686  * Deactivates a menu pointed by the function argument.\r
687  */\r
688 void fgDeactivateMenu( SFG_Window *window )\r
689 {\r
690     SFG_Window *parent_window = NULL;\r
691 \r
692     /* Check if there is an active menu attached to this window... */\r
693     SFG_Menu* menu = window->ActiveMenu;\r
694     SFG_MenuEntry *menuEntry;\r
695 \r
696     /* Did we find an active window? */\r
697     freeglut_return_if_fail( menu );\r
698 \r
699     parent_window = menu->ParentWindow;\r
700 \r
701     /* Hide the present menu's window */\r
702     fgSetWindow( menu->Window );\r
703     glutHideWindow( );\r
704 \r
705     /* Forget about having that menu active anymore, now: */\r
706     menu->Window->ActiveMenu = NULL;\r
707     menu->ParentWindow->ActiveMenu = NULL;\r
708     fghSetMenuParentWindow ( NULL, menu );\r
709     menu->IsActive = GL_FALSE;\r
710     menu->ActiveEntry = NULL;\r
711 \r
712     fgState.ActiveMenus--;\r
713 \r
714     /* Hide all submenu windows, and the root menu's window. */\r
715     for ( menuEntry = ( SFG_MenuEntry * )menu->Entries.First;\r
716           menuEntry;\r
717           menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )\r
718     {\r
719         menuEntry->IsActive = GL_FALSE;\r
720 \r
721         /* Is that an active submenu by any case? */\r
722         if( menuEntry->SubMenu )\r
723             fghDeactivateSubMenu( menuEntry );\r
724     }\r
725 \r
726     fgSetWindow ( parent_window ) ;\r
727 }\r
728 \r
729 /*\r
730  * Recalculates current menu's box size\r
731  */\r
732 void fghCalculateMenuBoxSize( void )\r
733 {\r
734     SFG_MenuEntry* menuEntry;\r
735     int width = 0, height = 0;\r
736 \r
737     /* Make sure there is a current menu set */\r
738     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
739 \r
740     /* The menu's box size depends on the menu entries: */\r
741     for( menuEntry = ( SFG_MenuEntry * )fgStructure.CurrentMenu->Entries.First;\r
742          menuEntry;\r
743          menuEntry = ( SFG_MenuEntry * )menuEntry->Node.Next )\r
744     {\r
745         /* Update the menu entry's width value */\r
746         menuEntry->Width = glutBitmapLength(\r
747             FREEGLUT_MENU_FONT,\r
748             (unsigned char *)menuEntry->Text\r
749         );\r
750 \r
751         /*\r
752          * If the entry is a submenu, then it needs to be wider to\r
753          * accomodate the arrow. JCJ 31 July 2003\r
754          */\r
755         if (menuEntry->SubMenu )\r
756             menuEntry->Width += glutBitmapLength(\r
757                 FREEGLUT_MENU_FONT,\r
758                 (unsigned char *)"_"\r
759             );\r
760 \r
761         /* Check if it's the biggest we've found */\r
762         if( menuEntry->Width > width )\r
763             width = menuEntry->Width;\r
764 \r
765         height += FREEGLUT_MENU_HEIGHT;\r
766     }\r
767 \r
768     /* Store the menu's box size now: */\r
769     fgStructure.CurrentMenu->Height = height + 2 * FREEGLUT_MENU_BORDER;\r
770     fgStructure.CurrentMenu->Width  = width  + 4 * FREEGLUT_MENU_BORDER;\r
771 }\r
772 \r
773 \r
774 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */\r
775 \r
776 /*\r
777  * Creates a new menu object, adding it to the freeglut structure\r
778  */\r
779 int FGAPIENTRY glutCreateMenu( void(* callback)( int ) )\r
780 {\r
781     /* The menu object creation code resides in freeglut_structure.c */\r
782     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCreateMenu" );\r
783     return fgCreateMenu( callback )->ID;\r
784 }\r
785 \r
786 #if TARGET_HOST_MS_WINDOWS\r
787 int FGAPIENTRY __glutCreateMenuWithExit( void(* callback)( int ), void (__cdecl *exit_function)(int) )\r
788 {\r
789   __glutExitFunc = exit_function;\r
790   return glutCreateMenu( callback );\r
791 }\r
792 #endif\r
793 \r
794 /*\r
795  * Destroys a menu object, removing all references to it\r
796  */\r
797 void FGAPIENTRY glutDestroyMenu( int menuID )\r
798 {\r
799     SFG_Menu* menu;\r
800 \r
801     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDestroyMenu" );\r
802     menu = fgMenuByID( menuID );\r
803 \r
804     freeglut_return_if_fail( menu );\r
805 \r
806     /* The menu object destruction code resides in freeglut_structure.c */\r
807     fgDestroyMenu( menu );\r
808 }\r
809 \r
810 /*\r
811  * Returns the ID number of the currently active menu\r
812  */\r
813 int FGAPIENTRY glutGetMenu( void )\r
814 {\r
815     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenu" );\r
816 \r
817     if( fgStructure.CurrentMenu )\r
818         return fgStructure.CurrentMenu->ID;\r
819 \r
820     return 0;\r
821 }\r
822 \r
823 /*\r
824  * Sets the current menu given its menu ID\r
825  */\r
826 void FGAPIENTRY glutSetMenu( int menuID )\r
827 {\r
828     SFG_Menu* menu;\r
829 \r
830     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenu" );\r
831     menu = fgMenuByID( menuID );\r
832 \r
833     freeglut_return_if_fail( menu );\r
834 \r
835     fgStructure.CurrentMenu = menu;\r
836 }\r
837 \r
838 /*\r
839  * Adds a menu entry to the bottom of the current menu\r
840  */\r
841 void FGAPIENTRY glutAddMenuEntry( const char* label, int value )\r
842 {\r
843     SFG_MenuEntry* menuEntry;\r
844     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddMenuEntry" );\r
845     menuEntry = (SFG_MenuEntry *)calloc( sizeof(SFG_MenuEntry), 1 );\r
846     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
847 \r
848     menuEntry->Text = strdup( label );\r
849     menuEntry->ID   = value;\r
850 \r
851     /* Have the new menu entry attached to the current menu */\r
852     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );\r
853 \r
854     fghCalculateMenuBoxSize( );\r
855 }\r
856 \r
857 /*\r
858  * Add a sub menu to the bottom of the current menu\r
859  */\r
860 void FGAPIENTRY glutAddSubMenu( const char *label, int subMenuID )\r
861 {\r
862     SFG_MenuEntry *menuEntry;\r
863     SFG_Menu *subMenu;\r
864 \r
865     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAddSubMenu" );\r
866     menuEntry = ( SFG_MenuEntry * )calloc( sizeof( SFG_MenuEntry ), 1 );\r
867     subMenu = fgMenuByID( subMenuID );\r
868 \r
869     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
870     freeglut_return_if_fail( subMenu );\r
871 \r
872     menuEntry->Text    = strdup( label );\r
873     menuEntry->SubMenu = subMenu;\r
874     menuEntry->ID      = -1;\r
875 \r
876     fgListAppend( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );\r
877     fghCalculateMenuBoxSize( );\r
878 }\r
879 \r
880 /*\r
881  * Changes the specified menu item in the current menu into a menu entry\r
882  */\r
883 void FGAPIENTRY glutChangeToMenuEntry( int item, const char* label, int value )\r
884 {\r
885     SFG_MenuEntry* menuEntry = NULL;\r
886 \r
887     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToMenuEntry" );\r
888     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
889 \r
890     /* Get n-th menu entry in the current menu, starting from one: */\r
891     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );\r
892 \r
893     freeglut_return_if_fail( menuEntry );\r
894 \r
895     /* We want it to become a normal menu entry, so: */\r
896     if( menuEntry->Text )\r
897         free( menuEntry->Text );\r
898 \r
899     menuEntry->Text    = strdup( label );\r
900     menuEntry->ID      = value;\r
901     menuEntry->SubMenu = NULL;\r
902     fghCalculateMenuBoxSize( );\r
903 }\r
904 \r
905 /*\r
906  * Changes the specified menu item in the current menu into a sub-menu trigger.\r
907  */\r
908 void FGAPIENTRY glutChangeToSubMenu( int item, const char* label,\r
909                                      int subMenuID )\r
910 {\r
911     SFG_Menu*      subMenu;\r
912     SFG_MenuEntry* menuEntry;\r
913 \r
914     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutChangeToSubMenu" );\r
915     subMenu = fgMenuByID( subMenuID );\r
916     menuEntry = NULL;\r
917 \r
918     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
919     freeglut_return_if_fail( subMenu );\r
920 \r
921     /* Get n-th menu entry in the current menu, starting from one: */\r
922     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );\r
923 \r
924     freeglut_return_if_fail( menuEntry );\r
925 \r
926     /* We want it to become a sub menu entry, so: */\r
927     if( menuEntry->Text )\r
928         free( menuEntry->Text );\r
929 \r
930     menuEntry->Text    = strdup( label );\r
931     menuEntry->SubMenu = subMenu;\r
932     menuEntry->ID      = -1;\r
933     fghCalculateMenuBoxSize( );\r
934 }\r
935 \r
936 /*\r
937  * Removes the specified menu item from the current menu\r
938  */\r
939 void FGAPIENTRY glutRemoveMenuItem( int item )\r
940 {\r
941     SFG_MenuEntry* menuEntry;\r
942 \r
943     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutRemoveMenuItem" );\r
944     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
945 \r
946     /* Get n-th menu entry in the current menu, starting from one: */\r
947     menuEntry = fghFindMenuEntry( fgStructure.CurrentMenu, item );\r
948 \r
949     freeglut_return_if_fail( menuEntry );\r
950 \r
951     fgListRemove( &fgStructure.CurrentMenu->Entries, &menuEntry->Node );\r
952     if ( menuEntry->Text )\r
953       free( menuEntry->Text );\r
954 \r
955     free( menuEntry );\r
956     fghCalculateMenuBoxSize( );\r
957 }\r
958 \r
959 /*\r
960  * Attaches a menu to the current window\r
961  */\r
962 void FGAPIENTRY glutAttachMenu( int button )\r
963 {\r
964     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutAttachMenu" );\r
965 \r
966     freeglut_return_if_fail( fgStructure.CurrentWindow );\r
967     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
968 \r
969     freeglut_return_if_fail( button >= 0 );\r
970     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );\r
971 \r
972     fgStructure.CurrentWindow->Menu[ button ] = fgStructure.CurrentMenu;\r
973 }\r
974 \r
975 /*\r
976  * Detaches a menu from the current window\r
977  */\r
978 void FGAPIENTRY glutDetachMenu( int button )\r
979 {\r
980     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutDetachMenu" );\r
981 \r
982     freeglut_return_if_fail( fgStructure.CurrentWindow );\r
983     freeglut_return_if_fail( fgStructure.CurrentMenu );\r
984 \r
985     freeglut_return_if_fail( button >= 0 );\r
986     freeglut_return_if_fail( button < FREEGLUT_MAX_MENUS );\r
987 \r
988     fgStructure.CurrentWindow->Menu[ button ] = NULL;\r
989 }\r
990 \r
991 /*\r
992  * A.Donev: Set and retrieve the menu's user data\r
993  */\r
994 void* FGAPIENTRY glutGetMenuData( void )\r
995 {\r
996     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetMenuData" );\r
997     return fgStructure.CurrentMenu->UserData;\r
998 }\r
999 \r
1000 void FGAPIENTRY glutSetMenuData(void* data)\r
1001 {\r
1002     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetMenuData" );\r
1003     fgStructure.CurrentMenu->UserData=data;\r
1004 }\r
1005 \r
1006 /*** END OF FILE ***/\r