draw.c 5.0 KB

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