draw.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * draw.c
  3. * Copyright © 2008, 2009 Martin Duquesnoy <xorg62@gmail.com>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of the nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "wmfs.h"
  33. /** Draw a string in a Drawable
  34. * \param d Drawable
  35. * \param x X position
  36. * \param y Y position
  37. * \param fg Foreground text color
  38. * \param pad Text padding
  39. * \param str String that will be draw
  40. */
  41. void
  42. draw_text(Drawable d, int x, int y, char* fg, int pad, char *str)
  43. {
  44. XftColor xftcolor;
  45. XftDraw *xftd;
  46. if(!str)
  47. return;
  48. /* To draw image everywhere we can draw text */
  49. #ifdef HAVE_IMLIB
  50. char *ostr = NULL;
  51. int i, ni;
  52. ImageAttr im[128];
  53. ostr = _strdup(str);
  54. if(strstr(str, "i["))
  55. {
  56. ni = parse_image_block(im, str);
  57. for(i = 0; i < ni; ++i)
  58. draw_image(d, im[i].x, im[i].y, im[i].w, im[i].h, im[i].name);
  59. }
  60. #endif /* HAVE_IMLIB */
  61. /* Transform X Drawable -> Xft Drawable */
  62. xftd = XftDrawCreate(dpy, d, DefaultVisual(dpy, SCREEN), DefaultColormap(dpy, SCREEN));
  63. /* Alloc text color */
  64. XftColorAllocName(dpy, DefaultVisual(dpy, SCREEN),
  65. DefaultColormap(dpy, SCREEN), fg, &xftcolor);
  66. XftDrawStringUtf8(xftd, &xftcolor, font, x, y, (FcChar8 *)str, strlen(str));
  67. /* Free the text color and XftDraw */
  68. XftColorFree(dpy, DefaultVisual(dpy, SCREEN), DefaultColormap(dpy, SCREEN), &xftcolor);
  69. XftDrawDestroy(xftd);
  70. #ifdef HAVE_IMLIB
  71. if(strstr(ostr, "i["))
  72. strcpy(str, ostr);
  73. IFREE(ostr);
  74. #endif /* HAVE_IMLIB */
  75. return;
  76. }
  77. /** Draw a Rectangle in a drawable
  78. * \param dr Drawable
  79. * \param x X position
  80. * \param y Y position
  81. * \param w Width
  82. * \param h Height
  83. * \param color Color of the rectangle
  84. */
  85. void
  86. draw_rectangle(Drawable dr, int x, int y, uint w, uint h, uint color)
  87. {
  88. XRectangle r = { x, y, w, h };
  89. XSetForeground(dpy, gc, color);
  90. XFillRectangles(dpy, dr, gc, &r, 1);
  91. return;
  92. }
  93. #ifdef HAVE_IMLIB
  94. /** Draw an image in a drawable
  95. * \param dr Drawable
  96. * \param x X position
  97. * \param y Y position
  98. * \param name Path of the image
  99. */
  100. void
  101. draw_image(Drawable dr, int x, int y, int w, int h, char *name)
  102. {
  103. Imlib_Image image;
  104. if(!name)
  105. return;
  106. imlib_set_cache_size(2048 * 1024);
  107. imlib_context_set_display(dpy);
  108. imlib_context_set_visual(DefaultVisual(dpy, DefaultScreen(dpy)));
  109. imlib_context_set_colormap(DefaultColormap(dpy, DefaultScreen(dpy)));
  110. imlib_context_set_drawable(dr);
  111. image = imlib_load_image(name);
  112. imlib_context_set_image(image);
  113. if(w <= 0)
  114. w = imlib_image_get_width();
  115. if(h <= 0)
  116. h = imlib_image_get_height();
  117. if(image)
  118. {
  119. imlib_render_image_on_drawable_at_size(x, y, w, h);
  120. imlib_free_image();
  121. }
  122. else
  123. warnx("Can't draw image: '%s'", name);
  124. return;
  125. }
  126. #endif /* HAVE_IMLIB */
  127. /** Calculates the text's size relatively to the font
  128. * \param text Text string
  129. * \return final text width
  130. */
  131. ushort
  132. textw(char *text)
  133. {
  134. XGlyphInfo gl;
  135. if(!text)
  136. return 0;
  137. #ifdef HAVE_IMLIB
  138. char *ostr = NULL;
  139. ImageAttr im[128];
  140. ostr = _strdup(text);
  141. if(strstr(text, "i["))
  142. parse_image_block(im, text);
  143. #endif /* HAVE_IMLIB */
  144. XftTextExtentsUtf8(dpy, font, (FcChar8 *)text, strlen(text), &gl);
  145. #ifdef HAVE_IMLIB
  146. if(strstr(ostr, "i["))
  147. strcpy(text, ostr);
  148. IFREE(ostr);
  149. #endif /* HAVE_IMLIB */
  150. return gl.width + font->descent;
  151. }