Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkCheckboxPeer.c
blob6c854eff855eb829cc65f98dd089b436a4b5305c
1 /* gtkcheckboxpeer.c -- Native implementation of GtkCheckboxPeer
2 Copyright (C) 1998, 1999, 2002, 2003, 2004 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
46 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_create
47 (JNIEnv *env, jobject obj, jobject group)
49 GtkWidget *button;
51 NSA_SET_GLOBAL_REF (env, obj);
53 gdk_threads_enter ();
55 if (group == NULL)
56 button = gtk_check_button_new_with_label ("");
57 else
59 void *native_group = NSA_GET_PTR (env, group);
60 button = gtk_radio_button_new_with_label_from_widget (native_group, "");
61 if (native_group == NULL)
63 /* Set the native group so we can use the correct value the
64 next time around. FIXME: this doesn't work! */
65 NSA_SET_PTR (env, group, button);
69 gdk_threads_leave ();
71 NSA_SET_PTR (env, obj, button);
74 JNIEXPORT void JNICALL
75 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_connectSignals
76 (JNIEnv *env, jobject obj)
78 void *ptr = NSA_GET_PTR (env, obj);
79 jobject *gref = NSA_GET_GLOBAL_REF (env, obj);
80 g_assert (gref);
82 gdk_threads_enter ();
84 g_signal_connect (G_OBJECT (ptr), "toggled",
85 GTK_SIGNAL_FUNC (item_toggled), *gref);
87 gdk_threads_leave ();
89 /* Connect the superclass signals. */
90 Java_gnu_java_awt_peer_gtk_GtkComponentPeer_connectSignals (env, obj);
93 JNIEXPORT void JNICALL
94 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_nativeSetCheckboxGroup
95 (JNIEnv *env, jobject obj, jobject group)
97 GtkRadioButton *button;
98 void *native_group, *ptr;
100 ptr = NSA_GET_PTR (env, obj);
102 gdk_threads_enter ();
104 /* FIXME: we can't yet switch between a checkbutton and a
105 radiobutton. However, AWT requires this. For now we just
106 crash. */
108 button = GTK_RADIO_BUTTON (ptr);
110 native_group = NSA_GET_PTR (env, group);
111 if (native_group == NULL)
112 gtk_radio_button_set_group (button, NULL);
113 else
114 gtk_radio_button_set_group (button,
115 gtk_radio_button_group
116 (GTK_RADIO_BUTTON (native_group)));
118 gdk_threads_leave ();
120 /* If the native group wasn't set on the new CheckboxGroup, then set
121 it now so that the right thing will happen with the next
122 radiobutton. The native state for a CheckboxGroup is a pointer
123 to one of the widgets in the group. We are careful to keep this
124 always pointing at a live widget; whenever a widget is destroyed
125 (or otherwise removed from the group), the CheckboxGroup peer is
126 notified. */
127 if (native_group == NULL)
128 NSA_SET_PTR (env, group, native_group);
131 JNIEXPORT void JNICALL
132 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkToggleButtonSetActive
133 (JNIEnv *env, jobject obj, jboolean is_active)
135 void *ptr;
137 ptr = NSA_GET_PTR (env, obj);
139 gdk_threads_enter ();
141 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ptr), is_active);
143 gdk_threads_leave ();
146 JNIEXPORT void JNICALL
147 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkWidgetModifyFont
148 (JNIEnv *env, jobject obj, jstring name, jint style, jint size)
150 const char *font_name;
151 void *ptr;
152 GtkWidget *button;
153 GtkWidget *label;
154 PangoFontDescription *font_desc;
156 ptr = NSA_GET_PTR (env, obj);
158 button = GTK_WIDGET (ptr);
159 label = gtk_bin_get_child (GTK_BIN(button));
161 if (!label)
162 return;
164 font_name = (*env)->GetStringUTFChars (env, name, NULL);
166 gdk_threads_enter();
168 font_desc = pango_font_description_from_string (font_name);
169 pango_font_description_set_size (font_desc, size * dpi_conversion_factor);
171 if (style & AWT_STYLE_BOLD)
172 pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
174 if (style & AWT_STYLE_ITALIC)
175 pango_font_description_set_style (font_desc, PANGO_STYLE_OBLIQUE);
177 gtk_widget_modify_font (GTK_WIDGET(label), font_desc);
179 pango_font_description_free (font_desc);
181 gdk_threads_leave();
183 (*env)->ReleaseStringUTFChars (env, name, font_name);
186 JNIEXPORT void JNICALL
187 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkButtonSetLabel
188 (JNIEnv *env, jobject obj, jstring label)
190 const char *c_label;
191 GtkWidget *label_widget;
192 void *ptr;
194 ptr = NSA_GET_PTR (env, obj);
196 c_label = (*env)->GetStringUTFChars (env, label, NULL);
198 gdk_threads_enter ();
200 label_widget = gtk_bin_get_child (GTK_BIN (ptr));
201 gtk_label_set_text (GTK_LABEL (label_widget), c_label);
203 gdk_threads_leave ();
205 (*env)->ReleaseStringUTFChars (env, label, c_label);
208 static void
209 item_toggled (GtkToggleButton *item, jobject peer)
211 (*gdk_env())->CallVoidMethod (gdk_env(), peer,
212 postItemEventID,
213 peer,
214 item->active ?
215 (jint) AWT_ITEM_SELECTED :
216 (jint) AWT_ITEM_DESELECTED);