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.

209 lines
4.5 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
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 = 3;
  66. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  67. }
  68. }
  69. /* extern */
  70. void
  71. drawall() {
  72. Client *c;
  73. for(c = clients; c; c = getnext(c->next))
  74. drawtitle(c);
  75. drawstatus();
  76. }
  77. void
  78. drawstatus() {
  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 == dotile ? TILESYMBOL : FLOATSYMBOL, 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. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  130. return color.pixel;
  131. }
  132. void
  133. setfont(const char *fontstr) {
  134. char **missing, *def;
  135. int i, n;
  136. missing = NULL;
  137. setlocale(LC_ALL, "");
  138. if(dc.font.set)
  139. XFreeFontSet(dpy, dc.font.set);
  140. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  141. if(missing) {
  142. while(n--)
  143. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  144. XFreeStringList(missing);
  145. if(dc.font.set) {
  146. XFreeFontSet(dpy, dc.font.set);
  147. dc.font.set = NULL;
  148. }
  149. }
  150. if(dc.font.set) {
  151. XFontSetExtents *font_extents;
  152. XFontStruct **xfonts;
  153. char **font_names;
  154. dc.font.ascent = dc.font.descent = 0;
  155. font_extents = XExtentsOfFontSet(dc.font.set);
  156. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  157. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  158. if(dc.font.ascent < (*xfonts)->ascent)
  159. dc.font.ascent = (*xfonts)->ascent;
  160. if(dc.font.descent < (*xfonts)->descent)
  161. dc.font.descent = (*xfonts)->descent;
  162. xfonts++;
  163. }
  164. }
  165. else {
  166. if(dc.font.xfont)
  167. XFreeFont(dpy, dc.font.xfont);
  168. dc.font.xfont = NULL;
  169. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  170. if (!dc.font.xfont)
  171. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  172. if (!dc.font.xfont)
  173. eprint("error, cannot init 'fixed' font\n");
  174. dc.font.ascent = dc.font.xfont->ascent;
  175. dc.font.descent = dc.font.xfont->descent;
  176. }
  177. dc.font.height = dc.font.ascent + dc.font.descent;
  178. }
  179. unsigned int
  180. textw(const char *text) {
  181. return textnw(text, strlen(text)) + dc.font.height;
  182. }