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.

207 lines
5.0 KiB

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