Merge from the pain train
[official-gcc.git] / libjava / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkButtonPeer.c
blob94e98bf965a06d2885684a28c81bae4f9ada2367
1 /* gtkbuttonpeer.c -- Native implementation of GtkButtonPeer
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_GtkButtonPeer.h"
42 static gboolean focus_in_cb (GtkWidget *widget,
43 GdkEventFocus *event,
44 jobject peer);
45 static gboolean focus_out_cb (GtkWidget *widget,
46 GdkEventFocus *event,
47 jobject peer);
49 static void block_expose_events_cb (GtkWidget *widget,
50 jobject peer);
52 JNIEXPORT void JNICALL
53 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_create
54 (JNIEnv *env, jobject obj, jstring label)
56 const char *c_label;
57 GtkWidget *eventbox;
58 GtkWidget *button;
60 NSA_SET_GLOBAL_REF (env, obj);
62 c_label = (*env)->GetStringUTFChars (env, label, NULL);
64 gdk_threads_enter ();
66 eventbox = gtk_event_box_new ();
67 button = gtk_button_new_with_label (c_label);
68 gtk_container_add (GTK_CONTAINER (eventbox), button);
69 gtk_widget_show (button);
71 gdk_threads_leave ();
73 (*env)->ReleaseStringUTFChars (env, label, c_label);
74 NSA_SET_PTR (env, obj, eventbox);
77 JNIEXPORT void JNICALL
78 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_connectSignals
79 (JNIEnv *env, jobject obj)
81 void *ptr;
82 jobject *gref;
83 GtkWidget *button;
85 ptr = NSA_GET_PTR (env, obj);
86 gref = NSA_GET_GLOBAL_REF (env, obj);
88 gdk_threads_enter ();
90 button = gtk_bin_get_child (GTK_BIN (ptr));
92 g_signal_connect (G_OBJECT (ptr), "event",
93 G_CALLBACK (pre_event_handler), *gref);
95 g_signal_connect (G_OBJECT (button), "event",
96 G_CALLBACK (pre_event_handler), *gref);
98 g_signal_connect (G_OBJECT (button), "focus-in-event",
99 G_CALLBACK (focus_in_cb), *gref);
101 g_signal_connect (G_OBJECT (button), "focus-out-event",
102 G_CALLBACK (focus_out_cb), *gref);
104 g_signal_connect_after (G_OBJECT (button), "pressed",
105 G_CALLBACK (block_expose_events_cb), *gref);
107 g_signal_connect_after (G_OBJECT (button), "released",
108 G_CALLBACK (block_expose_events_cb), *gref);
110 gdk_threads_leave ();
113 JNIEXPORT void JNICALL
114 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkSetLabel
115 (JNIEnv *env, jobject obj, jstring jtext)
117 const char *text;
118 GtkWidget *button;
119 GtkWidget *label;
120 void *ptr;
122 ptr = NSA_GET_PTR (env, obj);
124 text = (*env)->GetStringUTFChars (env, jtext, NULL);
126 gdk_threads_enter ();
128 button = gtk_bin_get_child (GTK_BIN (ptr));
129 label = gtk_bin_get_child (GTK_BIN (button));
130 gtk_label_set_text (GTK_LABEL (label), text);
132 gdk_threads_leave ();
134 (*env)->ReleaseStringUTFChars (env, jtext, text);
137 JNIEXPORT void JNICALL
138 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetModifyFont
139 (JNIEnv *env, jobject obj, jstring name, jint style, jint size)
141 const char *font_name;
142 void *ptr;
143 GtkWidget *button;
144 GtkWidget *label;
145 PangoFontDescription *font_desc;
147 ptr = NSA_GET_PTR (env, obj);
149 font_name = (*env)->GetStringUTFChars (env, name, NULL);
151 gdk_threads_enter();
153 button = gtk_bin_get_child (GTK_BIN (ptr));
154 label = gtk_bin_get_child (GTK_BIN (button));
156 font_desc = pango_font_description_from_string (font_name);
157 pango_font_description_set_size (font_desc, size * dpi_conversion_factor);
159 if (style & AWT_STYLE_BOLD)
160 pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
162 if (style & AWT_STYLE_ITALIC)
163 pango_font_description_set_style (font_desc, PANGO_STYLE_OBLIQUE);
165 gtk_widget_modify_font (GTK_WIDGET(label), font_desc);
167 pango_font_description_free (font_desc);
169 gdk_threads_leave();
171 (*env)->ReleaseStringUTFChars (env, name, font_name);
174 JNIEXPORT void JNICALL
175 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetSetBackground
176 (JNIEnv *env, jobject obj, jint red, jint green, jint blue)
178 GdkColor normal_color;
179 GdkColor prelight_color;
180 GdkColor active_color;
181 int prelight_red;
182 int prelight_blue;
183 int prelight_green;
184 GtkWidget *button;
185 void *ptr;
187 ptr = NSA_GET_PTR (env, obj);
189 normal_color.red = (red / 255.0) * 65535;
190 normal_color.green = (green / 255.0) * 65535;
191 normal_color.blue = (blue / 255.0) * 65535;
193 /* This calculation only approximate the active color produced by
194 Sun's AWT. */
195 active_color.red = 0.85 * (red / 255.0) * 65535;
196 active_color.green = 0.85 * (green / 255.0) * 65535;
197 active_color.blue = 0.85 * (blue / 255.0) * 65535;
199 /* There is no separate prelight color in Motif. */
200 prelight_red = 1.15 * (red / 255.0) * 65535;
201 prelight_green = 1.15 * (green / 255.0) * 65535;
202 prelight_blue = 1.15 * (blue / 255.0) * 65535;
204 prelight_color.red = prelight_red > 65535 ? 65535 : prelight_red;
205 prelight_color.green = prelight_green > 65535 ? 65535 : prelight_green;
206 prelight_color.blue = prelight_blue > 65535 ? 65535 : prelight_blue;
208 gdk_threads_enter ();
210 button = gtk_bin_get_child (GTK_BIN (ptr));
212 gtk_widget_modify_bg (button, GTK_STATE_NORMAL, &normal_color);
213 gtk_widget_modify_bg (button, GTK_STATE_ACTIVE, &active_color);
214 gtk_widget_modify_bg (button, GTK_STATE_PRELIGHT, &prelight_color);
216 gdk_threads_leave ();
219 JNIEXPORT void JNICALL
220 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetSetForeground
221 (JNIEnv *env, jobject obj, jint red, jint green, jint blue)
223 GdkColor color;
224 GtkWidget *button;
225 GtkWidget *label;
226 void *ptr;
228 ptr = NSA_GET_PTR (env, obj);
230 color.red = (red / 255.0) * 65535;
231 color.green = (green / 255.0) * 65535;
232 color.blue = (blue / 255.0) * 65535;
234 gdk_threads_enter ();
236 button = gtk_bin_get_child (GTK_BIN (ptr));
237 label = gtk_bin_get_child (GTK_BIN (button));
239 gtk_widget_modify_fg (label, GTK_STATE_NORMAL, &color);
240 gtk_widget_modify_fg (label, GTK_STATE_ACTIVE, &color);
241 gtk_widget_modify_fg (label, GTK_STATE_PRELIGHT, &color);
243 gdk_threads_leave ();
246 JNIEXPORT void JNICALL
247 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkActivate
248 (JNIEnv *env, jobject obj)
250 GtkWidget *button;
251 void *ptr;
253 ptr = NSA_GET_PTR (env, obj);
255 gdk_threads_enter ();
257 button = gtk_bin_get_child (GTK_BIN (ptr));
258 gtk_widget_activate (GTK_WIDGET (button));
260 gdk_threads_leave ();
263 JNIEXPORT void JNICALL
264 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetRequestFocus
265 (JNIEnv *env, jobject obj)
267 void *ptr;
268 GtkWidget *button;
270 ptr = NSA_GET_PTR (env, obj);
272 gdk_threads_enter ();
273 button = gtk_bin_get_child (GTK_BIN (ptr));
274 gtk_widget_grab_focus (button);
275 gdk_threads_leave ();
278 JNIEXPORT void JNICALL
279 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_setNativeBounds
280 (JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
282 GtkWidget *widget, *child;
283 void *ptr;
285 ptr = NSA_GET_PTR (env, obj);
287 gdk_threads_enter ();
289 widget = GTK_WIDGET (ptr);
291 /* We assume that -1 is a width or height and not a request for the
292 widget's natural size. */
293 width = width < 0 ? 0 : width;
294 height = height < 0 ? 0 : height;
295 child = gtk_bin_get_child (GTK_BIN (widget));
297 if (!(width == 0 && height == 0))
299 /* Set the event box's size request... */
300 gtk_widget_set_size_request (widget, width, height);
301 /* ...and the button's size request... */
302 gtk_widget_set_size_request (child, width, height);
303 /* ...and the label's size request. */
304 gtk_widget_set_size_request (gtk_bin_get_child (GTK_BIN (child)), width,
305 height);
306 if (widget->parent != NULL)
307 gtk_fixed_move (GTK_FIXED (widget->parent), widget, x, y);
310 gdk_threads_leave ();
313 static gboolean
314 focus_in_cb (GtkWidget *widget __attribute((unused)),
315 GdkEventFocus *event __attribute((unused)),
316 jobject peer)
318 gdk_threads_leave ();
319 (*gdk_env())->CallVoidMethod (gdk_env(), peer,
320 postFocusEventID,
321 AWT_FOCUS_GAINED,
322 JNI_FALSE);
323 gdk_threads_enter ();
324 return FALSE;
327 static gboolean
328 focus_out_cb (GtkWidget *widget __attribute((unused)),
329 GdkEventFocus *event __attribute((unused)),
330 jobject peer)
332 gdk_threads_leave ();
333 (*gdk_env())->CallVoidMethod (gdk_env(), peer,
334 postFocusEventID,
335 AWT_FOCUS_LOST,
336 JNI_FALSE);
337 gdk_threads_enter ();
338 return FALSE;
341 static void
342 block_expose_events_cb (GtkWidget *widget, jobject peer)
344 gdk_threads_leave ();
345 (*gdk_env())->CallVoidMethod (gdk_env(), peer,
346 beginNativeRepaintID);
347 gdk_threads_enter ();
349 gdk_window_process_updates (widget->window, TRUE);
351 gdk_threads_leave ();
352 (*gdk_env())->CallVoidMethod (gdk_env(), peer,
353 endNativeRepaintID);
354 gdk_threads_enter ();