OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / lib / classpath / examples / gnu / classpath / examples / jawt / DemoJAWT.c
1 /* DemoJAWT.c -- native portion of AWT Native Interface demo
2    Copyright (C) 2005  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath examples.
5
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA. */
20
21 #include "DemoJAWT.h"
22 #include "jawt_md.h"
23 #include <string.h>
24
25 JNIEXPORT void JNICALL
26 Java_gnu_classpath_examples_jawt_DemoJAWT_paintIt (JNIEnv* env,
27                                                    jobject canvas,
28                                                    jobject graphics,
29                                                    jboolean on)
30 {
31   JAWT awt;
32   JAWT_DrawingSurface* surface;
33   JAWT_DrawingSurfaceInfo* surface_info;
34   JAWT_X11DrawingSurfaceInfo* surface_info_x11;
35   jint lock;
36   GC gc;
37   int c;
38   char* test_string = "JAWT";
39   XColor orange;
40   XColor yellow;
41   XColor blue;
42   Display* display;
43   Drawable drawable;
44   Status status;
45
46   awt.version = JAWT_VERSION_1_3;
47   if (JAWT_GetAWT (env, &awt) == JNI_FALSE)
48     {
49       printf ("couldn't find AWT\n");
50       return;
51     }
52
53   surface = awt.GetDrawingSurface (env, canvas);
54   if (surface == NULL)
55     {
56       printf ("drawing surface is NULL\n");
57       return;
58     }
59
60   lock = surface->Lock (surface);
61   if ((lock & JAWT_LOCK_ERROR) != 0)
62     {
63       printf ("couldn't lock drawing surface\n");
64       awt.FreeDrawingSurface (surface);
65       return;
66     }
67
68   surface_info = surface->GetDrawingSurfaceInfo (surface);
69   if (surface_info == NULL)
70     {
71       printf ("couldn't get surface information\n");
72       surface->Unlock (surface);
73       awt.FreeDrawingSurface (surface);
74       return;
75     }
76
77   surface_info_x11 = (JAWT_X11DrawingSurfaceInfo*) surface_info->platformInfo;
78
79   display = surface_info_x11->display;
80   drawable = surface_info_x11->drawable;
81
82   gc = XCreateGC (display, drawable, 0, 0);
83   XSetBackground (display, gc, 0);
84
85   orange.red = 254 * 65535 / 255;
86   orange.green = 90 * 65535 / 255;
87   orange.blue = 16 * 65535 / 255;
88
89   /* assume color lookups succeed */
90   status = XAllocColor (display, DefaultColormap (display,
91                                                   DefaultScreen (display)),
92                         &orange);
93
94   if (!status)
95     {
96       printf ("color allocation failed\n");
97       goto cleanup;
98     }
99
100   yellow.red = 255 * 65535 / 255;
101   yellow.green = 255 * 65535 / 255;
102   yellow.blue = 0 * 65535 / 255;
103
104   XAllocColor (display, DefaultColormap (display,
105                                          DefaultScreen (display)),
106                &yellow);
107
108   if (!status)
109     {
110       printf ("color allocation failed\n");
111       goto cleanup;
112     }
113
114   blue.red = 16 * 65535 / 255;
115   blue.green = 30 * 65535 / 255;
116   blue.blue = 137 * 65535 / 255;
117
118   XAllocColor (display, DefaultColormap (display,
119                                          DefaultScreen (display)),
120                 &blue);
121
122   if (!status)
123     {
124       printf ("color allocation failed\n");
125       goto cleanup;
126     }
127
128   for (c = 5; c >= 0; c--)
129     {
130       if (c % 2 == on)
131         XSetForeground (display, gc, yellow.pixel);
132       else
133         XSetForeground (display, gc, orange.pixel);
134
135       XFillArc (display, drawable, gc, 140 - c * 15, 140 - c * 15, c * 30, c * 30, 0, 360 * 64);
136     }
137
138   XSetForeground (display, gc, blue.pixel);
139   XDrawString (display, drawable,
140                gc, 129, 145, test_string, strlen (test_string));
141
142  cleanup:
143   XFreeGC (display, gc);
144
145   surface->FreeDrawingSurfaceInfo (surface_info);
146
147   surface->Unlock (surface);
148
149   awt.FreeDrawingSurface (surface);
150 }