Configuration of dwm for Mac Computers
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.

163 lines
3.8 KiB

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