home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / lib / glut / a.c next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  26.9 KB  |  997 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees
  5.    and is provided without guarantee or warrantee expressed or
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdio.h>
  11. #include <errno.h>
  12. #include <unistd.h>
  13. #include <assert.h>
  14.  
  15. #include <X11/Xlib.h>
  16. #include <X11/cursorfont.h>  /* for XC_arrow */
  17.  
  18. #include <GL/glut.h>
  19. #include "glutint.h"
  20. #include "layerutil.h"
  21.  
  22. GLUTmenu *__glutCurrentMenu = NULL;
  23. void (*__glutMenuStatusFunc) (int, int, int);
  24. GLUTmenu *__glutMappedMenu;
  25. GLUTwindow *__glutMenuWindow;
  26. GLUTmenuItem *__glutItemSelected;
  27.  
  28. static GLUTmenu **menuList = NULL;
  29. static int menuListSize = 0;
  30. static XFontStruct *menuFont = NULL;
  31. static Cursor menuCursor;
  32. static Colormap menuColormap;
  33. static Visual *menuVisual;
  34. static int menuDepth;
  35. static int fontHeight;
  36. static GC blackGC, grayGC, whiteGC;
  37. static unsigned long menuBlack, menuWhite, menuGray;
  38. static unsigned long useSaveUnders;
  39.  
  40.  
  41. /*
  42.  * A replacement for XAllocColor.  This function should never fail
  43.  * to allocate a color.  When XAllocColor fails we return the nearest
  44.  * matching color.  If we have to allocate many colors this function
  45.  * isn't a great solution; the XQueryColors() could be done just once.
  46.  */
  47. static Status x_alloc_color( Display *dpy, Colormap cmap, int cmap_size,
  48.                              XColor *color )
  49. {
  50.    if (!XAllocColor(dpy, cmap, color)) {
  51.       int i, bestmatch;
  52.       double mindist;  /* 3*2^16^2 exceeds long int precision */
  53.       XColor *ctable;
  54.  
  55.       /* retrieve color table entries */
  56.       ctable = (XColor *) malloc( cmap_size * sizeof(XColor) );
  57.       for (i=0;i<cmap_size;i++) {
  58.          ctable[i].pixel = i;
  59.       }
  60.       XQueryColors( dpy, cmap, ctable, cmap_size );
  61.  
  62.       /* find best match */
  63.       bestmatch = -1;
  64.       mindist = 0.0;
  65.       for (i=0;i<cmap_size;i++) {
  66.          double dr = (double) color->red   - (double) ctable[i].red;
  67.          double dg = (double) color->green - (double) ctable[i].green;
  68.          double db = (double) color->blue  - (double) ctable[i].blue;
  69.          double dist = dr*dr + dg*dg + db*db;
  70.          if (bestmatch < 0 || dist < mindist) {
  71.             bestmatch = i;
  72.             mindist = dist;
  73.          }
  74.       }
  75.  
  76.       /* return result */
  77.       color->red   = ctable[bestmatch].red;
  78.       color->green = ctable[bestmatch].green;
  79.       color->blue  = ctable[bestmatch].blue;
  80.       if (XAllocColor( dpy, cmap, color )) {
  81.          /* just to be sure... */
  82.          assert( color->pixel == bestmatch );
  83.       }
  84.       else {
  85.          /* this should never happen but just in case... */
  86.          color->pixel = bestmatch;
  87.       }
  88.    }
  89.    return 1;
  90. }
  91.  
  92.  
  93. static void
  94. menuVisualSetup(void)
  95. {
  96.   XLayerVisualInfo template, *visual, *overlayVisuals;
  97.   XColor color;
  98.   Status status;
  99.   Bool presumablyMesa;
  100.   int layer, nVisuals, i, dummy;
  101.  
  102.   for (layer = 3; layer > 0; layer--) {
  103.     template.layer = layer;
  104.     template.vinfo.screen = __glutScreen;
  105.     overlayVisuals = __glutXGetLayerVisualInfo(__glutDisplay,
  106.       VisualScreenMask | VisualLayerMask, &template, &nVisuals);
  107.     if (overlayVisuals) {
  108.       for (i = 0; i < nVisuals; i++) {
  109.         visual = &overlayVisuals[i];
  110.         if (visual->vinfo.colormap_size >= 3) {
  111.           menuColormap = XCreateColormap(__glutDisplay, __glutRoot,
  112.             visual->vinfo.visual, AllocNone);
  113.           /* Allocate overlay colormap cells in defined order:
  114.              gray, black, white to match the IRIS GL allocation
  115.              scheme.  Increases likelihood of less overlay
  116.              colormap flashing. */
  117.           /* XXX nice if these 3 AllocColor's could be done in
  118.              one protocol round-trip */
  119.           color.red = color.green = color.blue = 0xaa00;
  120.           status = XAllocColor(__glutDisplay,
  121.             menuColormap, &color);
  122.           if (!status) {
  123.             XFreeColormap(__glutDisplay, menuColormap);
  124.             continue;
  125.           }
  126.           menuGray = color.pixel;
  127.           color.red = color.green = color.blue = 0x0000;
  128.           status = XAllocColor(__glutDisplay,
  129.             menuColormap, &color);
  130.           if (!status) {
  131.             XFreeColormap(__glutDisplay, menuColormap);
  132.             continue;
  133.           }
  134.           menuBlack = color.pixel;
  135.           color.red = color.green = color.blue = 0xffff;
  136.           status = XAllocColor(__glutDisplay,
  137.             menuColormap, &color);
  138.           if (!status) {
  139.             XFreeColormap(__glutDisplay, menuColormap);
  140.             continue;
  141.           }
  142.           menuWhite = color.pixel;
  143.           menuVisual = visual->vinfo.visual;
  144.           menuDepth = visual->vinfo.depth;
  145.           /* If using overlays, avoid requesting "save unders".
  146.            */
  147.           useSaveUnders = 0;
  148.           XFree(overlayVisuals);
  149.           return;
  150.         }
  151.       }
  152.     }
  153.   }
  154.   /* settle for default visual */
  155.   menuVisual = DefaultVisual(__glutDisplay, __glutScreen);
  156.   menuDepth = DefaultDepth(__glutDisplay, __glutScreen);
  157.   menuColormap = DefaultColormap(__glutDisplay, __glutScreen);
  158.   menuBlack = BlackPixel(__glutDisplay, __glutScreen);
  159.   menuWhite = WhitePixel(__glutDisplay, __glutScreen);
  160.   color.red = color.green = color.blue = 0xaa00;
  161.   status = x_alloc_color(__glutDisplay, menuColormap,
  162.                          menuVisual->map_entries, &color);
  163.   if (!status) {
  164.     __glutFatalError(
  165.       "could not allocate gray in default colormap");
  166.   }
  167.   menuGray = color.pixel;
  168.  
  169.   /* When no overlays are supported, we would like to use X
  170.      "save unders" to avoid exposes to windows obscured by
  171.      pop-up menus.  However, OpenGL's direct rendering support
  172.      means OpenGL interacts poorly with X backing store and
  173.      save unders.  X servers do not (in implementation
  174.      practice) redirect OpenGL rendering destined to obscured
  175.      window regions into backing store.
  176.  
  177.      Implementation solutions exist for this problem, but they
  178.      are expensive and high-end OpenGL implementations
  179.      typically provide fast rendering and/or overlays to
  180.      obviate the problem associated of user interfaces (pop-up
  181.      menus) forcing redraws of complex normal plane scenes.
  182.      (See support for overlays pop-up menus above.)
  183.  
  184.      Mesa 3D, however, does not support direct rendering.
  185.      Overlays are often unavailable to Mesa, and Mesa is also
  186.      relatively slow.  For these reasons, Mesa-rendering GLUT
  187.      programs can and should use X save unders.
  188.  
  189.      Look for the GLX extension.  If _not_ supported, we are
  190.      presumably using Mesa so enable save unders. */
  191.  
  192.   presumablyMesa = !XQueryExtension(__glutDisplay, "GLX",
  193.     &dummy, &dummy, &dummy);
  194.  
  195.   if (presumablyMesa)
  196.     useSaveUnders = CWSaveUnder;
  197.   else
  198.     useSaveUnders = 0;
  199.  
  200.   XFree(overlayVisuals);
  201. }
  202.  
  203. static void
  204. menuSetup(void)
  205. {
  206.   if (menuFont) {
  207.     /* menuFont overload to indicate menu initalization */
  208.     return;
  209.   }
  210.   menuFont = XLoadQueryFont(__glutDisplay,
  211.     "-*-helvetica-bold-o-normal--14-*-*-*-p-*-iso8859-1");
  212.   if (!menuFont) {
  213.     /* try back up font */
  214.     menuFont = XLoadQueryFont(__glutDisplay, "fixed");
  215.   }
  216.   if (!menuFont) {
  217.     __glutFatalError("could not load font.");
  218.   }
  219.   menuVisualSetup();
  220.   fontHeight = menuFont->ascent + menuFont->descent;
  221.   menuCursor = XCreateFontCursor(__glutDisplay, XC_arrow);
  222. }
  223.  
  224. static void
  225. menuGraphicsContextSetup(Window win)
  226. {
  227.   XGCValues gcvals;
  228.  
  229.   if (blackGC != None)
  230.     return;
  231.   gcvals.font = menuFont->fid;
  232.   gcvals.foreground = menuBlack;
  233.   blackGC = XCreateGC(__glutDisplay, win,
  234.     GCFont | GCForeground, &gcvals);
  235.   gcvals.foreground = menuGray;
  236.   grayGC = XCreateGC(__glutDisplay, win, GCForeground, &gcvals);
  237.   gcvals.foreground = menuWhite;
  238.   whiteGC = XCreateGC(__glutDisplay, win, GCForeground, &gcvals);
  239. }
  240.  
  241. /* OBSOLETE - use glutMenuStatusFunc instead. */
  242. void
  243. glutMenuStateFunc(GLUTmenuStateCB menuStateFunc)
  244. {
  245.   __glutMenuStatusFunc = (GLUTmenuStatusCB) menuStateFunc;
  246. }
  247.  
  248. void
  249. glutMenuStatusFunc(GLUTmenuStatusCB menuStatusFunc)
  250. {
  251.   __glutMenuStatusFunc = menuStatusFunc;
  252. }
  253.  
  254. void
  255. __glutSetMenu(GLUTmenu * menu)
  256. {
  257.   __glutCurrentMenu = menu;
  258. }
  259.  
  260. static void
  261. unmapMenu(GLUTmenu * menu)
  262. {
  263.   if (menu->cascade) {
  264.     unmapMenu(menu->cascade);
  265.     menu->cascade = NULL;
  266.   }
  267.   menu->anchor = NULL;
  268.   menu->highlighted = NULL;
  269.   XUnmapWindow(__glutDisplay, menu->win);
  270. }
  271.  
  272. void
  273. __glutFinishMenu(Window win, int x, int y)
  274. {
  275.   Window dummy;
  276.   int rc;
  277.  
  278.   unmapMenu(__glutMappedMenu);
  279.   XUngrabPointer(__glutDisplay, CurrentTime);
  280.  
  281.   /* This XFlush is needed to to make sure the pointer is
  282.      really ungrabbed when the application's menu callback is
  283.      called. Otherwise, a deadlock might happen because the
  284.      application may try to read from an terminal window, but
  285.      yet the ungrab hasn't really happened since it hasn't been
  286.      flushed out. */
  287.   XFlush(__glutDisplay);
  288.  
  289.   if (__glutMenuStatusFunc) {
  290.     if (win != __glutMenuWindow->win) {
  291.       /* The button release may have occurred in a window other
  292.          than the window requesting the pop-up menu (for
  293.          example, one of the submenu windows).  In this case, we
  294.          need to translate the coordinates into the coordinate
  295.          system of the window associated with the window. */
  296.       rc = XTranslateCoordinates(__glutDisplay, win, __glutMenuWindow->win,
  297.         x, y, &x, &y, &dummy);
  298.       assert(rc != False);  /* Will always be on same screen. */
  299.     }
  300.     __glutSetWindow(__glutMenuWindow);
  301.     __glutSetMenu(__glutMappedMenu);
  302.  
  303.     /* Setting __glutMappedMenu to NULL permits operations that
  304.        change menus or destroy the menu window again. */
  305.     __glutMappedMenu = NULL;
  306.  
  307.     __glutMenuStatusFunc(GLUT_MENU_NOT_IN_USE, x, y);
  308.   }
  309.   /* Setting __glutMappedMenu to NULL permits operations that
  310.      change menus or destroy the menu window again. */
  311.   __glutMappedMenu = NULL;
  312.  
  313.   /* If an item is selected and it is not a submenu trigger,
  314.      generate menu callback. */
  315.   if (__glutItemSelected && !__glutItemSelected->isTrigger) {
  316.     __glutSetWindow(__glutMenuWindow);
  317.     /* When menu callback is triggered, current menu should be
  318.        set to the callback menu. */
  319.     __glutSetMenu(__glutItemSelected->menu);
  320.     __glutItemSelected->menu->select(
  321.       __glutItemSelected->value);
  322.   }
  323.   __glutMenuWindow = NULL;
  324. }
  325.  
  326. #define MENU_BORDER 1
  327. #define MENU_GAP 2
  328. #define MENU_ARROW_GAP 6
  329. #define MENU_ARROW_WIDTH 8
  330.  
  331. static void
  332. mapMenu(GLUTmenu * menu, int x, int y)
  333. {
  334.   XWindowChanges changes;
  335.   unsigned int mask;
  336.   int subMenuExtension, num;
  337.  
  338.   /* If there are submenus, we need to provide extra space for
  339.      the submenu pull arrow.  */
  340.   if (menu->submenus > 0) {
  341.     subMenuExtension = MENU_ARROW_GAP + MENU_ARROW_WIDTH;
  342.   } else {
  343.     subMenuExtension = 0;
  344.   }
  345.  
  346.   changes.stack_mode = Above;
  347.   mask = CWStackMode | CWX | CWY;
  348.   /* If the menu isn't managed (ie, validated so all the
  349.      InputOnly subwindows are the right size), do so.  */
  350.   if (!menu->managed) {
  351.     GLUTmenuItem *item;
  352.  
  353.     item = menu->list;
  354.     num = menu->num;
  355.     while (item) {
  356.       XWindowChanges itemupdate;
  357.  
  358.       itemupdate.y = (num - 1) * fontHeight + MENU_GAP;
  359.       itemupdate.width = menu->pixwidth;
  360.       itemupdate.width += subMenuExtension;
  361.       XConfigureWindow(__glutDisplay, item->win,
  362.         CWWidth | CWY, &itemupdate);
  363.       item = item->next;
  364.       num--;
  365.     }
  366.     menu->pixheight = MENU_GAP +
  367.       fontHeight * menu->num + MENU_GAP;
  368.     changes.height = menu->pixheight;
  369.     changes.width = MENU_GAP +
  370.       menu->pixwidth + subMenuExtension + MENU_GAP;
  371.     mask |= CWWidth | CWHeight;
  372.     menu->managed = True;
  373.   }
  374.   /* make sure menu appears fully on screen */
  375.   if (y + menu->pixheight >= __glutScreenHeight) {
  376.     changes.y = __glutScreenHeight - menu->pixheight;
  377.   } else {
  378.     changes.y = y;
  379.   }
  380.   if (x + menu->pixwidth + subMenuExtension >=
  381.     __glutScreenWidth) {
  382.     changes.x = __glutScreenWidth -
  383.       menu->pixwidth + subMenuExtension;
  384.   } else {
  385.     changes.x = x;
  386.   }
  387.  
  388.   /* Rember where the menu is placed so submenus can be
  389.      properly placed relative to it. */
  390.   menu->x = changes.x;
  391.   menu->y = changes.y;
  392.  
  393.   XConfigureWindow(__glutDisplay, menu->win, mask, &changes);
  394.   XInstallColormap(__glutDisplay, menuColormap);
  395.   /* XXX The XRaiseWindow below should not be necessary because
  396.      the XConfigureWindow requests an Above stack mode (same as
  397.      XRaiseWindow), but some Sun users complained this was still
  398.      necessary.  Probably some window manager or X server bug on
  399.      these machines?? */
  400.   XRaiseWindow(__glutDisplay, menu->win);
  401.   XMapWindow(__glutDisplay, menu->win);
  402. }
  403.  
  404. void
  405. __glutStartMenu(GLUTmenu * menu, GLUTwindow * window,
  406.   int x, int y, int x_win, int y_win)
  407. {
  408.   int grab;
  409.  
  410.   assert(__glutMappedMenu == NULL);
  411.   grab = XGrabPointer(__glutDisplay, __glutRoot, True,
  412.     ButtonPressMask | ButtonReleaseMask,
  413.     GrabModeAsync, GrabModeAsync,
  414.     __glutRoot, menuCursor, CurrentTime);
  415.   if (grab != GrabSuccess) {
  416.     /* Somebody else has pointer grabbed, ignore menu
  417.        activation. */
  418.     return;
  419.   }
  420.   __glutMappedMenu = menu;
  421.   __glutMenuWindow = window;
  422.   __glutItemSelected = NULL;
  423.   if (__glutMenuStatusFunc) {
  424.     __glutSetMenu(menu);
  425.     __glutSetWindow(window);
  426.     __glutMenuStatusFunc(GLUT_MENU_IN_USE, x_win, y_win);
  427.   }
  428.   mapMenu(menu, x, y);
  429. }
  430.  
  431. static void
  432. paintSubMenuArrow(Window win, int x, int y)
  433. {
  434.   XPoint p[5];
  435.  
  436.   p[0].x = p[4].x = x;
  437.   p[0].y = p[4].y = y - menuFont->ascent + 1;
  438.   p[1].x = p[0].x + MENU_ARROW_WIDTH - 1;
  439.   p[1].y = p[0].y + (menuFont->ascent / 2) - 1;
  440.   p[2].x = p[1].x;
  441.   p[2].y = p[1].y + 1;
  442.   p[3].x = p[0].x;
  443.   p[3].y = p[0].y + menuFont->ascent - 2;
  444.   XFillPolygon(__glutDisplay, win,
  445.     whiteGC, p, 4, Convex, CoordModeOrigin);
  446.   XDrawLines(__glutDisplay, win, blackGC, p, 5, CoordModeOrigin);
  447. }
  448.  
  449. static void
  450. paintMenuItem(GLUTmenuItem * item, int num)
  451. {
  452.   Window win = item->menu->win;
  453.   GC gc;
  454.   int y;
  455.   int subMenuExtension;
  456.  
  457.   if (item->menu->submenus > 0) {
  458.     subMenuExtension = MENU_ARROW_GAP + MENU_ARROW_WIDTH;
  459.   } else {
  460.     subMenuExtension = 0;
  461.   }
  462.   if (item->menu->highlighted == item) {
  463.     gc = whiteGC;
  464.   } else {
  465.     gc = grayGC;
  466.   }
  467.   y = MENU_GAP + fontHeight * num - menuFont->descent;
  468.   XFillRectangle(__glutDisplay, win, gc,
  469.     MENU_GAP, y - fontHeight + menuFont->descent,
  470.     item->menu->pixwidth + subMenuExtension, fontHeight);
  471.   XDrawString(__glutDisplay, win, blackGC,
  472.     MENU_GAP, y, item->label, item->len);
  473.   if (item->isTrigger) {
  474.     paintSubMenuArrow(win,
  475.       item->menu->pixwidth + MENU_ARROW_GAP + 1, y);
  476.   }
  477. }
  478.  
  479. void
  480. __glutPaintMenu(GLUTmenu * menu)
  481. {
  482.   GLUTmenuItem *item;
  483.   int i = menu->num;
  484.   int y = MENU_GAP + fontHeight * i - menuFont->descent;
  485.  
  486.   item = menu->list;
  487.   while (item) {
  488.     if (item->menu->highlighted == item) {
  489.       paintMenuItem(item, i);
  490.     } else {
  491.       /* quick render of the menu item; assume background
  492.          already cleared to gray */
  493.       XDrawString(__glutDisplay, menu->win, blackGC,
  494.         2, y, item->label, item->len);
  495.       if (item->isTrigger) {
  496.         paintSubMenuArrow(menu->win,
  497.           menu->pixwidth + MENU_ARROW_GAP + 1, y);
  498.       }
  499.     }
  500.     i--;
  501.     y -= fontHeight;
  502.     item = item->next;
  503.   }
  504. }
  505.  
  506. GLUTmenuItem *
  507. __glutGetMenuItem(GLUTmenu * menu, Window win, int *which)
  508. {
  509.   GLUTmenuItem *item;
  510.   int i;
  511.  
  512.   i = menu->num;
  513.   item = menu->list;
  514.   while (item) {
  515.     if (item->win == win) {
  516.       *which = i;
  517.       return item;
  518.     }
  519.     if (item->isTrigger) {
  520.       GLUTmenuItem *subitem;
  521.  
  522.       subitem = __glutGetMenuItem(menuList[item->value],
  523.         win, which);
  524.       if (subitem) {
  525.         return subitem;
  526.       }
  527.     }
  528.     i--;
  529.     item = item->next;
  530.   }
  531.   return NULL;
  532. }
  533.  
  534. static int
  535. getMenuItemIndex(GLUTmenuItem * item)
  536. {
  537.   int count = 0;
  538.  
  539.   while (item) {
  540.     count++;
  541.     item = item->next;
  542.   }
  543.   return count;
  544. }
  545.  
  546. GLUTmenu *
  547. __glutGetMenu(Window win)
  548. {
  549.   GLUTmenu *menu;
  550.  
  551.   menu = __glutMappedMenu;
  552.   while (menu) {
  553.     if (win == menu->win) {
  554.       return menu;
  555.     }
  556.     menu = menu->cascade;
  557.   }
  558.   return NULL;
  559. }
  560.  
  561. GLUTmenu *
  562. __glutGetMenuByNum(int menunum)
  563. {
  564.   if (menunum < 1 || menunum > menuListSize) {
  565.     return NULL;
  566.   }
  567.   return menuList[menunum - 1];
  568. }
  569.  
  570. static int
  571. getUnusedMenuSlot(void)
  572. {
  573.   int i;
  574.  
  575.   /* Look for allocated, unused slot. */
  576.   for (i = 0; i < menuListSize; i++) {
  577.     if (!menuList[i]) {
  578.       return i;
  579.     }
  580.   }
  581.   /* Allocate a new slot. */
  582.   menuListSize++;
  583.   if (menuList) {
  584.     menuList = (GLUTmenu **)
  585.       realloc(menuList, menuListSize * sizeof(GLUTmenu *));
  586.   } else {
  587.     /* XXX Some realloc's do not correctly perform a malloc
  588.        when asked to perform a realloc on a NULL pointer,
  589.        though the ANSI C library spec requires this. */
  590.     menuList = (GLUTmenu **) malloc(sizeof(GLUTmenu *));
  591.   }
  592.   if (!menuList)
  593.     __glutFatalError("out of memory.");
  594.   menuList[menuListSize - 1] = NULL;
  595.   return menuListSize - 1;
  596. }
  597.  
  598. static void
  599. menuModificationError(void)
  600. {
  601.   /* XXX Remove the warning after GLUT 3.0. */
  602.   __glutWarning("The following is a new check for GLUT 3.0; update your code.");
  603.   __glutFatalError("menu manipulation not allowed while menus in use");
  604. }
  605.  
  606. int
  607. glutCreateMenu(GLUTselectCB selectFunc)
  608. {
  609.   XSetWindowAttributes wa;
  610.   GLUTmenu *menu;
  611.   int menuid;
  612.  
  613.   if (__glutMappedMenu)
  614.     menuModificationError();
  615.   if (!__glutDisplay)
  616.     __glutOpenXConnection(NULL);
  617.   menuid = getUnusedMenuSlot();
  618.   menu = (GLUTmenu *) malloc(sizeof(GLUTmenu));
  619.   if (!menu)
  620.     __glutFatalError("out of memory.");
  621.   menu->id = menuid;
  622.   menu->num = 0;
  623.   menu->submenus = 0;
  624.   menu->managed = False;
  625.   menu->pixwidth = 0;
  626.   menu->select = selectFunc;
  627.   menu->list = NULL;
  628.   menu->cascade = NULL;
  629.   menu->highlighted = NULL;
  630.   menu->anchor = NULL;
  631.   menuSetup();
  632.   wa.override_redirect = True;
  633.   wa.background_pixel = menuGray;
  634.   wa.border_pixel = menuBlack;
  635.   wa.colormap = menuColormap;
  636.   wa.event_mask = StructureNotifyMask | ExposureMask |
  637.     ButtonPressMask | ButtonReleaseMask |
  638.     EnterWindowMask | LeaveWindowMask;
  639.   /* Save unders really only enabled if useSaveUnders is set to
  640.      CWSaveUnder, ie. using Mesa 3D.  See earlier comments. */
  641.   wa.save_under = True;
  642.   menu->win = XCreateWindow(__glutDisplay, __glutRoot,
  643.   /* real position determined when mapped */
  644.     0, 0,
  645.   /* real size will be determined when menu is manged */
  646.     1, 1,
  647.     MENU_BORDER, menuDepth, InputOutput, menuVisual,
  648.     CWOverrideRedirect | CWBackPixel | CWBorderPixel |
  649.     CWEventMask | CWColormap | useSaveUnders,
  650.     &wa);
  651.   menuGraphicsContextSetup(menu->win);
  652.   menuList[menuid] = menu;
  653.   __glutSetMenu(menu);
  654.   return menuid + 1;
  655. }
  656.  
  657. /* CENTRY */
  658. void
  659. glutDestroyMenu(int menunum)
  660. {
  661.   GLUTmenu *menu = __glutGetMenuByNum(menunum);
  662.   GLUTmenuItem *item, *next;
  663.  
  664.   if (__glutMappedMenu)
  665.     menuModificationError();
  666.   assert(menu->id == menunum - 1);
  667.   XDestroySubwindows(__glutDisplay, menu->win);
  668.   XDestroyWindow(__glutDisplay, menu->win);
  669.   menuList[menunum - 1] = NULL;
  670.   /* free all menu entries */
  671.   item = menu->list;
  672.   while (item) {
  673.     assert(item->menu == menu);
  674.     next = item->next;
  675.     free(item->label);
  676.     free(item);
  677.     item = next;
  678.   }
  679.   if (__glutCurrentMenu == menu) {
  680.     __glutCurrentMenu = NULL;
  681.   }
  682.   free(menu);
  683. }
  684.  
  685. int
  686. glutGetMenu(void)
  687. {
  688.   if (__glutCurrentMenu) {
  689.     return __glutCurrentMenu->id + 1;
  690.   } else {
  691.     return 0;
  692.   }
  693. }
  694.  
  695. void
  696. glutSetMenu(int menuid)
  697. {
  698.   GLUTmenu *menu;
  699.  
  700.   if (menuid < 1 || menuid > menuListSize) {
  701.     __glutWarning("glutSetMenu attempted on bogus menu.");
  702.     return;
  703.   }
  704.   menu = menuList[menuid - 1];
  705.   if (!menu) {
  706.     __glutWarning("glutSetMenu attempted on bogus menu.");
  707.     return;
  708.   }
  709.   __glutSetMenu(menu);
  710. }
  711. /* ENDCENTRY */
  712.  
  713. static void
  714. setMenuItem(GLUTmenuItem * item, char *label,
  715.   int value, Bool isTrigger)
  716. {
  717.   GLUTmenu *menu;
  718.  
  719.   menu = item->menu;
  720.   item->label = strdup(label);
  721.   if (!item->label)
  722.     __glutFatalError("out of memory.");
  723.   item->isTrigger = isTrigger;
  724.   item->len = (int) strlen(label);
  725.   item->value = value;
  726.   item->pixwidth = XTextWidth(menuFont, label, item->len) + 4;
  727.   if (item->pixwidth > menu->pixwidth) {
  728.     menu->pixwidth = item->pixwidth;
  729.   }
  730.   menu->managed = False;
  731. }
  732.  
  733. /* CENTRY */
  734. void
  735. glutAddMenuEntry(char *label, int value)
  736. {
  737.   XSetWindowAttributes wa;
  738.   GLUTmenuItem *entry;
  739.  
  740.   if (__glutMappedMenu)
  741.     menuModificationError();
  742.   entry = (GLUTmenuItem *) malloc(sizeof(GLUTmenuItem));
  743.   if (!entry)
  744.     __glutFatalError("out of memory.");
  745.   entry->menu = __glutCurrentMenu;
  746.   setMenuItem(entry, label, value, False);
  747.   wa.event_mask = EnterWindowMask | LeaveWindowMask;
  748.   entry->win = XCreateWindow(__glutDisplay,
  749.     __glutCurrentMenu->win, MENU_GAP,
  750.     __glutCurrentMenu->num * fontHeight + MENU_GAP,  /* x & y */
  751.     entry->pixwidth, fontHeight,  /* width & height */
  752.     0, CopyFromParent, InputOnly, CopyFromParent,
  753.     CWEventMask, &wa);
  754.   XMapWindow(__glutDisplay, entry->win);
  755.   __glutCurrentMenu->num++;
  756.   entry->next = __glutCurrentMenu->list;
  757.   __glutCurrentMenu->list = entry;
  758. }
  759.  
  760. void
  761. glutAddSubMenu(char *label, int menu)
  762. {
  763.   XSetWindowAttributes wa;
  764.   GLUTmenuItem *submenu;
  765.  
  766.   if (__glutMappedMenu)
  767.     menuModificationError();
  768.   submenu = (GLUTmenuItem *) malloc(sizeof(GLUTmenuItem));
  769.   if (!submenu)
  770.     __glutFatalError("out of memory.");
  771.   __glutCurrentMenu->submenus++;
  772.   submenu->menu = __glutCurrentMenu;
  773.   setMenuItem(submenu, label, /* base 0 */ menu - 1, True);
  774.   wa.event_mask = EnterWindowMask | LeaveWindowMask;
  775.   submenu->win = XCreateWindow(__glutDisplay,
  776.     __glutCurrentMenu->win, MENU_GAP,
  777.     __glutCurrentMenu->num * fontHeight + MENU_GAP,  /* x & y */
  778.     submenu->pixwidth, fontHeight,  /* width & height */
  779.     0, CopyFromParent, InputOnly, CopyFromParent,
  780.     CWEventMask, &wa);
  781.   XMapWindow(__glutDisplay, submenu->win);
  782.   __glutCurrentMenu->num++;
  783.   submenu->next = __glutCurrentMenu->list;
  784.   __glutCurrentMenu->list = submenu;
  785. }
  786.  
  787. void
  788. glutChangeToMenuEntry(int num, char *label, int value)
  789. {
  790.   GLUTmenuItem *item;
  791.   int i;
  792.  
  793.   if (__glutMappedMenu)
  794.     menuModificationError();
  795.   i = __glutCurrentMenu->num;
  796.   item = __glutCurrentMenu->list;
  797.   while (item) {
  798.     if (i == num) {
  799.       if (item->isTrigger) {
  800.         /* If changing a submenu trigger to a menu entry, we
  801.            need to account for submenus.  */
  802.         item->menu->submenus--;
  803.       }
  804.       free(item->label);
  805.       setMenuItem(item, label, value, False);
  806.       return;
  807.     }
  808.     i--;
  809.     item = item->next;
  810.   }
  811.   __glutWarning("Current menu has no %d item.", num);
  812. }
  813.  
  814. void
  815. glutChangeToSubMenu(int num, char *label, int menu)
  816. {
  817.   GLUTmenuItem *item;
  818.   int i;
  819.  
  820.   if (__glutMappedMenu)
  821.     menuModificationError();
  822.   i = __glutCurrentMenu->num;
  823.   item = __glutCurrentMenu->list;
  824.   while (item) {
  825.     if (i == num) {
  826.       if (!item->isTrigger) {
  827.         /* If changing a menu entry to as submenu trigger, we
  828.            need to account for submenus.  */
  829.         item->menu->submenus++;
  830.       }
  831.       free(item->label);
  832.       setMenuItem(item, label, /* base 0 */ menu - 1, True);
  833.       return;
  834.     }
  835.     i--;
  836.     item = item->next;
  837.   }
  838.   __glutWarning("Current menu has no %d item.", num);
  839. }
  840.  
  841. void
  842. glutRemoveMenuItem(int num)
  843. {
  844.   GLUTmenuItem *item, **prev, *remaining;
  845.   int pixwidth, i;
  846.  
  847.   if (__glutMappedMenu)
  848.     menuModificationError();
  849.   i = __glutCurrentMenu->num;
  850.   prev = &__glutCurrentMenu->list;
  851.   item = __glutCurrentMenu->list;
  852.   /* If menu item is removed, the menu's pixwidth may need to
  853.      be recomputed. */
  854.   pixwidth = 0;
  855.   while (item) {
  856.     if (i == num) {
  857.       /* If this menu item's pixwidth is as wide as the menu's
  858.          pixwidth, removing this menu item will necessitate
  859.          shrinking the menu's pixwidth. */
  860.       if (item->pixwidth >= __glutCurrentMenu->pixwidth) {
  861.         /* Continue recalculating menu pixwidth, first skipping
  862.            the removed item. */
  863.         remaining = item->next;
  864.         while (remaining) {
  865.           if (remaining->pixwidth > pixwidth) {
  866.             pixwidth = remaining->pixwidth;
  867.           }
  868.           remaining = remaining->next;
  869.         }
  870.       }
  871.       __glutCurrentMenu->num--;
  872.       __glutCurrentMenu->managed = False;
  873.       __glutCurrentMenu->pixwidth = pixwidth;
  874.  
  875.       /* Patch up menu's item list. */
  876.       *prev = item->next;
  877.  
  878.       free(item->label);
  879.       free(item);
  880.       return;
  881.     }
  882.     if (item->pixwidth > pixwidth) {
  883.       pixwidth = item->pixwidth;
  884.     }
  885.     i--;
  886.     prev = &item->next;
  887.     item = item->next;
  888.   }
  889.   __glutWarning("Current menu has no %d item.", num);
  890. }
  891.  
  892. void
  893. glutAttachMenu(int button)
  894. {
  895.   if (__glutMappedMenu)
  896.     menuModificationError();
  897.   if (__glutCurrentWindow->menu[button] < 1) {
  898.     __glutCurrentWindow->buttonUses++;
  899.   }
  900.   __glutChangeWindowEventMask(
  901.     ButtonPressMask | ButtonReleaseMask, True);
  902.   __glutCurrentWindow->menu[button] = __glutCurrentMenu->id + 1;
  903. }
  904.  
  905. void
  906. glutDetachMenu(int button)
  907. {
  908.   if (__glutMappedMenu)
  909.     menuModificationError();
  910.   if (__glutCurrentWindow->menu[button] > 0) {
  911.     __glutCurrentWindow->buttonUses--;
  912.     __glutChangeWindowEventMask(ButtonPressMask | ButtonReleaseMask,
  913.       __glutCurrentWindow->buttonUses > 0);
  914.     __glutCurrentWindow->menu[button] = 0;
  915.   }
  916. }
  917. /* ENDCENTRY */
  918.  
  919. void
  920. __glutMenuItemEnterOrLeave(GLUTmenuItem * item,
  921.   int num, int type)
  922. {
  923.   int alreadyUp = 0;
  924.  
  925.   if (type == EnterNotify) {
  926.     GLUTmenuItem *prevItem = item->menu->highlighted;
  927.  
  928.     if (prevItem && prevItem != item) {
  929.       /* If there's an already higlighted item in this menu
  930.          that is different from this one (we could be
  931.          re-entering an item with an already cascaded
  932.          submenu!), unhighlight the previous item. */
  933.       item->menu->highlighted = NULL;
  934.       paintMenuItem(prevItem, getMenuItemIndex(prevItem));
  935.     }
  936.     item->menu->highlighted = item;
  937.     __glutItemSelected = item;
  938.     if (item->menu->cascade) {
  939.       if (!item->isTrigger) {
  940.         /* Entered a menu item that is not a submenu trigger,
  941.            so pop down the current submenu cascade of this
  942.            menu.  */
  943.         unmapMenu(item->menu->cascade);
  944.         item->menu->cascade = NULL;
  945.       } else {
  946.         GLUTmenu *submenu = menuList[item->value];
  947.  
  948.         if (submenu->anchor == item) {
  949.           /* We entered the submenu trigger for the submenu
  950.              that is already up, so don't take down the
  951.              submenu.  */
  952.           alreadyUp = 1;
  953.         } else {
  954.           /* Submenu already popped up for some other submenu
  955.              item of this menu; need to pop down that other
  956.              submenu cascade.  */
  957.           unmapMenu(item->menu->cascade);
  958.           item->menu->cascade = NULL;
  959.         }
  960.       }
  961.     }
  962.     if (!alreadyUp) {
  963.       /* Make sure the menu item gets painted with
  964.          highlighting. */
  965.       paintMenuItem(item, num);
  966.     } else {
  967.       /* If already up, should already be highlighted.  */
  968.     }
  969.   } else {
  970.     /* LeaveNotify: Handle leaving a menu item...  */
  971.     if (item->menu->cascade &&
  972.       item->menu->cascade->anchor == item) {
  973.       /* If there is a submenu casacaded from this item, do not
  974.  
  975.          change the highlighting on this item upon leaving. */
  976.     } else {
  977.       /* Unhighlight this menu item.  */
  978.       item->menu->highlighted = NULL;
  979.       paintMenuItem(item, num);
  980.     }
  981.     __glutItemSelected = NULL;
  982.   }
  983.   if (item->isTrigger) {
  984.     if (type == EnterNotify && !alreadyUp) {
  985.       GLUTmenu *submenu = menuList[item->value];
  986.  
  987.       mapMenu(submenu,
  988.         item->menu->x + item->menu->pixwidth +
  989.         MENU_ARROW_GAP + MENU_ARROW_WIDTH +
  990.         MENU_GAP + MENU_BORDER,
  991.         item->menu->y + fontHeight * (num - 1) + MENU_GAP);
  992.       item->menu->cascade = submenu;
  993.       submenu->anchor = item;
  994.     }
  995.   }
  996. }
  997.