PageRenderTime 81ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/src/draw.h

https://github.com/xorg62/wmfs
C Header | 134 lines | 99 code | 20 blank | 15 comment | 1 complexity | a5855906b867424c69e4fb58aea26b50 MD5 | raw file
Possible License(s): BSD-3-Clause, WTFPL
  1. /*
  2. * wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
  3. * For license, see COPYING.
  4. */
  5. #ifndef DRAW_H
  6. #define DRAW_H
  7. #include <string.h>
  8. #include <X11/Xlib.h>
  9. #ifdef HAVE_IMLIB2
  10. #include <Imlib2.h>
  11. #endif /* HAVE_IMLIB2 */
  12. #include "wmfs.h"
  13. #include "config.h"
  14. #include "screen.h"
  15. #ifdef HAVE_XFT
  16. #define TEXTY(t, w) ((t->font->height - t->font->descent) + ((w - t->font->height) / 2))
  17. #else
  18. #define TEXTY(t, w) ((t->font.height - t->font.de) + ((w - t->font.height) / 2))
  19. #endif /* HAVE_XFT */
  20. #define PAD (8)
  21. #ifdef HAVE_XFT
  22. static inline void
  23. draw_text(XftDraw *xftdraw, struct theme *t, int x, int y, FgColor fg, const char *str)
  24. {
  25. XftDrawStringUtf8(xftdraw, &fg, t->font, x, y, (XftChar8*)str, strlen(str));
  26. }
  27. #else
  28. static inline void
  29. draw_text(Drawable d, struct theme *t, int x, int y, FgColor fg, const char *str)
  30. {
  31. XSetForeground(W->dpy, W->gc, fg);
  32. XmbDrawString(W->dpy, d, t->font.fontset, W->gc, x, y, str, strlen(str));
  33. }
  34. #endif /* HAVE_XFT */
  35. static inline void
  36. draw_rect(Drawable d, struct geo *g, BgColor bg)
  37. {
  38. XSetForeground(W->dpy, W->gc, bg);
  39. XFillRectangle(W->dpy, d, W->gc, g->x, g->y, g->w, g->h);
  40. }
  41. #ifdef HAVE_IMLIB2
  42. /*
  43. * Draw image on drawable with g geo
  44. * Require that the image was loaded with draw_image_load()
  45. */
  46. static inline void
  47. draw_image(Drawable d, struct geo *g)
  48. {
  49. imlib_context_set_drawable(d);
  50. imlib_render_image_on_drawable_at_size(g->x, g->y, g->w, g->h);
  51. imlib_free_image();
  52. }
  53. /*
  54. * Load image, set it in imlib context, and return
  55. * width & height as argument 2 & 3
  56. */
  57. static inline void
  58. draw_image_load(char *path, int *w, int *h)
  59. {
  60. Imlib_Image image = imlib_load_image(path);
  61. imlib_context_set_image(image);
  62. *w = imlib_image_get_width();
  63. *h = imlib_image_get_height();
  64. }
  65. #endif /* HAVE_IMLIB2 */
  66. /*
  67. * For client use
  68. */
  69. static inline void
  70. draw_reversed_rect(Drawable dr, struct client *c, bool t)
  71. {
  72. struct geo *g = (t ? &c->tgeo : &c->geo);
  73. struct geo *ug = &c->screen->ugeo;
  74. int i = c->theme->client_border_width;
  75. if(c->flags & CLIENT_FREE)
  76. {
  77. XDrawRectangle(W->dpy, dr, W->rgc,
  78. ug->x + g->x + i,
  79. ug->y + g->y + i,
  80. g->w - (i * 2),
  81. g->h - (i * 2));
  82. }
  83. else
  84. {
  85. XDrawRectangle(W->dpy, dr, W->rgc,
  86. ug->x + g->x + i + (W->padding / 4),
  87. ug->y + g->y + i + (W->padding / 4),
  88. g->w - (i * 2) - (W->padding / 2),
  89. g->h - (i * 2) - (W->padding / 2));
  90. }
  91. }
  92. static inline void
  93. draw_line(Drawable d, int x1, int y1, int x2, int y2)
  94. {
  95. XDrawLine(W->dpy, d, W->gc, x1, y1, x2, y2);
  96. }
  97. #ifdef HAVE_XFT
  98. static inline unsigned short
  99. draw_textw(struct theme *t, const char *str)
  100. {
  101. XGlyphInfo r;
  102. XftTextExtentsUtf8(W->dpy, t->font, (XftChar8*) str, strlen(str), &r);
  103. return r.width + t->font->descent;
  104. }
  105. #else
  106. static inline unsigned short
  107. draw_textw(struct theme *t, const char *str)
  108. {
  109. XRectangle r;
  110. XmbTextExtents(t->font.fontset, str, strlen(str), NULL, &r);
  111. return r.width;
  112. }
  113. #endif /* HAVE_XFT */
  114. #endif /* DRAW_H */