X Windows Programming in C++: Part III

Continuing with our task of creating a C++ version of basicwin.c, we next look at XCreateSimpleWindow. Here’s some code snippets from basicwin.c showing the use of XCreateSimpleWindow:

/* basicwin.c */
#include <X11/Xlib.h>

Display *display;
int screen_num;

static char *progname; /* name this program was invoked by */

void main(argc, argv)
int argc;
char **argv;
{
	Window win;
/*
 .
 .
 .
 */

	/* connect to X server */
	if ( (display=XOpenDisplay(display_name)) == NULL )
	{
		(void) fprintf( stderr, "%s: cannot connect to X server %s\n", 
				progname, XDisplayName(display_name));
		exit( -1 );
	}

	/* get screen size from display structure macro */
	screen_num = DefaultScreen(display);
	display_width = DisplayWidth(display, screen_num);
	display_height = DisplayHeight(display, screen_num);

	/* Note that in a real application, x and y would default to 0
	 * but would be settable from the command line or resource database.  
	 */
	x = y = 0;

	/* size window with enough room for text */
	width = display_width/3, height = display_height/4;

	/* create opaque window */
	win = XCreateSimpleWindow(display, RootWindow(display,screen_num), 
			x, y, width, height, border_width, BlackPixel(display,
			screen_num), WhitePixel(display,screen_num));

/*
 .
 .
 .
 */

        /* Display window */
	XMapWindow(display, win);

/*
 .
 .
 .
 */

}

In the C++ version, we can do something like this:

/* basicwin.cpp */

#include " xwindow.h"

/*
 .
 .
 .
 */
int main(int argc, char **argv)
{
  try
  {
    CXDisplayPtr display(CXDisplay::OpenDisplay()) ;

    /* get screen size from display structure macro */
    display_width = display->GetDisplayWidth();
    display_height = display->GetDisplayHeight();

    /* Note that in a real application, x and y would default to 0
     * but would be settable from the command line or resource database.  
     */
    x = y = 0;

    /* size window with enough room for text */
    width = display_width / 3 ;
    height = display_height / 4;

    /* create opaque window */
    CXWindowPtr win(CXWindow::CreateWindow(display,
                    x, y, width, height, border_width,
                    display->GetBlackPixel(),
                    display->GetWhitePixel())) ;

/*
 .
 .
 .
 */
    /* Display window */
    win->Map();
/*
 .
 .
 .
 */
  }
  catch(bad_CXDisplay& e)
  {
    cerr << e.what() << endl ;
  }
  catch(bad_CXWindow& e)
  {
    cerr << e.what() << endl ;
  }
      
  return -1 ;
}

CXWindowPtr is defined in xwindow.h. Creating a window is handled by the constructor for the class CXWindow. XDestoryWindow is called by CXWindow’s destructor. CXWindowPtr wraps CXWindow in a boost::shared_ptr. The static member function, CXWindow::CreateWindow creates a window by calling XCreateSimpleWindow (future versions will likely also support XCreateWindow). CXWindow::CreateWindow creates a window and returns a CXWindowPtr which can be used as a pointer to a Window by dereferencing it. CXWindow’s destructor is called when the CXWindowPtr goes out of scope and all references to it are released.

— start —

/*******************************************************************
 *
 * xdisplay.h -- Wrap Window with a boost::shared_ptr
 *
 *  Copyright (C) 2007 by James A. Chappell (rlrrlrll@gmail.com)
 *
 *  Permission is hereby granted, free of charge, to any person
 *  obtaining a copy of this software and associated documentation
 *  files (the "Software"), to deal in the Software without
 *  restriction, including without limitation the rights to use,
 *  copy, modify, merge, publish, distribute, sublicense, and/or
 *  sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following
 *  condition:
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *  OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef __XWINDOW_H__
#define __XWINDOW_H__

#include "xdisplay.h"

class bad_CXWindow : public std::exception
{
public:
  virtual const char* what() const throw()
  {
    return "bad_CXWindow" ; // for now
  }
} ;


class CXWindow ;

typedef boost::shared_ptr<CXWindow> CXWindowPtr ;

class CXWindow : private boost::noncopyable
{
public:

  static CXWindowPtr CreateWindow(CXDisplayPtr& display,
                                  int x, int y,
                                  unsigned int width,
                                  unsigned int height,
                                  unsigned int border_width,
                                  unsigned long border,
                                  unsigned long background,
                                  int screen_num = USE_DEFAULT_SCREEN)
  {
    Window parent(display->GetRootWindow(screen_num)) ;
    return CXWindowPtr(new CXWindow(display, parent,
                                    x, y, width, height, 
                                    border_width, border, background)) ;
  }
  
  
  static CXWindowPtr CreateWindow(CXDisplayPtr& display,
                                  CXWindowPtr& parent,
                                  int x, int y,
                                  unsigned int width,
                                  unsigned int height,
                                  unsigned int border_width,
                                  unsigned long border,
                                  unsigned long background)
  {
    return CXWindowPtr(new CXWindow(display, *parent,
                                    x, y, width, height, 
                                    border_width, border, background)) ;
  }

  ~CXWindow()
  {
    XDestroyWindow(*display_, window_) ;
  }

  operator Window const () { return window_ ; }

  void GetWindowAttributes(XWindowAttributes &attr)
  {
    if (!XGetWindowAttributes(*display_, window_, &attr))
    {
      throw bad_CXWindow() ;
    }
  }

  void Map() const
  {
    XMapWindow(*display_, window_);
  }

  void Unmap()
  {
    XUnmapWindow(*display_, window_) ;
  }

private:
  Window window_ ;
  CXDisplayPtr display_ ;

  CXWindow(CXDisplayPtr& display, Window parent,
           int x, int y,
           unsigned int width, unsigned int height,
           unsigned int border_width,
           unsigned long border,
           unsigned long background)
    : display_(display)
  {
    window_ = XCreateSimpleWindow(*display_, parent,
                                   x, y, width, height, 
                                   border_width, border, background) ;
  }

  CXWindow() {} 
} ;

#endif

— end —

One thought on “X Windows Programming in C++: Part III”

  1. Thanks for the examples! I havejust bought the Xlib rel 5 programming book second hand and was thinking about how to do the same things in C++ Being new to C++ its nice to see compact examples that give a launching point to work with.

    Cheers.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

All trademarks and copyrights on this page are owned by their respective owners. Quotes from linked articles are probably the property of the publications linked or the property of the person(s) quoted. The rest © 2001- 2024 by James A. Chappell