Configuration file for DWM on MacBook Air
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.

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