rectangle.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "rectangle.hpp"
  2. static Bool isDestroyNotify( Display* dpy, XEvent* ev, XPointer win ) {
  3. return ev->type == DestroyNotify && ev->xdestroywindow.window == *((Window*)win);
  4. }
  5. slop::Rectangle::~Rectangle() {
  6. if ( m_window == None ) {
  7. return;
  8. }
  9. // Free up our color.
  10. XFreeColors( xengine->m_display, xengine->m_colormap, &m_color.pixel, 1, 0 );
  11. XDestroyWindow( xengine->m_display, m_window );
  12. XEvent event;
  13. // Block until the window is actually completely removed.
  14. XIfEvent( xengine->m_display, &event, &isDestroyNotify, (XPointer)&m_window );
  15. // Sleep for 0.1 seconds in hope that the screen actually cleared the window.
  16. usleep( 10000 );
  17. }
  18. slop::Rectangle::Rectangle( int sx, int sy, int ex, int ey, int border, float r, float g, float b ) {
  19. m_x = std::min( sx, ex );
  20. m_y = std::min( sy, ey );
  21. m_width = std::max( sx, ex ) - m_x;
  22. m_height = std::max( sy, ey ) - m_y;
  23. m_border = border;
  24. m_window = None;
  25. // If we don't have a border, we don't exist, so just die.
  26. if ( m_border == 0 ) {
  27. return;
  28. }
  29. // This sets up m_color
  30. int err = convertColor( r, g, b );
  31. if ( err ) {
  32. fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
  33. }
  34. XSetWindowAttributes attributes;
  35. // Set up the window so it's our color
  36. attributes.background_pixmap = None;
  37. attributes.background_pixel = m_color.pixel;
  38. // Not actually sure what this does, but it keeps the window from bugging out :u.
  39. attributes.override_redirect = True;
  40. // We must use our color map, because that's where our color is allocated.
  41. attributes.colormap = xengine->m_colormap;
  42. // Make sure we know when we've been successfully destroyed later!
  43. attributes.event_mask = StructureNotifyMask;
  44. unsigned long valueMask = CWBackPixmap | CWBackPixel | CWOverrideRedirect | CWColormap | CWEventMask;
  45. // Create the window
  46. m_window = XCreateWindow( xengine->m_display, xengine->m_root, m_x-m_border, m_y-m_border, m_width+m_border*2, m_height+m_border*2,
  47. 0, CopyFromParent, InputOutput,
  48. CopyFromParent, valueMask, &attributes );
  49. // Now punch a hole into it so it looks like a selection rectangle!
  50. XRectangle rect;
  51. rect.x = rect.y = m_border;
  52. rect.width = m_width;
  53. rect.height = m_height;
  54. XClassHint classhints;
  55. char name[] = "slop";
  56. classhints.res_name = name;
  57. classhints.res_class = name;
  58. XSetClassHint( xengine->m_display, m_window, &classhints );
  59. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  60. XMapWindow( xengine->m_display, m_window );
  61. }
  62. void slop::Rectangle::setGeo( int sx, int sy, int ex, int ey ) {
  63. int x = std::min( sx, ex );
  64. int y = std::min( sy, ey );
  65. int w = std::max( sx, ex ) - x;
  66. int h = std::max( sy, ey ) - y;
  67. if ( m_x == x && m_y == y && m_width == w && m_height == h ) {
  68. return;
  69. }
  70. m_x = x;
  71. m_y = y;
  72. m_width = w;
  73. m_height = h;
  74. // If we don't have a border, we don't exist, so just die.
  75. if ( m_border == 0 ) {
  76. return;
  77. }
  78. // Change the window size
  79. XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
  80. // Fill up our old hole
  81. XRectangle rect;
  82. rect.x = rect.y = 0;
  83. rect.width = m_width+m_border*2;
  84. rect.height = m_height+m_border*2;
  85. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
  86. // Then punch out another.
  87. rect.x = rect.y = m_border;
  88. rect.width = m_width;
  89. rect.height = m_height;
  90. XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
  91. XMoveWindow( xengine->m_display, m_window, m_x-m_border, m_y-m_border );
  92. }
  93. int slop::Rectangle::convertColor( float r, float g, float b ) {
  94. // Convert float colors to shorts.
  95. short red = short( floor( r * 65535.f ) );
  96. short green = short( floor( g * 65535.f ) );
  97. short blue = short( floor( b * 65535.f ) );
  98. XColor color;
  99. color.red = red;
  100. color.green = green;
  101. color.blue = blue;
  102. int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
  103. if ( err == BadColor ) {
  104. return err;
  105. }
  106. m_color = color;
  107. return 0;
  108. }