#include #include #include #include #include #define MAX(A,B) ((A) > (B) ? (A) : (B)) #define SB_WIDTH 40 /* Button Width */ #define SB_HEIGHT 15 /* Button Height */ #define SB_LONGLABEL 50 /* Longest MenuButton label */ #define SB_CURSOR XC_hand1 /* Cursor for inside MenuButton */ #define SB_INPUTMASK ExposureMask | EnterWindowMask | LeaveWindowMask | \ ButtonPressMask | ButtonReleaseMask XSetWindowAttributes setwinattr; int ls(), du(), pwd(), w(), clear(), csh(), date(), cal(), Exit(); struct { Window window; char label[SB_LONGLABEL]; int (*func)(); } MenuButton[9]; #define POSX 800 #define POSY 200 #define WIDTH 142 #define HEIGHT 85 #define lsButton 0 #define wButton 1 #define dateButton 2 #define duButton 3 #define clearButton 4 #define calButton 5 #define pwdButton 6 #define cshButton 7 #define quitButton 8 Display *display; Window main_window; /* The main uutility window */ XEvent event; /* Incoming event */ int screen; /* Display screen number */ GC gc; /* A Graphics Context to use */ XGCValues values; XFontStruct *font_info; main(argc, argv) int argc; char **argv; { int i; /* Connect to the X Server */ if ( (display=XOpenDisplay(argv[1])) == NULL ) { fprintf( stderr, "Could not open display" ); exit(1); } screen = DefaultScreen(display); /* Create a Window with geometry WIDTHxHEIGHT+POSX+POSY */ main_window = XCreateSimpleWindow( display, RootWindow(display,screen), POSX, POSY, WIDTH, HEIGHT, 2, BlackPixel(display,screen), WhitePixel(display,screen) ); /* XSelectInput(display,main_window,ButtonPressMask); */ /* Create a default graphics context */ values.foreground = BlackPixel( display, screen ); values.background = WhitePixel( display, screen ); gc = XCreateGC( display, main_window, GCForeground|GCBackground, &values); load_font(); MakeButton( 1, 1, "ls", ls, lsButton); MakeButton( 1, 31, "du", du, duButton ); MakeButton( 1, 61, "pwd",pwd, pwdButton ); MakeButton( 50, 1, "w",w, wButton ); MakeButton( 50, 31, "clear",clear,clearButton ); MakeButton( 50, 61, "csh", csh,cshButton ); MakeButton( 99, 1, "date",date, dateButton ); MakeButton( 99, 31, "cal", cal, calButton ); MakeButton( 99, 61, "Quit",Exit, quitButton ); XSelectInput( display, main_window, ExposureMask ); XMapWindow(display, main_window); for( ;; ) { for(i=0; i<=8;i++) if (XCheckWindowEvent(display, MenuButton[i].window, SB_INPUTMASK, &event )) HandleButton( i, &event ); } } load_font() { char *fontname = "9x15"; if ((font_info = XLoadQueryFont(display, fontname)) == NULL) { (void) fprintf(stderr,"Could not get font\n"); exit( -1 ); } XSetFont(display, gc, font_info->fid); } MakeButton( x, y, label, fun, id) int id; int x,y; /* Where to put it */ char *label; /* What to put in it */ int (*fun)(); { Cursor tempcursor; strncpy( MenuButton[id].label, label, SB_LONGLABEL ); MenuButton[id].func = fun; MenuButton[id].window = XCreateSimpleWindow( display, main_window, x, y, SB_WIDTH, SB_HEIGHT, 1, BlackPixel(display,screen), WhitePixel(display,screen) ); XSelectInput( display, MenuButton[id].window, SB_INPUTMASK ); tempcursor = XCreateFontCursor( display, SB_CURSOR ); XDefineCursor( display, MenuButton[id].window, tempcursor ); setwinattr.backing_store = Always; XChangeWindowAttributes(display, MenuButton[id].window, CWBackingStore, &setwinattr); XMapWindow( display, MenuButton[id].window ); } int HandleButton( id, event ) int id; XEvent *event; { switch( event->type ) { case Expose: ExposeButton( id ); break; case EnterNotify: XDrawRectangle( display, MenuButton[id].window, gc, 1,1, SB_WIDTH-3, SB_HEIGHT-3 ); break; case LeaveNotify: ExposeButton( id ); break; case ButtonPress: if ( event->xbutton.button == Button1 ) { XDrawRectangle( display, MenuButton[id].window, gc, 0,0, SB_WIDTH-1,SB_HEIGHT-1 ); } break; case ButtonRelease: MenuButton[id].func(); ExposeButton(id); XDrawRectangle( display, MenuButton[id].window, gc, 1,1, SB_WIDTH-3, SB_HEIGHT-3 ); break; default: break; } } ExposeButton( id ) int id; { int width, center; XClearWindow( display, MenuButton[id].window ); width = XTextWidth( font_info, MenuButton[id].label, strlen(MenuButton[id].label) ); center = MAX((SB_WIDTH-width)/2,4); XDrawString( display, MenuButton[id].window, gc, center, font_info->ascent, MenuButton[id].label, strlen(MenuButton[id].label) ); XFlush(display); } clear() { system("clear"); } cal() { system("cal"); } csh() { system("csh"); } date() { system("date"); } du() { system("du"); } pwd() { system("pwd"); } ls() { system("ls"); } w() { system("w"); } Exit() { printf("BY BY\n"); XDestroyWindow(display,main_window); exit(0); }