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.

223 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
  1. /*
  2. * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <X11/Xlocale.h>
  8. #include "dwm.h"
  9. void
  10. draw_bar()
  11. {
  12. int i;
  13. dc.x = dc.y = 0;
  14. dc.w = bw;
  15. drawtext(NULL, False, False);
  16. if(arrange == floating) {
  17. dc.w = textw("~");
  18. drawtext("~", False, False);
  19. }
  20. else
  21. dc.w = 0;
  22. for(i = 0; i < TLast; i++) {
  23. dc.x += dc.w;
  24. dc.w = textw(tags[i]);
  25. drawtext(tags[i], i == tsel, True);
  26. }
  27. if(sel) {
  28. dc.x += dc.w;
  29. dc.w = textw(sel->name);
  30. drawtext(sel->name, True, True);
  31. }
  32. dc.w = textw(stext);
  33. dc.x = bx + bw - dc.w;
  34. drawtext(stext, False, False);
  35. XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
  36. XFlush(dpy);
  37. }
  38. void
  39. draw_client(Client *c)
  40. {
  41. int i;
  42. if(c == sel) {
  43. draw_bar();
  44. XUnmapWindow(dpy, c->title);
  45. XSetWindowBorder(dpy, c->win, dc.fg);
  46. return;
  47. }
  48. XSetWindowBorder(dpy, c->win, dc.bg);
  49. XMapWindow(dpy, c->title);
  50. dc.x = dc.y = 0;
  51. dc.w = 0;
  52. for(i = 0; i < TLast; i++) {
  53. if(c->tags[i]) {
  54. dc.x += dc.w;
  55. dc.w = textw(c->tags[i]);
  56. drawtext(c->tags[i], False, True);
  57. }
  58. }
  59. dc.x += dc.w;
  60. dc.w = textw(c->name);
  61. drawtext(c->name, False, True);
  62. XCopyArea(dpy, dc.drawable, c->title, dc.gc,
  63. 0, 0, c->tw, c->th, 0, 0);
  64. XFlush(dpy);
  65. }
  66. static void
  67. drawborder(void)
  68. {
  69. XPoint points[5];
  70. XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
  71. XSetForeground(dpy, dc.gc, dc.border);
  72. points[0].x = dc.x;
  73. points[0].y = dc.y;
  74. points[1].x = dc.w - 1;
  75. points[1].y = 0;
  76. points[2].x = 0;
  77. points[2].y = dc.h - 1;
  78. points[3].x = -(dc.w - 1);
  79. points[3].y = 0;
  80. points[4].x = 0;
  81. points[4].y = -(dc.h - 1);
  82. XDrawLines(dpy, dc.drawable, dc.gc, points, 5, CoordModePrevious);
  83. }
  84. void
  85. drawtext(const char *text, Bool invert, Bool border)
  86. {
  87. int x, y, w, h;
  88. unsigned int len;
  89. static char buf[256];
  90. XGCValues gcv;
  91. XRectangle r = { dc.x, dc.y, dc.w, dc.h };
  92. XSetForeground(dpy, dc.gc, invert ? dc.fg : dc.bg);
  93. XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
  94. w = 0;
  95. if(border)
  96. drawborder();
  97. if(!text)
  98. return;
  99. len = strlen(text);
  100. if(len >= sizeof(buf))
  101. len = sizeof(buf) - 1;
  102. memcpy(buf, text, len);
  103. buf[len] = 0;
  104. h = dc.font.ascent + dc.font.descent;
  105. y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
  106. x = dc.x + (h / 2);
  107. /* shorten text if necessary */
  108. while(len && (w = textnw(buf, len)) > dc.w - h)
  109. buf[--len] = 0;
  110. if(w > dc.w)
  111. return; /* too long */
  112. gcv.foreground = invert ? dc.bg : dc.fg;
  113. gcv.background = invert ? dc.fg : dc.bg;
  114. if(dc.font.set) {
  115. XChangeGC(dpy, dc.gc, GCForeground | GCBackground, &gcv);
  116. XmbDrawImageString(dpy, dc.drawable, dc.font.set, dc.gc,
  117. x, y, buf, len);
  118. }
  119. else {
  120. gcv.font = dc.font.xfont->fid;
  121. XChangeGC(dpy, dc.gc, GCForeground | GCBackground | GCFont, &gcv);
  122. XDrawImageString(dpy, dc.drawable, dc.gc, x, y, buf, len);
  123. }
  124. }
  125. unsigned long
  126. initcolor(const char *colstr)
  127. {
  128. XColor color;
  129. Colormap cmap = DefaultColormap(dpy, screen);
  130. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  131. return color.pixel;
  132. }
  133. unsigned int
  134. textnw(char *text, unsigned int len)
  135. {
  136. XRectangle r;
  137. if(dc.font.set) {
  138. XmbTextExtents(dc.font.set, text, len, NULL, &r);
  139. return r.width;
  140. }
  141. return XTextWidth(dc.font.xfont, text, len);
  142. }
  143. unsigned int
  144. textw(char *text)
  145. {
  146. return textnw(text, strlen(text)) + dc.font.height;
  147. }
  148. void
  149. initfont(const char *fontstr)
  150. {
  151. char **missing, *def;
  152. int i, n;
  153. missing = NULL;
  154. setlocale(LC_ALL, "");
  155. if(dc.font.set)
  156. XFreeFontSet(dpy, dc.font.set);
  157. dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  158. if(missing) {
  159. while(n--)
  160. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  161. XFreeStringList(missing);
  162. if(dc.font.set) {
  163. XFreeFontSet(dpy, dc.font.set);
  164. dc.font.set = NULL;
  165. }
  166. }
  167. if(dc.font.set) {
  168. XFontSetExtents *font_extents;
  169. XFontStruct **xfonts;
  170. char **font_names;
  171. dc.font.ascent = dc.font.descent = 0;
  172. font_extents = XExtentsOfFontSet(dc.font.set);
  173. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  174. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  175. if(dc.font.ascent < (*xfonts)->ascent)
  176. dc.font.ascent = (*xfonts)->ascent;
  177. if(dc.font.descent < (*xfonts)->descent)
  178. dc.font.descent = (*xfonts)->descent;
  179. xfonts++;
  180. }
  181. }
  182. else {
  183. if(dc.font.xfont)
  184. XFreeFont(dpy, dc.font.xfont);
  185. dc.font.xfont = NULL;
  186. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  187. if (!dc.font.xfont)
  188. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  189. if (!dc.font.xfont)
  190. error("error, cannot init 'fixed' font\n");
  191. dc.font.ascent = dc.font.xfont->ascent;
  192. dc.font.descent = dc.font.xfont->descent;
  193. }
  194. dc.font.height = dc.font.ascent + dc.font.descent;
  195. }