Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkChoicePeer.c
blob46949d99a84cf25b0e6fd3d48f724b059e1008af
1 /* gtkchoicepeer.c -- Native implementation of GtkChoicePeer
2 Copyright (C) 1998, 1999 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. */
39 #include "gtkpeer.h"
40 #include "gnu_java_awt_peer_gtk_GtkChoicePeer.h"
42 static jmethodID postChoiceItemEventID;
43 static GtkWidget *choice_get_widget (GtkWidget *widget);
45 void
46 cp_gtk_choice_init_jni (void)
48 jclass gtkchoicepeer;
50 gtkchoicepeer = (*cp_gtk_gdk_env())->FindClass (cp_gtk_gdk_env(),
51 "gnu/java/awt/peer/gtk/GtkChoicePeer");
53 postChoiceItemEventID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(), gtkchoicepeer,
54 "postChoiceItemEvent",
55 "(Ljava/lang/String;I)V");
58 static void selection_changed_cb (GtkComboBox *combobox, jobject peer);
60 JNIEXPORT void JNICALL
61 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_create
62 (JNIEnv *env, jobject obj)
64 GtkWidget *combobox;
65 GtkWidget *eventbox;
66 jobject *gref;
68 gdk_threads_enter ();
70 NSA_SET_GLOBAL_REF (env, obj);
71 gref = NSA_GET_GLOBAL_REF (env, obj);
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);
78 NSA_SET_PTR (env, obj, eventbox);
80 gdk_threads_leave ();
83 JNIEXPORT void JNICALL
84 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_connectSignals
85 (JNIEnv *env, jobject obj)
87 void *ptr = NULL;
88 jobject *gref = NULL;
89 GtkWidget *bin;
91 gdk_threads_enter ();
93 ptr = NSA_GET_PTR (env, obj);
94 gref = NSA_GET_GLOBAL_REF (env, obj);
96 bin = choice_get_widget (GTK_WIDGET (ptr));
98 /* Choice signals */
99 g_signal_connect (G_OBJECT (bin), "changed",
100 G_CALLBACK (selection_changed_cb), *gref);
102 /* Component signals */
103 cp_gtk_component_connect_signals (G_OBJECT (bin), gref);
105 gdk_threads_leave ();
108 JNIEXPORT void JNICALL
109 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_append
110 (JNIEnv *env, jobject obj, jobjectArray items)
112 gpointer ptr;
113 jsize count, i;
114 GtkWidget *bin;
116 gdk_threads_enter ();
118 ptr = NSA_GET_PTR (env, obj);
119 bin = choice_get_widget (GTK_WIDGET (ptr));
121 count = (*env)->GetArrayLength (env, items);
123 for (i = 0; i < count; i++)
125 jobject item;
126 const char *label;
128 item = (*env)->GetObjectArrayElement (env, items, i);
129 label = (*env)->GetStringUTFChars (env, item, NULL);
131 gtk_combo_box_append_text (GTK_COMBO_BOX (bin), label);
133 (*env)->ReleaseStringUTFChars (env, item, label);
134 (*env)->DeleteLocalRef(env, item);
137 gdk_threads_leave ();
140 JNIEXPORT void JNICALL
141 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeAdd
142 (JNIEnv *env, jobject obj, jstring item, jint index)
144 void *ptr;
145 const char *label;
146 GtkWidget *bin;
148 gdk_threads_enter ();
150 ptr = NSA_GET_PTR (env, obj);
151 bin = choice_get_widget (GTK_WIDGET (ptr));
153 label = (*env)->GetStringUTFChars (env, item, 0);
155 gtk_combo_box_insert_text (GTK_COMBO_BOX (bin), index, label);
157 (*env)->ReleaseStringUTFChars (env, item, label);
159 gdk_threads_leave ();
162 JNIEXPORT void JNICALL
163 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeRemove
164 (JNIEnv *env, jobject obj, jint index)
166 void *ptr;
167 GtkWidget *bin;
169 gdk_threads_enter ();
171 ptr = NSA_GET_PTR (env, obj);
172 bin = choice_get_widget (GTK_WIDGET (ptr));
174 gtk_combo_box_remove_text (GTK_COMBO_BOX (bin), index);
176 gdk_threads_leave ();
179 JNIEXPORT void JNICALL
180 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeRemoveAll
181 (JNIEnv *env, jobject obj)
183 void *ptr;
184 GtkTreeModel *model;
185 GtkWidget *bin;
186 gint count, i;
188 gdk_threads_enter ();
190 ptr = NSA_GET_PTR (env, obj);
191 bin = choice_get_widget (GTK_WIDGET (ptr));
193 model = gtk_combo_box_get_model (GTK_COMBO_BOX (bin));
194 count = gtk_tree_model_iter_n_children (model, NULL);
196 /* First, unselect everything, to avoid problems when removing items. */
197 gtk_combo_box_set_active (GTK_COMBO_BOX (bin), -1);
199 for (i = count - 1; i >= 0; i--) {
200 gtk_combo_box_remove_text (GTK_COMBO_BOX (bin), i);
203 gdk_threads_leave ();
206 JNIEXPORT void JNICALL
207 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_selectNative
208 (JNIEnv *env, jobject obj, jint index)
210 gdk_threads_enter ();
212 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_selectNativeUnlocked
213 (env, obj, index);
215 gdk_threads_leave ();
218 JNIEXPORT void JNICALL
219 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_selectNativeUnlocked
220 (JNIEnv *env, jobject obj, jint index)
222 void *ptr;
223 GtkWidget *bin;
225 ptr = NSA_GET_PTR (env, obj);
226 bin = choice_get_widget (GTK_WIDGET (ptr));
228 gtk_combo_box_set_active (GTK_COMBO_BOX (bin), index);
231 JNIEXPORT jint JNICALL
232 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeGetSelected
233 (JNIEnv *env, jobject obj)
235 void *ptr;
236 int index;
237 GtkWidget *bin;
239 gdk_threads_enter ();
241 ptr = NSA_GET_PTR (env, obj);
242 bin = choice_get_widget (GTK_WIDGET (ptr));
244 index = gtk_combo_box_get_active (GTK_COMBO_BOX (bin));
246 gdk_threads_leave ();
248 return index;
251 static void
252 selection_changed_cb (GtkComboBox *combobox, jobject peer)
254 jstring label;
255 GtkTreeModel *model;
256 GtkTreeIter iter;
257 gchar *selected;
258 gint index;
260 index = gtk_combo_box_get_active(combobox);
262 if (index >= 0)
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);
269 (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
270 postChoiceItemEventID,
271 label,
272 (jint) AWT_ITEM_SELECTED);
276 static GtkWidget *
277 choice_get_widget (GtkWidget *widget)
279 GtkWidget *wid;
281 g_assert (GTK_IS_EVENT_BOX (widget));
282 wid = gtk_bin_get_child (GTK_BIN(widget));
284 return wid;