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.

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