Merge -r 127928:132243 from trunk
[official-gcc.git] / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.c
blob6066a6bb271264c4a1de5c7e7de3933dfd298ace
1 /* gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.c
2 Copyright (C) 2004, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 #include <glib.h>
39 #include <gdk/gdk.h>
41 #include "gdkfont.h"
42 #include "gdkdisplay.h"
43 #include "gnu_java_awt_peer_gtk_GdkGraphicsEnvironment.h"
45 jclass gdkGraphicsEnvironment_class;
47 JNIEXPORT void JNICALL
48 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_initIDs
49 (JNIEnv *env, jclass klazz __attribute__((unused)))
51 gtkpeer_init_display_IDs(env);
54 JNIEXPORT void JNICALL
55 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeInitState
56 (JNIEnv *env, jobject obj)
58 GdkDisplay *defaultDisplay;
60 gdk_threads_enter();
62 /* Retrieve the default display. */
63 defaultDisplay = gdk_display_get_default();
65 gdk_threads_leave();
67 /* Store display pointer in GdkGraphicsEnvironment instance. */
68 gtkpeer_set_display(env, obj, (void *) defaultDisplay);
71 static gint
72 cmp_families (const void *a, const void *b)
74 const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
75 const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
77 return g_utf8_collate (a_name, b_name);
80 JNIEXPORT void JNICALL
81 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetFontFamilies
82 (JNIEnv *env, jobject self __attribute__((unused)), jobjectArray family_name)
84 PangoContext *context = NULL;
85 PangoFontFamily **families = NULL;
86 int n_families = 0;
87 int idx = 0;
89 gdk_threads_enter ();
91 context = gdk_pango_context_get();
92 g_assert (context != NULL);
94 pango_context_list_families (context, &families, &n_families);
96 qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
98 for (idx = 0; idx < n_families; idx++)
100 const char *name_tmp = pango_font_family_get_name (families[idx]);
101 jstring name = (*env)->NewStringUTF (env, name_tmp);
102 (*env)->SetObjectArrayElement (env, family_name, idx, name);
103 (*env)->DeleteLocalRef(env, name);
105 g_free (families);
107 gdk_threads_leave ();
110 JNIEXPORT jint JNICALL
111 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetNumFontFamilies
112 (JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)))
114 PangoContext *context = NULL;
115 PangoFontFamily **families = NULL;
116 gint n_families = 0;
117 gint num = 0;
119 gdk_threads_enter ();
121 context = gdk_pango_context_get();
122 g_assert (context != NULL);
124 pango_context_list_families (context, &families, &n_families);
126 num = n_families;
127 g_free (families);
129 gdk_threads_leave ();
131 return num;
134 JNIEXPORT jobjectArray JNICALL
135 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetScreenDevices
136 (JNIEnv *env, jobject obj)
138 jmethodID gdkScreenGraphicsDevice_ctor, gdkScreenGraphicsDevice_init;
139 jclass gdkScreenGraphicsDevice_class;
140 int numScreens = 0, i = 0;
141 GdkDisplay *display;
142 jobjectArray array;
143 jobject instance;
145 gdkScreenGraphicsDevice_class = (*env)->FindClass
146 (env, "gnu/java/awt/peer/gtk/GdkScreenGraphicsDevice");
148 gdkScreenGraphicsDevice_ctor = (*env)->GetMethodID
149 (env, gdkScreenGraphicsDevice_class, "<init>",
150 "(Lgnu/java/awt/peer/gtk/GdkGraphicsEnvironment;)V");
152 gdkScreenGraphicsDevice_init = (*env)->GetMethodID
153 (env, gdkScreenGraphicsDevice_class, "init", "()V");
155 display = (GdkDisplay *) gtkpeer_get_display(env, obj);
157 gdk_threads_enter();
159 numScreens = gdk_display_get_n_screens(display);
162 /* Create a suitably sized array. */
163 array = (*env)->NewObjectArray(env,
164 numScreens,
165 gdkScreenGraphicsDevice_class,
166 NULL);
168 /* Create GdkScreenGraphicsDevice instances, store the native pointer to
169 * the GScreen object with them, run a 2nd initialization phase and
170 * put the new instance into the result array.
172 for ( ; i < numScreens ; i++)
174 instance = (*env)->NewObject (env,
175 gdkScreenGraphicsDevice_class,
176 gdkScreenGraphicsDevice_ctor,
177 obj);
179 gtkpeer_set_screen(env, instance, gdk_display_get_screen(display, i));
181 gdk_threads_leave();
182 (*env)->CallVoidMethod(env,
183 instance,
184 gdkScreenGraphicsDevice_init);
185 gdk_threads_enter();
187 (*env)->SetObjectArrayElement(env, array, i, instance);
190 gdk_threads_leave();
192 return array;
195 JNIEXPORT jobject JNICALL
196 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetDefaultScreenDevice
197 (JNIEnv *env, jobject obj)
199 jclass gdkScreenGraphicsDevice_class;
200 jmethodID gdkScreenGraphicsDevice_ctor, gdkScreenGraphicsDevice_init;
201 jobject defaultDevice;
202 GdkScreen *defaultScreen;
204 gdkScreenGraphicsDevice_class = (*env)->FindClass
205 (env, "gnu/java/awt/peer/gtk/GdkScreenGraphicsDevice");
207 gdkScreenGraphicsDevice_ctor = (*env)->GetMethodID
208 (env, gdkScreenGraphicsDevice_class, "<init>",
209 "(Lgnu/java/awt/peer/gtk/GdkGraphicsEnvironment;)V");
210 gdkScreenGraphicsDevice_init = (*env)->GetMethodID
211 (env, gdkScreenGraphicsDevice_class, "init", "()V");
213 /* Create the GdkScreenGraphicsDevice instance. */
214 defaultDevice = (*env)->NewObject(env, gdkScreenGraphicsDevice_class,
215 gdkScreenGraphicsDevice_ctor, obj);
217 gdk_threads_enter();
219 defaultScreen = gdk_screen_get_default();
221 gdk_threads_leave();
223 /* Class initialization will have set up the native_state storage
224 * mechanism for GdkScreenGraphicsDevice.
226 gtkpeer_set_screen(env, defaultDevice, defaultScreen);
228 (*env)->CallVoidMethod(env,
229 defaultDevice,
230 gdkScreenGraphicsDevice_init);
232 return defaultDevice;
235 JNIEXPORT jintArray JNICALL
236 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_getMouseCoordinates
237 (JNIEnv *env, jobject obj)
239 jintArray retArray;
240 jint *values;
241 GdkDisplay *display;
242 gint x, y, screenIndex;
243 GdkScreen *screen;
245 display = (GdkDisplay *) gtkpeer_get_display(env, obj);
246 g_assert (display != NULL);
248 gdk_threads_enter ();
250 gdk_display_get_pointer (display, &screen, &x, &y, NULL);
251 screenIndex = gdk_screen_get_number( screen );
253 gdk_threads_leave ();
255 retArray = (*env)->NewIntArray (env, 3);
256 values = (*env)->GetIntArrayElements (env, retArray, NULL);
258 values[0] = screenIndex;
259 values[1] = x;
260 values[2] = y;
262 (*env)->ReleaseIntArrayElements (env, retArray, values, 0);
264 return retArray;
267 JNIEXPORT jboolean JNICALL
268 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_isWindowUnderMouse
269 (JNIEnv *env, jobject obj, jobject windowPeer)
271 GdkDisplay *display = NULL;
272 gint x = 0;
273 gint y = 0;
274 GtkWidget *windowToTest = NULL;
275 GdkWindow *windowAtPointer = NULL;
276 jboolean retVal = JNI_FALSE;
278 display = (GdkDisplay *) gtkpeer_get_display (env, obj);
279 g_assert (display != NULL);
281 windowToTest = (GtkWidget *) gtkpeer_get_widget (env, windowPeer);
283 gdk_threads_enter ();
285 windowAtPointer = gdk_display_get_window_at_pointer (display, &x, &y);
287 while (windowAtPointer
288 && windowAtPointer != windowToTest->window)
289 windowAtPointer = gdk_window_get_parent (windowAtPointer);
291 gdk_threads_leave ();
293 if (windowAtPointer)
294 retVal = JNI_TRUE;
295 else
296 retVal = JNI_FALSE;
298 return retVal;