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.

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