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
2.9 KiB

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