#include "xwin.h" #include "Xrm.h" #include #include #include #include #include #include //----------------------------------------------------------------------------- // argc is a reference, so that the changes to argc by XrmParseCommand are // noticed by the caller (XOSView, in this case). BCG XWin::XWin() { } //----------------------------------------------------------------------------- XWin::XWin( int argc, char *argv[], int x, int y, int width, int height ) { std::cerr << "This constructor call is not supported! (" << __FILE__ << ":" << __LINE__ << ")" << std::endl; exit (-1); // FIXME BCG This constructor needs to do much of the work of the above // one. Or, we need to drop this as a constructor. As it is, it is // VERY MUCH out of date. x_ = x; y_ = y; width_ = width; height_ = height; (void) argc; (void) argv; } //----------------------------------------------------------------------------- void XWin::XWinInit (int argc, char** argv, char* geometry, Xrm* xrm) { (void) argc; (void) argv; // Avoid gcc warnings about unused variables. // Eventually, we may want to have XWin handle some arguments other // than resources, so argc and argv are left as parameters. BCG geometry_ = geometry; // Save for later use. width_ = height_ = x_ = y_ = 0; xrmptr_ = xrm; name_ = (char *)malloc(0); font_ = NULL; done_ = 0; // Set up the default Events events_ = NULL; addEvent( new Event( this, ClientMessage, &XWin::deleteEvent ) ); addEvent( new Event( this, MappingNotify, &XWin::mappingNotify ) ); //openDisplay(); // Done explicitly in xosview.cc. } XWin::~XWin( void ){ // delete the events Event *event = events_; while ( event != NULL ){ Event *save = event->next_; delete event; event = save; } XFree( title_.value ); XFree( iconname_.value ); XFree( sizehints_ ); XFree( wmhints_ ); XFree( classhints_ ); XFreeGC( display_, gc_ ); XFreeFont( display_, font_ ); XDestroyWindow( display_, window_ ); // close the connection to the display XCloseDisplay( display_ ); } //----------------------------------------------------------------------------- void XWin::init( int argc, char **argv ){ XGCValues gcv; XSetWindowAttributes xswa; Pixmap background_pixmap; setFont(); setColors(); getGeometry(); borderwidth_ = atoi(getResourceOrUseDefault("borderwidth", "1")); window_ = XCreateSimpleWindow(display_, DefaultRootWindow(display_), sizehints_->x, sizehints_->y, sizehints_->width, sizehints_->height, borderwidth_, fgcolor_, bgcolor_); setHints( argc, argv ); // Finally, create a graphics context for the main window gcv.font = font_->fid; gcv.foreground = fgcolor_; gcv.background = bgcolor_; gc_ = XCreateGC(display_, window_, (GCFont | GCForeground | GCBackground), &gcv); // Set main window's attributes (colormap, bit_gravity) xswa.colormap = colormap_; xswa.bit_gravity = NorthWestGravity; XChangeWindowAttributes(display_, window_, (CWColormap | CWBitGravity), &xswa); // If there is a pixmap file, set it as the background if (getPixmap(&background_pixmap)) XSetWindowBackgroundPixmap(display_,window_,background_pixmap); // add the events Event *tmp = events_; while ( tmp != NULL ){ selectEvents( tmp->mask_ ); tmp = tmp->next_; } // Map the main window map(); flush(); if(XGetWindowAttributes(display_, window_, &attr_) == 0){ std::cerr <<"Error getting attributes of Main." <res_name = (char*) xrmptr_->instanceName(); classhints_->res_class = (char*) xrmptr_->className(); // Set up the window manager hints if((wmhints_ = XAllocWMHints()) == NULL){ std::cerr <<"Error allocating Window Manager hints!" <flags = (InputHint|StateHint); wmhints_->input = True; wmhints_->initial_state = NormalState; // Set up XTextProperty for window name and icon name if(XStringListToTextProperty(&name_, 1, &title_) == 0){ std::cerr <<"Error creating XTextProperty!" <flags = PSize; sizehints_->height = height_; sizehints_->min_height = sizehints_->height; sizehints_->width = width_; sizehints_->min_width = sizehints_->width; sizehints_->x = x_; sizehints_->y = y_; // Construct a default geometry string snprintf(default_geometry, 80, "%dx%d+%d+%d", sizehints_->width, sizehints_->height, sizehints_->x, sizehints_->y); // Process the geometry specification bitmask = XGeometry(display_, DefaultScreen(display_), getResourceOrUseDefault("geometry", geometry_), default_geometry, 0, 1, 1, 0, 0, &(sizehints_->x), &(sizehints_->y), &(sizehints_->width), &(sizehints_->height)); // Check bitmask and set flags in XSizeHints structure if (bitmask & (WidthValue | HeightValue)){ sizehints_->flags |= PPosition; width_ = sizehints_->width; height_ = sizehints_->height; } if (bitmask & (XValue | YValue)){ sizehints_->flags |= USPosition; x_ = sizehints_->x; y_ = sizehints_->y; } } //----------------------------------------------------------------------------- void XWin::selectEvents( long mask ){ XWindowAttributes xAttr; XSetWindowAttributes xSwAttr; if ( XGetWindowAttributes( display_, window_, &xAttr ) != 0 ){ xSwAttr.event_mask = xAttr.your_event_mask | mask; XChangeWindowAttributes( display_, window_, CWEventMask, &xSwAttr ); } } void XWin::ignoreEvents( long mask ){ XWindowAttributes xAttr; XSetWindowAttributes xSwAttr; if ( XGetWindowAttributes( display_, window_, &xAttr ) != 0 ){ xSwAttr.event_mask = xAttr.your_event_mask & mask; XChangeWindowAttributes( display_, window_, CWEventMask, &xSwAttr ); } } //----------------------------------------------------------------------------- void XWin::checkevent( void ){ XEvent event; while ( XEventsQueued( display_, QueuedAfterReading ) ){ XNextEvent( display_, &event ); // call all of the Event's call back functions to process this event Event *tmp = events_; while ( tmp != NULL ){ tmp->callBack( event ); tmp = tmp->next_; } } } //----------------------------------------------------------------------------- void XWin::addEvent( Event *event ){ Event *tmp = events_; if ( events_ == NULL ) events_ = event; else { while ( tmp->next_ != NULL ) tmp = tmp->next_; tmp->next_ = event; } } //----------------------------------------------------------------------------- const char *XWin::getResourceOrUseDefault( const char *name, const char* defaultVal ){ const char* retval = xrmptr_->getResource (name); if (retval) return retval; else return defaultVal; } //----------------------------------------------------------------------------- const char *XWin::getResource( const char *name ){ const char* retval = xrmptr_->getResource (name); if (retval) return retval; else { std::cerr << "Error: Couldn't find '" << name << "' resource in the resource database!\n"; exit (-1); /* Some compilers aren't smart enough to know that exit() exits. */ return NULL; } } //----------------------------------------------------------------------------- void XWin::dumpResources( std::ostream &os ){ std::cerr << "Function not implemented!\n"; // BCG FIXME Need to make this. (void) os; // Keep gcc happy. } //----------------------------------------------------------------------------- unsigned long XWin::allocColor( const char *name ){ XColor exact, closest; if ( XAllocNamedColor( display_, colormap(), name, &closest, &exact ) == 0 ) std::cerr <<"XWin::allocColor() : failed to alloc : " <