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.

215 lines
4.5 KiB

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