Configuration of dwm for Mac Computers
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

18 years ago
  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 void
  8. drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]) {
  9. int x;
  10. XGCValues gcv;
  11. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  12. gcv.foreground = col[ColFG];
  13. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  14. x = (dc.font.ascent + dc.font.descent + 2) / 4;
  15. r.x = dc.x + 1;
  16. r.y = dc.y + 1;
  17. if(filled) {
  18. r.width = r.height = x + 1;
  19. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  20. }
  21. else if(empty) {
  22. r.width = r.height = x;
  23. XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  24. }
  25. }
  26. static Bool
  27. isoccupied(unsigned int t) {
  28. Client *c;
  29. for(c = clients; c; c = c->next)
  30. if(c->tags[t])
  31. return True;
  32. return False;
  33. }
  34. static unsigned int
  35. textnw(const char *text, unsigned int len) {
  36. XRectangle r;
  37. if(dc.font.set) {
  38. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  39. return r.width;
  40. }
  41. return XTextWidth(dc.font.xfont, text, len);
  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. }