/xlib++/xlib++/graphics_context.hpp

https://github.com/ereslibre/x11 · C++ Header · 202 lines · 136 code · 42 blank · 24 comment · 5 complexity · 1d525e8881552229ae3880a9c5b452c3 MD5 · raw file

  1. //
  2. //
  3. // Copyright 2002 Rob Tougher <robt@robtougher.com>
  4. //
  5. // This file is part of xlib++.
  6. //
  7. // xlib++ is free software; you can redistribute it and/or modify
  8. // it under the terms of the GNU General Public License as published by
  9. // the Free Software Foundation; either version 2 of the License, or
  10. // (at your option) any later version.
  11. //
  12. // xlib++ is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with xlib++; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. //
  21. //
  22. // graphics_context class
  23. #ifndef _xlib_graphics_context_class_
  24. #define _xlib_graphics_context_class_
  25. #include "display.hpp"
  26. #include "window.hpp"
  27. #include "exceptions.hpp"
  28. #include "color.hpp"
  29. #include "shapes.hpp"
  30. #include <vector>
  31. namespace xlib
  32. {
  33. class graphics_context
  34. {
  35. public:
  36. graphics_context ( display& d,
  37. int window_id )
  38. : m_display ( d ),
  39. m_window_id ( window_id )
  40. {
  41. XGCValues values;
  42. values.background = 1;
  43. m_gc = 0;
  44. m_gc = XCreateGC ( m_display,
  45. m_window_id,
  46. GCBackground,
  47. &values );
  48. if ( m_gc == 0 )
  49. {
  50. }
  51. };
  52. ~graphics_context(){};
  53. // drawing primitives
  54. void draw_line ( line l )
  55. {
  56. XDrawLine ( m_display,
  57. m_window_id,
  58. m_gc,
  59. l.point1().x(),
  60. l.point1().y(),
  61. l.point2().x(),
  62. l.point2().y() );
  63. }
  64. void draw_rectangle ( rectangle rect )
  65. {
  66. XDrawRectangle ( m_display,
  67. m_window_id,
  68. m_gc,
  69. rect.origin().x(),
  70. rect.origin().y(),
  71. rect.width(),
  72. rect.height() );
  73. }
  74. void draw_text ( point origin, std::string text )
  75. {
  76. XDrawString ( m_display,
  77. m_window_id,
  78. m_gc,
  79. origin.x(),
  80. origin.y(),
  81. text.c_str(),
  82. text.size() );
  83. }
  84. void fill_rectangle ( rectangle rect )
  85. {
  86. XFillRectangle ( m_display,
  87. m_window_id,
  88. m_gc,
  89. rect.origin().x(),
  90. rect.origin().y(),
  91. rect.width(),
  92. rect.height() );
  93. }
  94. void set_foreground ( color& c )
  95. {
  96. XSetForeground ( m_display,
  97. m_gc,
  98. c.pixel() );
  99. }
  100. void set_background ( color& c )
  101. {
  102. XSetBackground ( m_display,
  103. m_gc,
  104. c.pixel() );
  105. }
  106. rectangle get_text_rect ( std::string text )
  107. {
  108. int direction = 0, font_ascent = 0, font_descent = 0;
  109. XCharStruct char_struct;
  110. XQueryTextExtents ( m_display,
  111. XGContextFromGC(m_gc),
  112. text.c_str(),
  113. text.size(),
  114. &direction,
  115. &font_ascent,
  116. &font_descent,
  117. &char_struct );
  118. rectangle rect ( point(0,0),
  119. char_struct.rbearing - char_struct.lbearing,
  120. char_struct.ascent - char_struct.descent );
  121. return rect;
  122. }
  123. std::vector<int> get_character_widths ( std::string text )
  124. {
  125. // GContext gc_id = XGContextFromGC ( (GC)gc.id() );
  126. std::vector<int> char_widths;
  127. XFontStruct * font = XQueryFont ( m_display, (GContext)id() );
  128. for ( std::string::const_iterator it = text.begin();
  129. it != text.end();
  130. it++ )
  131. {
  132. std::string temp;
  133. temp += *it;
  134. int width = XTextWidth ( font,
  135. temp.c_str(),
  136. 1 );
  137. char_widths.push_back ( width );
  138. }
  139. return char_widths;
  140. }
  141. int get_text_height ()
  142. {
  143. XFontStruct * font = XQueryFont ( m_display, (GContext)id() );
  144. if ( font )
  145. {
  146. return font->max_bounds.ascent + font->max_bounds.descent;
  147. }
  148. else
  149. {
  150. return 0;
  151. }
  152. }
  153. long id() { return XGContextFromGC(m_gc); }
  154. private:
  155. display& m_display;
  156. int m_window_id;
  157. GC m_gc;
  158. };
  159. };
  160. #endif