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.

137 lines
3.0 KiB

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