OSDN Git Service

2008-11-10 Andrew Haley <aph@redhat.com>
[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 jclass gdkGraphicsEnvironment_class;
46
47 JNIEXPORT void JNICALL
48 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_initIDs
49 (JNIEnv *env, jclass klazz __attribute__((unused)))
50 {
51   gtkpeer_init_display_IDs(env);
52 }
53
54 JNIEXPORT void JNICALL
55 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeInitState
56 (JNIEnv *env, jobject obj)
57 {
58         GdkDisplay *defaultDisplay;
59         
60     gdk_threads_enter();
61     
62     /* Retrieve the default display. */
63     defaultDisplay = gdk_display_get_default();
64     
65     gdk_threads_leave();
66     
67     /* Store display pointer in GdkGraphicsEnvironment instance. */
68     gtkpeer_set_display(env, obj, (void *) defaultDisplay);
69 }
70
71 static gint
72 cmp_families (const void *a, const void *b)
73 {
74   const char *a_name = pango_font_family_get_name (*(PangoFontFamily **)a);
75   const char *b_name = pango_font_family_get_name (*(PangoFontFamily **)b);
76
77   return g_utf8_collate (a_name, b_name);
78 }
79
80 JNIEXPORT void JNICALL
81 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetFontFamilies
82 (JNIEnv *env, jobject  self __attribute__((unused)), jobjectArray family_name)
83 {
84   PangoContext *context = NULL;
85   PangoFontFamily **families = NULL;
86   int n_families = 0;
87   int idx = 0;
88
89   gdk_threads_enter ();
90
91   context = gdk_pango_context_get();
92   g_assert (context != NULL);
93
94   pango_context_list_families (context, &families, &n_families);
95
96   qsort (families, n_families, sizeof (PangoFontFamily *), cmp_families);
97
98   for (idx = 0;  idx < n_families;  idx++)
99     {
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);
104     }
105   g_free (families);
106
107   gdk_threads_leave ();
108 }
109
110 JNIEXPORT jint JNICALL
111 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetNumFontFamilies
112 (JNIEnv *env __attribute__((unused)), jobject obj __attribute__((unused)))
113 {
114   PangoContext *context = NULL;
115   PangoFontFamily **families = NULL;
116   gint n_families = 0;
117   gint num = 0;
118
119   gdk_threads_enter ();
120
121   context = gdk_pango_context_get();
122   g_assert (context != NULL);
123
124   pango_context_list_families (context, &families, &n_families);
125
126   num = n_families;
127   g_free (families);
128
129   gdk_threads_leave ();
130   
131   return num;
132 }
133
134 JNIEXPORT jobjectArray JNICALL
135 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetScreenDevices
136 (JNIEnv *env, jobject obj)
137 {
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;
144         
145         gdkScreenGraphicsDevice_class = (*env)->FindClass 
146     (env, "gnu/java/awt/peer/gtk/GdkScreenGraphicsDevice");
147     
148         gdkScreenGraphicsDevice_ctor = (*env)->GetMethodID 
149     (env, gdkScreenGraphicsDevice_class, "<init>",
150      "(Lgnu/java/awt/peer/gtk/GdkGraphicsEnvironment;)V");
151
152         gdkScreenGraphicsDevice_init = (*env)->GetMethodID 
153     (env, gdkScreenGraphicsDevice_class, "init", "()V");
154
155         display = (GdkDisplay *) gtkpeer_get_display(env, obj);
156         
157         gdk_threads_enter();
158         
159         numScreens = gdk_display_get_n_screens(display);
160         
161         
162         /* Create a suitably sized array. */
163         array = (*env)->NewObjectArray(env,
164                                    numScreens,
165                                    gdkScreenGraphicsDevice_class,
166                                    NULL);
167         
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.
171          */
172         for ( ; i < numScreens ; i++)
173         {
174                 instance = (*env)->NewObject (env, 
175                                       gdkScreenGraphicsDevice_class,
176                                       gdkScreenGraphicsDevice_ctor,
177                                       obj);
178                                                                           
179         gtkpeer_set_screen(env, instance, gdk_display_get_screen(display, i));
180                                                    
181         gdk_threads_leave();
182         (*env)->CallVoidMethod(env,
183                                instance,
184                                gdkScreenGraphicsDevice_init);
185                 gdk_threads_enter();
186                 
187         (*env)->SetObjectArrayElement(env, array, i, instance);
188     }
189         
190     gdk_threads_leave();
191         
192     return array;
193 }
194
195 JNIEXPORT jobject JNICALL
196 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_nativeGetDefaultScreenDevice
197 (JNIEnv *env, jobject obj)
198 {
199     jclass gdkScreenGraphicsDevice_class;
200     jmethodID gdkScreenGraphicsDevice_ctor, gdkScreenGraphicsDevice_init;
201     jobject defaultDevice;
202     GdkScreen *defaultScreen;
203     
204     gdkScreenGraphicsDevice_class = (*env)->FindClass 
205     (env, "gnu/java/awt/peer/gtk/GdkScreenGraphicsDevice");
206     
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");
212     
213     /* Create the GdkScreenGraphicsDevice instance. */
214     defaultDevice = (*env)->NewObject(env, gdkScreenGraphicsDevice_class,
215                                       gdkScreenGraphicsDevice_ctor, obj);
216                                                                            
217     gdk_threads_enter();
218         
219     defaultScreen = gdk_screen_get_default();
220         
221     gdk_threads_leave();
222                                                                            
223         /* Class initialization will have set up the native_state storage
224          * mechanism for GdkScreenGraphicsDevice.
225          */
226     gtkpeer_set_screen(env, defaultDevice, defaultScreen);
227
228     (*env)->CallVoidMethod(env,
229                            defaultDevice,
230                            gdkScreenGraphicsDevice_init);
231
232     return defaultDevice;       
233 }
234
235 JNIEXPORT jintArray JNICALL
236 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_getMouseCoordinates
237 (JNIEnv *env, jobject obj)
238 {
239   jintArray retArray;
240   jint *values;
241   GdkDisplay *display;
242   gint x, y, screenIndex;
243   GdkScreen *screen;
244
245   display = (GdkDisplay *) gtkpeer_get_display(env, obj);
246   g_assert (display != NULL);
247   
248   gdk_threads_enter ();
249   
250   gdk_display_get_pointer (display, &screen, &x, &y, NULL);
251   screenIndex = gdk_screen_get_number( screen );
252
253   gdk_threads_leave ();
254         
255   retArray = (*env)->NewIntArray (env, 3);
256   values = (*env)->GetIntArrayElements (env, retArray, NULL);
257   
258   values[0] = screenIndex;
259   values[1] = x;
260   values[2] = y;
261
262   (*env)->ReleaseIntArrayElements (env, retArray, values, 0);
263
264   return retArray;
265 }
266
267 JNIEXPORT jboolean JNICALL
268 Java_gnu_java_awt_peer_gtk_GdkGraphicsEnvironment_isWindowUnderMouse
269 (JNIEnv *env, jobject obj, jobject windowPeer)
270 {
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;
277
278   display = (GdkDisplay *) gtkpeer_get_display (env, obj);
279   g_assert (display != NULL);
280
281   windowToTest = (GtkWidget *) gtkpeer_get_widget (env, windowPeer);
282
283   gdk_threads_enter ();
284
285   windowAtPointer = gdk_display_get_window_at_pointer (display, &x, &y);
286
287   while (windowAtPointer
288          && windowAtPointer != windowToTest->window)
289     windowAtPointer = gdk_window_get_parent (windowAtPointer);
290
291   gdk_threads_leave ();
292
293   if (windowAtPointer)
294     retVal = JNI_TRUE;
295   else
296     retVal = JNI_FALSE;
297
298   return retVal;
299 }