OSDN Git Service

2006-08-14 Mark Wielaard <mark@klomp.org>
[pf3gnuchains/gcc-fork.git] / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.c
1 /* gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.c
2    Copyright (C) 2004, 2006 Free Software Foundation, Inc.
3
4    This file is part of GNU Classpath.
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    Linking this library statically or dynamically with other modules is
22    making a combined work based on this library.  Thus, the terms and
23    conditions of the GNU General Public License cover the whole
24    combination.
25
26    As a special exception, the copyright holders of this library give you
27    permission to link this library with independent modules to produce an
28    executable, regardless of the license terms of these independent
29    modules, and to copy and distribute the resulting executable under
30    terms of your choice, provided that you also meet, for each linked
31    independent module, the terms and conditions of the license of that
32    module.  An independent module is a module which is not derived from
33    or based on this library.  If you modify this library, you may extend
34    this exception to your version of the library, but you are not
35    obligated to do so.  If you do not wish to do so, delete this
36    exception statement from your version. */
37
38 #include <glib.h>
39 #include <gdk/gdk.h>
40
41 #include "gdkfont.h"
42 #include "gdkdisplay.h"
43 #include "gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.h"
44
45 struct state_table *cp_gtk_native_display_state_table;
46
47 jclass gdkGraphicsEnvironment_class;
48
49 JNIEXPORT void JNICALL
50 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_initStaticState
51 (JNIEnv *env, jclass klazz __attribute__((unused)))
52 {
53     gdkGraphicsEnvironment_class = (*env)->NewGlobalRef
54     (env, klazz);
55
56         NSA_DISPLAY_INIT(env, gdkGraphicsEnvironment_class);
57 }
58
59 JNIEXPORT void JNICALL
60 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeInitState
61 (JNIEnv *env, jobject obj)
62 {
63         GdkDisplay *defaultDisplay;
64         
65     gdk_threads_enter();
66     
67     /* Retrieve the default display. */
68     defaultDisplay = gdk_display_get_default();
69     
70     gdk_threads_leave();
71     
72     /* Store display pointer in GdkGraphicsEnvironment instance. */
73     NSA_SET_DISPLAY_PTR(env, obj, (void *) defaultDisplay);
74 }
75
76 static gint
77 cmp_families (const void *a, const void *b)
78 {
79   const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
80   const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
81
82   return g_utf8_collate (a_name, b_name);
83 }
84
85 JNIEXPORT void JNICALL
86 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetFontFamilies
87 (JNIEnv *env, jobject  self __attribute__((unused)), jobjectArray family_name)
88 {
89   PangoContext *context = NULL;
90   PangoFontFamily **families = NULL;
91   int n_families = 0;
92   int idx = 0;
93
94   gdk_threads_enter ();
95
96   context = gdk_pango_context_get();
97   g_assert (context != NULL);
98
99   pango_context_list_families (context, &families, &n_families);
100
101   qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
102
103   for (idx = 0;  idx < n_families;  idx++)
104     {
105       const char *name_tmp =  pango_font_family_get_name (families[idx]);
106       jstring name = (*env)->NewStringUTF (env, name_tmp);
107       (*env)->SetObjectArrayElement (env, family_name, idx, name);
108       (*env)->DeleteLocalRef(env, name);
109     }
110   g_free (families);
111
112   gdk_threads_leave ();
113 }
114
115 JNIEXPORT jint JNICALL
116 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetNumFontFamilies
117 (JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)))
118 {
119   PangoContext *context = NULL;
120   PangoFontFamily **families = NULL;
121   gint n_families = 0;
122   gint num = 0;
123
124   gdk_threads_enter ();
125
126   context = gdk_pango_context_get();
127   g_assert (context != NULL);
128
129   pango_context_list_families (context, &families, &n_families);
130
131   num = n_families;
132   g_free (families);
133
134   gdk_threads_leave ();
135   
136   return num;
137 }
138
139 JNIEXPORT jobjectArray JNICALL
140 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetScreenDevices
141 (JNIEnv *env, jobject obj)
142 {
143         jmethodID gdkScreenGraphicsDevice_ctor, gdkScreenGraphicsDevice_init;
144         jclass gdkScreenGraphicsDevice_class;
145         int numScreens = 0, i = 0;
146         GdkDisplay *display;
147         jobjectArray array;
148         jobject instance;
149         
150         gdkScreenGraphicsDevice_class = (*env)->FindClass 
151     (env, "gnu/java/awt/peer/gtk/GdkScreenGraphicsDevice");
152     
153         gdkScreenGraphicsDevice_ctor = (*env)->GetMethodID 
154     (env, gdkScreenGraphicsDevice_class, "<init>",
155      "(Lgnu/java/awt/peer/gtk/GdkGraphicsEnvironment;)V");
156
157         gdkScreenGraphicsDevice_init = (*env)->GetMethodID 
158     (env, gdkScreenGraphicsDevice_class, "init", "()V");
159
160         display = (GdkDisplay *) NSA_GET_DISPLAY_PTR(env, obj);
161         
162         gdk_threads_enter();
163         
164         numScreens = gdk_display_get_n_screens(display);
165         
166         
167         /* Create a suitably sized array. */
168         array = (*env)->NewObjectArray(env,
169                                    numScreens,
170                                    gdkScreenGraphicsDevice_class,
171                                    NULL);
172         
173         /* Create GdkScreenGraphicsDevice instances, store the native pointer to
174          * the GScreen object with them, run a 2nd initialization phase and
175          * put the new instance into the result array.
176          */
177         for ( ; i < numScreens ; i++)
178         {
179                 instance = (*env)->NewObject (env, 
180                                       gdkScreenGraphicsDevice_class,
181                                       gdkScreenGraphicsDevice_ctor,
182                                       obj);
183                                                                           
184         NSA_SET_SCREEN_PTR(env,
185                            instance,
186                            gdk_display_get_screen(display, i));
187                                                    
188         gdk_threads_leave();
189         (*env)->CallVoidMethod(env,
190                                instance,
191                                gdkScreenGraphicsDevice_init);
192                 gdk_threads_enter();
193                 
194         (*env)->SetObjectArrayElement(env, array, i, instance);
195     }
196         
197     gdk_threads_leave();
198         
199     return array;
200 }
201
202 JNIEXPORT jobject JNICALL
203 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetDefaultScreenDevice
204 (JNIEnv *env, jobject obj)
205 {
206     jclass gdkScreenGraphicsDevice_class;
207     jmethodID gdkScreenGraphicsDevice_ctor, gdkScreenGraphicsDevice_init;
208     jobject defaultDevice;
209     GdkScreen *defaultScreen;
210     
211     gdkScreenGraphicsDevice_class = (*env)->FindClass 
212     (env, "gnu/java/awt/peer/gtk/GdkScreenGraphicsDevice");
213     
214     gdkScreenGraphicsDevice_ctor = (*env)->GetMethodID 
215     (env, gdkScreenGraphicsDevice_class, "<init>",
216      "(Lgnu/java/awt/peer/gtk/GdkGraphicsEnvironment;)V");
217     
218     gdkScreenGraphicsDevice_init = (*env)->GetMethodID 
219     (env, gdkScreenGraphicsDevice_class, "init", "()V");
220     
221     /* Create the GdkScreenGraphicsDevice instance. */
222     defaultDevice = (*env)->NewObject (env, 
223                                                                            gdkScreenGraphicsDevice_class,
224                                                                            gdkScreenGraphicsDevice_ctor,
225                                                                            obj);
226                                                                            
227     gdk_threads_enter();
228         
229     defaultScreen = gdk_screen_get_default();
230         
231     gdk_threads_leave();
232                                                                            
233         /* Class initialization will have set up the native_state storage
234          * mechanism for GdkScreenGraphicsDevice.
235          */
236     NSA_SET_SCREEN_PTR(env, defaultDevice, defaultScreen);
237
238     (*env)->CallVoidMethod(env,
239                            defaultDevice,
240                            gdkScreenGraphicsDevice_init);
241
242     return defaultDevice;       
243 }
244
245 JNIEXPORT jintArray JNICALL
246 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_getMouseCoordinates
247 (JNIEnv *env, jobject obj)
248 {
249   jintArray retArray;
250   jint *values;
251   GdkDisplay *display;
252   gint x, y, screenIndex;
253   GdkScreen *screen;
254
255   display = (GdkDisplay *) NSA_GET_DISPLAY_PTR(env, obj);
256   g_assert (display != NULL);
257   
258   gdk_threads_enter ();
259   
260   gdk_display_get_pointer (display, &screen, &x, &y, NULL);
261   screenIndex = gdk_screen_get_number( screen );
262
263   gdk_threads_leave ();
264         
265   retArray = (*env)->NewIntArray (env, 3);
266   values = (*env)->GetIntArrayElements (env, retArray, NULL);
267   
268   values[0] = screenIndex;
269   values[1] = x;
270   values[2] = y;
271
272   (*env)->ReleaseIntArrayElements (env, retArray, values, 0);
273
274   return retArray;
275 }