My dmenu build
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

draw.c 3.5 KiB

18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
18 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. XFreeStringList(missing);
  100. if(dc.font.set) {
  101. XFreeFontSet(dpy, dc.font.set);
  102. dc.font.set = NULL;
  103. }
  104. }
  105. if(dc.font.set) {
  106. XFontSetExtents *font_extents;
  107. XFontStruct **xfonts;
  108. char **font_names;
  109. dc.font.ascent = dc.font.descent = 0;
  110. font_extents = XExtentsOfFontSet(dc.font.set);
  111. n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
  112. for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
  113. if(dc.font.ascent < (*xfonts)->ascent)
  114. dc.font.ascent = (*xfonts)->ascent;
  115. if(dc.font.descent < (*xfonts)->descent)
  116. dc.font.descent = (*xfonts)->descent;
  117. xfonts++;
  118. }
  119. }
  120. else {
  121. if(dc.font.xfont)
  122. XFreeFont(dpy, dc.font.xfont);
  123. dc.font.xfont = NULL;
  124. dc.font.xfont = XLoadQueryFont(dpy, fontstr);
  125. if (!dc.font.xfont)
  126. dc.font.xfont = XLoadQueryFont(dpy, "fixed");
  127. if (!dc.font.xfont)
  128. eprint("error, cannot init 'fixed' font\n");
  129. dc.font.ascent = dc.font.xfont->ascent;
  130. dc.font.descent = dc.font.xfont->descent;
  131. }
  132. dc.font.height = dc.font.ascent + dc.font.descent;
  133. }
  134. unsigned int
  135. textw(const char *text)
  136. {
  137. return textnw(text, strlen(text)) + dc.font.height;
  138. }