draw.c 4.5 KB

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