📄 menu.c
字号:
0, 0, 0, 0, x, y); UnmapMenu(menu); return(TRUE); } } }}/* * Create the menu windows for later use. */CreateMenus(){ MenuLink *ptr; /* * If MaxColors isn't set, then jam it to an impossibly high * number. */ if (MaxColors == 0) MaxColors = 25000; for(ptr = Menus; ptr; ptr = ptr->next) InitMenu(ptr->menu);}/* * Initialize a menu. */InitMenu(menu)MenuInfo *menu;{ MenuLine *ml; /* Menu lines pointer. */ int width; /* Width of an item name. */ int maxwidth; /* Maximum width of item names. */ int len; /* Length of an item name. */ int count = 1; /* Number of items + 1 for name. */ XSetWindowAttributes attr; /* * Determine the name of the longest menu item. */ maxwidth = XTextWidth(MFontInfo, menu->name, strlen(menu->name)); if (maxwidth == 0) Error("InitMenu -> Couldn't get length of menu name"); for(ml = menu->line; ml; ml = ml->next) { if ((len = strlen(ml->name)) == 0) break; width = XTextWidth(MFontInfo, ml->name, strlen(ml->name)); if (width == 0) Error("InitMenu -> Couldn't get length of menu item name"); if (width > maxwidth) maxwidth = width; count++; } /* * Get the color cells for the menu items. */ GetMenuColors(menu); /* * Stash the menu parameters in the menu info structure. */ menu->iheight = MFontInfo->ascent + MFontInfo->descent + (VMenuPad << 1); menu->height = menu->iheight * count; menu->width = maxwidth + (HMenuPad << 1); menu->image =(Pixmap)NULL; /* * Create the menu window. */ attr.border_pixel = MBorder; /* * SaveUnders Enabled If The Server Supports It */ attr.save_under = DoesSaveUnders( DefaultScreenOfDisplay( dpy ) ); /* * We want enter, leave, and mouse motion events for menus. */ attr.event_mask = ( EnterWindowMask | LeaveWindowMask | PointerMotionMask ); /* * Define a cursor for the window */ attr.cursor = MenuCursor; menu->w = XCreateWindow( dpy, RootWindow( dpy, scr ), 0, 0, menu->width, menu->height, MBorderWidth, DefaultDepth( dpy, scr ), InputOutput, DefaultVisual( dpy, scr ), CWBorderPixel | CWSaveUnder | CWEventMask | CWCursor, &attr ); if (menu->w == (Window)NULL) Error("InitMenu -> Couldn't create menu window"); /* * For monochrome servers, we *do* want to set a background pixel */ if (alternateGC) { XSetWindowBackground( dpy, menu->w, MBackground ); } /* * Store the window name. */ XStoreName(dpy, menu->w, menu->name);}/* * Map a menu. */MapMenu(menu, x, y)MenuInfo *menu;int x, y;{ int item; Window w; MenuLine *ml; XWindowChanges values; w = menu->w; /* * Move the menu into place, normalizing the coordinates, if necessary; * then map it. */ x -= (menu->width >> 1); if (x < 0) x = 0; else if (x + menu->width >= ScreenWidth) x = ScreenWidth - menu->width - (MBorderWidth << 1); if (y < 0) y = 0; else if (y + menu->height >= ScreenHeight) y = ScreenHeight - menu->height - (MBorderWidth << 1); values.x = x; values.y = y; values.stack_mode = Above; XConfigureWindow(dpy, w, CWX|CWY|CWStackMode, &values); /* * Map the window and draw the text items. */ XMapWindow(dpy, w); DisplayLine(w, 0, menu->width, menu->iheight, menu->name, menu->bg.pixel, menu->fg.pixel, 0); if (alternateGC) { XFillRectangle(dpy, menu->w, MenuInvGC, 0, 0, menu->width, menu->iheight); XDrawRectangle(dpy, menu->w, MenuInvGC, 1, 1, menu->width - 3, menu->iheight - 3); } else { XSetForeground(dpy, MenuGC, menu->bg.pixel ); XDrawRectangle(dpy, menu->w, MenuGC, 1, 1, menu->width - 3, menu->iheight - 3); } item = menu->iheight; for(ml = menu->line; ml; ml = ml->next) { DisplayLine(w, item, menu->width, menu->iheight, ml->name, ml->fg.pixel, ml->bg.pixel, 0); item += menu->iheight; } /* * Position the mouse cursor in the menu header (or in the first item * if "autoselect" is set). */ XFlush(dpy);}/* * Unmap a menu, restoring the contents of the screen underneath * if necessary. (Restore portion is a future.) */UnmapMenu(menu)MenuInfo *menu;{ /* * Unmap and flush. */ XUnmapWindow(dpy, menu->w); XFlush(dpy);}/* * Get the context for invoking a window manager function. */GetContext(w, x, y)Window *w;int *x, *y;{ XEvent button_event; /* Button input event. */ while (TRUE) { /* * Get the next mouse button event. Spin our wheels until * a button event is returned (ie. GetButton == TRUE). * Note that mouse events within an icon window are handled * in the "GetButton" function or by the icon's owner if * it is not uwm. */ if (!GetButton(&button_event)) continue; /* * If the button event received is not a ButtonPress event * then continue until we find one. */ if (button_event.type != ButtonPress) continue; /* * Okay, determine the event window and mouse coordinates. */ status = XTranslateCoordinates(dpy, RootWindow(dpy, scr), RootWindow(dpy, scr), ((XButtonPressedEvent *)&button_event)->x, ((XButtonPressedEvent *)&button_event)->y, x, y, w); if (status == FAILURE) continue; if (*w == 0) *w = RootWindow(dpy, scr); return; }}/* * Get the color cells for a menu. This function is slightly brain-damaged * in that once MaxColors <= 1, then it refuses to even try to allocate any * more colors, even though the colors may have already been allocated. It * probably ought to be done right someday. */GetMenuColors(menu)MenuInfo *menu;{ register MenuLine *ml; /* Menu lines pointer. */ /* * If we have more than 2 colors available, then attempt to get * the color map entries requested by the user. * Otherwise, default to standard black and white. * * The boolean "alternateGC" is true iff we can use just two colors. */ if (DisplayCells(dpy, scr) > 2) { /* * Get the menu header colors first. */ if (!(menu->foreground && menu->background && MaxColors > 1 && XParseColor(dpy, DefaultColormap(dpy, scr), menu->foreground, &menu->fg) && XAllocColor(dpy, DefaultColormap(dpy, scr), &menu->fg) && XParseColor(dpy, DefaultColormap(dpy, scr), menu->background, &menu->bg) && XAllocColor(dpy, DefaultColormap(dpy, scr), &menu->bg))) { menu->fg.pixel = MTextForground; menu->bg.pixel = MTextBackground; } else { AdjustMaxColors(menu->fg.pixel); AdjustMaxColors(menu->bg.pixel); alternateGC = False; /* since we just allocated colors */ } /* * Get the menu highlight colors. */ if (!(menu->fghighlight && menu->bghighlight && MaxColors > 1 && XParseColor( dpy, DefaultColormap(dpy, scr), menu->fghighlight, &menu->hlfg) && XAllocColor(dpy, DefaultColormap(dpy, scr), &menu->hlfg) && XParseColor( dpy, DefaultColormap(dpy, scr), menu->bghighlight, &menu->hlbg) && XAllocColor(dpy, DefaultColormap(dpy, scr), &menu->hlbg))) { menu->hlfg.pixel = MTextBackground; menu->hlbg.pixel = MTextForground; } else { AdjustMaxColors(menu->hlfg.pixel); AdjustMaxColors(menu->hlbg.pixel); alternateGC = False; } /* * Get the menu item colors. */ for(ml = menu->line; ml; ml = ml->next) { if (!(ml->foreground && ml->background && MaxColors > 1 && XParseColor(dpy, DefaultColormap(dpy, scr), ml->foreground, &ml->fg) && XAllocColor(dpy, DefaultColormap(dpy, scr), &ml->fg) && XParseColor(dpy, DefaultColormap(dpy, scr), ml->background, &ml->bg) && XAllocColor(dpy, DefaultColormap(dpy, scr), &ml->bg))) { ml->fg.pixel = MTextForground; ml->bg.pixel = MTextBackground; } else { AdjustMaxColors(ml->fg.pixel); AdjustMaxColors(ml->bg.pixel); } } } else { /* * Only 2 colors available, so default to standard black and white. */ menu->fg.pixel = MTextForground; menu->bg.pixel = MTextBackground; menu->hlfg.pixel = MTextBackground; menu->hlbg.pixel = MTextForground; for(ml = menu->line; ml; ml = ml->next) { ml->fg.pixel = MTextForground; ml->bg.pixel = MTextBackground; } }}/* * Decrement "MaxColors" if this pixel value has never been used in a * menu before. */AdjustMaxColors(pixel)int pixel;{ register MenuLink *mptr; register MenuLine *lptr; int count = 0; for(mptr = Menus; mptr; mptr = mptr->next) { if (mptr->menu->fg.pixel == pixel) ++count; if (mptr->menu->bg.pixel == pixel) ++count; if (mptr->menu->hlfg.pixel == pixel) ++count; if (mptr->menu->hlbg.pixel == pixel) ++count; for(lptr = mptr->menu->line; lptr; lptr = lptr->next) { if (lptr->fg.pixel == pixel) ++count; if (lptr->bg.pixel == pixel) ++count; } if (count > 1) return; } --MaxColors;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -