// // file name : X_func.C // written by Ting-jen Yen // #include "window.h" #include extern int byte_per_pixel; // W-init initialize the X system, opens a window. Then it does // all the setup for the window with the X interface. void W_init(int x,int y) { char *display_name = NULL, *font_name="8x13"; XSetWindowAttributes attributes; XVisualInfo vTemplate; XVisualInfo * visualList; int visualsMatched; int i,j; XGCValues xgcvalues; XColor xcolor[256]; if ((display=XOpenDisplay(display_name)) == NULL) { cerr << "W-init: cannot connect to X server "<= StaticColor && visualList[i].c_class <= TrueColor) if (i > j) j = i; } if (j == -1) { cerr << "No visual mode available" << endl; if (visualList) XFree(visualList); XCloseDisplay(display); exit(-1); } visual = visualList[j].visual; byte_per_pixel = visualList[j].depth / 8; // Now, create a private colormap, and initial colors with R:G:B = 3:3:2 bits // And get default black and white color. We initiate the colors from // the default system colormap to reduce the difference of the colormaps. colormap = XCreateColormap(display, RootWindow(display,screen), visual, AllocNone); if (visualList[j].c_class <= PseudoColor) { for (i = 0 ; i < 256; i ++) xcolor[i].pixel = i; XQueryColors(display, DefaultColormap(display,screen), xcolor, 256); for (i = 0 ; i < 256; i ++) { if (xcolor[i].red == 0xff00 && // if it is white color already, xcolor[i].green == 0xff00 && // don't do any modify xcolor[i].blue == 0xff00) continue; xcolor[i].red &= 0xe000; xcolor[i].green &= 0xe000; xcolor[i].blue &= 0xc000; } for (i = 0; i < 256; i ++) XAllocColor(display, colormap, &xcolor[i]); B = BlackPixel(display, screen); W = WhitePixel(display, screen); } else { xcolor[0].red = 0xff00; xcolor[0].green = 0xff00; xcolor[0].blue = 0xff00; xcolor[0].flags = DoRed | DoGreen | DoBlue; XAllocColor(display, colormap, xcolor); W = xcolor[0].pixel; // get white color pixel value xcolor[0].red = 0; xcolor[0].green = 0; xcolor[0].blue = 0; xcolor[0].flags = DoRed | DoGreen | DoBlue; XAllocColor(display, colormap, xcolor); B = xcolor[0].pixel; // get black color pixel value } // Set properties of the new window then open the window. attributes.colormap = colormap; attributes.background_pixel = W; attributes.border_pixel = B; window = XCreateWindow(display, RootWindow(display, screen), 0, 0, x, y, 5, visualList[j].depth, InputOutput, visual, CWBorderPixel|CWBackPixel|CWColormap, &attributes); XSetWindowColormap(display, window, colormap); // This window will not any events except expose event and // resize event. XSelectInput(display, window,ExposureMask | StructureNotifyMask); // Create a "Graphics Context" and then set some basic components // of it. gcx = XCreateGC(display, window,0L , &xgcvalues); gc_inv = XCreateGC(display, window,0L , &xgcvalues); XSetFunction(display, gcx, GXcopy); XSetFunction(display, gc_inv, GXcopy); XSetForeground(display, gcx, B); XSetForeground(display, gc_inv, W); XSetBackground(display, gcx, W); XSetBackground(display, gc_inv, B); XSetFillStyle(display, gcx, FillSolid); XSetLineAttributes(display,gcx, 1, LineSolid, CapRound, JoinRound); XSetLineAttributes(display,gc_inv, 1, LineSolid, CapRound, JoinRound); XFree(visualList); // Allocate a font. if ((font_info1 = XLoadQueryFont(display, font_name)) == NULL) exit(-1); XSetFont(display, gcx, font_info1->fid); hand_cursor = XCreateFontCursor(display, XC_hand2); } // // CreateButton will open a window on the parent // window with given position, and with size enough // enclose the given name. No other things are done. // The event handler will have to handle redraw and // call back function of this button explicitly. // Window CreateButton(int x, int y, Window parent, char *name) { Window button; int w, h; w = XTextWidth(font_info1, name, strlen(name)) + 6; h = font_info1->max_bounds.ascent+font_info1->max_bounds.descent+6; button = XCreateSimpleWindow(display, parent, x, y, w, h, 2, B, W); XDrawString(display, button, gcx, 3, h-font_info1->max_bounds.descent, name, strlen(name)); XSelectInput(display, button, ButtonPressMask | ButtonReleaseMask | ExposureMask | EnterWindowMask | LeaveWindowMask); return button; } // // CreateCanvas will simply create a window of given position // and given size, and setup the event it would accept. // Window CreateCanvas(int x, int y, int w, int h, Window parent, long event_mask) { Window canvas; canvas = XCreateSimpleWindow(display, parent, x, y, w, h, 1, B, W); XSelectInput(display, canvas, event_mask); return canvas; }