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.

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