OSDN Git Service

2003-10-02 Thomas Fitzsimmons <fitzsim@redhat.com>
[pf3gnuchains/gcc-fork.git] / libjava / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkMainThread.c
1 /* gtkmainthread.c -- Native implementation of GtkMainThread
2    Copyright (C) 1998, 1999, 2002 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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
39 #include "gtkpeer.h"
40 #include "gnu_java_awt_peer_gtk_GtkMainThread.h"
41 #include "gthread-jni.h"
42
43 #ifdef JVM_SUN
44   struct state_table *native_state_table;
45 #endif
46
47 jmethodID setBoundsCallbackID;
48
49 jmethodID postActionEventID;
50 jmethodID postMenuActionEventID;
51 jmethodID postMouseEventID;
52 jmethodID postConfigureEventID;
53 jmethodID postExposeEventID;
54 jmethodID postKeyEventID;
55 jmethodID postFocusEventID;
56 jmethodID postAdjustmentEventID;
57 jmethodID postItemEventID;
58 jmethodID postListItemEventID;
59 jmethodID postTextEventID;
60 JNIEnv *gdk_env;
61
62 #ifdef PORTABLE_NATIVE_SYNC
63 JavaVM *gdk_vm;
64 #endif
65
66 /*
67  * Call gtk_init.  It is very important that this happen before any other
68  * gtk calls.
69  */
70
71 JNIEXPORT void JNICALL 
72 Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkInit (JNIEnv *env, jclass clazz)
73 {
74   int argc = 1;
75   char **argv;
76   char *homedir, *rcpath = NULL;
77 /*    jclass gtkgenericpeer; */
78   jclass gtkcomponentpeer, gtkwindowpeer, gtkscrollbarpeer, gtklistpeer,
79     gtkmenuitempeer, gtktextcomponentpeer, window;
80
81   NSA_INIT (env, clazz);
82
83   /* GTK requires a program's argc and argv variables, and requires that they
84      be valid.  */
85
86   argv = (char **) malloc (sizeof (char *) * 2);
87   argv[0] = "";
88   argv[1] = NULL;
89
90   /* until we have JDK 1.2 JNI, assume we have a VM with threads that 
91      match what GLIB was compiled for */
92 #ifdef PORTABLE_NATIVE_SYNC
93   (*env)->GetJavaVM( env, &gdk_vm );
94   g_thread_init ( &g_thread_jni_functions );
95   printf("called gthread init\n");
96 #else
97   g_thread_init ( NULL );
98 #endif
99
100   /* From GDK 2.0 onwards we have to explicitly call gdk_threads_init */
101   gdk_threads_init();
102
103   gtk_init (&argc, &argv);
104
105   gdk_rgb_init ();
106   gtk_widget_set_default_colormap (gdk_rgb_get_cmap ());
107   gtk_widget_set_default_visual (gdk_rgb_get_visual ());
108
109   /* Make sure queued calls don't get sent to GTK/GDK while 
110      we're shutting down. */
111   atexit (gdk_threads_enter);
112
113   gdk_env = env;
114   gdk_event_handler_set ((GdkEventFunc)awt_event_handler, NULL, NULL);
115
116   if ((homedir = getenv ("HOME")))
117     {
118       rcpath = (char *) malloc (strlen (homedir) + strlen (RC_FILE) + 2);
119       sprintf (rcpath, "%s/%s", homedir, RC_FILE);
120     }
121   
122   gtk_rc_parse ((rcpath) ? rcpath : RC_FILE);
123
124   if (rcpath)
125     free (rcpath);
126
127   free (argv);
128
129   /* setup cached IDs for posting GTK events to Java */
130 /*    gtkgenericpeer = (*env)->FindClass (env,  */
131 /*                                    "gnu/java/awt/peer/gtk/GtkGenericPeer"); */
132
133   window = (*env)->FindClass (env, "java/awt/Window");
134
135   gtkcomponentpeer = (*env)->FindClass (env,
136                                      "gnu/java/awt/peer/gtk/GtkComponentPeer");
137   gtkwindowpeer = (*env)->FindClass (env,
138                                      "gnu/java/awt/peer/gtk/GtkWindowPeer");
139   gtkscrollbarpeer = (*env)->FindClass (env, 
140                                      "gnu/java/awt/peer/gtk/GtkScrollbarPeer");
141   gtklistpeer = (*env)->FindClass (env, "gnu/java/awt/peer/gtk/GtkListPeer");
142   gtkmenuitempeer = (*env)->FindClass (env,
143                                      "gnu/java/awt/peer/gtk/GtkMenuItemPeer");
144   gtktextcomponentpeer = (*env)->FindClass (env,
145                                      "gnu/java/awt/peer/gtk/GtkTextComponentPeer");
146 /*    gdkColor = (*env)->FindClass (env, */
147 /*                              "gnu/java/awt/peer/gtk/GdkColor"); */
148 /*    gdkColorID = (*env)->GetMethodID (env, gdkColor, "<init>", "(III)V"); */
149 /*    postActionEventID = (*env)->GetMethodID (env, gtkgenericpeer,  */
150 /*                                         "postActionEvent",  */
151 /*                                         "(Ljava/lang/String;I)V"); */
152
153   setBoundsCallbackID = (*env)->GetMethodID (env, window,
154                                              "setBoundsCallback",
155                                              "(IIII)V");
156
157   postMenuActionEventID = (*env)->GetMethodID (env, gtkmenuitempeer,
158                                                "postMenuActionEvent",
159                                                "()V");
160   postMouseEventID = (*env)->GetMethodID (env, gtkcomponentpeer, 
161                                           "postMouseEvent", "(IJIIIIZ)V");
162   postConfigureEventID = (*env)->GetMethodID (env, gtkwindowpeer, 
163                                           "postConfigureEvent", "(IIIIIIII)V");
164   postExposeEventID = (*env)->GetMethodID (env, gtkcomponentpeer, 
165                                           "postExposeEvent", "(IIII)V");
166   postKeyEventID = (*env)->GetMethodID (env, gtkcomponentpeer,
167                                         "postKeyEvent", "(IJIICI)V");
168   postFocusEventID = (*env)->GetMethodID (env, gtkcomponentpeer,
169                                           "postFocusEvent", "(IZ)V");
170   postAdjustmentEventID = (*env)->GetMethodID (env, gtkscrollbarpeer,
171                                                "postAdjustmentEvent", 
172                                                "(II)V");
173   postItemEventID = (*env)->GetMethodID (env, gtkcomponentpeer,
174                                          "postItemEvent", 
175                                          "(Ljava/lang/Object;I)V");
176   postListItemEventID = (*env)->GetMethodID (env, gtklistpeer,
177                                              "postItemEvent",
178                                              "(II)V");
179   postTextEventID = (*env)->GetMethodID (env, gtktextcomponentpeer,
180                                              "postTextEvent",
181                                              "()V");
182 }
183
184 /*
185  * Run gtk_main and block.
186  */ 
187 JNIEXPORT void JNICALL 
188 Java_gnu_java_awt_peer_gtk_GtkMainThread_gtkMain (JNIEnv *env, jobject obj)
189 {
190   gdk_threads_enter ();
191   gtk_main ();
192   gdk_threads_leave ();
193 }