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.

210 lines
4.6 KiB

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