/src/FbTk/FbDrawable.cc

https://github.com/rpavlik/fluxbox · C++ · 161 lines · 111 code · 21 blank · 29 comment · 50 complexity · 5ed09623e901abdfcb16afa1ec96ce5f MD5 · raw file

  1. // FbDrawable.cc for FbTk - Fluxbox ToolKit
  2. // Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a
  5. // copy of this software and associated documentation files (the "Software"),
  6. // to deal in the Software without restriction, including without limitation
  7. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. // and/or sell copies of the Software, and to permit persons to whom the
  9. // Software is furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. // DEALINGS IN THE SOFTWARE.
  21. #include "FbDrawable.hh"
  22. #include "App.hh"
  23. namespace FbTk {
  24. Display *FbDrawable::s_display = 0;
  25. FbDrawable::FbDrawable() {
  26. if (s_display == 0) {
  27. s_display = FbTk::App::instance()->display();
  28. }
  29. }
  30. void FbDrawable::copyArea(Drawable src, GC gc,
  31. int src_x, int src_y,
  32. int dest_x, int dest_y,
  33. unsigned int width, unsigned int height) {
  34. if (drawable() == 0 || src == 0 || gc == 0)
  35. return;
  36. XCopyArea(display(),
  37. src, drawable(), gc,
  38. src_x, src_y,
  39. width, height,
  40. dest_x, dest_y);
  41. }
  42. void FbDrawable::fillRectangle(GC gc, int x, int y,
  43. unsigned int width, unsigned int height) {
  44. if (drawable() == 0 || gc == 0)
  45. return;
  46. XFillRectangle(display(),
  47. drawable(), gc,
  48. x, y,
  49. width, height);
  50. }
  51. void FbDrawable::drawRectangle(GC gc, int x, int y,
  52. unsigned int width, unsigned int height) {
  53. if (drawable() == 0 || gc == 0)
  54. return;
  55. XDrawRectangle(display(),
  56. drawable(), gc,
  57. x, y,
  58. width, height);
  59. }
  60. void FbDrawable::drawLine(GC gc, int start_x, int start_y,
  61. int end_x, int end_y) {
  62. if (drawable() == 0 || gc == 0)
  63. return;
  64. XDrawLine(display(),
  65. drawable(),
  66. gc,
  67. start_x, start_y,
  68. end_x, end_y);
  69. }
  70. void FbDrawable::fillPolygon(GC gc, XPoint *points, int npoints,
  71. int shape, int mode) {
  72. if (drawable() == 0 || gc == 0 || points == 0 || npoints == 0)
  73. return;
  74. XFillPolygon(display(),
  75. drawable(), gc, points, npoints,
  76. shape, mode);
  77. }
  78. // x, y, width and height define a space within which we're drawing a triangle (centred)
  79. // scale defines number of triangles that'd fit in a space of 100 width x 100 height
  80. // (i.e. 200 = half size, 300 = a third). Its a bit backwards but it allows more flexibility
  81. void FbDrawable::drawTriangle(GC gc, FbDrawable::TriangleType type,
  82. int x, int y, unsigned int width, unsigned int height,
  83. int scale) {
  84. if (drawable() == 0 || gc == 0 || width == 0 || height == 0)
  85. return;
  86. XPoint pts[3];
  87. if (scale < 100) scale = 100; // not bigger than the space allowed
  88. else if (scale > 10000) scale = 10000; // not too small...
  89. int arrowscale_n = scale;
  90. int arrowscale_d = 100;
  91. unsigned int ax = arrowscale_d * width / arrowscale_n;
  92. unsigned int ay = arrowscale_d * height / arrowscale_n;
  93. // if these aren't an even number, left and right arrows end up different
  94. if (type == FbTk::FbDrawable::LEFT ||
  95. type == FbTk::FbDrawable::RIGHT) {
  96. if (( ax % 2 ) == 1) ax--;
  97. if (( ay % 2 ) == 1) ay--;
  98. } else {
  99. if (( ax % 2 ) == 0) ax--;
  100. }
  101. switch (type) {
  102. case FbTk::FbDrawable::LEFT:
  103. // start at the tip
  104. pts[0].x = (width / 2) - (ax / 2); pts[0].y = height / 2;
  105. pts[1].x = ax; pts[1].y = -ay / 2;
  106. pts[2].x = 0; pts[2].y = ay;
  107. break;
  108. case FbTk::FbDrawable::RIGHT:
  109. pts[0].x = (width / 2) + (ax / 2); pts[0].y = height / 2;
  110. pts[1].x = - ax; pts[1].y = ay / 2;
  111. pts[2].x = 0; pts[2].y = - ay;
  112. break;
  113. case FbTk::FbDrawable::UP:
  114. pts[0].x = (width / 2); pts[0].y = (height / 2) - (ay / 2)-1;
  115. pts[1].x = ax / 2; pts[1].y = ay+1;
  116. pts[2].x = - ax; pts[2].y = 0;
  117. break;
  118. case FbTk::FbDrawable::DOWN:
  119. /* I tried and tried, but couldn't get the left diagonal of the down
  120. arrow to be symmetrical with the right (for small widths)!
  121. So we opt for this setup. It is symmetrical with larger widths */
  122. pts[0].x = (width / 2) ; pts[0].y = (height / 2) + (ay / 2);
  123. pts[1].x = -ax/2+1; pts[1].y = -ay;
  124. pts[2].x = ax-1; pts[2].y = 0;
  125. break;
  126. }
  127. // re-centre on the specified points
  128. pts[0].x += x;
  129. pts[0].y += y;
  130. fillPolygon(gc,
  131. pts, 3,
  132. Convex, CoordModePrevious);
  133. }
  134. XImage *FbDrawable::image(int x, int y, unsigned int width, unsigned int height) const {
  135. return XGetImage(display(), drawable(),
  136. x, y, width, height,
  137. AllPlanes, // plane mask
  138. ZPixmap);
  139. }
  140. } // end namespace FbTk