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.

155 lines
3.3 KiB

  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <X11/Xlib.h>
  5. #include "draw.h"
  6. #include "util.h"
  7. Draw *
  8. draw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h) {
  9. Draw *draw = (Draw *)calloc(1, sizeof(Draw));
  10. draw->dpy = dpy;
  11. draw->screen = screen;
  12. draw->win = win;
  13. draw->w = w;
  14. draw->h = h;
  15. draw->drawable = XCreatePixmap(dpy, win, w, h, DefaultDepth(dpy, screen));
  16. draw->gc = XCreateGC(dpy, win, 0, NULL);
  17. XSetLineAttributes(dpy, draw->gc, 1, LineSolid, CapButt, JoinMiter);
  18. return draw;
  19. }
  20. void
  21. draw_resize(Draw *draw, unsigned int w, unsigned int h) {
  22. if(!draw)
  23. return;
  24. draw->w = w;
  25. draw->h = h;
  26. XFreePixmap(draw->dpy, draw->drawable);
  27. draw->drawable = XCreatePixmap(draw->dpy, draw->win, w, h, DefaultDepth(draw->dpy, draw->screen));
  28. }
  29. void
  30. draw_free(Draw *draw) {
  31. XFreePixmap(draw->dpy, draw->drawable);
  32. XFreeGC(draw->dpy, draw->gc);
  33. free(draw);
  34. }
  35. Fnt *
  36. draw_font_create(Draw *draw, const char *fontname) {
  37. Fnt *font;
  38. char *def, **missing;
  39. int n;
  40. if(!draw)
  41. return NULL;
  42. font = (Fnt *)calloc(1, sizeof(Fnt));
  43. font->set = XCreateFontSet(draw->dpy, fontname, &missing, &n, &def);
  44. if(missing) {
  45. while(n--)
  46. fprintf(stderr, "draw: missing fontset: %s\n", missing[n]);
  47. XFreeStringList(missing);
  48. }
  49. if(font->set) {
  50. XFontStruct **xfonts;
  51. char **font_names;
  52. XExtentsOfFontSet(font->set);
  53. n = XFontsOfFontSet(font->set, &xfonts, &font_names);
  54. while(n--) {
  55. font->ascent = MAX(font->ascent, (*xfonts)->ascent);
  56. font->descent = MAX(font->descent,(*xfonts)->descent);
  57. xfonts++;
  58. }
  59. }
  60. else {
  61. if(!(font->xfont = XLoadQueryFont(draw->dpy, fontname))
  62. && !(font->xfont = XLoadQueryFont(draw->dpy, "fixed")))
  63. die("error, cannot load font: '%s'\n", fontname);
  64. font->ascent = font->xfont->ascent;
  65. font->descent = font->xfont->descent;
  66. }
  67. font->h = font->ascent + font->descent;
  68. return font;
  69. }
  70. void
  71. draw_font_free(Draw *draw, Fnt *font) {
  72. if(!draw || !font)
  73. return;
  74. if(font->set)
  75. XFreeFontSet(draw->dpy, font->set);
  76. else
  77. XFreeFont(draw->dpy, font->xfont);
  78. free(font);
  79. }
  80. Col *
  81. draw_col_create(Draw *draw, const char *colname) {
  82. Col *col = (Col *)calloc(1, sizeof(Col));
  83. Colormap cmap = DefaultColormap(draw->dpy, draw->screen);
  84. XColor color;
  85. if(!XAllocNamedColor(draw->dpy, cmap, colname, &color, &color))
  86. die("error, cannot allocate color '%s'\n", colname);
  87. col->rgb = color.pixel;
  88. return col;
  89. }
  90. void
  91. draw_col_free(Draw *draw, Col *col) {
  92. if(!col)
  93. return;
  94. free(col);
  95. }
  96. void
  97. draw_setfont(Draw *draw, Fnt *font) {
  98. if(!draw || !font)
  99. return;
  100. draw->font = font;
  101. }
  102. void
  103. draw_setfg(Draw *draw, Col *col) {
  104. if(!draw || !col)
  105. return;
  106. draw->fg = col;
  107. }
  108. void
  109. draw_setbg(Draw *draw, Col *col) {
  110. if(!draw || !col)
  111. return;
  112. draw->bg = col;
  113. }
  114. void
  115. draw_rect(Draw *draw, int x, int y, unsigned int w, unsigned int h) {
  116. if(!draw)
  117. return;
  118. /* TODO: draw the rectangle */
  119. }
  120. void
  121. draw_text(Draw *draw, int x, int y, const char *text) {
  122. if(!draw)
  123. return;
  124. /* TODO: draw the text */
  125. }
  126. void
  127. draw_map(Draw *draw, int x, int y, unsigned int w, unsigned int h) {
  128. if(!draw)
  129. return;
  130. /* TODO: map the draw contents in the region */
  131. }
  132. void
  133. draw_getextents(Draw *draw, const char *text, TextExtents *extents) {
  134. if(!draw || !extents)
  135. return;
  136. /* TODO: get extents */
  137. }