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.

175 lines
4.1 KiB

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
18 years ago
18 years ago
18 years ago
19 years ago
18 years ago
18 years ago
18 years ago
18 years ago
19 years ago
18 years ago
19 years ago
18 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
  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. XSetForeground(dpy, dc.gc, col[ColBG]);
  34. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  35. if(!text)
  36. return;
  37. w = 0;
  38. olen = len = strlen(text);
  39. if(len >= sizeof buf)
  40. len = sizeof buf - 1;
  41. memcpy(buf, text, len);
  42. buf[len] = 0;
  43. h = dc.font.ascent + dc.font.descent;
  44. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  45. x = dc.x + (h / 2);
  46. /* shorten text if necessary */
  47. while(len && (w = textnw(buf, len)) > dc.w - h)
  48. buf[--len] = 0;
  49. if(len < olen) {
  50. if(len > 1)
  51. buf[len - 1] = '.';
  52. if(len > 2)
  53. buf[len - 2] = '.';
  54. if(len > 3)
  55. buf[len - 3] = '.';
  56. }
  57. if(w > dc.w)
  58. return; /* too long */
  59. gcv.foreground = col[ColFG];
  60. if(dc.font.set) {
  61. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  62. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  63. }
  64. else {
  65. gcv.font = dc.font.xfont->fid;
  66. XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
  67. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  68. }
  69. x = (h + 2) / 4;
  70. r.x = dc.x + 1;
  71. r.y = dc.y + 1;
  72. if(filledsquare) {
  73. r.width = r.height = x + 1;
  74. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  75. }
  76. else if(emptysquare) {
  77. r.width = r.height = x;
  78. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  79. }
  80. }
  81. /* extern */
  82. void
  83. drawstatus(void) {
  84. int i, x;
  85. dc.x = dc.y = 0;
  86. for(i = 0; i < ntags; i++) {
  87. dc.w = textw(tags[i]);
  88. if(seltag[i])
  89. drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
  90. else
  91. drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
  92. dc.x += dc.w;
  93. }
  94. dc.w = bmw;
  95. drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.norm, False, False);
  96. x = dc.x + dc.w;
  97. dc.w = textw(stext);
  98. dc.x = sw - dc.w;
  99. if(dc.x < x) {
  100. dc.x = x;
  101. dc.w = sw - x;
  102. }
  103. drawtext(stext, dc.norm, False, False);
  104. if((dc.w = dc.x - x) > bh) {
  105. dc.x = x;
  106. drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
  107. }
  108. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
  109. XSync(dpy, False);
  110. }
  111. unsigned long
  112. getcolor(const char *colstr) {
  113. Colormap cmap = DefaultColormap(dpy, screen);
  114. XColor color;
  115. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  116. eprint("error, cannot allocate color '%s'\n", colstr);
  117. return color.pixel;
  118. }
  119. void
  120. setfont(const char *fontstr) {
  121. char *def, **missing;
  122. int i, n;
  123. missing = NULL;
  124. if(dc.font.set)
  125. XFreeFontSet(dpy, dc.font.set);
  126. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  127. if(missing) {
  128. while(n--)
  129. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  130. XFreeStringList(missing);
  131. }
  132. if(dc.font.set) {
  133. XFontSetExtents *font_extents;
  134. XFontStruct **xfonts;
  135. char **font_names;
  136. dc.font.ascent = dc.font.descent = 0;
  137. font_extents = XExtentsOfFontSet(dc.font.set);
  138. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  139. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  140. if(dc.font.ascent < (*xfonts)->ascent)
  141. dc.font.ascent = (*xfonts)->ascent;
  142. if(dc.font.descent < (*xfonts)->descent)
  143. dc.font.descent = (*xfonts)->descent;
  144. xfonts++;
  145. }
  146. }
  147. else {
  148. if(dc.font.xfont)
  149. XFreeFont(dpy, dc.font.xfont);
  150. dc.font.xfont = NULL;
  151. if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
  152. eprint("error, cannot load font: '%s'\n", fontstr);
  153. dc.font.ascent = dc.font.xfont->ascent;
  154. dc.font.descent = dc.font.xfont->descent;
  155. }
  156. dc.font.height = dc.font.ascent + dc.font.descent;
  157. }
  158. unsigned int
  159. textw(const char *text) {
  160. return textnw(text, strlen(text)) + dc.font.height;
  161. }