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.

172 lines
3.7 KiB

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
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
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
19 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(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(Brush *b, Bool border, const char *text)
  29. {
  30. int x, y, w, h;
  31. unsigned int len;
  32. static char buf[256];
  33. XGCValues gcv;
  34. XRectangle r = { b->x, b->y, b->w, b->h };
  35. XSetForeground(dpy, b->gc, b->bg);
  36. XFillRectangles(dpy, b->drawable, b->gc, &r, 1);
  37. w = 0;
  38. if(border)
  39. drawborder(b);
  40. if(!text)
  41. return;
  42. len = strlen(text);
  43. if(len >= sizeof(buf))
  44. len = sizeof(buf) - 1;
  45. memcpy(buf, text, len);
  46. buf[len] = 0;
  47. h = b->font.ascent + b->font.descent;
  48. y = b->y + (b->h / 2) - (h / 2) + b->font.ascent;
  49. x = b->x + (h / 2);
  50. /* shorten text if necessary */
  51. while(len && (w = textnw(&b->font, buf, len)) > b->w - h)
  52. buf[--len] = 0;
  53. if(w > b->w)
  54. return; /* too long */
  55. gcv.foreground = b->fg;
  56. gcv.background = b->bg;
  57. if(b->font.set) {
  58. XChangeGC(dpy, b->gc, GCForeground | GCBackground, &gcv);
  59. XmbDrawImageString(dpy, b->drawable, b->font.set, b->gc,
  60. x, y, buf, len);
  61. }
  62. else {
  63. gcv.font = b->font.xfont->fid;
  64. XChangeGC(dpy, b->gc, GCForeground | GCBackground | GCFont, &gcv);
  65. XDrawImageString(dpy, b->drawable, b->gc, x, y, buf, len);
  66. }
  67. }
  68. static unsigned long
  69. xloadcolors(Colormap cmap, const char *colstr)
  70. {
  71. XColor color;
  72. XAllocNamedColor(dpy, cmap, colstr, &color, &color);
  73. return color.pixel;
  74. }
  75. void
  76. loadcolors(int scr, Brush *b,
  77. const char *bg, const char *fg, const char *border)
  78. {
  79. Colormap cmap = DefaultColormap(dpy, scr);
  80. b->bg = xloadcolors(cmap, bg);
  81. b->fg = xloadcolors(cmap, fg);
  82. b->border = xloadcolors(cmap, border);
  83. }
  84. unsigned int
  85. textnw(Fnt *font, char *text, unsigned int len)
  86. {
  87. XRectangle r;
  88. if(font->set) {
  89. XmbTextExtents(font->set, text, len, NULL, &r);
  90. return r.width;
  91. }
  92. return XTextWidth(font->xfont, text, len);
  93. }
  94. unsigned int
  95. textw(Fnt *font, char *text)
  96. {
  97. return textnw(font, text, strlen(text));
  98. }
  99. unsigned int
  100. texth(Fnt *font)
  101. {
  102. return font->height + 4;
  103. }
  104. void
  105. loadfont(Fnt *font, const char *fontstr)
  106. {
  107. char **missing, *def;
  108. int i, n;
  109. missing = NULL;
  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. 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. }