Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkButtonPeer.c
blob6a9066cbe536e1d4e74f204bde675dee27b7dc8f
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 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 jmethodID postActionEventID;
44 void
45 cp_gtk_button_init_jni (void)
47 jclass gtkbuttonpeer;
49 gtkbuttonpeer = (*cp_gtk_gdk_env())->FindClass (cp_gtk_gdk_env(),
50 "gnu/java/awt/peer/gtk/GtkButtonPeer");
52 postActionEventID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(),
53 gtkbuttonpeer,
54 "postActionEvent", "(I)V");
57 static void clicked_cb (GtkButton *button,
58 jobject peer);
60 JNIEXPORT void JNICALL
61 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_create
62 (JNIEnv *env, jobject obj, jstring label)
64 const char *c_label;
65 GtkWidget *eventbox;
66 GtkWidget *button;
68 gdk_threads_enter ();
70 NSA_SET_GLOBAL_REF (env, obj);
72 c_label = (*env)->GetStringUTFChars (env, label, NULL);
74 eventbox = gtk_event_box_new ();
75 button = gtk_button_new_with_label (c_label);
76 gtk_container_add (GTK_CONTAINER (eventbox), button);
77 gtk_widget_show (button);
79 (*env)->ReleaseStringUTFChars (env, label, c_label);
80 NSA_SET_PTR (env, obj, eventbox);
82 gdk_threads_leave ();
85 JNIEXPORT void JNICALL
86 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetGetPreferredDimensions
87 (JNIEnv *env, jobject obj, jintArray jdims)
89 void *ptr;
90 jint *dims;
91 GtkWidget *button;
92 GtkWidget *label;
93 GtkRequisition current_req;
94 GtkRequisition current_label_req;
95 GtkRequisition natural_req;
97 gdk_threads_enter ();
99 ptr = NSA_GET_PTR (env, obj);
101 button = gtk_bin_get_child (GTK_BIN (ptr));
102 label = gtk_bin_get_child (GTK_BIN (button));
104 dims = (*env)->GetIntArrayElements (env, jdims, 0);
105 dims[0] = dims[1] = 0;
107 /* Save the button's current size request. */
108 gtk_widget_size_request (GTK_WIDGET (button), &current_req);
110 /* Save the label's current size request. */
111 gtk_widget_size_request (GTK_WIDGET (label), &current_label_req);
113 /* Get the widget's "natural" size request. */
114 gtk_widget_set_size_request (GTK_WIDGET (button), -1, -1);
115 gtk_widget_set_size_request (GTK_WIDGET (label), -1, -1);
117 gtk_widget_size_request (GTK_WIDGET (button), &natural_req);
119 /* Reset the button's size request. */
120 gtk_widget_set_size_request (GTK_WIDGET (button),
121 current_req.width, current_req.height);
123 /* Reset the label's size request. */
124 gtk_widget_set_size_request (GTK_WIDGET (label),
125 current_label_req.width, current_label_req.height);
127 dims[0] = natural_req.width;
128 dims[1] = natural_req.height;
130 (*env)->ReleaseIntArrayElements (env, jdims, dims, 0);
132 gdk_threads_leave ();
135 JNIEXPORT void JNICALL
136 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_connectSignals
137 (JNIEnv *env, jobject obj)
139 void *ptr;
140 jobject *gref;
141 GtkWidget *button;
143 gdk_threads_enter ();
145 ptr = NSA_GET_PTR (env, obj);
146 gref = NSA_GET_GLOBAL_REF (env, obj);
148 button = gtk_bin_get_child (GTK_BIN (ptr));
150 /* Button signals */
151 g_signal_connect (G_OBJECT (button), "clicked",
152 G_CALLBACK (clicked_cb), *gref);
154 /* Component signals */
155 cp_gtk_component_connect_signals (G_OBJECT (button), gref);
157 gdk_threads_leave ();
160 JNIEXPORT void JNICALL
161 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkSetLabel
162 (JNIEnv *env, jobject obj, jstring jtext)
164 const char *text;
165 GtkWidget *button;
166 GtkWidget *label;
167 void *ptr;
169 gdk_threads_enter ();
171 ptr = NSA_GET_PTR (env, obj);
173 text = (*env)->GetStringUTFChars (env, jtext, NULL);
175 button = gtk_bin_get_child (GTK_BIN (ptr));
176 label = gtk_bin_get_child (GTK_BIN (button));
177 gtk_label_set_text (GTK_LABEL (label), text);
179 (*env)->ReleaseStringUTFChars (env, jtext, text);
181 gdk_threads_leave ();
184 JNIEXPORT void JNICALL
185 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetModifyFont
186 (JNIEnv *env, jobject obj, jstring name, jint style, jint size)
188 const char *font_name;
189 void *ptr;
190 GtkWidget *button;
191 GtkWidget *label;
192 PangoFontDescription *font_desc;
194 gdk_threads_enter();
196 ptr = NSA_GET_PTR (env, obj);
198 font_name = (*env)->GetStringUTFChars (env, name, NULL);
200 button = gtk_bin_get_child (GTK_BIN (ptr));
201 label = gtk_bin_get_child (GTK_BIN (button));
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_GtkButtonPeer_gtkWidgetSetBackground
224 (JNIEnv *env, jobject obj, jint red, jint green, jint blue)
226 GdkColor normal_color;
227 GdkColor prelight_color;
228 GdkColor active_color;
229 int prelight_red;
230 int prelight_blue;
231 int prelight_green;
232 GtkWidget *button;
233 void *ptr;
235 gdk_threads_enter ();
237 ptr = NSA_GET_PTR (env, obj);
239 normal_color.red = (red / 255.0) * 65535;
240 normal_color.green = (green / 255.0) * 65535;
241 normal_color.blue = (blue / 255.0) * 65535;
243 /* This calculation only approximate the active color produced by
244 Sun's AWT. */
245 active_color.red = 0.85 * (red / 255.0) * 65535;
246 active_color.green = 0.85 * (green / 255.0) * 65535;
247 active_color.blue = 0.85 * (blue / 255.0) * 65535;
249 /* There is no separate prelight color in Motif. */
250 prelight_red = 1.15 * (red / 255.0) * 65535;
251 prelight_green = 1.15 * (green / 255.0) * 65535;
252 prelight_blue = 1.15 * (blue / 255.0) * 65535;
254 prelight_color.red = prelight_red > 65535 ? 65535 : prelight_red;
255 prelight_color.green = prelight_green > 65535 ? 65535 : prelight_green;
256 prelight_color.blue = prelight_blue > 65535 ? 65535 : prelight_blue;
258 button = gtk_bin_get_child (GTK_BIN (ptr));
260 gtk_widget_modify_bg (button, GTK_STATE_NORMAL, &normal_color);
261 gtk_widget_modify_bg (button, GTK_STATE_ACTIVE, &active_color);
262 gtk_widget_modify_bg (button, GTK_STATE_PRELIGHT, &prelight_color);
264 gdk_threads_leave ();
267 JNIEXPORT void JNICALL
268 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetSetForeground
269 (JNIEnv *env, jobject obj, jint red, jint green, jint blue)
271 GdkColor color;
272 GtkWidget *button;
273 GtkWidget *label;
274 void *ptr;
276 gdk_threads_enter ();
278 ptr = NSA_GET_PTR (env, obj);
280 color.red = (red / 255.0) * 65535;
281 color.green = (green / 255.0) * 65535;
282 color.blue = (blue / 255.0) * 65535;
284 button = gtk_bin_get_child (GTK_BIN (ptr));
285 label = gtk_bin_get_child (GTK_BIN (button));
287 gtk_widget_modify_fg (label, GTK_STATE_NORMAL, &color);
288 gtk_widget_modify_fg (label, GTK_STATE_ACTIVE, &color);
289 gtk_widget_modify_fg (label, GTK_STATE_PRELIGHT, &color);
291 gdk_threads_leave ();
294 JNIEXPORT void JNICALL
295 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkActivate
296 (JNIEnv *env, jobject obj)
298 GtkWidget *button;
299 void *ptr;
301 gdk_threads_enter ();
303 ptr = NSA_GET_PTR (env, obj);
305 button = gtk_bin_get_child (GTK_BIN (ptr));
306 gtk_widget_activate (GTK_WIDGET (button));
308 gdk_threads_leave ();
311 JNIEXPORT void JNICALL
312 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetRequestFocus
313 (JNIEnv *env, jobject obj)
315 void *ptr;
316 GtkWidget *button;
318 gdk_threads_enter ();
320 ptr = NSA_GET_PTR (env, obj);
322 button = gtk_bin_get_child (GTK_BIN (ptr));
323 gtk_widget_grab_focus (button);
325 gdk_threads_leave ();
328 JNIEXPORT void JNICALL
329 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_setNativeBounds
330 (JNIEnv *env, jobject obj, jint x, jint y, jint width, jint height)
332 GtkWidget *widget, *child;
333 void *ptr;
335 gdk_threads_enter ();
337 ptr = NSA_GET_PTR (env, obj);
339 widget = GTK_WIDGET (ptr);
341 /* We assume that -1 is a width or height and not a request for the
342 widget's natural size. */
343 width = width < 0 ? 0 : width;
344 height = height < 0 ? 0 : height;
345 child = gtk_bin_get_child (GTK_BIN (widget));
347 if (!(width == 0 && height == 0))
349 /* Set the event box's size request... */
350 gtk_widget_set_size_request (widget, width, height);
351 /* ...and the button's size request... */
352 gtk_widget_set_size_request (child, width, height);
353 /* ...and the label's size request. */
354 gtk_widget_set_size_request (gtk_bin_get_child (GTK_BIN (child)), width,
355 height);
356 if (widget->parent != NULL)
357 gtk_fixed_move (GTK_FIXED (widget->parent), widget, x, y);
360 gdk_threads_leave ();
363 static void
364 clicked_cb (GtkButton* button __attribute__((unused)),
365 jobject peer)
367 GdkEventButton* event;
369 event = (GdkEventButton*) gtk_get_current_event ();
370 g_assert (event);
372 (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
373 postActionEventID,
374 cp_gtk_state_to_awt_mods (event->state));
376 gdk_event_free ((GdkEvent*) event);