Configuration file for DWM on MacBook Air
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.

171 lines
3.8 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
  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 "draw.h"
  8. #include "util.h"
  9. static void
  10. drawborder(Display *dpy, Brush *b)
  11. {
  12. XPoint points[5];
  13. XSetLineAttributes(dpy, b->gc, 1, LineSolid, CapButt, JoinMiter);
  14. XSetForeground(dpy, b->gc, b->border);
  15. points[0].x = b->x;
  16. points[0].y = b->y;
  17. points[1].x = b->w - 1;
  18. points[1].y = 0;
  19. points[2].x = 0;
  20. points[2].y = b->h - 1;
  21. points[3].x = -(b->w - 1);
  22. points[3].y = 0;
  23. points[4].x = 0;
  24. points[4].y = -(b->h - 1);
  25. XDrawLines(dpy, b->drawable, b->gc, points, 5, CoordModePrevious);
  26. }
  27. void
  28. draw(Display *dpy, Brush *b, Bool border, const char *text)
  29. {
  30. unsigned int x, y, w, h, len;
  31. static char buf[256];
  32. XGCValues gcv;
  33. XRectangle r = { b->x, b->y, b->w, b->h };
  34. XSetForeground(dpy, b->gc, b->bg);
  35. XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
  36. if(border)
  37. drawborder(dpy, b);
  38. if(!text)
  39. return;
  40. len = strlen(text);
  41. if(len >= sizeof(buf))
  42. len = sizeof(buf) - 1;
  43. memcpy(buf, text, len);
  44. buf[len] = 0;
  45. h = b->font.ascent + b->font.descent;
  46. y = b->y + (b->h / 2) - (h / 2) + b->font.ascent;
  47. x = b->x + (h / 2);
  48. /* shorten text if necessary */
  49. while(len && (w = textnw(&b->font, buf, len)) > b->w - h)
  50. buf[--len] = 0;
  51. if(w > b->w)
  52. return; /* too long */
  53. gcv.foreground = b->fg;
  54. gcv.background = b->bg;
  55. if(b->font.set) {
  56. XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
  57. XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
  58. x, y, buf, len);
  59. }
  60. else {
  61. gcv.font = b->font.xfont->fid;
  62. XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
  63. XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
  64. }
  65. }
  66. static unsigned long
  67. xloadcolors(Display *dpy, Colormap cmap, const char *colstr)
  68. {
  69. XColor color;
  70. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  71. return color.pixel;
  72. }
  73. void
  74. loadcolors(Display *dpy, int screen, Brush *b,
  75. const char *bg, const char *fg, const char *border)
  76. {
  77. Colormap cmap = DefaultColormap(dpy, screen);
  78. b->bg = xloadcolors(dpy, cmap, bg);
  79. b->fg = xloadcolors(dpy, cmap, fg);
  80. b->border = xloadcolors(dpy, cmap, border);
  81. }
  82. unsigned int
  83. textnw(Fnt *font, char *text, unsigned int len)
  84. {
  85. XRectangle r;
  86. if(font->set) {
  87. XmbTextExtents(font->set, text, len, NULL, &r);
  88. return r.width;
  89. }
  90. return XTextWidth(font->xfont, text, len);
  91. }
  92. unsigned int
  93. textw(Fnt *font, char *text)
  94. {
  95. return textnw(font, text, strlen(text));
  96. }
  97. unsigned int
  98. texth(Fnt *font)
  99. {
  100. return font->height + 4;
  101. }
  102. void
  103. loadfont(Display *dpy, Fnt *font, const char *fontstr)
  104. {
  105. char **missing, *def;
  106. int n;
  107. missing = NULL;
  108. def = "?";
  109. setlocale(LC_ALL, "");
  110. if(font->set)
  111. XFreeFontSet(dpy, font->set);
  112. font->set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
  113. if(missing) {
  114. while(n--)
  115. fprintf(stderr, "missing fontset: %s\n", missing[n]);
  116. XFreeStringList(missing);
  117. if(font->set) {
  118. XFreeFontSet(dpy, font->set);
  119. font->set = NULL;
  120. }
  121. }
  122. if(font->set) {
  123. XFontSetExtents *font_extents;
  124. XFontStruct **xfonts;
  125. char **font_names;
  126. unsigned int i;
  127. font->ascent = font->descent = 0;
  128. font_extents = XExtentsOfFontSet(font->set);
  129. n = XFontsOfFontSet(font->set, &xfonts, &font_names);
  130. for(i = 0, font->ascent = 0, font->descent = 0; i < n; i++) {
  131. if(font->ascent < (*xfonts)->ascent)
  132. font->ascent = (*xfonts)->ascent;
  133. if(font->descent < (*xfonts)->descent)
  134. font->descent = (*xfonts)->descent;
  135. xfonts++;
  136. }
  137. }
  138. else {
  139. if(font->xfont)
  140. XFreeFont(dpy, font->xfont);
  141. font->xfont = NULL;
  142. font->xfont = XLoadQueryFont(dpy, fontstr);
  143. if (!font->xfont)
  144. font->xfont = XLoadQueryFont(dpy, "fixed");
  145. if (!font->xfont)
  146. error("error, cannot load 'fixed' font\n");
  147. font->ascent = font->xfont->ascent;
  148. font->descent = font->xfont->descent;
  149. }
  150. font->height = font->ascent + font->descent;
  151. }