OSDN Git Service

2006-08-02 Sven de Marothy <sven@physto.se>
[pf3gnuchains/gcc-fork.git] / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkButtonPeer.c
1 /* gtkbuttonpeer.c -- Native implementation of GtkButtonPeer
2    Copyright (C) 1998, 1999 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_GtkButtonPeer.h"
41
42 static jmethodID postActionEventID;
43
44 void
45 cp_gtk_button_init_jni (void)
46 {
47   jclass gtkbuttonpeer;
48
49   gtkbuttonpeer = (*cp_gtk_gdk_env())->FindClass (cp_gtk_gdk_env(),
50                                            "gnu/java/awt/peer/gtk/GtkButtonPeer");
51
52   postActionEventID = (*cp_gtk_gdk_env())->GetMethodID (cp_gtk_gdk_env(),
53                                                         gtkbuttonpeer,
54                                                   "postActionEvent", "(I)V");
55 }
56
57 static void clicked_cb (GtkButton *button,
58                         jobject peer);
59
60 JNIEXPORT void JNICALL
61 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_create
62   (JNIEnv *env, jobject obj, jstring label)
63 {
64   const char *c_label;
65   GtkWidget *eventbox;
66   GtkWidget *button;
67
68   gdk_threads_enter ();
69
70   NSA_SET_GLOBAL_REF (env, obj);
71
72   c_label = (*env)->GetStringUTFChars (env, label, NULL);
73
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);
78
79   (*env)->ReleaseStringUTFChars (env, label, c_label);
80   NSA_SET_PTR (env, obj, eventbox);
81
82   gdk_threads_leave ();
83 }
84
85 JNIEXPORT void JNICALL 
86 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetGetPreferredDimensions
87   (JNIEnv *env, jobject obj, jintArray jdims)
88 {
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;
96
97   gdk_threads_enter ();
98
99   ptr = NSA_GET_PTR (env, obj);
100
101   button = gtk_bin_get_child (GTK_BIN (ptr));
102   label = gtk_bin_get_child (GTK_BIN (button));
103
104   dims = (*env)->GetIntArrayElements (env, jdims, 0);
105   dims[0] = dims[1] = 0;
106
107   /* Save the button's current size request. */
108   gtk_widget_size_request (GTK_WIDGET (button), &current_req);
109
110   /* Save the label's current size request. */
111   gtk_widget_size_request (GTK_WIDGET (label), &current_label_req);
112
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);
116
117   gtk_widget_size_request (GTK_WIDGET (button), &natural_req);
118
119   /* Reset the button's size request. */
120   gtk_widget_set_size_request (GTK_WIDGET (button),
121                                current_req.width, current_req.height);
122
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);
126
127   dims[0] = natural_req.width;
128   dims[1] = natural_req.height;
129
130   (*env)->ReleaseIntArrayElements (env, jdims, dims, 0);
131
132   gdk_threads_leave ();
133 }
134
135 JNIEXPORT void JNICALL
136 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_connectSignals
137   (JNIEnv *env, jobject obj)
138 {
139   void *ptr;
140   jobject *gref;
141   GtkWidget *button;
142
143   gdk_threads_enter ();
144
145   ptr = NSA_GET_PTR (env, obj);
146   gref = NSA_GET_GLOBAL_REF (env, obj);
147
148   button = gtk_bin_get_child (GTK_BIN (ptr));
149
150   /* Button signals */
151   g_signal_connect (G_OBJECT (button), "clicked",
152                     G_CALLBACK (clicked_cb), *gref);
153
154   /* Component signals */
155   cp_gtk_component_connect_signals (G_OBJECT (button), gref);
156
157   gdk_threads_leave ();
158 }
159
160 JNIEXPORT void JNICALL 
161 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkSetLabel
162   (JNIEnv *env, jobject obj, jstring jtext)
163 {
164   const char *text;
165   GtkWidget *button;
166   GtkWidget *label;
167   void *ptr;
168
169   gdk_threads_enter ();
170
171   ptr = NSA_GET_PTR (env, obj);
172
173   text = (*env)->GetStringUTFChars (env, jtext, NULL);
174
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);
178
179   (*env)->ReleaseStringUTFChars (env, jtext, text);
180
181   gdk_threads_leave ();
182 }
183
184 JNIEXPORT void JNICALL 
185 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetModifyFont
186   (JNIEnv *env, jobject obj, jstring name, jint style, jint size)
187 {
188   const char *font_name;
189   void *ptr;
190   GtkWidget *button;
191   GtkWidget *label;
192   PangoFontDescription *font_desc;
193
194   gdk_threads_enter();
195
196   ptr = NSA_GET_PTR (env, obj);
197
198   font_name = (*env)->GetStringUTFChars (env, name, NULL);
199
200   button = gtk_bin_get_child (GTK_BIN (ptr));
201   label = gtk_bin_get_child (GTK_BIN (button));
202
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);
206
207   if (style & AWT_STYLE_BOLD)
208     pango_font_description_set_weight (font_desc, PANGO_WEIGHT_BOLD);
209
210   if (style & AWT_STYLE_ITALIC)
211     pango_font_description_set_style (font_desc, PANGO_STYLE_OBLIQUE);
212
213   gtk_widget_modify_font (GTK_WIDGET(label), font_desc);
214
215   pango_font_description_free (font_desc);
216
217   (*env)->ReleaseStringUTFChars (env, name, font_name);
218
219   gdk_threads_leave();
220 }
221
222 JNIEXPORT void JNICALL 
223 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetSetBackground
224   (JNIEnv *env, jobject obj, jint red, jint green, jint blue)
225 {
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;
234
235   gdk_threads_enter ();
236
237   ptr = NSA_GET_PTR (env, obj);
238
239   normal_color.red = (red / 255.0) * 65535;
240   normal_color.green = (green / 255.0) * 65535;
241   normal_color.blue = (blue / 255.0) * 65535;
242
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;
248
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;
253
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;
257
258   button = gtk_bin_get_child (GTK_BIN (ptr));
259
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);
263
264   gdk_threads_leave ();
265 }
266
267 JNIEXPORT void JNICALL 
268 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetSetForeground
269   (JNIEnv *env, jobject obj, jint red, jint green, jint blue)
270 {
271   GdkColor color;
272   GtkWidget *button;
273   GtkWidget *label;
274   void *ptr;
275
276   gdk_threads_enter ();
277
278   ptr = NSA_GET_PTR (env, obj);
279
280   color.red = (red / 255.0) * 65535;
281   color.green = (green / 255.0) * 65535;
282   color.blue = (blue / 255.0) * 65535;
283
284   button = gtk_bin_get_child (GTK_BIN (ptr));
285   label = gtk_bin_get_child (GTK_BIN (button));
286
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);
290
291   gdk_threads_leave ();
292 }
293
294 JNIEXPORT void JNICALL
295 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkActivate
296   (JNIEnv *env, jobject obj)
297 {
298   GtkWidget *button;
299   void *ptr;
300
301   gdk_threads_enter ();
302
303   ptr = NSA_GET_PTR (env, obj);
304
305   button = gtk_bin_get_child (GTK_BIN (ptr));
306   gtk_widget_activate (GTK_WIDGET (button));
307
308   gdk_threads_leave ();
309 }
310
311 JNIEXPORT void JNICALL
312 Java_gnu_java_awt_peer_gtk_GtkButtonPeer_gtkWidgetRequestFocus
313   (JNIEnv *env, jobject obj)
314 {
315   void *ptr;
316   GtkWidget *button;
317
318   gdk_threads_enter ();
319
320   ptr = NSA_GET_PTR (env, obj);
321
322   button = gtk_bin_get_child (GTK_BIN (ptr));
323   gtk_widget_grab_focus (button);
324
325   gdk_threads_leave ();
326 }
327
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)
331 {
332   GtkWidget *widget, *child;
333   void *ptr;
334
335   gdk_threads_enter ();
336
337   ptr = NSA_GET_PTR (env, obj);
338
339   widget = GTK_WIDGET (ptr);
340
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));
346
347   if (!(width == 0 && height == 0))
348     {
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);
358     }
359
360   gdk_threads_leave ();
361 }
362
363 static void
364 clicked_cb (GtkButton* button __attribute__((unused)),
365             jobject peer)
366 {
367   GdkEventButton* event;
368
369   event = (GdkEventButton*) gtk_get_current_event ();
370   g_assert (event);
371
372   (*cp_gtk_gdk_env())->CallVoidMethod (cp_gtk_gdk_env(), peer,
373                                        postActionEventID,
374                                        cp_gtk_state_to_awt_mods (event->state));
375
376   gdk_event_free ((GdkEvent*) event);
377 }