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.

217 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
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
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
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. for(i = 0; i < ntags; i++) {
  86. dc.w = textw(tags[i]);
  87. if(seltag[i])
  88. drawtext(tags[i], dc.sel, sel && sel->tags[i]);
  89. else
  90. drawtext(tags[i], dc.norm, sel && sel->tags[i]);
  91. dc.x += dc.w;
  92. }
  93. dc.w = bmw;
  94. drawtext(arrange == dotile ? TILESYMBOL : FLOATSYMBOL, dc.status, False);
  95. x = dc.x + dc.w;
  96. dc.w = textw(stext);
  97. dc.x = bx + bw - dc.w;
  98. if(dc.x < x) {
  99. dc.x = x;
  100. dc.w = bw - x;
  101. }
  102. drawtext(stext, dc.status, False);
  103. if((dc.w = dc.x - x) > bh) {
  104. dc.x = x;
  105. if(sel)
  106. drawtext(sel->name, dc.sel, False);
  107. else
  108. drawtext(NULL, dc.norm, False);
  109. }
  110. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  111. XSync(dpy, False);
  112. }
  113. void
  114. drawtitle(Client *c)
  115. {
  116. if(c == sel && issel) {
  117. drawstatus();
  118. XUnmapWindow(dpy, c->twin);
  119. XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
  120. return;
  121. }
  122. XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
  123. XMapWindow(dpy, c->twin);
  124. dc.x = dc.y = 0;
  125. dc.w = c->tw;
  126. drawtext(c->name, dc.norm, False);
  127. XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
  128. XSync(dpy, False);
  129. }
  130. unsigned long
  131. getcolor(const char *colstr)
  132. {
  133. Colormap cmap = DefaultColormap(dpy, screen);
  134. XColor color;
  135. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  136. return color.pixel;
  137. }
  138. void
  139. setfont(const char *fontstr)
  140. {
  141. char **missing, *def;
  142. int i, n;
  143. missing = NULL;
  144. setlocale(LC_ALL, "");
  145. if(dc.font.set)
  146. XFreeFontSet(dpy, dc.font.set);
  147. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  148. if(missing) {
  149. while(n--)
  150. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  151. XFreeStringList(missing);
  152. if(dc.font.set) {
  153. XFreeFontSet(dpy, dc.font.set);
  154. dc.font.set = NULL;
  155. }
  156. }
  157. if(dc.font.set) {
  158. XFontSetExtents *font_extents;
  159. XFontStruct **xfonts;
  160. char **font_names;
  161. dc.font.ascent = dc.font.descent = 0;
  162. font_extents = XExtentsOfFontSet(dc.font.set);
  163. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  164. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  165. if(dc.font.ascent < (*xfonts)->ascent)
  166. dc.font.ascent = (*xfonts)->ascent;
  167. if(dc.font.descent < (*xfonts)->descent)
  168. dc.font.descent = (*xfonts)->descent;
  169. xfonts++;
  170. }
  171. }
  172. else {
  173. if(dc.font.xfont)
  174. XFreeFont(dpy, dc.font.xfont);
  175. dc.font.xfont = NULL;
  176. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  177. if (!dc.font.xfont)
  178. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  179. if (!dc.font.xfont)
  180. eprint("error, cannot init 'fixed' font\n");
  181. dc.font.ascent = dc.font.xfont->ascent;
  182. dc.font.descent = dc.font.xfont->descent;
  183. }
  184. dc.font.height = dc.font.ascent + dc.font.descent;
  185. }
  186. unsigned int
  187. textw(const char *text)
  188. {
  189. return textnw(text, strlen(text)) + dc.font.height;
  190. }