draw.c 3.4 KB

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