draw.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 && ((dc.w = dc.x - x) >= bh)) {
  112. dc.x = x;
  113. drawtext(sel->name, istile);
  114. }
  115. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  116. XSync(dpy, False);
  117. }
  118. void
  119. drawtitle(Client *c)
  120. {
  121. int i;
  122. Bool istile = arrange == dotile;
  123. if(c == sel && issel) {
  124. drawstatus();
  125. XUnmapWindow(dpy, c->title);
  126. XSetWindowBorder(dpy, c->win, dc.fg);
  127. return;
  128. }
  129. XSetWindowBorder(dpy, c->win, dc.bg);
  130. XMapWindow(dpy, c->title);
  131. dc.x = dc.y = 0;
  132. dc.w = 0;
  133. for(i = 0; i < ntags; i++) {
  134. if(c->tags[i]) {
  135. dc.x += dc.w;
  136. dc.w = textw(tags[i]);
  137. drawtext(tags[i], !istile);
  138. }
  139. }
  140. dc.x += dc.w;
  141. dc.w = c->tw - dc.x;
  142. drawtext(c->name, !istile);
  143. XCopyArea(dpy, dc.drawable, c->title, dc.gc, 0, 0, c->tw, c->th, 0, 0);
  144. XSync(dpy, False);
  145. }
  146. unsigned long
  147. getcolor(const char *colstr)
  148. {
  149. Colormap cmap = DefaultColormap(dpy, screen);
  150. XColor color;
  151. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  152. return color.pixel;
  153. }
  154. void
  155. setfont(const char *fontstr)
  156. {
  157. char **missing, *def;
  158. int i, n;
  159. missing = NULL;
  160. setlocale(LC_ALL, "");
  161. if(dc.font.set)
  162. XFreeFontSet(dpy, dc.font.set);
  163. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  164. if(missing) {
  165. while(n--)
  166. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  167. XFreeStringList(missing);
  168. if(dc.font.set) {
  169. XFreeFontSet(dpy, dc.font.set);
  170. dc.font.set = NULL;
  171. }
  172. }
  173. if(dc.font.set) {
  174. XFontSetExtents *font_extents;
  175. XFontStruct **xfonts;
  176. char **font_names;
  177. dc.font.ascent = dc.font.descent = 0;
  178. font_extents = XExtentsOfFontSet(dc.font.set);
  179. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  180. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  181. if(dc.font.ascent < (*xfonts)->ascent)
  182. dc.font.ascent = (*xfonts)->ascent;
  183. if(dc.font.descent < (*xfonts)->descent)
  184. dc.font.descent = (*xfonts)->descent;
  185. xfonts++;
  186. }
  187. }
  188. else {
  189. if(dc.font.xfont)
  190. XFreeFont(dpy, dc.font.xfont);
  191. dc.font.xfont = NULL;
  192. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  193. if (!dc.font.xfont)
  194. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  195. if (!dc.font.xfont)
  196. eprint("error, cannot init 'fixed' font\n");
  197. dc.font.ascent = dc.font.xfont->ascent;
  198. dc.font.descent = dc.font.xfont->descent;
  199. }
  200. dc.font.height = dc.font.ascent + dc.font.descent;
  201. }
  202. unsigned int
  203. textw(const char *text)
  204. {
  205. return textnw(text, strlen(text)) + dc.font.height;
  206. }