Merge from the pain train
[official-gcc.git] / libjava / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkChoicePeer.c
blobb4e64e62d0265bd6c4336a6ea873c17a02a3f189
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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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 void selection_changed (GtkComboBox *combobox, jobject peer);
44 JNIEXPORT void JNICALL
45 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_create
46 (JNIEnv *env, jobject obj)
48 GtkWidget *combobox;
49 jobject *gref;
51 NSA_SET_GLOBAL_REF (env, obj);
52 gref = NSA_GET_GLOBAL_REF (env, obj);
54 gdk_threads_enter ();
56 combobox = gtk_combo_box_new_text ();
58 g_signal_connect (combobox, "changed",
59 G_CALLBACK (selection_changed), *gref);
61 gdk_threads_leave ();
63 NSA_SET_PTR (env, obj, combobox);
66 JNIEXPORT void JNICALL
67 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_append
68 (JNIEnv *env, jobject obj, jobjectArray items)
70 gpointer ptr;
71 jsize count, i;
73 ptr = NSA_GET_PTR (env, obj);
75 gdk_threads_enter ();
77 count = (*env)->GetArrayLength (env, items);
79 for (i = 0; i < count; i++)
81 jobject item;
82 const char *label;
84 item = (*env)->GetObjectArrayElement (env, items, i);
85 label = (*env)->GetStringUTFChars (env, item, NULL);
87 gtk_combo_box_append_text (GTK_COMBO_BOX (ptr), label);
89 (*env)->ReleaseStringUTFChars (env, item, label);
92 gdk_threads_leave ();
95 JNIEXPORT void JNICALL
96 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeAdd
97 (JNIEnv *env, jobject obj, jstring item, jint index)
99 void *ptr;
100 const char *label;
102 ptr = NSA_GET_PTR (env, obj);
104 label = (*env)->GetStringUTFChars (env, item, 0);
106 gdk_threads_enter ();
107 gtk_combo_box_insert_text (GTK_COMBO_BOX (ptr), index, label);
108 gdk_threads_leave ();
110 (*env)->ReleaseStringUTFChars (env, item, label);
113 JNIEXPORT void JNICALL
114 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeRemove
115 (JNIEnv *env, jobject obj, jint index)
117 void *ptr;
119 ptr = NSA_GET_PTR (env, obj);
121 gdk_threads_enter ();
122 gtk_combo_box_remove_text (GTK_COMBO_BOX (ptr), index);
123 gdk_threads_leave ();
126 JNIEXPORT void JNICALL
127 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeRemoveAll
128 (JNIEnv *env, jobject obj)
130 void *ptr;
131 GtkTreeModel *model;
132 gint count, i;
134 ptr = NSA_GET_PTR (env, obj);
136 gdk_threads_enter ();
138 model = gtk_combo_box_get_model (GTK_COMBO_BOX (ptr));
139 count = gtk_tree_model_iter_n_children (model, NULL);
141 /* First, unselect everything, to avoid problems when removing items. */
142 gtk_combo_box_set_active (GTK_COMBO_BOX (ptr), -1);
144 for (i = count - 1; i >= 0; i--) {
145 gtk_combo_box_remove_text (GTK_COMBO_BOX (ptr), i);
148 gdk_threads_leave ();
151 JNIEXPORT void JNICALL
152 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_select
153 (JNIEnv *env, jobject obj, jint index)
155 void *ptr;
157 ptr = NSA_GET_PTR (env, obj);
159 gdk_threads_enter ();
160 gtk_combo_box_set_active (GTK_COMBO_BOX (ptr), index);
161 gdk_threads_leave ();
164 JNIEXPORT jint JNICALL
165 Java_gnu_java_awt_peer_gtk_GtkChoicePeer_nativeGetSelected
166 (JNIEnv *env, jobject obj)
168 void *ptr;
169 int index;
171 ptr = NSA_GET_PTR (env, obj);
173 gdk_threads_enter ();
174 index = gtk_combo_box_get_active (GTK_COMBO_BOX (ptr));
175 gdk_threads_leave ();
177 return index;
180 static void selection_changed (GtkComboBox *combobox, jobject peer)
182 jstring label;
183 GtkTreeModel *model;
184 GtkTreeIter iter;
185 gchar *selected;
186 gint index;
188 index = gtk_combo_box_get_active(combobox);
190 if (index >= 0)
192 model = gtk_combo_box_get_model (combobox);
194 gtk_combo_box_get_active_iter (combobox, &iter);
196 gtk_tree_model_get (model, &iter, 0, &selected, -1);
198 gdk_threads_leave ();
200 label = (*gdk_env())->NewStringUTF (gdk_env(), selected);
201 (*gdk_env())->CallVoidMethod (gdk_env(), peer,
202 choicePostItemEventID,
203 label,
204 (jint) AWT_ITEM_SELECTED);
205 gdk_threads_enter ();