OSDN Git Service

2006-08-14 Mark Wielaard <mark@klomp.org>
[pf3gnuchains/gcc-fork.git] / libjava / classpath / native / jni / gtk-peer / gnu_java_awt_peer_gtk_GtkMenuPeer.c
1 /* gtkmenupeer.c -- Native implementation of GtkMenuPeer
2    Copyright (C) 1999, 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_GtkMenuPeer.h"
41
42 JNIEXPORT void JNICALL
43 Java_gnu_java_awt_peer_gtk_GtkMenuPeer_setupAccelGroup
44   (JNIEnv *env, jobject obj, jobject parent)
45 {
46   void *ptr1, *ptr2;
47
48   gdk_threads_enter ();
49
50   ptr1 = NSA_GET_PTR (env, obj);
51
52   if (!parent)
53     {
54       gtk_menu_set_accel_group (GTK_MENU (GTK_MENU_ITEM (ptr1)->submenu), 
55                                 gtk_accel_group_new ());
56     }
57   else
58     {
59       GtkAccelGroup *parent_accel;
60
61       ptr2 = NSA_GET_PTR (env, parent);
62       parent_accel = gtk_menu_get_accel_group (GTK_MENU (GTK_MENU_ITEM (ptr2)->submenu));
63       
64       gtk_menu_set_accel_group (GTK_MENU (GTK_MENU_ITEM (ptr1)->submenu),
65                                 parent_accel);
66     }
67       
68   gdk_threads_leave ();
69 }
70
71
72 JNIEXPORT void JNICALL
73 Java_gnu_java_awt_peer_gtk_GtkMenuPeer_create
74   (JNIEnv *env, jobject obj, jstring label)
75 {
76   GtkWidget *menu_title, *menu, *toplevel;
77   const char *str;
78
79   gdk_threads_enter ();
80   
81   NSA_SET_GLOBAL_REF (env, obj);
82
83   str = (*env)->GetStringUTFChars (env, label, NULL);
84
85   menu = gtk_menu_new ();
86   
87   if (str != NULL)
88     menu_title = gtk_menu_item_new_with_label (str);
89   else
90     menu_title = gtk_menu_item_new();
91
92   gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_title), menu);
93
94   /* Allow this menu to grab the pointer. */
95   toplevel = gtk_widget_get_toplevel (menu);
96   if (GTK_IS_WINDOW (toplevel))
97     {
98       gtk_window_group_add_window (cp_gtk_global_window_group,
99                                    GTK_WINDOW(toplevel));
100     }
101
102   gtk_widget_show (menu_title);
103
104   NSA_SET_PTR (env, obj, menu_title);
105
106   (*env)->ReleaseStringUTFChars (env, label, str);
107
108   gdk_threads_leave ();
109 }
110
111 JNIEXPORT void JNICALL
112 Java_gnu_java_awt_peer_gtk_GtkMenuPeer_addTearOff
113   (JNIEnv *env, jobject obj)
114 {
115   void *ptr1;
116   GtkWidget *menu, *item;
117
118   gdk_threads_enter ();
119
120   ptr1 = NSA_GET_PTR (env, obj);
121
122   menu = gtk_menu_item_get_submenu (GTK_MENU_ITEM (ptr1));
123   item = gtk_tearoff_menu_item_new ();
124   gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
125   gtk_widget_show (item);
126
127   gdk_threads_leave ();
128 }
129
130 JNIEXPORT void JNICALL
131 Java_gnu_java_awt_peer_gtk_GtkMenuPeer_addItem
132   (JNIEnv *env, jobject obj, jobject menuitempeer, jint key, jboolean shift)
133 {
134   void *ptr1, *ptr2;
135   GtkWidget *menu;
136
137   gdk_threads_enter ();
138
139   ptr1 = NSA_GET_PTR (env, obj);
140   ptr2 = NSA_GET_PTR (env, menuitempeer);
141
142   menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(ptr1));
143   gtk_menu_shell_append (GTK_MENU_SHELL(menu), GTK_WIDGET (ptr2));
144
145   if (key)
146     {
147       gtk_widget_add_accelerator (GTK_WIDGET (ptr2), "activate",
148                                   gtk_menu_get_accel_group (GTK_MENU (menu)), key, 
149                                   (GDK_CONTROL_MASK
150                                    | ((shift) ? GDK_SHIFT_MASK : 0)), 
151                                   GTK_ACCEL_VISIBLE);
152     }
153
154   gdk_threads_leave ();
155 }
156
157 JNIEXPORT void JNICALL
158 Java_gnu_java_awt_peer_gtk_GtkMenuPeer_delItem
159   (JNIEnv *env, jobject obj, jint index)
160 {
161   void *ptr;
162   GList *list;
163   GtkWidget *menu;
164   
165   gdk_threads_enter ();
166
167   ptr = NSA_GET_PTR (env, obj);
168
169   menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(ptr));
170
171   list = gtk_container_get_children (GTK_CONTAINER (menu));
172   list = g_list_nth (list, index);
173   gtk_container_remove (GTK_CONTAINER (menu), GTK_WIDGET (list->data));
174
175   gdk_threads_leave ();
176 }