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.

134 lines
2.8 KiB

18 years ago
  1. /* See LICENSE file for copyright and license details. */
  2. #include "dwm.h"
  3. #include <string.h>
  4. /* static */
  5. static void
  6. drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) {
  7. int x;
  8. XGCValues gcv;
  9. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  10. gcv.foreground = col[ColFG];
  11. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  12. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  13. r.x = dc.x + 1;
  14. r.y = dc.y + 1;
  15. if(filled) {
  16. r.width = r.height = x + 1;
  17. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  18. }
  19. else if(empty) {
  20. r.width = r.height = x;
  21. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  22. }
  23. }
  24. static Bool
  25. isoccupied(unsigned int t) {
  26. Client *c;
  27. for(c = clients; c; c = c->next)
  28. if(c->tags[t])
  29. return True;
  30. return False;
  31. }
  32. static unsigned int
  33. textnw(const char *text, unsigned int len) {
  34. XRectangle r;
  35. if(dc.font.set) {
  36. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  37. return r.width;
  38. }
  39. return XTextWidth(dc.font.xfont, text, len);
  40. }
  41. /* extern */
  42. void
  43. drawbar(void) {
  44. int i, x;
  45. dc.x = dc.y = 0;
  46. for(i = 0; i < ntags; i++) {
  47. dc.w = textw(tags[i]);
  48. if(seltags[i]) {
  49. drawtext(tags[i], dc.sel);
  50. drawsquare(sel && sel->tags[i], isoccupied(i), dc.sel);
  51. }
  52. else {
  53. drawtext(tags[i], dc.norm);
  54. drawsquare(sel && sel->tags[i], isoccupied(i), dc.norm);
  55. }
  56. dc.x += dc.w;
  57. }
  58. dc.w = blw;
  59. drawtext(getsymbol(), dc.norm);
  60. x = dc.x + dc.w;
  61. dc.w = textw(stext);
  62. dc.x = sw - dc.w;
  63. if(dc.x < x) {
  64. dc.x = x;
  65. dc.w = sw - x;
  66. }
  67. drawtext(stext, dc.norm);
  68. if((dc.w = dc.x - x) > bh) {
  69. dc.x = x;
  70. if(sel) {
  71. drawtext(sel->name, dc.sel);
  72. drawsquare(sel->ismax, sel->isfloating, dc.sel);
  73. }
  74. else
  75. drawtext(NULL, dc.norm);
  76. }
  77. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
  78. XSync(dpy, False);
  79. }
  80. void
  81. drawtext(const char *text, unsigned long col[ColLast]) {
  82. int x, y, w, h;
  83. static char buf[256];
  84. unsigned int len, olen;
  85. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  86. XSetForeground(dpy, dc.gc, col[ColBG]);
  87. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  88. if(!text)
  89. return;
  90. w = 0;
  91. olen = len = strlen(text);
  92. if(len >= sizeof buf)
  93. len = sizeof buf - 1;
  94. memcpy(buf, text, len);
  95. buf[len] = 0;
  96. h = dc.font.ascent + dc.font.descent;
  97. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  98. x = dc.x + (h / 2);
  99. /* shorten text if necessary */
  100. while(len && (w = textnw(buf, len)) > dc.w - h)
  101. buf[--len] = 0;
  102. if(len < olen) {
  103. if(len > 1)
  104. buf[len - 1] = '.';
  105. if(len > 2)
  106. buf[len - 2] = '.';
  107. if(len > 3)
  108. buf[len - 3] = '.';
  109. }
  110. if(w > dc.w)
  111. return; /* too long */
  112. XSetForeground(dpy, dc.gc, col[ColFG]);
  113. if(dc.font.set)
  114. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  115. else
  116. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  117. }
  118. unsigned int
  119. textw(const char *text) {
  120. return textnw(text, strlen(text)) + dc.font.height;
  121. }