You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
4.3 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
18 years ago
18 years ago
19 years ago
18 years ago
18 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
  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. drawstatus(void) {
  94. int i, x;
  95. dc.x = dc.y = 0;
  96. for(i = 0; i < ntags; i++) {
  97. dc.w = textw(tags[i]);
  98. if(seltag[i])
  99. drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
  100. else
  101. drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
  102. dc.x += dc.w;
  103. }
  104. dc.w = bmw;
  105. drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.norm, False, False);
  106. x = dc.x + dc.w;
  107. dc.w = textw(stext);
  108. dc.x = bw - dc.w;
  109. if(dc.x < x) {
  110. dc.x = x;
  111. dc.w = bw - x;
  112. }
  113. drawtext(stext, dc.norm, False, False);
  114. if((dc.w = dc.x - x) > bh) {
  115. dc.x = x;
  116. drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
  117. }
  118. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  119. XSync(dpy, False);
  120. }
  121. unsigned long
  122. getcolor(const char *colstr) {
  123. Colormap cmap = DefaultColormap(dpy, screen);
  124. XColor color;
  125. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  126. eprint("error, cannot allocate color '%s'\n", colstr);
  127. return color.pixel;
  128. }
  129. void
  130. setfont(const char *fontstr) {
  131. char *def, **missing;
  132. int i, n;
  133. missing = NULL;
  134. if(dc.font.set)
  135. XFreeFontSet(dpy, dc.font.set);
  136. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  137. if(missing) {
  138. while(n--)
  139. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  140. XFreeStringList(missing);
  141. }
  142. if(dc.font.set) {
  143. XFontSetExtents *font_extents;
  144. XFontStruct **xfonts;
  145. char **font_names;
  146. dc.font.ascent = dc.font.descent = 0;
  147. font_extents = XExtentsOfFontSet(dc.font.set);
  148. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  149. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  150. if(dc.font.ascent < (*xfonts)->ascent)
  151. dc.font.ascent = (*xfonts)->ascent;
  152. if(dc.font.descent < (*xfonts)->descent)
  153. dc.font.descent = (*xfonts)->descent;
  154. xfonts++;
  155. }
  156. }
  157. else {
  158. if(dc.font.xfont)
  159. XFreeFont(dpy, dc.font.xfont);
  160. dc.font.xfont = NULL;
  161. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
  162. eprint("error, cannot load font: '%s'\n", fontstr);
  163. dc.font.ascent = dc.font.xfont->ascent;
  164. dc.font.descent = dc.font.xfont->descent;
  165. }
  166. dc.font.height = dc.font.ascent + dc.font.descent;
  167. }
  168. unsigned int
  169. textw(const char *text) {
  170. return textnw(text, strlen(text)) + dc.font.height;
  171. }