/clxclient-3.6.1/xdraw.cc

# · C++ · 137 lines · 91 code · 27 blank · 19 comment · 22 complexity · 1c97aa89776b2a7e956a8d8a7686aaca MD5 · raw file

  1. // ---------------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 2003-2008 Fons Adriaensen <fons@kokkinizita.net>
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published
  7. // by the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. //
  19. // ---------------------------------------------------------------------------------
  20. #include <string.h>
  21. #include "clxclient.h"
  22. X_draw::X_draw (Display *dpy, Drawable drw, GC gct, XftDraw *xft) :
  23. _dpy (dpy), _drw (drw), _gct (gct),
  24. _xft_draw (xft), _xft_color (0), _xft_font (0),
  25. _xx (0), _yy (0)
  26. {
  27. if (_xft_draw && XftDrawDrawable (_xft_draw) != _drw) XftDrawChange (_xft_draw, _drw);
  28. }
  29. int X_draw::textwidth (const char *str)
  30. {
  31. int len;
  32. XGlyphInfo G;
  33. if (str && (len = strlen (str)))
  34. {
  35. if (_xft_font)
  36. {
  37. XftTextExtentsUtf8 (_dpy, _xft_font, (const FcChar8 *) str, len, &G);
  38. return G.width;
  39. }
  40. else if (_x11_font) return XTextWidth (_x11_font, str, len);
  41. }
  42. return 0;
  43. }
  44. void X_draw::drawstring (const char *str, int xal)
  45. {
  46. int len, dx = 0;
  47. XGlyphInfo G;
  48. if (str && (len = strlen (str)))
  49. {
  50. if (_xft_font)
  51. {
  52. if (xal >= 0)
  53. {
  54. XftTextExtentsUtf8 (_dpy, _xft_font, (const FcChar8 *) str, len, &G);
  55. dx = G.width;
  56. if (xal == 0) dx /= 2;
  57. }
  58. XftDrawStringUtf8 (_xft_draw, _xft_color, _xft_font, _xx - dx, _yy, (const FcChar8 *) str, len);
  59. }
  60. else if (_x11_font)
  61. {
  62. if (xal >= 0)
  63. {
  64. dx = XTextWidth (_x11_font, str, len);
  65. if (xal == 0) dx /= 2;
  66. }
  67. XDrawString (_dpy, _drw, _gct, _xx - dx, _yy, str, len);
  68. }
  69. }
  70. }
  71. void X_draw::drawpoints (int n, XPoint *P)
  72. {
  73. XDrawPoints (_dpy, _drw, _gct, P, n, CoordModeOrigin);
  74. }
  75. void X_draw::drawlines (int n, XPoint *P)
  76. {
  77. int k, m;
  78. m = (XMaxRequestSize (_dpy) - 3) / 2;
  79. while (n > 1)
  80. {
  81. if (n > m) k = m;
  82. else k = n;
  83. XDrawLines (_dpy, _drw, _gct, P, k, CoordModeOrigin);
  84. P += k - 1;
  85. n -= k - 1;
  86. }
  87. }
  88. void X_draw::drawsegments (int n, XSegment *S)
  89. {
  90. XDrawSegments (_dpy, _drw, _gct, S, n);
  91. }
  92. void X_draw::setclip (int x0, int y0, int x1, int y1)
  93. {
  94. XRectangle R;
  95. R.x = x0;
  96. R.y = y0;
  97. R.width = x1 - x0;
  98. R.height = y1 - y0;
  99. XSetClipRectangles (_dpy, _gct, 0, 0, &R, 1, Unsorted);
  100. }
  101. void X_draw::movepix (int dx, int dy, int xs, int ys)
  102. {
  103. XGCValues G;
  104. G.graphics_exposures = True;
  105. G.function = GXcopy;
  106. XChangeGC (_dpy, _gct, GCGraphicsExposures | GCFunction, &G);
  107. XCopyArea (_dpy, _drw, _drw, _gct, dx, dy, xs, ys, 0, 0);
  108. G.graphics_exposures = False;
  109. XChangeGC (_dpy, _gct, GCGraphicsExposures, &G);
  110. }