draw.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <X11/Xlocale.h>
  8. #include "wm.h"
  9. static void
  10. drawborder(void)
  11. {
  12. XPoint points[5];
  13. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  14. XSetForeground(dpy, dc.gc, dc.border);
  15. points[0].x = dc.x;
  16. points[0].y = dc.y;
  17. points[1].x = dc.w - 1;
  18. points[1].y = 0;
  19. points[2].x = 0;
  20. points[2].y = dc.h - 1;
  21. points[3].x = -(dc.w - 1);
  22. points[3].y = 0;
  23. points[4].x = 0;
  24. points[4].y = -(dc.h - 1);
  25. XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
  26. }
  27. void
  28. draw(Bool border, const char *text)
  29. {
  30. int x, y, w, h;
  31. unsigned int len;
  32. static char buf[256];
  33. XGCValues gcv;
  34. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  35. XSetForeground(dpy, dc.gc, dc.bg);
  36. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  37. w = 0;
  38. if(border)
  39. drawborder();
  40. if(!text)
  41. return;
  42. len = strlen(text);
  43. if(len >= sizeof(buf))
  44. len = sizeof(buf) - 1;
  45. memcpy(buf, text, len);
  46. buf[len] = 0;
  47. h = dc.font.ascent + dc.font.descent;
  48. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  49. x = dc.x + (h / 2);
  50. /* shorten text if necessary */
  51. while(len && (w = textnw(&dc.font, buf, len)) > dc.w - h)
  52. buf[--len] = 0;
  53. if(w > dc.w)
  54. return; /* too long */
  55. gcv.foreground = dc.fg;
  56. gcv.background = dc.bg;
  57. if(dc.font.set) {
  58. XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
  59. XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
  60. x, y, buf, len);
  61. }
  62. else {
  63. gcv.font = dc.font.xfont->fid;
  64. XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
  65. XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  66. }
  67. }
  68. static unsigned long
  69. xinitcolors(Colormap cmap, const char *colstr)
  70. {
  71. XColor color;
  72. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  73. return color.pixel;
  74. }
  75. void
  76. initcolors(const char *bg, const char *fg, const char *border)
  77. {
  78. Colormap cmap = DefaultColormap(dpy, screen);
  79. dc.bg = xinitcolors(cmap, bg);
  80. dc.fg = xinitcolors(cmap, fg);
  81. dc.border = xinitcolors(cmap, border);
  82. }
  83. unsigned int
  84. textnw(Fnt *font, char *text, unsigned int len)
  85. {
  86. XRectangle r;
  87. if(font->set) {
  88. XmbTextExtents(font->set, text, len, NULL, &r);
  89. return r.width;
  90. }
  91. return XTextWidth(font->xfont, text, len);
  92. }
  93. unsigned int
  94. textw(Fnt *font, char *text)
  95. {
  96. return textnw(font, text, strlen(text));
  97. }
  98. unsigned int
  99. texth(Fnt *font)
  100. {
  101. return font->height + 4;
  102. }
  103. void
  104. initfont(Fnt *font, const char *fontstr)
  105. {
  106. char **missing, *def;
  107. int i, n;
  108. missing = NULL;
  109. setlocale(LC_ALL, "");
  110. if(font->set)
  111. XFreeFontSet(dpy, font->set);
  112. font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  113. if(missing) {
  114. while(n--)
  115. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  116. XFreeStringList(missing);
  117. if(font->set) {
  118. XFreeFontSet(dpy, font->set);
  119. font->set = NULL;
  120. }
  121. }
  122. if(font->set) {
  123. XFontSetExtents *font_extents;
  124. XFontStruct **xfonts;
  125. char **font_names;
  126. font->ascent = font->descent = 0;
  127. font_extents = XExtentsOfFontSet(font->set);
  128. n = XFontsOfFontSet(font->set, &xfonts, &font_names);
  129. for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
  130. if(font->ascent < (*xfonts)->ascent)
  131. font->ascent = (*xfonts)->ascent;
  132. if(font->descent < (*xfonts)->descent)
  133. font->descent = (*xfonts)->descent;
  134. xfonts++;
  135. }
  136. }
  137. else {
  138. if(font->xfont)
  139. XFreeFont(dpy, font->xfont);
  140. font->xfont = NULL;
  141. font->xfont = XLoadQueryFont(dpy, fontstr);
  142. if (!font->xfont)
  143. font->xfont = XLoadQueryFont(dpy, "fixed");
  144. if (!font->xfont)
  145. error("error, cannot init 'fixed' font\n");
  146. font->ascent = font->xfont->ascent;
  147. font->descent = font->xfont->descent;
  148. }
  149. font->height = font->ascent + font->descent;
  150. }