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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/* gdkgraphics.c
Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of the peer AWT libraries of GNU Classpath.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published
by the Free Software Foundation, either version 2 of the License, or
(at your option) any later verion.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA. */
#include "gtkpeer.h"
#include "gnu_java_awt_peer_gtk_GdkGraphics.h"
#include <gdk/gdkprivate.h>
#include <gdk/gdkx.h>
#define GDK_STABLE_IS_PIXMAP(d) (((GdkWindowPrivate *)d)->window_type == GDK_WINDOW_PIXMAP)
GdkPoint *
translate_points (JNIEnv *env, jintArray xpoints, jintArray ypoints,
jint npoints, jint x_offset, jint y_offset);
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_copyState
(JNIEnv *env, jobject obj, jobject old)
{
struct graphics *g, *g_old;
g = (struct graphics *) malloc (sizeof (struct graphics));
g_old = (struct graphics *) NSA_GET_PTR (env, old);
*g = *g_old;
gdk_threads_enter ();
g->gc = gdk_gc_new (g->drawable);
gdk_gc_copy (g->gc, g_old->gc);
if (GDK_STABLE_IS_PIXMAP (g->drawable))
gdk_pixmap_ref (g->drawable);
else /* GDK_IS_WINDOW (g->drawable) */
gdk_window_ref (g->drawable);
gdk_colormap_ref (g->cm);
gdk_threads_leave ();
NSA_SET_PTR (env, obj, g);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_initState__II
(JNIEnv *env, jobject obj, jint width, jint height)
{
struct graphics *g;
g = (struct graphics *) malloc (sizeof (struct graphics));
g->x_offset = g->y_offset = 0;
gdk_threads_enter ();
g->drawable = (GdkDrawable *) gdk_pixmap_new (NULL, width, height,
gdk_rgb_get_visual ()->depth);
g->cm = gdk_rgb_get_cmap ();
gdk_colormap_ref (g->cm);
g->gc = gdk_gc_new (g->drawable);
gdk_threads_leave ();
NSA_SET_PTR (env, obj, g);
}
/* copy the native state of the peer (GtkWidget *) to the native state
of the graphics object */
JNIEXPORT jintArray JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_initState__Lgnu_java_awt_peer_gtk_GtkComponentPeer_2
(JNIEnv *env, jobject obj, jobject peer)
{
struct graphics *g = (struct graphics *) malloc (sizeof (struct graphics));
void *ptr;
GtkWidget *widget;
GdkColor color;
jintArray array;
jint *rgb;
ptr = NSA_GET_PTR (env, peer);
g->x_offset = g->y_offset = 0;
gdk_threads_enter ();
widget = GTK_WIDGET (ptr);
if (GTK_IS_WINDOW (widget))
{
g->drawable = find_gtk_layout (widget)->bin_window;
}
else if (GTK_IS_LAYOUT (widget))
{
g->drawable = (GdkDrawable *) GTK_LAYOUT (widget)->bin_window;
}
else
{
g->drawable = (GdkDrawable *) widget->window;
}
gdk_window_ref (g->drawable);
g->cm = gtk_widget_get_colormap (widget);
gdk_colormap_ref (g->cm);
g->gc = gdk_gc_new (g->drawable);
gdk_gc_copy (g->gc, widget->style->fg_gc[GTK_STATE_NORMAL]);
color = widget->style->fg[GTK_STATE_NORMAL];
gdk_threads_leave ();
array = (*env)->NewIntArray (env, 3);
rgb = (*env)->GetIntArrayElements (env, array, NULL);
rgb[0] = color.red >> 8;
rgb[1] = color.green >> 8;
rgb[2] = color.blue >> 8;
(*env)->ReleaseIntArrayElements (env, array, rgb, 0);
NSA_SET_PTR (env, obj, g);
return array;
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_dispose
(JNIEnv *env, jobject obj)
{
struct graphics *g;
g = (struct graphics *) NSA_DEL_PTR (env, obj);
if (!g) return; /* dispose has been called more than once */
gdk_threads_enter ();
XFlush (GDK_DISPLAY ());
gdk_gc_destroy (g->gc);
if (GDK_STABLE_IS_PIXMAP (g->drawable))
gdk_pixmap_unref (g->drawable);
else /* GDK_IS_WINDOW (g->drawable) */
gdk_window_unref (g->drawable);
gdk_colormap_unref (g->cm);
gdk_threads_leave ();
free (g);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_translateNative
(JNIEnv *env, jobject obj, jint x, jint y)
{
struct graphics *g;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
g->x_offset += x;
g->y_offset += y;
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_drawString
(JNIEnv *env, jobject obj, jstring str, jint x, jint y,
jstring fname, jint size)
{
struct graphics *g;
const char *cfname, *cstr;
gchar *xlfd;
g = (struct graphics *) NSA_GET_PTR (env, obj);
cfname = (*env)->GetStringUTFChars (env, fname, NULL);
xlfd = g_strdup_printf (cfname, (size * 10));
(*env)->ReleaseStringUTFChars (env, fname, cfname);
cstr = (*env)->GetStringUTFChars (env, str, NULL);
gdk_threads_enter ();
gdk_draw_string (g->drawable, gdk_font_load (xlfd), g->gc,
x + g->x_offset, y + g->y_offset, cstr);
gdk_threads_leave ();
(*env)->ReleaseStringUTFChars (env, str, cstr);
g_free (xlfd);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_drawLine
(JNIEnv *env, jobject obj, jint x, jint y, jint x2, jint y2)
{
struct graphics *g;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
gdk_draw_line (g->drawable, g->gc,
x + g->x_offset, y + g->y_offset,
x2 + g->x_offset, y2 + g->y_offset);
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_fillRect
(JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
{
struct graphics *g;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
gdk_draw_rectangle (g->drawable, g->gc, TRUE,
x + g->x_offset, y + g->y_offset, width, height);
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_drawRect
(JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
{
struct graphics *g;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
gdk_draw_rectangle (g->drawable, g->gc, FALSE,
x + g->x_offset, y + g->y_offset, width, height);
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_copyArea
(JNIEnv *env, jobject obj, jint x, jint y,
jint width, jint height, jint dx, jint dy)
{
struct graphics *g;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
gdk_window_copy_area ((GdkWindow *)g->drawable,
g->gc,
x + g->x_offset + dx, y + g->y_offset + dy,
(GdkWindow *)g->drawable,
x + g->x_offset, y + g->y_offset,
width, height);
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_copyPixmap
(JNIEnv *env, jobject obj, jobject offscreen,
jint x, jint y, jint width, jint height)
{
struct graphics *g1, *g2;
g1 = (struct graphics *) NSA_GET_PTR (env, obj);
g2 = (struct graphics *) NSA_GET_PTR (env, offscreen);
gdk_threads_enter ();
gdk_window_copy_area ((GdkWindow *)g1->drawable,
g1->gc,
x + g1->x_offset, y + g1->y_offset,
(GdkWindow *)g2->drawable,
0 + g2->x_offset, 0 + g2->y_offset,
width, height);
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_clearRect
(JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
{
struct graphics *g;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
gdk_window_clear_area ((GdkWindow *)g->drawable,
x + g->x_offset, y + g->y_offset, width, height);
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_setFunction
(JNIEnv *env, jobject obj, jint func)
{
struct graphics *g;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
gdk_gc_set_function (g->gc, func);
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_setFGColor
(JNIEnv *env, jobject obj, jint red, jint green, jint blue)
{
GdkColor color;
struct graphics *g;
color.red = red << 8;
color.green = green << 8;
color.blue = blue << 8;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
gdk_color_alloc (g->cm, &color);
gdk_gc_set_foreground (g->gc, &color);
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_drawArc
(JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height,
jint angle1, jint angle2)
{
struct graphics *g;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
gdk_draw_arc (g->drawable, g->gc, FALSE,
x + g->x_offset, y + g->y_offset,
width, height, angle1 << 6, angle2 << 6);
gdk_threads_leave ();
}
GdkPoint *
translate_points (JNIEnv *env, jintArray xpoints, jintArray ypoints,
jint npoints, jint x_offset, jint y_offset)
{
GdkPoint *points;
jint *x, *y;
int i;
/* allocate one more point than necessary, in case we need to tack
on an extra due to the semantics of Java polygons. */
points = g_malloc (sizeof (GdkPoint) * (npoints + 1));
x = (*env)->GetIntArrayElements (env, xpoints, NULL);
y = (*env)->GetIntArrayElements (env, ypoints, NULL);
for (i = 0; i < npoints; i++)
{
points[i].x = x[i] + x_offset;
points[i].y = y[i] + y_offset;
}
(*env)->ReleaseIntArrayElements (env, xpoints, x, JNI_ABORT);
(*env)->ReleaseIntArrayElements (env, ypoints, y, JNI_ABORT);
return points;
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_drawPolyline
(JNIEnv *env, jobject obj, jintArray xpoints, jintArray ypoints,
jint npoints)
{
struct graphics *g;
GdkPoint *points;
g = (struct graphics *) NSA_GET_PTR (env, obj);
points = translate_points (env, xpoints, ypoints, npoints,
g->x_offset, g->y_offset);
gdk_threads_enter ();
gdk_draw_lines (g->drawable, g->gc, points, npoints);
gdk_threads_leave ();
g_free (points);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_drawPolygon
(JNIEnv *env, jobject obj, jintArray xpoints, jintArray ypoints,
jint npoints)
{
struct graphics *g;
GdkPoint *points;
g = (struct graphics *) NSA_GET_PTR (env, obj);
points = translate_points (env, xpoints, ypoints, npoints,
g->x_offset, g->y_offset);
/* make sure the polygon is closed, per Java semantics.
if it's not, we close it. */
if (points[0].x != points[npoints-1].x || points[0].y != points[npoints-1].y)
points[npoints++] = points[0];
gdk_threads_enter ();
gdk_draw_lines (g->drawable, g->gc, points, npoints);
gdk_threads_leave ();
g_free (points);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_fillPolygon
(JNIEnv *env, jobject obj, jintArray xpoints, jintArray ypoints,
jint npoints)
{
struct graphics *g;
GdkPoint *points;
g = (struct graphics *) NSA_GET_PTR (env, obj);
points = translate_points (env, xpoints, ypoints, npoints,
g->x_offset, g->y_offset);
gdk_threads_enter ();
gdk_draw_polygon (g->drawable, g->gc, TRUE, points, npoints);
gdk_threads_leave ();
g_free (points);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_fillArc
(JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height,
jint angle1, jint angle2)
{
struct graphics *g;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
gdk_draw_arc (g->drawable, g->gc, TRUE,
x + g->x_offset, y + g->y_offset,
width, height, angle1 << 6, angle2 << 6);
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_drawOval
(JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
{
struct graphics *g;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
gdk_draw_arc (g->drawable, g->gc, FALSE,
x + g->x_offset, y + g->y_offset,
width, height, 0, 23040);
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_fillOval
(JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
{
struct graphics *g;
g = (struct graphics *) NSA_GET_PTR (env, obj);
gdk_threads_enter ();
gdk_draw_arc (g->drawable, g->gc, TRUE,
x + g->x_offset, y + g->y_offset,
width, height, 0, 23040);
gdk_threads_leave ();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics_setClipRectangle
(JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
{
struct graphics *g;
GdkRectangle rectangle;
g = (struct graphics *) NSA_GET_PTR (env, obj);
rectangle.x = x + g->x_offset;
rectangle.y = y + g->y_offset;
rectangle.width = width;
rectangle.height = height;
gdk_threads_enter ();
gdk_gc_set_clip_rectangle (g->gc, &rectangle);
gdk_threads_leave ();
}
|