PageRenderTime 197ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libpolys/tests/cxxtest/X11Gui.h

https://github.com/YueRen/Sources
C Header | 327 lines | 268 code | 49 blank | 10 comment | 27 complexity | 8cbbcb4f9a3437b4dad9f6cbe49a14d7 MD5 | raw file
  1. #ifndef __cxxtest__X11Gui_h__
  2. #define __cxxtest__X11Gui_h__
  3. //
  4. // X11Gui displays a simple progress bar using X11
  5. //
  6. // It accepts the following command-line arguments:
  7. // -title <title> - Sets the application title
  8. // -fn or -font <font> - Sets the font
  9. // -bg or -background <color> - Sets the background color (default=Grey)
  10. // -fg or -foreground <color> - Sets the text color (default=Black)
  11. // -green/-yellow/-red <color> - Sets the colors of the bar
  12. //
  13. #include <cxxtest/Gui.h>
  14. #include <X11/Xlib.h>
  15. #include <X11/Xutil.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. namespace CxxTest
  20. {
  21. class X11Gui : public GuiListener
  22. {
  23. public:
  24. void enterGui( int &argc, char **argv )
  25. {
  26. parseCommandLine( argc, argv );
  27. }
  28. void enterWorld( const WorldDescription &wd )
  29. {
  30. openDisplay();
  31. if ( _display ) {
  32. createColors();
  33. createWindow();
  34. createGc();
  35. createFont();
  36. centerWindow();
  37. initializeEvents();
  38. initializeBar( wd );
  39. processEvents();
  40. }
  41. }
  42. void guiEnterTest( const char *suiteName, const char *testName )
  43. {
  44. if ( _display ) {
  45. ++ _testsDone;
  46. setWindowName( suiteName, testName );
  47. redraw();
  48. }
  49. }
  50. void yellowBar()
  51. {
  52. if ( _display ) {
  53. _barColor = getColor( _yellowName );
  54. getTotalTests();
  55. processEvents();
  56. }
  57. }
  58. void redBar()
  59. {
  60. if ( _display ) {
  61. _barColor = getColor( _redName );
  62. getTotalTests();
  63. processEvents();
  64. }
  65. }
  66. void leaveGui()
  67. {
  68. if ( _display ) {
  69. freeFontInfo();
  70. destroyGc();
  71. destroyWindow();
  72. closeDisplay();
  73. }
  74. }
  75. private:
  76. const char *_programName;
  77. Display *_display;
  78. Window _window;
  79. unsigned _numTotalTests, _testsDone;
  80. char _strTotalTests[WorldDescription::MAX_STRLEN_TOTAL_TESTS];
  81. const char *_foregroundName, *_backgroundName;
  82. const char *_greenName, *_yellowName, *_redName;
  83. unsigned long _foreground, _background, _barColor;
  84. int _width, _height;
  85. GC _gc;
  86. const char *_fontName;
  87. XID _fontId;
  88. XFontStruct *_fontInfo;
  89. int _textHeight, _textDescent;
  90. long _eventMask;
  91. Colormap _colormap;
  92. void parseCommandLine( int &argc, char **argv )
  93. {
  94. _programName = argv[0];
  95. _fontName = 0;
  96. _foregroundName = "Black";
  97. _backgroundName = "Grey";
  98. _greenName = "Green";
  99. _yellowName = "Yellow";
  100. _redName = "Red";
  101. for ( int i = 1; i + 1 < argc; ++ i ) {
  102. if ( !strcmp( argv[i], "-title" ) )
  103. _programName = argv[++ i];
  104. else if ( !strcmp( argv[i], "-fn" ) || !strcmp( argv[i], "-font" ) )
  105. _fontName = argv[++ i];
  106. else if ( !strcmp( argv[i], "-fg" ) || !strcmp( argv[i], "-foreground" ) )
  107. _foregroundName = argv[++ i];
  108. else if ( !strcmp( argv[i], "-bg" ) || !strcmp( argv[i], "-background" ) )
  109. _backgroundName = argv[++ i];
  110. else if ( !strcmp( argv[i], "-green" ) )
  111. _greenName = argv[++ i];
  112. else if ( !strcmp( argv[i], "-yellow" ) )
  113. _yellowName = argv[++ i];
  114. else if ( !strcmp( argv[i], "-red" ) )
  115. _redName = argv[++ i];
  116. }
  117. }
  118. void openDisplay()
  119. {
  120. _display = XOpenDisplay( NULL );
  121. }
  122. void createColors()
  123. {
  124. _colormap = DefaultColormap( _display, 0 );
  125. _foreground = getColor( _foregroundName );
  126. _background = getColor( _backgroundName );
  127. }
  128. unsigned long getColor( const char *colorName )
  129. {
  130. XColor color;
  131. XParseColor( _display, _colormap, colorName, &color );
  132. XAllocColor( _display, _colormap, &color );
  133. return color.pixel;
  134. }
  135. void createWindow()
  136. {
  137. _window = XCreateSimpleWindow( _display, RootWindow( _display, 0 ), 0, 0, 1, 1, 0, 0, _background );
  138. }
  139. void createGc()
  140. {
  141. _gc = XCreateGC( _display, _window, 0, 0 );
  142. }
  143. void createFont()
  144. {
  145. if ( !loadFont() )
  146. useDefaultFont();
  147. getFontInfo();
  148. _textHeight = _fontInfo->ascent + _fontInfo->descent;
  149. _textDescent = _fontInfo->descent;
  150. }
  151. bool loadFont()
  152. {
  153. if ( !_fontName )
  154. return false;
  155. _fontId = XLoadFont( _display, _fontName );
  156. return (XSetFont( _display, _gc, _fontId ) == Success);
  157. }
  158. void useDefaultFont()
  159. {
  160. _fontId = XGContextFromGC( _gc );
  161. }
  162. void getFontInfo()
  163. {
  164. _fontInfo = XQueryFont( _display, _fontId );
  165. }
  166. void freeFontInfo()
  167. {
  168. XFreeFontInfo( NULL, _fontInfo, 1 );
  169. }
  170. void initializeEvents()
  171. {
  172. _eventMask = ExposureMask;
  173. XSelectInput( _display, _window, _eventMask );
  174. }
  175. void initializeBar( const WorldDescription &wd )
  176. {
  177. getTotalTests( wd );
  178. _testsDone = 0;
  179. _barColor = getColor( _greenName );
  180. }
  181. void getTotalTests()
  182. {
  183. getTotalTests( tracker().world() );
  184. }
  185. void getTotalTests( const WorldDescription &wd )
  186. {
  187. _numTotalTests = wd.numTotalTests();
  188. wd.strTotalTests( _strTotalTests );
  189. }
  190. void centerWindow()
  191. {
  192. XMapWindow( _display, _window );
  193. Screen *screen = XDefaultScreenOfDisplay( _display );
  194. int screenWidth = WidthOfScreen( screen );
  195. int screenHeight = HeightOfScreen( screen );
  196. int xCenter = screenWidth / 2;
  197. int yCenter = screenHeight / 2;
  198. _width = (screenWidth * 4) / 5;
  199. _height = screenHeight / 14;
  200. XMoveResizeWindow( _display, _window, xCenter - (_width / 2), yCenter - (_height / 2), _width, _height );
  201. }
  202. void processEvents()
  203. {
  204. redraw();
  205. XEvent event;
  206. while( XCheckMaskEvent( _display, _eventMask, &event ) )
  207. redraw();
  208. }
  209. void setWindowName( const char *suiteName, const char *testName )
  210. {
  211. unsigned length = strlen( _programName ) + strlen( suiteName ) + strlen( testName ) + sizeof( " - ::()" );
  212. char *name = (char *)malloc( length );
  213. sprintf( name, "%s - %s::%s()", _programName, suiteName, testName );
  214. XSetStandardProperties( _display, _window, name, 0, 0, 0, 0, 0 );
  215. free( name );
  216. }
  217. void redraw()
  218. {
  219. getWindowSize();
  220. drawSolidBar();
  221. drawDividers();
  222. drawPercentage();
  223. flush();
  224. }
  225. void getWindowSize()
  226. {
  227. XWindowAttributes attributes;
  228. XGetWindowAttributes( _display, _window, &attributes );
  229. _width = attributes.width;
  230. _height = attributes.height;
  231. }
  232. void drawSolidBar()
  233. {
  234. unsigned barWidth = (_width * _testsDone) / _numTotalTests;
  235. XSetForeground( _display, _gc, _barColor );
  236. XFillRectangle( _display, _window, _gc, 0, 0, barWidth, _height );
  237. XSetForeground( _display, _gc, _background );
  238. XFillRectangle( _display, _window, _gc, barWidth, 0, _width + 1 - barWidth, _height );
  239. }
  240. void drawDividers()
  241. {
  242. if(_width / _numTotalTests < 5)
  243. return;
  244. for ( unsigned i = 1; i < _testsDone; ++ i ) {
  245. int x = (_width * i) / _numTotalTests;
  246. XDrawLine( _display, _window, _gc, x, 0, x, _height);
  247. }
  248. }
  249. void drawPercentage()
  250. {
  251. XSetForeground( _display, _gc, _foreground );
  252. char str[sizeof("1000000000 of ") + sizeof(_strTotalTests) + sizeof(" (100%)")];
  253. sprintf( str, "%u of %s (%u%%)", _testsDone, _strTotalTests, (_testsDone * 100) / _numTotalTests );
  254. unsigned len = strlen( str );
  255. int textWidth = XTextWidth( _fontInfo, str, len );
  256. XDrawString( _display, _window, _gc,
  257. (_width - textWidth) / 2, ((_height + _textHeight) / 2) - _textDescent,
  258. str, len );
  259. }
  260. void flush()
  261. {
  262. XFlush( _display );
  263. }
  264. void destroyGc()
  265. {
  266. XFreeGC( _display, _gc );
  267. }
  268. void destroyWindow()
  269. {
  270. XDestroyWindow( _display, _window );
  271. }
  272. void closeDisplay()
  273. {
  274. XCloseDisplay( _display );
  275. }
  276. };
  277. };
  278. #endif //__cxxtest__X11Gui_h__