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.

161 lines
3.4 KiB

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