dmenu for lunch applications in dwm
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.

191 lines
4.5 KiB

13 years ago
13 years ago
  1. /* See LICENSE file for copyright and license details. */
  2. #include <locale.h>
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <X11/Xlib.h>
  8. #include "draw.h"
  9. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  10. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  11. #define DEFFONT "fixed"
  12. static Bool loadfont(DC *dc, const char *fontstr);
  13. void
  14. drawrect(DC *dc, int x, int y, unsigned int w, unsigned int h, Bool fill, unsigned long color) {
  15. XRectangle r = { dc->x + x, dc->y + y, w, h };
  16. if(!fill) {
  17. r.width -= 1;
  18. r.height -= 1;
  19. }
  20. XSetForeground(dc->dpy, dc->gc, color);
  21. (fill ? XFillRectangles : XDrawRectangles)(dc->dpy, dc->canvas, dc->gc, &r, 1);
  22. }
  23. void
  24. drawtext(DC *dc, const char *text, unsigned long col[ColLast]) {
  25. char buf[256];
  26. size_t n, mn;
  27. /* shorten text if necessary */
  28. n = strlen(text);
  29. for(mn = MIN(n, sizeof buf); textnw(dc, text, mn) > dc->w - dc->font.height/2; mn--)
  30. if(mn == 0)
  31. return;
  32. memcpy(buf, text, mn);
  33. if(mn < n)
  34. for(n = MAX(mn-3, 0); n < mn; buf[n++] = '.');
  35. drawrect(dc, 0, 0, dc->w, dc->h, True, BG(dc, col));
  36. drawtextn(dc, buf, mn, col);
  37. }
  38. void
  39. drawtextn(DC *dc, const char *text, size_t n, unsigned long col[ColLast]) {
  40. int x, y;
  41. x = dc->x + dc->font.height/2;
  42. y = dc->y + dc->font.ascent+1;
  43. XSetForeground(dc->dpy, dc->gc, FG(dc, col));
  44. if(dc->font.set)
  45. XmbDrawString(dc->dpy, dc->canvas, dc->font.set, dc->gc, x, y, text, n);
  46. else {
  47. XSetFont(dc->dpy, dc->gc, dc->font.xfont->fid);
  48. XDrawString(dc->dpy, dc->canvas, dc->gc, x, y, text, n);
  49. }
  50. }
  51. void
  52. eprintf(const char *fmt, ...) {
  53. va_list ap;
  54. fprintf(stderr, "%s: ", progname);
  55. va_start(ap, fmt);
  56. vfprintf(stderr, fmt, ap);
  57. va_end(ap);
  58. exit(EXIT_FAILURE);
  59. }
  60. void
  61. freedc(DC *dc) {
  62. if(dc->font.set)
  63. XFreeFontSet(dc->dpy, dc->font.set);
  64. if(dc->font.xfont)
  65. XFreeFont(dc->dpy, dc->font.xfont);
  66. if(dc->canvas)
  67. XFreePixmap(dc->dpy, dc->canvas);
  68. XFreeGC(dc->dpy, dc->gc);
  69. XCloseDisplay(dc->dpy);
  70. free(dc);
  71. }
  72. unsigned long
  73. getcolor(DC *dc, const char *colstr) {
  74. Colormap cmap = DefaultColormap(dc->dpy, DefaultScreen(dc->dpy));
  75. XColor color;
  76. if(!XAllocNamedColor(dc->dpy, cmap, colstr, &color, &color))
  77. eprintf("cannot allocate color '%s'\n", colstr);
  78. return color.pixel;
  79. }
  80. DC *
  81. initdc(void) {
  82. DC *dc;
  83. if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
  84. weprintf("no locale support\n");
  85. if(!(dc = calloc(1, sizeof *dc)))
  86. eprintf("cannot malloc %u bytes\n", sizeof *dc);
  87. if(!(dc->dpy = XOpenDisplay(NULL)))
  88. eprintf("cannot open display\n");
  89. dc->gc = XCreateGC(dc->dpy, DefaultRootWindow(dc->dpy), 0, NULL);
  90. XSetLineAttributes(dc->dpy, dc->gc, 1, LineSolid, CapButt, JoinMiter);
  91. return dc;
  92. }
  93. void
  94. initfont(DC *dc, const char *fontstr) {
  95. if(!loadfont(dc, fontstr ? fontstr : DEFFONT)) {
  96. if(fontstr != NULL)
  97. weprintf("cannot load font '%s'\n", fontstr);
  98. if(fontstr == NULL || !loadfont(dc, DEFFONT))
  99. eprintf("cannot load font '%s'\n", DEFFONT);
  100. }
  101. dc->font.height = dc->font.ascent + dc->font.descent;
  102. }
  103. Bool
  104. loadfont(DC *dc, const char *fontstr) {
  105. char *def, **missing;
  106. int i, n;
  107. if(!*fontstr)
  108. return False;
  109. if((dc->font.set = XCreateFontSet(dc->dpy, fontstr, &missing, &n, &def))) {
  110. char **names;
  111. XFontStruct **xfonts;
  112. n = XFontsOfFontSet(dc->font.set, &xfonts, &names);
  113. for(i = dc->font.ascent = dc->font.descent = 0; i < n; i++) {
  114. dc->font.ascent = MAX(dc->font.ascent, xfonts[i]->ascent);
  115. dc->font.descent = MAX(dc->font.descent, xfonts[i]->descent);
  116. }
  117. }
  118. else if((dc->font.xfont = XLoadQueryFont(dc->dpy, fontstr))) {
  119. dc->font.ascent = dc->font.xfont->ascent;
  120. dc->font.descent = dc->font.xfont->descent;
  121. }
  122. if(missing)
  123. XFreeStringList(missing);
  124. return (dc->font.set || dc->font.xfont);
  125. }
  126. void
  127. mapdc(DC *dc, Window win, unsigned int w, unsigned int h) {
  128. XCopyArea(dc->dpy, dc->canvas, win, dc->gc, 0, 0, w, h, 0, 0);
  129. }
  130. void
  131. resizedc(DC *dc, unsigned int w, unsigned int h) {
  132. if(dc->canvas)
  133. XFreePixmap(dc->dpy, dc->canvas);
  134. dc->canvas = XCreatePixmap(dc->dpy, DefaultRootWindow(dc->dpy), w, h,
  135. DefaultDepth(dc->dpy, DefaultScreen(dc->dpy)));
  136. dc->x = dc->y = 0;
  137. dc->w = w;
  138. dc->h = h;
  139. dc->invert = False;
  140. }
  141. int
  142. textnw(DC *dc, const char *text, size_t len) {
  143. if(dc->font.set) {
  144. XRectangle r;
  145. XmbTextExtents(dc->font.set, text, len, NULL, &r);
  146. return r.width;
  147. }
  148. return XTextWidth(dc->font.xfont, text, len);
  149. }
  150. int
  151. textw(DC *dc, const char *text) {
  152. return textnw(dc, text, strlen(text)) + dc->font.height;
  153. }
  154. void
  155. weprintf(const char *fmt, ...) {
  156. va_list ap;
  157. fprintf(stderr, "%s: ", progname);
  158. va_start(ap, fmt);
  159. vfprintf(stderr, fmt, ap);
  160. va_end(ap);
  161. }