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