// ----------------------------------------------------------- // // Implementation of the X11 interface (Revision 2.0) // // ----------------------------------------------------------- #include "window.h" void window::open (int w, int h) { display = XOpenDisplay (NULL); if (display == NULL) { cerr << "Cannot open display " << XDisplayName (NULL) << endl; exit (1); } screen = DefaultScreen (display); XSetWindowAttributes attrib; XSizeHints size_hints; XWMHints wm_hints; attrib.bit_gravity = NorthWestGravity; attrib.backing_store = Always; win = XCreateWindow (display, RootWindow(display,screen), 1, 1, w, h, BORDER_WIDTH, DefaultDepth(display,screen), InputOutput, DefaultVisual(display,screen), CWBackingStore | CWBitGravity, &attrib); XSelectInput (display, win, PointerMotionMask | ButtonPressMask | ButtonReleaseMask | ExposureMask | StructureNotifyMask | LeaveWindowMask ); XSetWindowBackground (display, win, WhitePixel(display,screen)); Cursor cursor = XCreateFontCursor (display, XC_crosshair); XDefineCursor (display, win, cursor); XStoreName (display, win, DWIN_NAME); XSetIconName (display, win, ICON_NAME); icon_pixmap = XCreateBitmapFromData (display, win, icon_bits, icon_width, icon_height); size_hints.flags = PPosition; size_hints.x = 1; size_hints.y = 1; wm_hints.flags = StateHint | IconPixmapHint | InputHint; wm_hints.initial_state = NormalState; wm_hints.icon_pixmap = icon_pixmap; wm_hints.input = True; XSetWMProperties (display, win, 0, 0, 0, 0, &size_hints, &wm_hints, 0); XMapWindow (display, win); while (1) { XNextEvent (display, &report); if (report.type == Expose) break; } gc = DefaultGC (display, screen); gc_val.foreground = 1; gc_val.background = 0; gc_val.function = GXxor; gc_val.line_width = LINE_WIDTH; XChangeGC (display, gc, GCForeground | GCBackground | GCFunction | GCLineWidth, &gc_val); XClearWindow (display, win); XFlush (display); } // ----------------------------------------------------------- void window::close () { XDestroyWindow (display, win); XCloseDisplay (display); } // ----------------------------------------------------------- Button window::event_handler (Pstate tracking, UINT x1, UINT y1, UINT& x, UINT& y) { int x2, y2; Pstate pen=off; while (1) { XNextEvent (display, &report); switch (report.type) { case DestroyNotify: cerr << "Window was destroyed, exiting ..." << endl << endl; exit (1); break; case ConfigureNotify: width = report.xconfigure.width; height = report.xconfigure.height; break; case ButtonRelease: if (tracking && pen) drawLine (x1, y1, x2, y2); x = report.xbutton.x; y = report.xbutton.y; return (Button) report.xbutton.button; case MotionNotify: if (tracking) { if (pen) XDrawLine (display, win, gc, x1, y1, x2, y2); else pen = on; x2 = report.xmotion.x; y2 = report.xmotion.y; XDrawLine (display, win, gc, x1, y1, x2, y2); } break; case LeaveNotify: if (tracking && pen) { drawLine (x1, y1, x2, y2); pen = off; } break; default: // other events can be ignored break; } } } // -----------------------------------------------------------