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_GtkChoicePeer.c
1 /* gtkchoicepeer.c -- Native implementation of GtkChoicePeer
2    Copyright (C) 1998, 1999 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
39 #include "gtkpeer.h"
40 #include "gnu_java_awt_peer_gtk_GtkChoicePeer.h"
41
42 static jmethodID postChoiceItemEventID;
43 static GtkWidget *choice_get_widget (GtkWidget *widget);
44
45 void
46 cp_gtk_choice_init_jni (void)
47 {
48   jclass gtkchoicepeer;
49
50   gtkchoicepeer = (*cp_gtk_gdk_env())->FindClass (cp_gtk_gdk_env(),
51                                         "gnu/java/awt/peer/gtk/GtkChoicePeer");
52
53   postChoiceItemEventID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(), gtkchoicepeer,
54                                                "postChoiceItemEvent",
55                                                "(Ljava/lang/String;I)V");
56 }
57
58 static void selection_changed_cb (GtkComboBox *combobox, jobject peer);
59
60 JNIEXPORT void JNICALL 
61 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_create 
62   (JNIEnv *env, jobject obj)
63 {
64   GtkWidget *combobox;
65   GtkWidget *eventbox;
66   jobject *gref;
67
68   gdk_threads_enter ();
69   
70   NSA_SET_GLOBAL_REF (env, obj);
71   gref = NSA_GET_GLOBAL_REF (env, obj);
72   
73   eventbox = gtk_event_box_new ();
74   combobox = gtk_combo_box_new_text ();
75   gtk_container_add (GTK_CONTAINER (eventbox), combobox);
76   gtk_widget_show (combobox);  
77
78   NSA_SET_PTR (env, obj, eventbox);
79
80   gdk_threads_leave ();
81 }
82
83 JNIEXPORT void JNICALL
84 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_connectSignals
85   (JNIEnv *env, jobject obj)
86 {
87   void *ptr = NULL;
88   jobject *gref = NULL;
89   GtkWidget *bin;
90
91   gdk_threads_enter ();
92
93   ptr = NSA_GET_PTR (env, obj);
94   gref = NSA_GET_GLOBAL_REF (env, obj);
95
96   bin = choice_get_widget (GTK_WIDGET (ptr));
97
98   /* Choice signals */
99   g_signal_connect (G_OBJECT (bin), "changed",
100                     G_CALLBACK (selection_changed_cb), *gref);
101
102   /* Component signals */
103   cp_gtk_component_connect_signals (G_OBJECT (bin), gref);
104
105   gdk_threads_leave ();
106 }
107
108 JNIEXPORT void JNICALL 
109 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_append 
110   (JNIEnv *env, jobject obj, jobjectArray items)
111 {
112   gpointer ptr;
113   jsize count, i;
114   GtkWidget *bin;
115
116   gdk_threads_enter ();
117
118   ptr = NSA_GET_PTR (env, obj);
119   bin = choice_get_widget (GTK_WIDGET (ptr));
120   
121   count = (*env)->GetArrayLength (env, items);
122
123   for (i = 0; i < count; i++) 
124     {
125       jobject item;
126       const char *label;
127
128       item = (*env)->GetObjectArrayElement (env, items, i);
129       label = (*env)->GetStringUTFChars (env, item, NULL);
130
131       gtk_combo_box_append_text (GTK_COMBO_BOX (bin), label);
132
133       (*env)->ReleaseStringUTFChars (env, item, label);
134       (*env)->DeleteLocalRef(env, item);
135     }
136
137   gdk_threads_leave ();
138 }
139
140 JNIEXPORT void JNICALL 
141 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeAdd 
142   (JNIEnv *env, jobject obj, jstring item, jint index)
143 {
144   void *ptr;
145   const char *label;
146   GtkWidget *bin;
147
148   gdk_threads_enter ();
149
150   ptr = NSA_GET_PTR (env, obj);
151   bin = choice_get_widget (GTK_WIDGET (ptr));
152     
153   label = (*env)->GetStringUTFChars (env, item, 0);      
154
155   gtk_combo_box_insert_text (GTK_COMBO_BOX (bin), index, label);
156
157   (*env)->ReleaseStringUTFChars (env, item, label);
158
159   gdk_threads_leave ();
160 }
161
162 JNIEXPORT void JNICALL 
163 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeRemove 
164   (JNIEnv *env, jobject obj, jint index)
165 {
166   void *ptr;
167   GtkWidget *bin;
168   
169   gdk_threads_enter ();
170
171   ptr = NSA_GET_PTR (env, obj);
172   bin = choice_get_widget (GTK_WIDGET (ptr));
173   
174   gtk_combo_box_remove_text (GTK_COMBO_BOX (bin), index);
175
176   gdk_threads_leave ();
177 }
178
179 JNIEXPORT void JNICALL 
180 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeRemoveAll 
181   (JNIEnv *env, jobject obj)
182 {
183   void *ptr;
184   GtkTreeModel *model;
185   GtkWidget *bin;
186   gint count, i;
187
188   gdk_threads_enter ();
189
190   ptr = NSA_GET_PTR (env, obj);
191   bin = choice_get_widget (GTK_WIDGET (ptr));
192   
193   model = gtk_combo_box_get_model (GTK_COMBO_BOX (bin));
194   count = gtk_tree_model_iter_n_children (model, NULL);
195
196   /* First, unselect everything, to avoid problems when removing items. */
197   gtk_combo_box_set_active (GTK_COMBO_BOX (bin), -1);
198
199   for (i = count - 1; i >= 0; i--) {
200     gtk_combo_box_remove_text (GTK_COMBO_BOX (bin), i);
201   }
202
203   gdk_threads_leave ();
204 }
205
206 JNIEXPORT void JNICALL 
207 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_selectNative
208   (JNIEnv *env, jobject obj, jint index)
209 {
210   gdk_threads_enter ();
211
212   Java_gnu_java_awt_peer_gtk_GtkChoicePeer_selectNativeUnlocked
213     (env, obj, index);
214
215   gdk_threads_leave ();
216 }
217
218 JNIEXPORT void JNICALL 
219 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_selectNativeUnlocked
220   (JNIEnv *env, jobject obj, jint index)
221 {
222   void *ptr;
223   GtkWidget *bin;
224   
225   ptr = NSA_GET_PTR (env, obj);
226   bin = choice_get_widget (GTK_WIDGET (ptr));
227   
228   gtk_combo_box_set_active (GTK_COMBO_BOX (bin), index);
229 }
230
231 JNIEXPORT jint JNICALL 
232 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeGetSelected 
233   (JNIEnv *env, jobject obj)
234 {
235   void *ptr;
236   int index;
237   GtkWidget *bin;
238
239   gdk_threads_enter ();
240
241   ptr = NSA_GET_PTR (env, obj);
242   bin = choice_get_widget (GTK_WIDGET (ptr));
243   
244   index = gtk_combo_box_get_active (GTK_COMBO_BOX (bin));
245
246   gdk_threads_leave ();
247
248   return index;
249 }
250
251 static void
252 selection_changed_cb (GtkComboBox *combobox, jobject peer)
253 {
254   jstring label;
255   GtkTreeModel *model;
256   GtkTreeIter iter;
257   gchar *selected;
258   gint index;
259
260   index = gtk_combo_box_get_active(combobox);
261
262   if (index >= 0)
263     {
264       model = gtk_combo_box_get_model (combobox);
265       gtk_combo_box_get_active_iter (combobox, &iter);
266       gtk_tree_model_get (model, &iter, 0, &selected, -1);
267       label = (*cp_gtk_gdk_env())->NewStringUTF (cp_gtk_gdk_env(), selected);
268
269       (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
270                                     postChoiceItemEventID,
271                                     label,
272                                     (jint) AWT_ITEM_SELECTED);
273     }
274 }
275
276 static GtkWidget *
277 choice_get_widget (GtkWidget *widget)
278 {
279   GtkWidget *wid;
280
281   g_assert (GTK_IS_EVENT_BOX (widget));
282   wid = gtk_bin_get_child (GTK_BIN(widget));
283
284   return wid;
285 }