My dmenu build
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.

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