/src/mot/iupmot_draw.c

https://github.com/kmx/mirror-iup · C · 247 lines · 193 code · 48 blank · 6 comment · 15 complexity · fd9e9a9a26f249738cbecc561317de87 MD5 · raw file

  1. /** \file
  2. * \brief Draw Functions
  3. *
  4. * See Copyright Notice in "iup.h"
  5. */
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <memory.h>
  10. #include <Xm/Xm.h>
  11. #include <X11/Xlib.h>
  12. #include "iup.h"
  13. #include "iup_attrib.h"
  14. #include "iup_class.h"
  15. #include "iup_str.h"
  16. #include "iup_object.h"
  17. #include "iup_image.h"
  18. #include "iup_draw.h"
  19. #include "iupmot_drv.h"
  20. #include "iupmot_color.h"
  21. struct _IdrawCanvas{
  22. Ihandle* ih;
  23. int w, h;
  24. Window wnd;
  25. Pixmap pixmap;
  26. GC pixmap_gc, gc;
  27. };
  28. static void motDrawGetGeometry(Display *dpy, Drawable wnd, int *_w, int *_h, int *_d)
  29. {
  30. Window root;
  31. int x, y;
  32. unsigned int w, h, b, d;
  33. XGetGeometry(dpy, wnd, &root, &x, &y, &w, &h, &b, &d);
  34. *_w = w;
  35. *_h = h;
  36. *_d = d;
  37. }
  38. IdrawCanvas* iupDrawCreateCanvas(Ihandle* ih)
  39. {
  40. IdrawCanvas* dc = calloc(1, sizeof(IdrawCanvas));
  41. int depth;
  42. dc->ih = ih;
  43. dc->wnd = XtWindow(ih->handle);
  44. dc->gc = XCreateGC(iupmot_display, dc->wnd, 0, NULL);
  45. motDrawGetGeometry(iupmot_display, dc->wnd, &dc->w, &dc->h, &depth);
  46. dc->pixmap = XCreatePixmap(iupmot_display, dc->wnd, dc->w, dc->h, depth);
  47. dc->pixmap_gc = XCreateGC(iupmot_display, dc->pixmap, 0, NULL);
  48. return dc;
  49. }
  50. void iupDrawKillCanvas(IdrawCanvas* dc)
  51. {
  52. XFreeGC(iupmot_display, dc->pixmap_gc);
  53. XFreePixmap(iupmot_display, dc->pixmap);
  54. XFreeGC(iupmot_display, dc->gc);
  55. free(dc);
  56. }
  57. void iupDrawUpdateSize(IdrawCanvas* dc)
  58. {
  59. int w, h, depth;
  60. motDrawGetGeometry(iupmot_display, dc->wnd, &w, &h, &depth);
  61. if (w != dc->w || h != dc->h)
  62. {
  63. dc->w = w;
  64. dc->h = h;
  65. XFreeGC(iupmot_display, dc->pixmap_gc);
  66. XFreePixmap(iupmot_display, dc->pixmap);
  67. dc->pixmap = XCreatePixmap(iupmot_display, dc->wnd, dc->w, dc->h, depth);
  68. dc->pixmap_gc = XCreateGC(iupmot_display, dc->pixmap, 0, NULL);
  69. }
  70. }
  71. void iupDrawFlush(IdrawCanvas* dc)
  72. {
  73. XCopyArea(iupmot_display, dc->pixmap, dc->wnd, dc->gc, 0, 0, dc->w, dc->h, 0, 0);
  74. }
  75. void iupDrawGetSize(IdrawCanvas* dc, int *w, int *h)
  76. {
  77. if (w) *w = dc->w;
  78. if (h) *h = dc->h;
  79. }
  80. void iupDrawParentBackground(IdrawCanvas* dc)
  81. {
  82. unsigned char r=0, g=0, b=0;
  83. char* color = iupBaseNativeParentGetBgColorAttrib(dc->ih);
  84. iupStrToRGB(color, &r, &g, &b);
  85. iupDrawRectangle(dc, 0, 0, dc->w-1, dc->h-1, r, g, b, IUP_DRAW_FILL);
  86. }
  87. void iupDrawRectangle(IdrawCanvas* dc, int x1, int y1, int x2, int y2, unsigned char r, unsigned char g, unsigned char b, int style)
  88. {
  89. XSetForeground(iupmot_display, dc->pixmap_gc, iupmotColorGetPixel(r, g, b));
  90. if (style==IUP_DRAW_FILL)
  91. XFillRectangle(iupmot_display, dc->pixmap, dc->pixmap_gc, x1, y1, x2-x1+1, y2-y1+1);
  92. else
  93. {
  94. XGCValues gcval;
  95. if (style==IUP_DRAW_STROKE_DASH)
  96. gcval.line_style = LineOnOffDash;
  97. else
  98. gcval.line_style = LineSolid;
  99. XChangeGC(iupmot_display, dc->pixmap_gc, GCLineStyle, &gcval);
  100. XDrawRectangle(iupmot_display, dc->pixmap, dc->pixmap_gc, x1, y1, x2-x1, y2-y1);
  101. }
  102. }
  103. void iupDrawLine(IdrawCanvas* dc, int x1, int y1, int x2, int y2, unsigned char r, unsigned char g, unsigned char b, int style)
  104. {
  105. XGCValues gcval;
  106. if (style==IUP_DRAW_STROKE_DASH)
  107. gcval.line_style = LineOnOffDash;
  108. else
  109. gcval.line_style = LineSolid;
  110. XChangeGC(iupmot_display, dc->pixmap_gc, GCLineStyle, &gcval);
  111. XSetForeground(iupmot_display, dc->pixmap_gc, iupmotColorGetPixel(r, g, b));
  112. XDrawLine(iupmot_display, dc->pixmap, dc->pixmap_gc, x1, y1, x2, y2);
  113. }
  114. void iupDrawArc(IdrawCanvas* dc, int x1, int y1, int x2, int y2, double a1, double a2, unsigned char r, unsigned char g, unsigned char b, int style)
  115. {
  116. XSetForeground(iupmot_display, dc->pixmap_gc, iupmotColorGetPixel(r, g, b));
  117. if (style==IUP_DRAW_FILL)
  118. {
  119. XSetArcMode(iupmot_display, dc->pixmap_gc, ArcPieSlice);
  120. XFillArc(iupmot_display, dc->pixmap, dc->pixmap_gc, x1, y1, x2-x1+1, y2-y1+1, iupROUND(a1*64), iupROUND((a2 - a1)*64));
  121. }
  122. else
  123. {
  124. XGCValues gcval;
  125. if (style==IUP_DRAW_STROKE_DASH)
  126. gcval.line_style = LineOnOffDash;
  127. else
  128. gcval.line_style = LineSolid;
  129. XChangeGC(iupmot_display, dc->pixmap_gc, GCLineStyle, &gcval);
  130. XDrawArc(iupmot_display, dc->pixmap, dc->pixmap_gc, x1, y1, x2-x1+1, y2-y1+1, iupROUND(a1*64), iupROUND((a2 - a1)*64));
  131. }
  132. }
  133. void iupDrawPolygon(IdrawCanvas* dc, int* points, int count, unsigned char r, unsigned char g, unsigned char b, int style)
  134. {
  135. int i;
  136. XPoint* pnt = (XPoint*)malloc(count*sizeof(XPoint)); /* XPoint uses short for coordinates */
  137. for (i = 0; i < count; i++)
  138. {
  139. pnt[i].x = (short)points[2*i];
  140. pnt[i].y = (short)points[2*i+1];
  141. }
  142. XSetForeground(iupmot_display, dc->pixmap_gc, iupmotColorGetPixel(r, g, b));
  143. if (style==IUP_DRAW_FILL)
  144. XFillPolygon(iupmot_display, dc->pixmap, dc->pixmap_gc, pnt, count, Complex, CoordModeOrigin);
  145. else
  146. {
  147. XGCValues gcval;
  148. if (style==IUP_DRAW_STROKE_DASH)
  149. gcval.line_style = LineOnOffDash;
  150. else
  151. gcval.line_style = LineSolid;
  152. XChangeGC(iupmot_display, dc->pixmap_gc, GCLineStyle, &gcval);
  153. XDrawLines(iupmot_display, dc->pixmap, dc->pixmap_gc, pnt, count, CoordModeOrigin);
  154. }
  155. free(pnt);
  156. }
  157. void iupDrawSetClipRect(IdrawCanvas* dc, int x1, int y1, int x2, int y2)
  158. {
  159. XRectangle rect;
  160. rect.x = (short)x1;
  161. rect.y = (short)y1;
  162. rect.width = (unsigned short)(x2-x1+1);
  163. rect.height = (unsigned short)(y2-y1+1);
  164. XSetClipRectangles(iupmot_display, dc->pixmap_gc, 0, 0, &rect, 1, Unsorted);
  165. }
  166. void iupDrawResetClip(IdrawCanvas* dc)
  167. {
  168. XSetClipMask(iupmot_display, dc->pixmap_gc, None);
  169. }
  170. void iupDrawText(IdrawCanvas* dc, const char* text, int len, int x, int y, unsigned char r, unsigned char g, unsigned char b, const char* font)
  171. {
  172. XFontStruct* xfont = (XFontStruct*)iupmotGetFontStruct(font);
  173. XSetForeground(iupmot_display, dc->pixmap_gc, iupmotColorGetPixel(r, g, b));
  174. XSetFont(iupmot_display, dc->pixmap_gc, xfont->fid);
  175. XDrawString(iupmot_display, dc->pixmap, dc->pixmap_gc, x, y+xfont->ascent, text, len);
  176. }
  177. void iupDrawImage(IdrawCanvas* dc, const char* name, int make_inactive, int x, int y, int *img_w, int *img_h)
  178. {
  179. int bpp;
  180. Pixmap pixmap = (Pixmap)iupImageGetImage(name, dc->ih, make_inactive);
  181. if (!pixmap)
  182. return;
  183. /* must use this info, since image can be a driver image loaded from resources */
  184. iupdrvImageGetInfo((void*)pixmap, img_w, img_h, &bpp);
  185. XCopyArea(iupmot_display, pixmap, dc->pixmap, dc->pixmap_gc, 0, 0, *img_w, *img_h, x, y);
  186. }
  187. void iupDrawSelectRect(IdrawCanvas* dc, int x, int y, int w, int h)
  188. {
  189. XSetFunction(iupmot_display, dc->pixmap_gc, GXxor);
  190. XSetForeground(iupmot_display, dc->pixmap_gc, iupmotColorGetPixel(255, 255, 255));
  191. XFillRectangle(iupmot_display, dc->pixmap, dc->pixmap_gc, x, y, w, h);
  192. XSetFunction(iupmot_display, dc->pixmap_gc, GXcopy);
  193. }
  194. #include <Xm/XmP.h>
  195. #include <Xm/DrawP.h>
  196. void iupDrawFocusRect(IdrawCanvas* dc, int x, int y, int w, int h)
  197. {
  198. XmeDrawHighlight(iupmot_display, dc->wnd, dc->gc, x, y, w, h, 1);
  199. }