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.

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