draw.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dwm.h"
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <X11/Xlocale.h>
  9. /* static */
  10. static void
  11. drawborder(void)
  12. {
  13. XPoint points[5];
  14. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  15. XSetForeground(dpy, dc.gc, dc.border);
  16. points[0].x = dc.x;
  17. points[0].y = dc.y;
  18. points[1].x = dc.w - 1;
  19. points[1].y = 0;
  20. points[2].x = 0;
  21. points[2].y = dc.h - 1;
  22. points[3].x = -(dc.w - 1);
  23. points[3].y = 0;
  24. points[4].x = 0;
  25. points[4].y = -(dc.h - 1);
  26. XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
  27. }
  28. static unsigned int
  29. textnw(char *text, unsigned int len)
  30. {
  31. XRectangle r;
  32. if(dc.font.set) {
  33. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  34. return r.width;
  35. }
  36. return XTextWidth(dc.font.xfont, text, len);
  37. }
  38. static void
  39. drawtext(const char *text, Bool invert, Bool border)
  40. {
  41. int x, y, w, h;
  42. unsigned int len;
  43. static char buf[256];
  44. XGCValues gcv;
  45. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  46. XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
  47. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  48. w = 0;
  49. if(border)
  50. drawborder();
  51. if(!text)
  52. return;
  53. len = strlen(text);
  54. if(len >= sizeof(buf))
  55. len = sizeof(buf) - 1;
  56. memcpy(buf, text, len);
  57. buf[len] = 0;
  58. h = dc.font.ascent + dc.font.descent;
  59. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  60. x = dc.x + (h / 2);
  61. /* shorten text if necessary */
  62. while(len && (w = textnw(buf, len)) > dc.w - h)
  63. buf[--len] = 0;
  64. if(w > dc.w)
  65. return; /* too long */
  66. gcv.foreground = invert ? dc.bg : dc.fg;
  67. gcv.background = invert ? dc.fg : dc.bg;
  68. if(dc.font.set) {
  69. XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
  70. XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
  71. x, y, buf, len);
  72. }
  73. else {
  74. gcv.font = dc.font.xfont->fid;
  75. XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
  76. XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  77. }
  78. }
  79. /* extern */
  80. void
  81. drawall()
  82. {
  83. Client *c;
  84. for(c = clients; c; c = getnext(c->next, tsel))
  85. drawtitle(c);
  86. drawstatus();
  87. }
  88. void
  89. drawstatus()
  90. {
  91. int i;
  92. Bool istile = arrange == dotile;
  93. dc.x = dc.y = 0;
  94. dc.w = bw;
  95. drawtext(NULL, !istile, False);
  96. dc.w = 0;
  97. for(i = 0; i < TLast; i++) {
  98. dc.x += dc.w;
  99. dc.w = textw(tags[i]);
  100. if(istile)
  101. drawtext(tags[i], (i == tsel), True);
  102. else
  103. drawtext(tags[i], (i != tsel), True);
  104. }
  105. if(sel) {
  106. dc.x += dc.w;
  107. dc.w = textw(sel->name);
  108. drawtext(sel->name, istile, True);
  109. }
  110. dc.w = textw(stext);
  111. dc.x = bx + bw - dc.w;
  112. drawtext(stext, !istile, False);
  113. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  114. XSync(dpy, False);
  115. }
  116. void
  117. drawtitle(Client *c)
  118. {
  119. int i;
  120. Bool istile = arrange == dotile;
  121. if(c == sel) {
  122. drawstatus();
  123. XUnmapWindow(dpy, c->title);
  124. XSetWindowBorder(dpy, c->win, dc.fg);
  125. return;
  126. }
  127. XSetWindowBorder(dpy, c->win, dc.bg);
  128. XMapWindow(dpy, c->title);
  129. dc.x = dc.y = 0;
  130. dc.w = 0;
  131. for(i = 0; i < TLast; i++) {
  132. if(c->tags[i]) {
  133. dc.x += dc.w;
  134. dc.w = textw(c->tags[i]);
  135. drawtext(c->tags[i], !istile, True);
  136. }
  137. }
  138. dc.x += dc.w;
  139. dc.w = textw(c->name);
  140. drawtext(c->name, !istile, True);
  141. XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
  142. XSync(dpy, False);
  143. }
  144. unsigned long
  145. getcolor(const char *colstr)
  146. {
  147. XColor color;
  148. Colormap cmap = DefaultColormap(dpy, screen);
  149. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  150. return color.pixel;
  151. }
  152. void
  153. setfont(const char *fontstr)
  154. {
  155. char **missing, *def;
  156. int i, n;
  157. missing = NULL;
  158. setlocale(LC_ALL, "");
  159. if(dc.font.set)
  160. XFreeFontSet(dpy, dc.font.set);
  161. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  162. if(missing) {
  163. while(n--)
  164. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  165. XFreeStringList(missing);
  166. if(dc.font.set) {
  167. XFreeFontSet(dpy, dc.font.set);
  168. dc.font.set = NULL;
  169. }
  170. }
  171. if(dc.font.set) {
  172. XFontSetExtents *font_extents;
  173. XFontStruct **xfonts;
  174. char **font_names;
  175. dc.font.ascent = dc.font.descent = 0;
  176. font_extents = XExtentsOfFontSet(dc.font.set);
  177. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  178. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  179. if(dc.font.ascent < (*xfonts)->ascent)
  180. dc.font.ascent = (*xfonts)->ascent;
  181. if(dc.font.descent < (*xfonts)->descent)
  182. dc.font.descent = (*xfonts)->descent;
  183. xfonts++;
  184. }
  185. }
  186. else {
  187. if(dc.font.xfont)
  188. XFreeFont(dpy, dc.font.xfont);
  189. dc.font.xfont = NULL;
  190. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  191. if (!dc.font.xfont)
  192. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  193. if (!dc.font.xfont)
  194. eprint("error, cannot init 'fixed' font\n");
  195. dc.font.ascent = dc.font.xfont->ascent;
  196. dc.font.descent = dc.font.xfont->descent;
  197. }
  198. dc.font.height = dc.font.ascent + dc.font.descent;
  199. }
  200. unsigned int
  201. textw(char *text)
  202. {
  203. return textnw(text, strlen(text)) + dc.font.height;
  204. }