1
#ifndef __cxxtest__X11Gui_h__
2
#define __cxxtest__X11Gui_h__
5
// X11Gui displays a simple progress bar using X11
7
// It accepts the following command-line arguments:
8
// -title <title> - Sets the application title
9
// -fn or -font <font> - Sets the font
10
// -bg or -background <color> - Sets the background color (default=Grey)
11
// -fg or -foreground <color> - Sets the text color (default=Black)
12
// -green/-yellow/-red <color> - Sets the colors of the bar
15
#include <cxxtest/Gui.h>
18
#include <X11/Xutil.h>
25
class X11Gui : public GuiListener
28
void enterGui( int &argc, char **argv )
30
parseCommandLine( argc, argv );
33
void enterWorld( const WorldDescription &wd )
48
void guiEnterTest( const char *suiteName, const char *testName )
52
setWindowName( suiteName, testName );
60
_barColor = getColor( _yellowName );
69
_barColor = getColor( _redName );
86
const char *_programName;
89
unsigned _numTotalTests, _testsDone;
90
char _strTotalTests[WorldDescription::MAX_STRLEN_TOTAL_TESTS];
91
const char *_foregroundName, *_backgroundName;
92
const char *_greenName, *_yellowName, *_redName;
93
unsigned long _foreground, _background, _barColor;
96
const char *_fontName;
98
XFontStruct *_fontInfo;
99
int _textHeight, _textDescent;
103
void parseCommandLine( int &argc, char **argv )
105
_programName = argv[0];
108
_foregroundName = "Black";
109
_backgroundName = "Grey";
110
_greenName = "Green";
111
_yellowName = "Yellow";
114
for ( int i = 1; i + 1 < argc; ++ i ) {
115
if ( !strcmp( argv[i], "-title" ) )
116
_programName = argv[++ i];
117
else if ( !strcmp( argv[i], "-fn" ) || !strcmp( argv[i], "-font" ) )
118
_fontName = argv[++ i];
119
else if ( !strcmp( argv[i], "-fg" ) || !strcmp( argv[i], "-foreground" ) )
120
_foregroundName = argv[++ i];
121
else if ( !strcmp( argv[i], "-bg" ) || !strcmp( argv[i], "-background" ) )
122
_backgroundName = argv[++ i];
123
else if ( !strcmp( argv[i], "-green" ) )
124
_greenName = argv[++ i];
125
else if ( !strcmp( argv[i], "-yellow" ) )
126
_yellowName = argv[++ i];
127
else if ( !strcmp( argv[i], "-red" ) )
128
_redName = argv[++ i];
134
_display = XOpenDisplay( NULL );
139
_colormap = DefaultColormap( _display, 0 );
140
_foreground = getColor( _foregroundName );
141
_background = getColor( _backgroundName );
144
unsigned long getColor( const char *colorName )
147
XParseColor( _display, _colormap, colorName, &color );
148
XAllocColor( _display, _colormap, &color );
154
_window = XCreateSimpleWindow( _display, RootWindow( _display, 0 ), 0, 0, 1, 1, 0, 0, _background );
159
_gc = XCreateGC( _display, _window, 0, 0 );
167
_textHeight = _fontInfo->ascent + _fontInfo->descent;
168
_textDescent = _fontInfo->descent;
175
_fontId = XLoadFont( _display, _fontName );
176
return (XSetFont( _display, _gc, _fontId ) == Success);
179
void useDefaultFont()
181
_fontId = XGContextFromGC( _gc );
186
_fontInfo = XQueryFont( _display, _fontId );
191
XFreeFontInfo( NULL, _fontInfo, 1 );
194
void initializeEvents()
196
_eventMask = ExposureMask;
197
XSelectInput( _display, _window, _eventMask );
200
void initializeBar( const WorldDescription &wd )
204
_barColor = getColor( _greenName );
209
getTotalTests( tracker().world() );
212
void getTotalTests( const WorldDescription &wd )
214
_numTotalTests = wd.numTotalTests();
215
wd.strTotalTests( _strTotalTests );
220
XMapWindow( _display, _window );
222
Screen *screen = XDefaultScreenOfDisplay( _display );
223
int screenWidth = WidthOfScreen( screen );
224
int screenHeight = HeightOfScreen( screen );
225
int xCenter = screenWidth / 2;
226
int yCenter = screenHeight / 2;
228
_width = (screenWidth * 4) / 5;
229
_height = screenHeight / 14;
231
XMoveResizeWindow( _display, _window, xCenter - (_width / 2), yCenter - (_height / 2), _width, _height );
239
while( XCheckMaskEvent( _display, _eventMask, &event ) )
243
void setWindowName( const char *suiteName, const char *testName )
245
unsigned length = strlen( _programName ) + strlen( suiteName ) + strlen( testName ) + sizeof( " - ::()" );
246
char *name = (char *)malloc( length );
247
sprintf( name, "%s - %s::%s()", _programName, suiteName, testName );
248
XSetStandardProperties( _display, _window, name, 0, 0, 0, 0, 0 );
263
XWindowAttributes attributes;
264
XGetWindowAttributes( _display, _window, &attributes );
265
_width = attributes.width;
266
_height = attributes.height;
271
unsigned barWidth = (_width * _testsDone) / _numTotalTests;
273
XSetForeground( _display, _gc, _barColor );
274
XFillRectangle( _display, _window, _gc, 0, 0, barWidth, _height );
276
XSetForeground( _display, _gc, _background );
277
XFillRectangle( _display, _window, _gc, barWidth, 0, _width + 1 - barWidth, _height );
282
if(_width / _numTotalTests < 5)
284
for ( unsigned i = 1; i < _testsDone; ++ i ) {
285
int x = (_width * i) / _numTotalTests;
286
XDrawLine( _display, _window, _gc, x, 0, x, _height);
290
void drawPercentage()
292
XSetForeground( _display, _gc, _foreground );
294
char str[sizeof("1000000000 of ") + sizeof(_strTotalTests) + sizeof(" (100%)")];
295
sprintf( str, "%u of %s (%u%%)", _testsDone, _strTotalTests, (_testsDone * 100) / _numTotalTests );
296
unsigned len = strlen( str );
298
int textWidth = XTextWidth( _fontInfo, str, len );
300
XDrawString( _display, _window, _gc,
301
(_width - textWidth) / 2, ((_height + _textHeight) / 2) - _textDescent,
312
XFreeGC( _display, _gc );
317
XDestroyWindow( _display, _window );
322
XCloseDisplay( _display );
327
#endif //__cxxtest__X11Gui_h__