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.

232 lines
5.0 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dwm.h"
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <X11/Xlocale.h>
  9. /* static */
  10. static unsigned int
  11. textnw(const char *text, unsigned int len)
  12. {
  13. XRectangle r;
  14. if(dc.font.set) {
  15. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  16. return r.width;
  17. }
  18. return XTextWidth(dc.font.xfont, text, len);
  19. }
  20. static void
  21. drawtext(const char *text, Bool invert, Bool underline)
  22. {
  23. int x, y, w, h;
  24. static char buf[256];
  25. unsigned int len, olen;
  26. XGCValues gcv;
  27. XPoint points[5];
  28. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  29. XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
  30. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  31. points[0].x = dc.x;
  32. points[0].y = dc.y;
  33. points[1].x = dc.w - 1;
  34. points[1].y = 0;
  35. points[2].x = 0;
  36. points[2].y = dc.h - 1;
  37. points[3].x = -(dc.w - 1);
  38. points[3].y = 0;
  39. points[4].x = 0;
  40. points[4].y = -(dc.h - 1);
  41. XSetForeground(dpy, dc.gc, dc.border);
  42. XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
  43. if(!text)
  44. return;
  45. w = 0;
  46. olen = len = strlen(text);
  47. if(len >= sizeof(buf))
  48. len = sizeof(buf) - 1;
  49. memcpy(buf, text, len);
  50. buf[len] = 0;
  51. h = dc.font.ascent + dc.font.descent;
  52. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  53. x = dc.x + (h / 2);
  54. /* shorten text if necessary */
  55. while(len && (w = textnw(buf, len)) > dc.w - h)
  56. buf[--len] = 0;
  57. if(len < olen) {
  58. if(len > 1)
  59. buf[len - 1] = '.';
  60. if(len > 2)
  61. buf[len - 2] = '.';
  62. if(len > 3)
  63. buf[len - 3] = '.';
  64. }
  65. if(w > dc.w)
  66. return; /* too long */
  67. gcv.foreground = invert ? dc.bg : dc.fg;
  68. gcv.background = invert ? dc.fg : dc.bg;
  69. if(dc.font.set) {
  70. XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
  71. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  72. }
  73. else {
  74. gcv.font = dc.font.xfont->fid;
  75. XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
  76. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  77. }
  78. if(underline) {
  79. points[0].x = dc.x + (h / 2) - 1;
  80. points[0].y = dc.y + dc.h - 3;
  81. points[1].x = dc.w - h + 2;
  82. points[1].y = 0;
  83. XDrawLines(dpy, dc.drawable, dc.gc, points, 2, CoordModePrevious);
  84. }
  85. }
  86. /* extern */
  87. void
  88. drawall()
  89. {
  90. Client *c;
  91. for(c = clients; c; c = getnext(c->next))
  92. drawtitle(c);
  93. drawstatus();
  94. }
  95. void
  96. drawstatus()
  97. {
  98. int i, x;
  99. Bool istile = arrange == dotile;
  100. dc.x = dc.y = 0;
  101. dc.w = bw;
  102. drawtext(NULL, !istile, False);
  103. dc.w = 0;
  104. for(i = 0; i < ntags; i++) {
  105. dc.x += dc.w;
  106. dc.w = textw(tags[i]);
  107. if(istile)
  108. drawtext(tags[i], seltag[i], sel && sel->tags[i]);
  109. else
  110. drawtext(tags[i], !seltag[i], sel && sel->tags[i]);
  111. }
  112. x = dc.x + dc.w;
  113. dc.w = textw(stext);
  114. dc.x = bx + bw - dc.w;
  115. if(dc.x < x) {
  116. dc.x = x;
  117. dc.w = bw - x;
  118. }
  119. drawtext(stext, !istile, False);
  120. if(sel && ((dc.w = dc.x - x) > bh)) {
  121. dc.x = x;
  122. drawtext(sel->name, istile, False);
  123. }
  124. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  125. XSync(dpy, False);
  126. }
  127. void
  128. drawtitle(Client *c)
  129. {
  130. int i;
  131. Bool istile = arrange == dotile;
  132. if(c == sel && issel) {
  133. drawstatus();
  134. XUnmapWindow(dpy, c->twin);
  135. XSetWindowBorder(dpy, c->win, dc.fg);
  136. return;
  137. }
  138. XSetWindowBorder(dpy, c->win, dc.bg);
  139. XMapWindow(dpy, c->twin);
  140. dc.x = dc.y = 0;
  141. dc.w = c->tw;
  142. drawtext(c->name, !istile, False);
  143. XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
  144. XSync(dpy, False);
  145. }
  146. unsigned long
  147. getcolor(const char *colstr)
  148. {
  149. Colormap cmap = DefaultColormap(dpy, screen);
  150. XColor color;
  151. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  152. return color.pixel;
  153. }
  154. void
  155. setfont(const char *fontstr)
  156. {
  157. char **missing, *def;
  158. int i, n;
  159. missing = NULL;
  160. setlocale(LC_ALL, "");
  161. if(dc.font.set)
  162. XFreeFontSet(dpy, dc.font.set);
  163. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  164. if(missing) {
  165. while(n--)
  166. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  167. XFreeStringList(missing);
  168. if(dc.font.set) {
  169. XFreeFontSet(dpy, dc.font.set);
  170. dc.font.set = NULL;
  171. }
  172. }
  173. if(dc.font.set) {
  174. XFontSetExtents *font_extents;
  175. XFontStruct **xfonts;
  176. char **font_names;
  177. dc.font.ascent = dc.font.descent = 0;
  178. font_extents = XExtentsOfFontSet(dc.font.set);
  179. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  180. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  181. if(dc.font.ascent < (*xfonts)->ascent)
  182. dc.font.ascent = (*xfonts)->ascent;
  183. if(dc.font.descent < (*xfonts)->descent)
  184. dc.font.descent = (*xfonts)->descent;
  185. xfonts++;
  186. }
  187. }
  188. else {
  189. if(dc.font.xfont)
  190. XFreeFont(dpy, dc.font.xfont);
  191. dc.font.xfont = NULL;
  192. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  193. if (!dc.font.xfont)
  194. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  195. if (!dc.font.xfont)
  196. eprint("error, cannot init 'fixed' font\n");
  197. dc.font.ascent = dc.font.xfont->ascent;
  198. dc.font.descent = dc.font.xfont->descent;
  199. }
  200. dc.font.height = dc.font.ascent + dc.font.descent;
  201. }
  202. unsigned int
  203. textw(const char *text)
  204. {
  205. return textnw(text, strlen(text)) + dc.font.height;
  206. }