draw.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* (C)opyright MMIV-MMVI 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. #include <X11/Xlocale.h>
  8. /* static */
  9. static Bool
  10. isoccupied(unsigned int t)
  11. {
  12. Client *c;
  13. for(c = clients; c; c = c->next)
  14. if(c->tags[t])
  15. return True;
  16. return False;
  17. }
  18. static unsigned int
  19. textnw(const char *text, unsigned int len) {
  20. XRectangle r;
  21. if(dc.font.set) {
  22. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  23. return r.width;
  24. }
  25. return XTextWidth(dc.font.xfont, text, len);
  26. }
  27. static void
  28. drawtext(const char *text, unsigned long col[ColLast], Bool ldot, Bool rdot) {
  29. int x, y, w, h;
  30. static char buf[256];
  31. unsigned int len, olen;
  32. XGCValues gcv;
  33. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  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. if(ldot) {
  71. r.x = dc.x + 2;
  72. r.y = dc.y + 2;
  73. r.width = r.height = (h + 2) / 4;
  74. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  75. }
  76. if(rdot) {
  77. r.width = r.height = (h + 2) / 4;
  78. r.x = dc.x + dc.w - r.width - 2;
  79. r.y = dc.y + dc.h - r.height - 2;
  80. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  81. }
  82. }
  83. /* extern */
  84. void
  85. drawall(void) {
  86. Client *c;
  87. for(c = clients; c; c = getnext(c->next))
  88. drawtitle(c);
  89. drawstatus();
  90. }
  91. void
  92. drawstatus(void) {
  93. int i, x;
  94. dc.x = dc.y = 0;
  95. for(i = 0; i < ntags; i++) {
  96. dc.w = textw(tags[i]);
  97. if(seltag[i])
  98. drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
  99. else
  100. drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
  101. dc.x += dc.w;
  102. }
  103. dc.w = bmw;
  104. drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False, False);
  105. x = dc.x + dc.w;
  106. dc.w = textw(stext);
  107. dc.x = bw - dc.w;
  108. if(dc.x < x) {
  109. dc.x = x;
  110. dc.w = bw - x;
  111. }
  112. drawtext(stext, dc.status, False, False);
  113. if((dc.w = dc.x - x) > bh) {
  114. dc.x = x;
  115. drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
  116. }
  117. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  118. XSync(dpy, False);
  119. }
  120. void
  121. drawtitle(Client *c) {
  122. if(c == sel && issel) {
  123. drawstatus();
  124. XUnmapWindow(dpy, c->twin);
  125. XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
  126. return;
  127. }
  128. XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
  129. XMapWindow(dpy, c->twin);
  130. dc.x = dc.y = 0;
  131. dc.w = c->tw;
  132. drawtext(c->name, dc.norm, False,False);
  133. XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
  134. XSync(dpy, False);
  135. }
  136. unsigned long
  137. getcolor(const char *colstr) {
  138. Colormap cmap = DefaultColormap(dpy, screen);
  139. XColor color;
  140. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  141. eprint("error, cannot allocate color '%s'\n", colstr);
  142. return color.pixel;
  143. }
  144. void
  145. setfont(const char *fontstr) {
  146. char **missing, *def;
  147. int i, n;
  148. missing = NULL;
  149. setlocale(LC_ALL, "");
  150. if(dc.font.set)
  151. XFreeFontSet(dpy, dc.font.set);
  152. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  153. if(missing) {
  154. while(n--)
  155. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  156. XFreeStringList(missing);
  157. if(dc.font.set) {
  158. XFreeFontSet(dpy, dc.font.set);
  159. dc.font.set = NULL;
  160. }
  161. }
  162. if(dc.font.set) {
  163. XFontSetExtents *font_extents;
  164. XFontStruct **xfonts;
  165. char **font_names;
  166. dc.font.ascent = dc.font.descent = 0;
  167. font_extents = XExtentsOfFontSet(dc.font.set);
  168. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  169. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  170. if(dc.font.ascent < (*xfonts)->ascent)
  171. dc.font.ascent = (*xfonts)->ascent;
  172. if(dc.font.descent < (*xfonts)->descent)
  173. dc.font.descent = (*xfonts)->descent;
  174. xfonts++;
  175. }
  176. }
  177. else {
  178. if(dc.font.xfont)
  179. XFreeFont(dpy, dc.font.xfont);
  180. dc.font.xfont = NULL;
  181. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  182. if (!dc.font.xfont)
  183. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  184. if (!dc.font.xfont)
  185. eprint("error, cannot init 'fixed' font\n");
  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. }