PageRenderTime 68ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/eterm/Eterm/src/draw.c

https://github.com/kakaroto/e17
C | 238 lines | 185 code | 26 blank | 27 comment | 41 complexity | 01be7b921641f78421993cf3920b3660 MD5 | raw file
  1. /*
  2. * Copyright (C) 1997-2009, Michael Jennings
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies of the Software, its documentation and marketing & publicity
  13. * materials, and acknowledgment shall be given in the documentation, materials
  14. * and software packages that this Software was used.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. static const char cvs_ident[] = "$Id$";
  24. #include "config.h"
  25. #include "feature.h"
  26. #include "draw.h"
  27. #include "misc.h"
  28. #include "pixmap.h"
  29. #include "startup.h"
  30. void
  31. draw_shadow(Drawable d, GC gc_top, GC gc_bottom, int x, int y, int w, int h, int shadow)
  32. {
  33. ASSERT(w != 0);
  34. ASSERT(h != 0);
  35. LOWER_BOUND(shadow, 1);
  36. for (w += x - 1, h += y - 1; shadow > 0; shadow--, w--, h--) {
  37. XDrawLine(Xdisplay, d, gc_top, x, y, w, y);
  38. XDrawLine(Xdisplay, d, gc_top, x, y, x, h);
  39. x++;
  40. y++;
  41. XDrawLine(Xdisplay, d, gc_bottom, w, h, w, y);
  42. XDrawLine(Xdisplay, d, gc_bottom, w, h, x, h);
  43. }
  44. }
  45. void
  46. draw_shadow_from_colors(Drawable d, Pixel top, Pixel bottom, int x, int y, int w, int h, int shadow)
  47. {
  48. static GC gc_top = (GC) 0, gc_bottom = (GC) 0;
  49. if (gc_top == 0) {
  50. gc_top = LIBAST_X_CREATE_GC(0, NULL);
  51. gc_bottom = LIBAST_X_CREATE_GC(0, NULL);
  52. }
  53. XSetForeground(Xdisplay, gc_top, top);
  54. XSetForeground(Xdisplay, gc_bottom, bottom);
  55. draw_shadow(d, gc_top, gc_bottom, x, y, w, h, shadow);
  56. }
  57. void
  58. draw_arrow(Drawable d, GC gc_top, GC gc_bottom, int x, int y, int w, int shadow, unsigned char type)
  59. {
  60. BOUND(shadow, 1, 2);
  61. switch (type) {
  62. case DRAW_ARROW_UP:
  63. for (; shadow > 0; shadow--, x++, y++, w--) {
  64. XDrawLine(Xdisplay, d, gc_top, x, y + w, x + w / 2, y);
  65. XDrawLine(Xdisplay, d, gc_bottom, x + w, y + w, x + w / 2, y);
  66. XDrawLine(Xdisplay, d, gc_bottom, x + w, y + w, x, y + w);
  67. }
  68. break;
  69. case DRAW_ARROW_DOWN:
  70. for (; shadow > 0; shadow--, x++, y++, w--) {
  71. XDrawLine(Xdisplay, d, gc_top, x, y, x + w / 2, y + w);
  72. XDrawLine(Xdisplay, d, gc_top, x, y, x + w, y);
  73. XDrawLine(Xdisplay, d, gc_bottom, x + w, y, x + w / 2, y + w);
  74. }
  75. break;
  76. case DRAW_ARROW_LEFT:
  77. for (; shadow > 0; shadow--, x++, y++, w--) {
  78. XDrawLine(Xdisplay, d, gc_bottom, x + w, y + w, x + w, y);
  79. XDrawLine(Xdisplay, d, gc_bottom, x + w, y + w, x, y + w / 2);
  80. XDrawLine(Xdisplay, d, gc_top, x, y + w / 2, x + w, y);
  81. }
  82. break;
  83. case DRAW_ARROW_RIGHT:
  84. for (; shadow > 0; shadow--, x++, y++, w--) {
  85. XDrawLine(Xdisplay, d, gc_top, x, y, x, y + w);
  86. XDrawLine(Xdisplay, d, gc_top, x, y, x + w, y + w / 2);
  87. XDrawLine(Xdisplay, d, gc_bottom, x, y + w, x + w, y + w / 2);
  88. }
  89. break;
  90. default:
  91. break;
  92. }
  93. }
  94. void
  95. draw_arrow_from_colors(Drawable d, Pixel top, Pixel bottom, int x, int y, int w, int shadow, unsigned char type)
  96. {
  97. static GC gc_top = (GC) 0, gc_bottom = (GC) 0;
  98. if (gc_top == 0) {
  99. gc_top = LIBAST_X_CREATE_GC(0, NULL);
  100. gc_bottom = LIBAST_X_CREATE_GC(0, NULL);
  101. }
  102. XSetForeground(Xdisplay, gc_top, top);
  103. XSetForeground(Xdisplay, gc_bottom, bottom);
  104. draw_arrow(d, gc_top, gc_bottom, x, y, w, shadow, type);
  105. }
  106. void
  107. draw_box(Drawable d, GC gc_top, GC gc_bottom, int x, int y, int w, int h)
  108. {
  109. XDrawLine(Xdisplay, d, gc_top, x + w, y, x, y);
  110. XDrawLine(Xdisplay, d, gc_top, x, y, x, y + h);
  111. XDrawLine(Xdisplay, d, gc_bottom, x, y + h, x + w, y + h);
  112. XDrawLine(Xdisplay, d, gc_bottom, x + w, y + h, x + w, y);
  113. }
  114. #define SHADE_PIXEL(pixel, dir, tmp) do {(tmp) = ((((double)pixel)/depth_factor) + ((dir) ? 0.2 : -0.2)) * depth_factor; \
  115. if ((tmp) > (depth_factor-1)) (tmp) = depth_factor - 1; else if ((tmp) < 0) (tmp) = 0;} while (0)
  116. #define MOD_PIXEL_HIGH(x, y, up) do {v = XGetPixel(ximg, (x), (y)); r = (int) ((v >> br) & mr); g = (int) ((v >> bg) & mg); b = (int) ((v << bb) & mb); \
  117. SHADE_PIXEL(r, (up), dv); r = (int) dv; SHADE_PIXEL(g, (up), dv); g = (int) dv; SHADE_PIXEL(b, (up), dv); b = (int) dv; \
  118. v = ((r & mr) << br) | ((g & mg) << bg) | ((b & mb) >> bb); XPutPixel(ximg, (x), (y), v);} while (0)
  119. void
  120. bevel_pixmap(Pixmap p, int w, int h, Imlib_Border * bord, unsigned char up)
  121. {
  122. XImage *ximg;
  123. register unsigned long v;
  124. double dv;
  125. short x, y, xbound, ybound;
  126. unsigned int r, g, b;
  127. int real_depth = 0, depth_factor;
  128. register int br, bg, bb; /* Bitshifts */
  129. register unsigned int mr, mg, mb; /* Bitmasks */
  130. GC gc;
  131. if (!bord)
  132. return;
  133. depth_factor = 1 << Xdepth;
  134. if (Xdepth <= 8) {
  135. D_PIXMAP(("Depth of %d is not supported. Punt!\n", Xdepth));
  136. return;
  137. } else if (Xdepth == 16) {
  138. XWindowAttributes xattr;
  139. XGetWindowAttributes(Xdisplay, Xroot, &xattr);
  140. if ((xattr.visual->red_mask == 0x7c00) && (xattr.visual->green_mask == 0x3e0) && (xattr.visual->blue_mask == 0x1f)) {
  141. real_depth = 15;
  142. depth_factor = 1 << 15;
  143. }
  144. }
  145. if (!real_depth) {
  146. real_depth = Xdepth;
  147. }
  148. ximg = XGetImage(Xdisplay, p, 0, 0, w, h, -1, ZPixmap);
  149. if (!ximg) {
  150. return;
  151. }
  152. /* Determine bitshift and bitmask values */
  153. switch (real_depth) {
  154. case 15:
  155. br = 7;
  156. bg = 2;
  157. bb = 3;
  158. mr = mg = mb = 0xf8;
  159. break;
  160. case 16:
  161. br = 8;
  162. bg = bb = 3;
  163. mr = mb = 0xf8;
  164. mg = 0xfc;
  165. break;
  166. case 24:
  167. case 32:
  168. br = 16;
  169. bg = 8;
  170. bb = 0;
  171. mr = mg = mb = 0xff;
  172. break;
  173. default:
  174. return;
  175. }
  176. /* Left edge */
  177. for (y = bord->top; y < h; y++) {
  178. xbound = h - y;
  179. if (xbound > bord->left)
  180. xbound = bord->left;
  181. for (x = 0; x < xbound; x++) {
  182. MOD_PIXEL_HIGH(x, y, up);
  183. }
  184. }
  185. /* Right edge */
  186. ybound = h - bord->bottom;
  187. for (y = 0; y < ybound; y++) {
  188. xbound = bord->right - y;
  189. if (xbound < 0)
  190. xbound = 0;
  191. for (x = xbound; x < bord->right; x++) {
  192. MOD_PIXEL_HIGH(x + (w - bord->right), y, !up);
  193. }
  194. }
  195. /* Top edge */
  196. for (y = 0; y < bord->top; y++) {
  197. xbound = w - y;
  198. for (x = 0; x < xbound; x++) {
  199. MOD_PIXEL_HIGH(x, y, up);
  200. }
  201. }
  202. /* Bottom edge */
  203. for (y = h - bord->bottom; y < h; y++) {
  204. for (x = h - y - 1; x < w; x++) {
  205. MOD_PIXEL_HIGH(x, y, !up);
  206. }
  207. }
  208. gc = LIBAST_X_CREATE_GC(0, NULL);
  209. XPutImage(Xdisplay, p, gc, ximg, 0, 0, 0, 0, w, h);
  210. LIBAST_X_FREE_GC(gc);
  211. XDestroyImage(ximg);
  212. }