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.

198 lines
4.5 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
  1. /* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dwm.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <X11/Xlocale.h>
  8. /* static */
  9. static unsigned int
  10. textnw(const char *text, unsigned int len) {
  11. XRectangle r;
  12. if(dc.font.set) {
  13. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  14. return r.width;
  15. }
  16. return XTextWidth(dc.font.xfont, text, len);
  17. }
  18. static void
  19. drawtext(const char *text, unsigned long col[ColLast], Bool highlight) {
  20. int x, y, w, h;
  21. static char buf[256];
  22. unsigned int len, olen;
  23. XGCValues gcv;
  24. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  25. XSetForeground(dpy, dc.gc, col[ColBG]);
  26. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  27. if(!text)
  28. return;
  29. w = 0;
  30. olen = len = strlen(text);
  31. if(len >= sizeof(buf))
  32. len = sizeof(buf) - 1;
  33. memcpy(buf, text, len);
  34. buf[len] = 0;
  35. h = dc.font.ascent + dc.font.descent;
  36. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  37. x = dc.x + (h / 2);
  38. /* shorten text if necessary */
  39. while(len && (w = textnw(buf, len)) > dc.w - h)
  40. buf[--len] = 0;
  41. if(len < olen) {
  42. if(len > 1)
  43. buf[len - 1] = '.';
  44. if(len > 2)
  45. buf[len - 2] = '.';
  46. if(len > 3)
  47. buf[len - 3] = '.';
  48. }
  49. if(w > dc.w)
  50. return; /* too long */
  51. gcv.foreground = col[ColFG];
  52. if(dc.font.set) {
  53. XChangeGC(dpy, dc.gc, GCForeground, &gcv);
  54. XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
  55. }
  56. else {
  57. gcv.font = dc.font.xfont->fid;
  58. XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
  59. XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  60. }
  61. if(highlight) {
  62. r.x = dc.x + 2;
  63. r.y = dc.y + 2;
  64. r.width = r.height = (h + 2) / 4;
  65. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  66. }
  67. }
  68. /* extern */
  69. void
  70. drawall(void) {
  71. Client *c;
  72. for(c = clients; c; c = getnext(c->next))
  73. drawtitle(c);
  74. drawstatus();
  75. }
  76. void
  77. drawstatus(void) {
  78. int i, x;
  79. dc.x = dc.y = 0;
  80. for(i = 0; i < ntags; i++) {
  81. dc.w = textw(tags[i]);
  82. if(seltag[i])
  83. drawtext(tags[i], dc.sel, sel && sel->tags[i]);
  84. else
  85. drawtext(tags[i], dc.norm, sel && sel->tags[i]);
  86. dc.x += dc.w;
  87. }
  88. dc.w = bmw;
  89. drawtext(arrange == dofloat ? FLOATSYMBOL : TILESYMBOL, dc.status, False);
  90. x = dc.x + dc.w;
  91. dc.w = textw(stext);
  92. dc.x = bx + bw - dc.w;
  93. if(dc.x < x) {
  94. dc.x = x;
  95. dc.w = bw - x;
  96. }
  97. drawtext(stext, dc.status, False);
  98. if((dc.w = dc.x - x) > bh) {
  99. dc.x = x;
  100. if(sel)
  101. drawtext(sel->name, dc.sel, False);
  102. else
  103. drawtext(NULL, dc.norm, False);
  104. }
  105. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  106. XSync(dpy, False);
  107. }
  108. void
  109. drawtitle(Client *c) {
  110. if(c == sel && issel) {
  111. drawstatus();
  112. XUnmapWindow(dpy, c->twin);
  113. XSetWindowBorder(dpy, c->win, dc.sel[ColBG]);
  114. return;
  115. }
  116. XSetWindowBorder(dpy, c->win, dc.norm[ColBG]);
  117. XMapWindow(dpy, c->twin);
  118. dc.x = dc.y = 0;
  119. dc.w = c->tw;
  120. drawtext(c->name, dc.norm, False);
  121. XCopyArea(dpy, dc.drawable, c->twin, dc.gc, 0, 0, c->tw, c->th, 0, 0);
  122. XSync(dpy, False);
  123. }
  124. unsigned long
  125. getcolor(const char *colstr) {
  126. Colormap cmap = DefaultColormap(dpy, screen);
  127. XColor color;
  128. if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
  129. eprint("error, cannot allocate color '%s'\n", colstr);
  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. }