2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkCheckboxPeer.c
blobb6b329d41d9d1379eb50ecbc39b73cca3187b939
1 /* gtkcheckboxpeer.c -- Native implementation of GtkCheckboxPeer
2 Copyright (C) 1998, 1999, 2002, 2003 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_GtkCheckboxPeer.h"
41 #include "gnu_java_awt_peer_gtk_GtkComponentPeer.h"
43 static void item_toggled (GtkToggleButton *item, jobject peer);
45 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GtkCheckboxGroupPeer_dispose
46 (JNIEnv *env, jobject obj)
48 /* The actual underlying widget is owned by a different class. So
49 we just clean up the hash table here. */
50 NSA_DEL_PTR (env, obj);
53 JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GtkCheckboxGroupPeer_remove
54 (JNIEnv *env, jobject obj, jobject checkbox)
56 GtkRadioButton *button;
57 void *ptr;
58 GSList *list;
60 ptr = NSA_GET_PTR (env, checkbox);
61 gdk_threads_enter ();
62 button = GTK_RADIO_BUTTON (ptr);
64 /* Update the group to point to some other widget in the group. We
65 have to do this because Gtk doesn't have a separate object to
66 represent a radio button's group. */
67 for (list = gtk_radio_button_group (button); list != NULL;
68 list = list->next)
70 if (list->data != button)
71 break;
74 gdk_threads_leave ();
76 NSA_SET_PTR (env, obj, list ? list->data : NULL);
79 JNIEXPORT void JNICALL
80 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_nativeCreate
81 (JNIEnv *env, jobject obj, jobject group, jboolean state)
83 GtkWidget *button;
85 /* Create global reference and save it for future use */
86 NSA_SET_GLOBAL_REF (env, obj);
88 gdk_threads_enter ();
90 if (group == NULL)
91 button = gtk_check_button_new_with_label ("");
92 else
94 void *native_group = NSA_GET_PTR (env, group);
95 button = gtk_radio_button_new_with_label_from_widget (native_group, "");
96 if (native_group == NULL)
98 /* Set the native group so we can use the correct value the
99 next time around. FIXME: this doesn't work! */
100 NSA_SET_PTR (env, group, button);
103 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), state);
105 gdk_threads_leave ();
107 NSA_SET_PTR (env, obj, button);
110 JNIEXPORT void JNICALL
111 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_connectSignals
112 (JNIEnv *env, jobject obj)
114 void *ptr = NSA_GET_PTR (env, obj);
115 jobject *gref = NSA_GET_GLOBAL_REF (env, obj);
116 g_assert (gref);
118 gdk_threads_enter ();
120 g_signal_connect (G_OBJECT (ptr), "toggled",
121 GTK_SIGNAL_FUNC (item_toggled), *gref);
123 gdk_threads_leave ();
125 /* Connect the superclass signals. */
126 Java_gnu_java_awt_peer_gtk_GtkComponentPeer_connectSignals (env, obj);
129 JNIEXPORT void JNICALL
130 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_nativeSetCheckboxGroup
131 (JNIEnv *env, jobject obj, jobject group)
133 GtkRadioButton *button;
134 void *native_group, *ptr;
136 ptr = NSA_GET_PTR (env, obj);
138 gdk_threads_enter ();
140 /* FIXME: we can't yet switch between a checkbutton and a
141 radiobutton. However, AWT requires this. For now we just
142 crash. */
144 button = GTK_RADIO_BUTTON (ptr);
146 native_group = NSA_GET_PTR (env, group);
147 if (native_group == NULL)
148 gtk_radio_button_set_group (button, NULL);
149 else
150 gtk_radio_button_set_group (button,
151 gtk_radio_button_group
152 (GTK_RADIO_BUTTON (native_group)));
154 gdk_threads_leave ();
156 /* If the native group wasn't set on the new CheckboxGroup, then set
157 it now so that the right thing will happen with the next
158 radiobutton. The native state for a CheckboxGroup is a pointer
159 to one of the widgets in the group. We are careful to keep this
160 always pointing at a live widget; whenever a widget is destroyed
161 (or otherwise removed from the group), the CheckboxGroup peer is
162 notified. */
163 if (native_group == NULL)
164 NSA_SET_PTR (env, group, native_group);
167 static void
168 item_toggled (GtkToggleButton *item, jobject peer)
170 //g_print ("toggled\n");
171 (*gdk_env)->CallVoidMethod (gdk_env, peer,
172 postItemEventID,
173 peer,
174 item->active ?
175 (jint) AWT_ITEM_SELECTED :
176 (jint) AWT_ITEM_DESELECTED);