Loading...
Searching...
No Matches
X11Gui.h
Go to the documentation of this file.
#ifndef __cxxtest__X11Gui_h__
#define __cxxtest__X11Gui_h__
//
// X11Gui displays a simple progress bar using X11
//
// It accepts the following command-line arguments:
// -title <title> - Sets the application title
// -fn or -font <font> - Sets the font
// -bg or -background <color> - Sets the background color (default=Grey)
// -fg or -foreground <color> - Sets the text color (default=Black)
// -green/-yellow/-red <color> - Sets the colors of the bar
//
#include <cxxtest/Gui.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
namespace CxxTest
{
class X11Gui : public GuiListener
{
public:
void enterGui( int &argc, char **argv )
{
parseCommandLine( argc, argv );
}
void enterWorld( const WorldDescription &wd )
{
openDisplay();
if ( _display ) {
createColors();
createWindow();
createGc();
createFont();
centerWindow();
initializeEvents();
initializeBar( wd );
processEvents();
}
}
void guiEnterTest( const char *suiteName, const char *testName )
{
if ( _display ) {
++ _testsDone;
setWindowName( suiteName, testName );
redraw();
}
}
void yellowBar()
{
if ( _display ) {
_barColor = getColor( _yellowName );
getTotalTests();
processEvents();
}
}
void redBar()
{
if ( _display ) {
_barColor = getColor( _redName );
getTotalTests();
processEvents();
}
}
void leaveGui()
{
if ( _display ) {
freeFontInfo();
destroyGc();
destroyWindow();
closeDisplay();
}
}
private:
const char *_programName;
Display *_display;
Window _window;
unsigned _numTotalTests, _testsDone;
char _strTotalTests[WorldDescription::MAX_STRLEN_TOTAL_TESTS];
const char *_foregroundName, *_backgroundName;
const char *_greenName, *_yellowName, *_redName;
unsigned long _foreground, _background, _barColor;
int _width, _height;
GC _gc;
const char *_fontName;
XID _fontId;
XFontStruct *_fontInfo;
int _textHeight, _textDescent;
long _eventMask;
Colormap _colormap;
void parseCommandLine( int &argc, char **argv )
{
_programName = argv[0];
_fontName = 0;
_foregroundName = "Black";
_backgroundName = "Grey";
_greenName = "Green";
_yellowName = "Yellow";
_redName = "Red";
for ( int i = 1; i + 1 < argc; ++ i ) {
if ( !strcmp( argv[i], "-title" ) )
_programName = argv[++ i];
else if ( !strcmp( argv[i], "-fn" ) || !strcmp( argv[i], "-font" ) )
_fontName = argv[++ i];
else if ( !strcmp( argv[i], "-fg" ) || !strcmp( argv[i], "-foreground" ) )
_foregroundName = argv[++ i];
else if ( !strcmp( argv[i], "-bg" ) || !strcmp( argv[i], "-background" ) )
_backgroundName = argv[++ i];
else if ( !strcmp( argv[i], "-green" ) )
_greenName = argv[++ i];
else if ( !strcmp( argv[i], "-yellow" ) )
_yellowName = argv[++ i];
else if ( !strcmp( argv[i], "-red" ) )
_redName = argv[++ i];
}
}
void openDisplay()
{
_display = XOpenDisplay( NULL );
}
void createColors()
{
_colormap = DefaultColormap( _display, 0 );
_foreground = getColor( _foregroundName );
_background = getColor( _backgroundName );
}
unsigned long getColor( const char *colorName )
{
XColor color;
XParseColor( _display, _colormap, colorName, &color );
XAllocColor( _display, _colormap, &color );
return color.pixel;
}
void createWindow()
{
_window = XCreateSimpleWindow( _display, RootWindow( _display, 0 ), 0, 0, 1, 1, 0, 0, _background );
}
void createGc()
{
_gc = XCreateGC( _display, _window, 0, 0 );
}
void createFont()
{
if ( !loadFont() )
useDefaultFont();
getFontInfo();
_textHeight = _fontInfo->ascent + _fontInfo->descent;
_textDescent = _fontInfo->descent;
}
bool loadFont()
{
if ( !_fontName )
return false;
_fontId = XLoadFont( _display, _fontName );
return (XSetFont( _display, _gc, _fontId ) == Success);
}
void useDefaultFont()
{
_fontId = XGContextFromGC( _gc );
}
void getFontInfo()
{
_fontInfo = XQueryFont( _display, _fontId );
}
void freeFontInfo()
{
XFreeFontInfo( NULL, _fontInfo, 1 );
}
void initializeEvents()
{
_eventMask = ExposureMask;
XSelectInput( _display, _window, _eventMask );
}
void initializeBar( const WorldDescription &wd )
{
getTotalTests( wd );
_testsDone = 0;
_barColor = getColor( _greenName );
}
void getTotalTests()
{
getTotalTests( tracker().world() );
}
void getTotalTests( const WorldDescription &wd )
{
_numTotalTests = wd.numTotalTests();
wd.strTotalTests( _strTotalTests );
}
void centerWindow()
{
XMapWindow( _display, _window );
Screen *screen = XDefaultScreenOfDisplay( _display );
int screenWidth = WidthOfScreen( screen );
int screenHeight = HeightOfScreen( screen );
int xCenter = screenWidth / 2;
int yCenter = screenHeight / 2;
_width = (screenWidth * 4) / 5;
_height = screenHeight / 14;
XMoveResizeWindow( _display, _window, xCenter - (_width / 2), yCenter - (_height / 2), _width, _height );
}
void processEvents()
{
redraw();
XEvent event;
while( XCheckMaskEvent( _display, _eventMask, &event ) )
redraw();
}
void setWindowName( const char *suiteName, const char *testName )
{
unsigned length = strlen( _programName ) + strlen( suiteName ) + strlen( testName ) + sizeof( " - ::()" );
char *name = (char *)malloc( length );
sprintf( name, "%s - %s::%s()", _programName, suiteName, testName );
XSetStandardProperties( _display, _window, name, 0, 0, 0, 0, 0 );
free( name );
}
void redraw()
{
getWindowSize();
drawSolidBar();
drawDividers();
drawPercentage();
flush();
}
void getWindowSize()
{
XWindowAttributes attributes;
XGetWindowAttributes( _display, _window, &attributes );
_width = attributes.width;
_height = attributes.height;
}
void drawSolidBar()
{
unsigned barWidth = (_width * _testsDone) / _numTotalTests;
XSetForeground( _display, _gc, _barColor );
XFillRectangle( _display, _window, _gc, 0, 0, barWidth, _height );
XSetForeground( _display, _gc, _background );
XFillRectangle( _display, _window, _gc, barWidth, 0, _width + 1 - barWidth, _height );
}
void drawDividers()
{
if(_width / _numTotalTests < 5)
return;
for ( unsigned i = 1; i < _testsDone; ++ i ) {
int x = (_width * i) / _numTotalTests;
XDrawLine( _display, _window, _gc, x, 0, x, _height);
}
}
void drawPercentage()
{
XSetForeground( _display, _gc, _foreground );
char str[sizeof("1000000000 of ") + sizeof(_strTotalTests) + sizeof(" (100%)")];
sprintf( str, "%u of %s (%u%%)", _testsDone, _strTotalTests, (_testsDone * 100) / _numTotalTests );
unsigned len = strlen( str );
int textWidth = XTextWidth( _fontInfo, str, len );
XDrawString( _display, _window, _gc,
(_width - textWidth) / 2, ((_height + _textHeight) / 2) - _textDescent,
str, len );
}
void flush()
{
XFlush( _display );
}
void destroyGc()
{
XFreeGC( _display, _gc );
}
void destroyWindow()
{
XDestroyWindow( _display, _window );
}
void closeDisplay()
{
XCloseDisplay( _display );
}
};
};
#endif //__cxxtest__X11Gui_h__
Gui.h
i
int i
Definition cfEzgcd.cc:132
x
Variable x
Definition cfModGcd.cc:4090
CxxTest::GuiListener::GuiListener
GuiListener()
Definition Gui.h:16
CxxTest::WorldDescription
Definition Descriptions.h:54
CxxTest::WorldDescription::MAX_STRLEN_TOTAL_TESTS
@ MAX_STRLEN_TOTAL_TESTS
Definition Descriptions.h:62
CxxTest::WorldDescription::strTotalTests
char * strTotalTests(char *) const
Definition Descriptions.cpp:16
CxxTest::WorldDescription::numTotalTests
virtual unsigned numTotalTests(void) const =0
CxxTest::X11Gui
Definition X11Gui.h:26
CxxTest::X11Gui::leaveGui
void leaveGui()
Definition X11Gui.h:75
CxxTest::X11Gui::_testsDone
unsigned _testsDone
Definition X11Gui.h:89
CxxTest::X11Gui::enterWorld
void enterWorld(const WorldDescription &wd)
Definition X11Gui.h:33
CxxTest::X11Gui::createColors
void createColors()
Definition X11Gui.h:137
CxxTest::X11Gui::redraw
void redraw()
Definition X11Gui.h:252
CxxTest::X11Gui::_eventMask
long _eventMask
Definition X11Gui.h:100
CxxTest::X11Gui::getFontInfo
void getFontInfo()
Definition X11Gui.h:184
CxxTest::X11Gui::closeDisplay
void closeDisplay()
Definition X11Gui.h:320
CxxTest::X11Gui::initializeEvents
void initializeEvents()
Definition X11Gui.h:194
CxxTest::X11Gui::destroyGc
void destroyGc()
Definition X11Gui.h:310
CxxTest::X11Gui::_strTotalTests
char _strTotalTests[WorldDescription::MAX_STRLEN_TOTAL_TESTS]
Definition X11Gui.h:90
CxxTest::X11Gui::processEvents
void processEvents()
Definition X11Gui.h:234
CxxTest::X11Gui::useDefaultFont
void useDefaultFont()
Definition X11Gui.h:179
CxxTest::X11Gui::_gc
GC _gc
Definition X11Gui.h:95
CxxTest::X11Gui::_height
int _height
Definition X11Gui.h:94
CxxTest::X11Gui::_greenName
const char * _greenName
Definition X11Gui.h:92
CxxTest::X11Gui::guiEnterTest
void guiEnterTest(const char *suiteName, const char *testName)
Definition X11Gui.h:48
CxxTest::X11Gui::_textHeight
int _textHeight
Definition X11Gui.h:99
CxxTest::X11Gui::drawPercentage
void drawPercentage()
Definition X11Gui.h:290
CxxTest::X11Gui::_foreground
unsigned long _foreground
Definition X11Gui.h:93
CxxTest::X11Gui::drawSolidBar
void drawSolidBar()
Definition X11Gui.h:269
CxxTest::X11Gui::_width
int _width
Definition X11Gui.h:94
CxxTest::X11Gui::redBar
void redBar()
Definition X11Gui.h:66
CxxTest::X11Gui::freeFontInfo
void freeFontInfo()
Definition X11Gui.h:189
CxxTest::X11Gui::createGc
void createGc()
Definition X11Gui.h:157
CxxTest::X11Gui::_display
Display * _display
Definition X11Gui.h:87
CxxTest::X11Gui::_numTotalTests
unsigned _numTotalTests
Definition X11Gui.h:89
CxxTest::X11Gui::setWindowName
void setWindowName(const char *suiteName, const char *testName)
Definition X11Gui.h:243
CxxTest::X11Gui::_colormap
Colormap _colormap
Definition X11Gui.h:101
CxxTest::X11Gui::parseCommandLine
void parseCommandLine(int &argc, char **argv)
Definition X11Gui.h:103
CxxTest::X11Gui::centerWindow
void centerWindow()
Definition X11Gui.h:218
CxxTest::X11Gui::_window
Window _window
Definition X11Gui.h:88
CxxTest::X11Gui::_fontName
const char * _fontName
Definition X11Gui.h:96
CxxTest::X11Gui::initializeBar
void initializeBar(const WorldDescription &wd)
Definition X11Gui.h:200
CxxTest::X11Gui::_barColor
unsigned long _barColor
Definition X11Gui.h:93
CxxTest::X11Gui::_fontId
XID _fontId
Definition X11Gui.h:97
CxxTest::X11Gui::flush
void flush()
Definition X11Gui.h:305
CxxTest::X11Gui::_background
unsigned long _background
Definition X11Gui.h:93
CxxTest::X11Gui::getTotalTests
void getTotalTests(const WorldDescription &wd)
Definition X11Gui.h:212
CxxTest::X11Gui::_fontInfo
XFontStruct * _fontInfo
Definition X11Gui.h:98
CxxTest::X11Gui::createWindow
void createWindow()
Definition X11Gui.h:152
CxxTest::X11Gui::loadFont
bool loadFont()
Definition X11Gui.h:171
CxxTest::X11Gui::getColor
unsigned long getColor(const char *colorName)
Definition X11Gui.h:144
CxxTest::X11Gui::destroyWindow
void destroyWindow()
Definition X11Gui.h:315
CxxTest::X11Gui::_textDescent
int _textDescent
Definition X11Gui.h:99
CxxTest::X11Gui::_backgroundName
const char * _backgroundName
Definition X11Gui.h:91
CxxTest::X11Gui::drawDividers
void drawDividers()
Definition X11Gui.h:280
CxxTest::X11Gui::_yellowName
const char * _yellowName
Definition X11Gui.h:92
CxxTest::X11Gui::openDisplay
void openDisplay()
Definition X11Gui.h:132
CxxTest::X11Gui::_programName
const char * _programName
Definition X11Gui.h:86
CxxTest::X11Gui::enterGui
void enterGui(int &argc, char **argv)
Definition X11Gui.h:28
CxxTest::X11Gui::createFont
void createFont()
Definition X11Gui.h:162
CxxTest::X11Gui::_redName
const char * _redName
Definition X11Gui.h:92
CxxTest::X11Gui::_foregroundName
const char * _foregroundName
Definition X11Gui.h:91
CxxTest::X11Gui::getTotalTests
void getTotalTests()
Definition X11Gui.h:207
CxxTest::X11Gui::yellowBar
void yellowBar()
Definition X11Gui.h:57
CxxTest::X11Gui::getWindowSize
void getWindowSize()
Definition X11Gui.h:261
name
char name(const Variable &v)
Definition factory.h:189
length
static BOOLEAN length(leftv result, leftv arg)
Definition interval.cc:257
CxxTest
Definition Descriptions.cpp:7
CxxTest::tracker
TestTracker & tracker()
Definition TestTracker.h:111
free
#define free
Definition omAllocFunc.c:14
malloc
#define malloc
Definition omAllocFunc.c:12
NULL
#define NULL
Definition omList.c:12