Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkCheckboxPeer.c
blob191cdda8fcc345055a67c7971551186244f5e45b
1 /* gtkcheckboxpeer.c -- Native implementation of GtkCheckboxPeer
2 Copyright (C) 1998, 1999, 2002, 2003, 2004, 2006
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 #include "gtkpeer.h"
41 #include "gnu_java_awt_peer_gtk_GtkCheckboxPeer.h"
42 #include "gnu_java_awt_peer_gtk_GtkComponentPeer.h"
44 static jmethodID postItemEventID;
45 static GtkWidget *combobox_get_widget (GtkWidget *widget);
47 void
48 cp_gtk_checkbox_init_jni (void)
50 jclass gtkcheckboxpeer;
52 gtkcheckboxpeer = (*cp_gtk_gdk_env())->FindClass (cp_gtk_gdk_env(),
53 "gnu/java/awt/peer/gtk/GtkCheckboxPeer");
55 postItemEventID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(), gtkcheckboxpeer,
56 "postItemEvent",
57 "(Ljava/lang/Object;Z)V");
60 static void item_toggled_cb (GtkToggleButton *item, jobject peer);
62 JNIEXPORT void JNICALL
63 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_create
64 (JNIEnv *env, jobject obj, jobject group)
66 GtkWidget *button;
67 GtkWidget *eventbox;
69 gdk_threads_enter ();
71 NSA_SET_GLOBAL_REF (env, obj);
72 eventbox = gtk_event_box_new ();
74 if (group == NULL)
76 button = gtk_check_button_new_with_label ("");
77 gtk_container_add (GTK_CONTAINER (eventbox), button);
78 gtk_widget_show (button);
80 else
82 void *native_group = NSA_GET_PTR (env, group);
83 button = gtk_radio_button_new_with_label_from_widget (native_group, "");
84 gtk_container_add (GTK_CONTAINER (eventbox), button);
85 gtk_widget_show (button);
87 if (native_group == NULL)
89 /* Set the native group so we can use the correct value the
90 next time around. FIXME: this doesn't work! */
91 NSA_SET_PTR (env, group, button);
95 NSA_SET_PTR (env, obj, eventbox);
97 gdk_threads_leave ();
100 JNIEXPORT void JNICALL
101 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_connectSignals
102 (JNIEnv *env, jobject obj)
104 void *ptr = NULL;
105 jobject *gref = NULL;
106 GtkWidget *bin;
108 gdk_threads_enter ();
110 ptr = NSA_GET_PTR (env, obj);
111 gref = NSA_GET_GLOBAL_REF (env, obj);
112 bin = combobox_get_widget (GTK_WIDGET (ptr));
114 /* Checkbox signals */
115 g_signal_connect (G_OBJECT (bin), "toggled",
116 G_CALLBACK (item_toggled_cb), *gref);
118 /* Component signals */
119 cp_gtk_component_connect_signals (G_OBJECT (bin), gref);
121 gdk_threads_leave ();
124 JNIEXPORT void JNICALL
125 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_nativeSetCheckboxGroup
126 (JNIEnv *env, jobject obj, jobject group)
128 GtkRadioButton *button;
129 void *native_group, *ptr;
130 GtkWidget *bin;
132 gdk_threads_enter ();
134 ptr = NSA_GET_PTR (env, obj);
135 bin = combobox_get_widget (GTK_WIDGET (ptr));
137 /* FIXME: we can't yet switch between a checkbutton and a
138 radiobutton. However, AWT requires this. For now we just
139 crash. */
141 button = GTK_RADIO_BUTTON (bin);
143 native_group = NSA_GET_PTR (env, group);
144 if (native_group == NULL)
145 gtk_radio_button_set_group (button, NULL);
146 else
147 gtk_radio_button_set_group (button,
148 gtk_radio_button_get_group
149 (GTK_RADIO_BUTTON (native_group)));
151 /* If the native group wasn't set on the new CheckboxGroup, then set
152 it now so that the right thing will happen with the next
153 radiobutton. The native state for a CheckboxGroup is a pointer
154 to one of the widgets in the group. We are careful to keep this
155 always pointing at a live widget; whenever a widget is destroyed
156 (or otherwise removed from the group), the CheckboxGroup peer is
157 notified. */
158 if (native_group == NULL)
159 NSA_SET_PTR (env, group, native_group);
161 gdk_threads_leave ();
164 JNIEXPORT void JNICALL
165 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkToggleButtonSetActive
166 (JNIEnv *env, jobject obj, jboolean is_active)
168 void *ptr;
169 GtkWidget *bin;
171 gdk_threads_enter ();
173 ptr = NSA_GET_PTR (env, obj);
174 bin = combobox_get_widget (GTK_WIDGET (ptr));
176 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (bin), is_active);
178 gdk_threads_leave ();
181 JNIEXPORT void JNICALL
182 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkWidgetModifyFont
183 (JNIEnv *env, jobject obj, jstring name, jint style, jint size)
185 const char *font_name;
186 void *ptr;
187 GtkWidget *button;
188 GtkWidget *label;
189 PangoFontDescription *font_desc;
191 gdk_threads_enter();
193 ptr = NSA_GET_PTR (env, obj);
195 button = combobox_get_widget (GTK_WIDGET (ptr));
196 label = gtk_bin_get_child (GTK_BIN(button));
198 if (!label)
199 return;
201 font_name = (*env)->GetStringUTFChars (env, name, NULL);
203 font_desc = pango_font_description_from_string (font_name);
204 pango_font_description_set_size (font_desc,
205 size * cp_gtk_dpi_conversion_factor);
207 if (style & AWT_STYLE_BOLD)
208 pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
210 if (style & AWT_STYLE_ITALIC)
211 pango_font_description_set_style (font_desc, PANGO_STYLE_OBLIQUE);
213 gtk_widget_modify_font (GTK_WIDGET(label), font_desc);
215 pango_font_description_free (font_desc);
217 (*env)->ReleaseStringUTFChars (env, name, font_name);
219 gdk_threads_leave();
222 JNIEXPORT void JNICALL
223 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkButtonSetLabel
224 (JNIEnv *env, jobject obj, jstring label)
226 const char *c_label;
227 GtkWidget *label_widget;
228 void *ptr;
230 gdk_threads_enter ();
232 ptr = NSA_GET_PTR (env, obj);
234 c_label = (*env)->GetStringUTFChars (env, label, NULL);
236 label_widget = gtk_bin_get_child (GTK_BIN (combobox_get_widget (GTK_WIDGET (ptr))));
237 gtk_label_set_text (GTK_LABEL (label_widget), c_label);
239 (*env)->ReleaseStringUTFChars (env, label, c_label);
241 gdk_threads_leave ();
244 static void
245 item_toggled_cb (GtkToggleButton *item, jobject peer)
247 (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
248 postItemEventID,
249 peer,
250 item->active);
253 static GtkWidget *
254 combobox_get_widget (GtkWidget *widget)
256 GtkWidget *wid;
258 g_assert (GTK_IS_EVENT_BOX (widget));
259 wid = gtk_bin_get_child (GTK_BIN(widget));
261 return wid;