📄 glut_menu.c
字号:
/* Copyright (c) Mark J. Kilgard, 1994, 1997. *//* This program is freely distributable without licensing fees and is provided without guarantee or warrantee expressed or implied. This program is -not- in the public domain. *//* The Win32 GLUT file win32_menu.c completely re-implements all the menuing functionality implemented. This file is used only by the X Window System version of GLUT. */#ifdef __VMS#include <GL/vms_x_fix.h>#endif#include <stdlib.h>#include <string.h>#include <stdio.h>#include <errno.h>#include <assert.h>#include <unistd.h>#include <X11/Xlib.h>#include <X11/cursorfont.h> /* for XC_arrow */#include "glutint.h"#include "layerutil.h"void (CDECL *__glutMenuStatusFunc) (int, int, int);GLUTmenuItem *__glutItemSelected;GLUTmenu **__glutMenuList = NULL;static int menuListSize = 0;static XFontStruct *menuFont = NULL;static Cursor menuCursor;static Colormap menuColormap;static Visual *menuVisual;static int menuDepth;static int fontHeight;static GC blackGC, grayGC, whiteGC;static unsigned long menuBlack, menuWhite, menuGray;static unsigned long useSaveUnders;/* A replacement for XAllocColor (originally by Brian Paul). This function should never fail to allocate a color. When XAllocColor fails, we return the nearest matching color. If we have to allocate many colors this function isn't a great solution; the XQueryColors() could be done just once. */static voidnoFaultXAllocColor(Display * dpy, Colormap cmap, int cmapSize, XColor * color){ XColor *ctable, subColor; int i, bestmatch; double mindist; /* 3*2^16^2 exceeds 32-bit long int precision. */ for (;;) { /* First try just using XAllocColor. */ if (XAllocColor(dpy, cmap, color)) { return; } /* Retrieve color table entries. */ /* XXX alloca canidate. */ ctable = (XColor *) malloc(cmapSize * sizeof(XColor)); for (i = 0; i < cmapSize; i++) ctable[i].pixel = i; XQueryColors(dpy, cmap, ctable, cmapSize); /* Find best match. */ bestmatch = -1; mindist = 0.0; for (i = 0; i < cmapSize; i++) { double dr = (double) color->red - (double) ctable[i].red; double dg = (double) color->green - (double) ctable[i].green; double db = (double) color->blue - (double) ctable[i].blue; double dist = dr * dr + dg * dg + db * db; if (bestmatch < 0 || dist < mindist) { bestmatch = i; mindist = dist; } } /* Return result. */ subColor.red = ctable[bestmatch].red; subColor.green = ctable[bestmatch].green; subColor.blue = ctable[bestmatch].blue; free(ctable); if (XAllocColor(dpy, cmap, &subColor)) { *color = subColor; return; } /* Extremely unlikely, but possibly color was deallocated and reallocated by someone else before we could XAllocColor the color cell we located. If so, loop again... */ }}static intifSunCreator(void){ char *xvendor, *glvendor, *renderer; int isSunCreator = 0; /* Until proven that it is. */ int savedDisplayMode = 0; char *savedDisplayString = 0; GLUTwindow *window;#define VENDOR_SUN "Sun Microsystems"#define RENDERER_CREATOR "Creator" /* Check the X vendor string first. It is easier to check than the OpenGL vendor and renderer strings since it doesn't require a valid OpenGL rendering context. Bail early if not connected to a Sun. */ xvendor = ServerVendor(__glutDisplay); if (!strncmp(xvendor, VENDOR_SUN, sizeof(VENDOR_SUN) - 1)) { /* We need a valid current OpenGL rendering context to be able to call glGetString successfully. If there is not a current window, set up a temporary one just to call glGetString with (gag, expensive). */ if (__glutCurrentWindow) { window = NULL; } else { savedDisplayMode = __glutDisplayMode; savedDisplayString = __glutDisplayString; __glutDisplayMode = GLUT_RGB | GLUT_SINGLE; __glutDisplayString = NULL; window = __glutCreateWindow(NULL, 0, 0, 1, 1, 0); } glvendor = (char *) glGetString(GL_VENDOR); if (!strncmp(glvendor, VENDOR_SUN, sizeof(VENDOR_SUN) - 1)) { renderer = (char *) glGetString(GL_RENDERER); if (!strncmp(renderer, RENDERER_CREATOR, sizeof(RENDERER_CREATOR) - 1)) { isSunCreator = 1; } } /* Destroy the temporary window for glGetString if one needed to be created. */ if (window) { __glutDestroyWindow(window, window); __glutDisplayMode = savedDisplayMode; __glutDisplayString = savedDisplayString; } } return isSunCreator;}static voidmenuVisualSetup(void){ XLayerVisualInfo template, *visual, *overlayVisuals; XColor color; Status status; Bool presumablyMesa; int layer, nVisuals, i, dummy; unsigned long *placeHolders = NULL; int numPlaceHolders = 0; Bool allocateHigh; allocateHigh = ifSunCreator(); /* Start with the highest overlay layer and work down. I don't think any hardware has more than 3 overlay layers. */ for (layer = 3; layer > 0; layer--) { template.layer = layer; template.vinfo.screen = __glutScreen; overlayVisuals = __glutXGetLayerVisualInfo(__glutDisplay, VisualScreenMask | VisualLayerMask, &template, &nVisuals); if (overlayVisuals) { /* First, check if the default visual is in this layer. If the default visual is in this layer, we try to use it since it has pre-defined black and white pixels and using the default visual will probably minimize colormap flashing problems. Suggested by Thomas Roell (thomas@xig.com). */ for (i = 0; i < nVisuals; i++) { visual = &overlayVisuals[i]; if (visual->vinfo.colormap_size >= 3) { /* Compare visual IDs just to be safe. */ if (visual->vinfo.visual->visualid == DefaultVisual(__glutDisplay, __glutScreen)->visualid) { /* Settle for default visual. */ menuVisual = DefaultVisual(__glutDisplay, __glutScreen); menuDepth = DefaultDepth(__glutDisplay, __glutScreen); menuColormap = DefaultColormap(__glutDisplay, __glutScreen); menuBlack = BlackPixel(__glutDisplay, __glutScreen); menuWhite = WhitePixel(__glutDisplay, __glutScreen); color.red = color.green = color.blue = 0xaa00; noFaultXAllocColor(__glutDisplay, menuColormap, menuVisual->map_entries, &color); menuGray = color.pixel; useSaveUnders = 0; XFree(overlayVisuals); return; } } } for (i = 0; i < nVisuals; i++) { visual = &overlayVisuals[i]; if (visual->vinfo.colormap_size >= 3) { if (allocateHigh) { /* For Sun's Creator graphics, try to force the read-only colors to the high end of the colormap by first allocating read-write place-holder cells for all but the last three cells. This helps avoid colormap flashing problems. */ numPlaceHolders = visual->vinfo.colormap_size - 3; if (numPlaceHolders > 0) { placeHolders = (unsigned long *) malloc(numPlaceHolders * sizeof(unsigned long)); /* A malloc failure would be harmless. */ } } menuColormap = XCreateColormap(__glutDisplay, __glutRoot, visual->vinfo.visual, AllocNone); if (placeHolders) { /* Again for Sun's Creator graphics, do the actual read-write place-holder cell allocation. */ status = XAllocColorCells(__glutDisplay, menuColormap, False, 0, 0, placeHolders, numPlaceHolders); if (!status) { XFreeColormap(__glutDisplay, menuColormap); free(placeHolders); continue; } } /* Allocate overlay colormap cells in defined order: gray, black, white to match the IRIS GL allocation scheme. Increases likelihood of less overlay colormap flashing. */ /* XXX Nice if these 3 AllocColor's could be done in one protocol round-trip. */ color.red = color.green = color.blue = 0xaa00; status = XAllocColor(__glutDisplay, menuColormap, &color); if (!status) { XFreeColormap(__glutDisplay, menuColormap); if (placeHolders) { free(placeHolders); } continue; } menuGray = color.pixel; color.red = color.green = color.blue = 0x0000; status = XAllocColor(__glutDisplay, menuColormap, &color); if (!status) { XFreeColormap(__glutDisplay, menuColormap); if (placeHolders) { free(placeHolders); } continue; } menuBlack = color.pixel; color.red = color.green = color.blue = 0xffff; status = XAllocColor(__glutDisplay, menuColormap, &color); if (!status) { XFreeColormap(__glutDisplay, menuColormap); if (placeHolders) { free(placeHolders); } continue; } if (placeHolders) { /* Now free the placeholder cells. */ XFreeColors(__glutDisplay, menuColormap, placeHolders, numPlaceHolders, 0); free(placeHolders); } menuWhite = color.pixel; menuVisual = visual->vinfo.visual; menuDepth = visual->vinfo.depth; /* If using overlays, do not request "save unders". */ useSaveUnders = 0; XFree(overlayVisuals); return; } } XFree(overlayVisuals); } } /* Settle for default visual. */ menuVisual = DefaultVisual(__glutDisplay, __glutScreen); menuDepth = DefaultDepth(__glutDisplay, __glutScreen); menuColormap = DefaultColormap(__glutDisplay, __glutScreen); menuBlack = BlackPixel(__glutDisplay, __glutScreen); menuWhite = WhitePixel(__glutDisplay, __glutScreen); color.red = color.green = color.blue = 0xaa00; noFaultXAllocColor(__glutDisplay, menuColormap, menuVisual->map_entries, &color); menuGray = color.pixel; /* When no overlays are supported, we would like to use X "save unders" to avoid exposes to windows obscured by pop-up menus. However, OpenGL's direct rendering support means OpenGL interacts poorly with X backing store and save unders. X servers do not (in implementation practice) redirect OpenGL rendering destined to obscured window regions into backing store. Implementation solutions exist for this problem, but they are expensive and high-end OpenGL implementations typically provide fast rendering and/or overlays to obviate the problem associated of user interfaces (pop-up menus) forcing redraws of complex normal plane scenes. (See support for overlays pop-up menus above.) Mesa 3D, however, does not support direct rendering. Overlays are often unavailable to Mesa, and Mesa is also relatively slow. For these reasons, Mesa-rendering GLUT programs can and should use X save unders. Look for the GLX extension. If _not_ supported, we are presumably using Mesa so enable save unders. */ presumablyMesa = !XQueryExtension(__glutDisplay, "GLX", &dummy, &dummy, &dummy); if (presumablyMesa) { useSaveUnders = CWSaveUnder; } else { useSaveUnders = 0; }}static voidmenuSetup(void){ if (menuFont) { /* MenuFont overload to indicate menu initalization. */ return; } menuFont = XLoadQueryFont(__glutDisplay, "-*-helvetica-bold-o-normal--14-*-*-*-p-*-iso8859-1"); if (!menuFont) { /* Try back up font. */ menuFont = XLoadQueryFont(__glutDisplay, "fixed"); } if (!menuFont) { __glutFatalError("could not load font."); } menuVisualSetup(); fontHeight = menuFont->ascent + menuFont->descent; menuCursor = XCreateFontCursor(__glutDisplay, XC_arrow);}static voidmenuGraphicsContextSetup(Window win){ XGCValues gcvals; if (blackGC != None) { return; } gcvals.font = menuFont->fid; gcvals.foreground = menuBlack; blackGC = XCreateGC(__glutDisplay, win, GCFont | GCForeground, &gcvals); gcvals.foreground = menuGray; grayGC = XCreateGC(__glutDisplay, win, GCForeground, &gcvals); gcvals.foreground = menuWhite; whiteGC = XCreateGC(__glutDisplay, win, GCForeground, &gcvals);}void__glutSetMenu(GLUTmenu * menu){ __glutCurrentMenu = menu;}static voidunmapMenu(GLUTmenu * menu){ if (menu->cascade) { unmapMenu(menu->cascade); menu->cascade = NULL; } menu->anchor = NULL; menu->highlighted = NULL; XUnmapWindow(__glutDisplay, menu->win);}static voidfinishMenu(Window win, int x, int y){ Window dummy; int rc; unmapMenu(__glutMappedMenu); XUngrabPointer(__glutDisplay, CurrentTime); /* Popping up an overlay popup menu will install its own colormap. If the window associated with the menu has an overlay, install that window's overlay colormap so the overlay isn't left using the popup menu's colormap. */ if (__glutMenuWindow->overlay) { XInstallColormap(__glutDisplay, __glutMenuWindow->overlay->colormap->cmap); } /* This XFlush is needed to to make sure the pointer is really ungrabbed when the application's menu callback is called. Otherwise, a deadlock might happen because the application may try to read from an terminal window, but yet the ungrab hasn't really happened since it hasn't been flushed out. */ XFlush(__glutDisplay); if (__glutMenuStatusFunc) { if (win != __glutMenuWindow->win) { /* The button release may have occurred in a window other than the window requesting the pop-up menu (for example, one of the submenu windows). In this case, we need to translate the coordinates into the coordinate system of the window associated with the window. */ rc = XTranslateCoordinates(__glutDisplay, win, __glutMenuWindow->win, x, y, &x, &y, &dummy); assert(rc != False); /* Will always be on same screen. */ } __glutSetWindow(__glutMenuWindow); __glutSetMenu(__glutMappedMenu); /* Setting __glutMappedMenu to NULL permits operations that change menus or destroy the menu window again. */ __glutMappedMenu = NULL; __glutMenuStatusFunc(GLUT_MENU_NOT_IN_USE, x, y); } /* Setting __glutMappedMenu to NULL permits operations that change menus or destroy the menu window again. */ __glutMappedMenu = NULL; /* If an item is selected and it is not a submenu trigger, generate menu callback. */ if (__glutItemSelected && !__glutItemSelected->isTrigger) { __glutSetWindow(__glutMenuWindow); /* When menu callback is triggered, current menu should be set to the callback menu. */ __glutSetMenu(__glutItemSelected->menu); __glutItemSelected->menu->select( __glutItemSelected->value); } __glutMenuWindow = NULL;}#define MENU_BORDER 1#define MENU_GAP 2#define MENU_ARROW_GAP 6#define MENU_ARROW_WIDTH 8static voidmapMenu(GLUTmenu * menu, int x, int y){ XWindowChanges changes; unsigned int mask; int subMenuExtension, num; /* If there are submenus, we need to provide extra space for the submenu pull arrow. */ if (menu->submenus > 0) { subMenuExtension = MENU_ARROW_GAP + MENU_ARROW_WIDTH; } else { subMenuExtension = 0; } changes.stack_mode = Above; mask = CWStackMode | CWX | CWY; /* If the menu isn't managed (ie, validated so all the InputOnly subwindows are the right size), do so. */ if (!menu->managed) { GLUTmenuItem *item; item = menu->list; num = menu->num; while (item) { XWindowChanges itemupdate; itemupdate.y = (num - 1) * fontHeight + MENU_GAP; itemupdate.width = menu->pixwidth; itemupdate.width += subMenuExtension; XConfigureWindow(__glutDisplay, item->win, CWWidth | CWY, &itemupdate); item = item->next; num--; } menu->pixheight = MENU_GAP + fontHeight * menu->num + MENU_GAP; changes.height = menu->pixheight; changes.width = MENU_GAP + menu->pixwidth + subMenuExtension + MENU_GAP; mask |= CWWidth | CWHeight; menu->managed = True; } /* Make sure menu appears fully on screen. */ if (y + menu->pixheight >= __glutScreenHeight) { changes.y = __glutScreenHeight - menu->pixheight; } else { changes.y = y; } if (x + menu->pixwidth + subMenuExtension >= __glutScreenWidth) { changes.x = __glutScreenWidth - menu->pixwidth + subMenuExtension;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -