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.

135 lines
2.8 KiB

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