draw.c 4.8 KB

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