1 /* gtkcheckboxpeer.c -- Native implementation of GtkCheckboxPeer
2    Copyright (C) 1998, 1999, 2002, 2003, 2004 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
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.
10 
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.
15 
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.
20 
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.
25 
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. */
37 
38 
39 #include "gtkpeer.h"
40 #include "gnu_java_awt_peer_gtk_GtkCheckboxPeer.h"
41 #include "gnu_java_awt_peer_gtk_GtkComponentPeer.h"
42 
43 static jmethodID postItemEventID;
44 
45 void
cp_gtk_checkbox_init_jni(void)46 cp_gtk_checkbox_init_jni (void)
47 {
48   jclass gtkcheckboxpeer;
49 
50   gtkcheckboxpeer = (*cp_gtk_gdk_env())->FindClass (cp_gtk_gdk_env(),
51                                              "gnu/java/awt/peer/gtk/GtkCheckboxPeer");
52 
53   postItemEventID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(), gtkcheckboxpeer,
54                                                "postItemEvent",
55                                                "(Ljava/lang/Object;I)V");
56 }
57 
58 static void item_toggled_cb (GtkToggleButton *item, jobject peer);
59 
60 JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_create(JNIEnv * env,jobject obj,jobject group)61 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_create
62   (JNIEnv *env, jobject obj, jobject group)
63 {
64   GtkWidget *button;
65 
66   gdk_threads_enter ();
67 
68   NSA_SET_GLOBAL_REF (env, obj);
69 
70   if (group == NULL)
71     button = gtk_check_button_new_with_label ("");
72   else
73     {
74       void *native_group = NSA_GET_PTR (env, group);
75       button = gtk_radio_button_new_with_label_from_widget (native_group, "");
76       if (native_group == NULL)
77 	{
78 	  /* Set the native group so we can use the correct value the
79 	     next time around.  FIXME: this doesn't work!  */
80 	  NSA_SET_PTR (env, group, button);
81 	}
82     }
83 
84   NSA_SET_PTR (env, obj, button);
85 
86   gdk_threads_leave ();
87 }
88 
89 JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_connectSignals(JNIEnv * env,jobject obj)90 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_connectSignals
91   (JNIEnv *env, jobject obj)
92 {
93   void *ptr = NULL;
94   jobject *gref = NULL;
95 
96   gdk_threads_enter ();
97 
98   ptr = NSA_GET_PTR (env, obj);
99   gref = NSA_GET_GLOBAL_REF (env, obj);
100 
101   /* Checkbox signals */
102   g_signal_connect (G_OBJECT (ptr), "toggled",
103                     G_CALLBACK (item_toggled_cb), *gref);
104 
105   /* Component signals */
106   cp_gtk_component_connect_signals (G_OBJECT (ptr), gref);
107 
108   gdk_threads_leave ();
109 }
110 
111 JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_nativeSetCheckboxGroup(JNIEnv * env,jobject obj,jobject group)112 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_nativeSetCheckboxGroup
113   (JNIEnv *env, jobject obj, jobject group)
114 {
115   GtkRadioButton *button;
116   void *native_group, *ptr;
117 
118   gdk_threads_enter ();
119 
120   ptr = NSA_GET_PTR (env, obj);
121 
122   /* FIXME: we can't yet switch between a checkbutton and a
123      radiobutton.  However, AWT requires this.  For now we just
124      crash.  */
125 
126   button = GTK_RADIO_BUTTON (ptr);
127 
128   native_group = NSA_GET_PTR (env, group);
129   if (native_group == NULL)
130     gtk_radio_button_set_group (button, NULL);
131   else
132     gtk_radio_button_set_group (button,
133 				gtk_radio_button_get_group
134 				(GTK_RADIO_BUTTON (native_group)));
135 
136   /* If the native group wasn't set on the new CheckboxGroup, then set
137      it now so that the right thing will happen with the next
138      radiobutton.  The native state for a CheckboxGroup is a pointer
139      to one of the widgets in the group.  We are careful to keep this
140      always pointing at a live widget; whenever a widget is destroyed
141      (or otherwise removed from the group), the CheckboxGroup peer is
142      notified.  */
143   if (native_group == NULL)
144     NSA_SET_PTR (env, group, native_group);
145 
146   gdk_threads_leave ();
147 }
148 
149 JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkToggleButtonSetActive(JNIEnv * env,jobject obj,jboolean is_active)150 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkToggleButtonSetActive
151   (JNIEnv *env, jobject obj, jboolean is_active)
152 {
153   void *ptr;
154 
155   gdk_threads_enter ();
156 
157   ptr = NSA_GET_PTR (env, obj);
158 
159   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (ptr), is_active);
160 
161   gdk_threads_leave ();
162 }
163 
164 JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkWidgetModifyFont(JNIEnv * env,jobject obj,jstring name,jint style,jint size)165 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkWidgetModifyFont
166   (JNIEnv *env, jobject obj, jstring name, jint style, jint size)
167 {
168   const char *font_name;
169   void *ptr;
170   GtkWidget *button;
171   GtkWidget *label;
172   PangoFontDescription *font_desc;
173 
174   gdk_threads_enter();
175 
176   ptr = NSA_GET_PTR (env, obj);
177 
178   button = GTK_WIDGET (ptr);
179   label = gtk_bin_get_child (GTK_BIN(button));
180 
181   if (!label)
182     return;
183 
184   font_name = (*env)->GetStringUTFChars (env, name, NULL);
185 
186   font_desc = pango_font_description_from_string (font_name);
187   pango_font_description_set_size (font_desc,
188                                    size * cp_gtk_dpi_conversion_factor);
189 
190   if (style & AWT_STYLE_BOLD)
191     pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
192 
193   if (style & AWT_STYLE_ITALIC)
194     pango_font_description_set_style (font_desc, PANGO_STYLE_OBLIQUE);
195 
196   gtk_widget_modify_font (GTK_WIDGET(label), font_desc);
197 
198   pango_font_description_free (font_desc);
199 
200   (*env)->ReleaseStringUTFChars (env, name, font_name);
201 
202   gdk_threads_leave();
203 }
204 
205 JNIEXPORT void JNICALL
Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkButtonSetLabel(JNIEnv * env,jobject obj,jstring label)206 Java_gnu_java_awt_peer_gtk_GtkCheckboxPeer_gtkButtonSetLabel
207   (JNIEnv *env, jobject obj, jstring label)
208 {
209   const char *c_label;
210   GtkWidget *label_widget;
211   void *ptr;
212 
213   gdk_threads_enter ();
214 
215   ptr = NSA_GET_PTR (env, obj);
216 
217   c_label = (*env)->GetStringUTFChars (env, label, NULL);
218 
219   label_widget = gtk_bin_get_child (GTK_BIN (ptr));
220   gtk_label_set_text (GTK_LABEL (label_widget), c_label);
221 
222   (*env)->ReleaseStringUTFChars (env, label, c_label);
223 
224   gdk_threads_leave ();
225 }
226 
227 static void
item_toggled_cb(GtkToggleButton * item,jobject peer)228 item_toggled_cb (GtkToggleButton *item, jobject peer)
229 {
230   (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
231                                 postItemEventID,
232                                 peer,
233                                 item->active ?
234                                 (jint) AWT_ITEM_SELECTED :
235                                 (jint) AWT_ITEM_DESELECTED);
236 }
237