Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/native/libawt_lwawt/awt/CMenu.m
41152 views
1
/*
2
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
#import <JavaRuntimeSupport/JavaRuntimeSupport.h>
27
28
29
#import "CMenu.h"
30
#import "CMenuBar.h"
31
#import "ThreadUtilities.h"
32
#import "JNIUtilities.h"
33
34
#import "sun_lwawt_macosx_CMenu.h"
35
36
@implementation CMenu
37
38
- (id)initWithPeer:(jobject)peer {
39
AWT_ASSERT_APPKIT_THREAD;
40
// Create the new NSMenu
41
self = [super initWithPeer:peer asSeparator:NO];
42
if (self) {
43
fMenu = [NSMenu javaMenuWithTitle:@""];
44
[fMenu retain];
45
[fMenu setAutoenablesItems:NO];
46
}
47
return self;
48
}
49
50
- (void)dealloc {
51
[fMenu release];
52
fMenu = nil;
53
[super dealloc];
54
}
55
56
- (void)addJavaSubmenu:(CMenu *)submenu {
57
[ThreadUtilities performOnMainThread:@selector(addNativeItem_OnAppKitThread:) on:self withObject:submenu waitUntilDone:YES];
58
}
59
60
- (void)addJavaMenuItem:(CMenuItem *)theMenuItem {
61
[ThreadUtilities performOnMainThread:@selector(addNativeItem_OnAppKitThread:) on:self withObject:theMenuItem waitUntilDone:YES];
62
}
63
64
- (void)addNativeItem_OnAppKitThread:(CMenuItem *)itemModified {
65
AWT_ASSERT_APPKIT_THREAD;
66
[itemModified addNSMenuItemToMenu:[self menu]];
67
}
68
69
- (void)setJavaMenuTitle:(NSString *)title {
70
71
if (title) {
72
[ThreadUtilities performOnMainThread:@selector(setNativeMenuTitle_OnAppKitThread:) on:self withObject:title waitUntilDone:YES];
73
}
74
}
75
76
- (void)setNativeMenuTitle_OnAppKitThread:(NSString *)title {
77
AWT_ASSERT_APPKIT_THREAD;
78
79
[fMenu setTitle:title];
80
// If we are a submenu we need to set our name in the parent menu's menu item.
81
NSMenu *parent = [fMenu supermenu];
82
if (parent) {
83
NSInteger index = [parent indexOfItemWithSubmenu:fMenu];
84
NSMenuItem *menuItem = [parent itemAtIndex:index];
85
[menuItem setTitle:title];
86
}
87
}
88
89
- (void)deleteJavaItem:(jint)index {
90
91
[ThreadUtilities performOnMainThread:@selector(deleteNativeJavaItem_OnAppKitThread:) on:self withObject:[NSNumber numberWithInt:index] waitUntilDone:YES];
92
}
93
94
- (void)deleteNativeJavaItem_OnAppKitThread:(NSNumber *)number {
95
AWT_ASSERT_APPKIT_THREAD;
96
97
int n = [number intValue];
98
if (n < [[self menu] numberOfItems]) {
99
[[self menu] removeItemAtIndex:n];
100
}
101
}
102
103
- (void)addNSMenuItemToMenu:(NSMenu *)inMenu {
104
if (fMenuItem == nil) return;
105
[fMenuItem setSubmenu:fMenu];
106
[inMenu addItem:fMenuItem];
107
}
108
109
- (NSMenu *)menu {
110
return [[fMenu retain] autorelease];
111
}
112
113
- (void)setNativeEnabled_OnAppKitThread:(NSNumber *)boolNumber {
114
AWT_ASSERT_APPKIT_THREAD;
115
116
@synchronized(self) {
117
fIsEnabled = [boolNumber boolValue];
118
119
NSMenu* supermenu = [fMenu supermenu];
120
[[supermenu itemAtIndex:[supermenu indexOfItemWithSubmenu:fMenu]] setEnabled:fIsEnabled];
121
}
122
}
123
124
- (NSString *)description {
125
return [NSString stringWithFormat:@"CMenu[ %@ ]", fMenu];
126
}
127
128
@end
129
130
CMenu * createCMenu (jobject cPeerObjGlobal) {
131
132
__block CMenu *aCMenu = nil;
133
134
[ThreadUtilities performOnMainThreadWaiting:YES block:^(){
135
136
aCMenu = [[CMenu alloc] initWithPeer:cPeerObjGlobal];
137
// the aCMenu is released in CMenuComponent.dispose()
138
}];
139
140
if (aCMenu == nil) {
141
return 0L;
142
}
143
144
return aCMenu;
145
146
}
147
148
/*
149
* Class: sun_lwawt_macosx_CMenu
150
* Method: nativeCreateSubMenu
151
* Signature: (J)J
152
*/
153
JNIEXPORT jlong JNICALL
154
Java_sun_lwawt_macosx_CMenu_nativeCreateSubMenu
155
(JNIEnv *env, jobject peer, jlong parentMenu)
156
{
157
CMenu *aCMenu = nil;
158
JNI_COCOA_ENTER(env);
159
160
jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);
161
162
aCMenu = createCMenu (cPeerObjGlobal);
163
164
// Add it to the parent menu
165
[((CMenu *)jlong_to_ptr(parentMenu)) addJavaSubmenu: aCMenu];
166
167
JNI_COCOA_EXIT(env);
168
169
return ptr_to_jlong(aCMenu);
170
}
171
172
173
174
/*
175
* Class: sun_lwawt_macosx_CMenu
176
* Method: nativeCreateMenu
177
* Signature: (JZ)J
178
*/
179
JNIEXPORT jlong JNICALL
180
Java_sun_lwawt_macosx_CMenu_nativeCreateMenu
181
(JNIEnv *env, jobject peer,
182
jlong parentMenuBar, jboolean isHelpMenu, jint insertLocation)
183
{
184
CMenu *aCMenu = nil;
185
CMenuBar *parent = (CMenuBar *)jlong_to_ptr(parentMenuBar);
186
JNI_COCOA_ENTER(env);
187
188
jobject cPeerObjGlobal = (*env)->NewGlobalRef(env, peer);
189
190
aCMenu = createCMenu (cPeerObjGlobal);
191
192
// Add it to the menu bar.
193
[parent javaAddMenu:aCMenu atIndex:insertLocation];
194
195
// If the menu is already the help menu (because we are creating an entire
196
// menu bar) we need to note that now, because we can't rely on
197
// setHelpMenu() being called again.
198
if (isHelpMenu == JNI_TRUE) {
199
[parent javaSetHelpMenu: aCMenu];
200
}
201
202
JNI_COCOA_EXIT(env);
203
return ptr_to_jlong(aCMenu);
204
}
205
206
207
/*
208
* Class: sun_lwawt_macosx_CMenu
209
* Method: nativeSetMenuTitle
210
* Signature: (JLjava/lang/String;)V
211
*/
212
JNIEXPORT void JNICALL
213
Java_sun_lwawt_macosx_CMenu_nativeSetMenuTitle
214
(JNIEnv *env, jobject peer, jlong menuObject, jstring label)
215
{
216
JNI_COCOA_ENTER(env);
217
// Set the menu's title.
218
[((CMenu *)jlong_to_ptr(menuObject)) setJavaMenuTitle:JavaStringToNSString(env, label)];
219
JNI_COCOA_EXIT(env);
220
}
221
222
/*
223
* Class: sun_lwawt_macosx_CMenu
224
* Method: nativeDeleteItem
225
* Signature: (JI)V
226
*/
227
JNIEXPORT void JNICALL
228
Java_sun_lwawt_macosx_CMenu_nativeDeleteItem
229
(JNIEnv *env, jobject peer, jlong menuObject, jint index)
230
{
231
JNI_COCOA_ENTER(env);
232
// Remove the specified item.
233
[((CMenu *)jlong_to_ptr(menuObject)) deleteJavaItem: index];
234
JNI_COCOA_EXIT(env);
235
}
236
237
/*
238
* Class: sun_lwawt_macosx_CMenu
239
* Method: nativeGetNSMenu
240
* Signature: (J)J
241
*/
242
JNIEXPORT jlong JNICALL
243
Java_sun_lwawt_macosx_CMenu_nativeGetNSMenu
244
(JNIEnv *env, jobject peer, jlong menuObject)
245
{
246
NSMenu* nsMenu = NULL;
247
248
JNI_COCOA_ENTER(env);
249
// Strong retain this menu; it'll get released in Java_apple_laf_ScreenMenu_addMenuListeners
250
nsMenu = [[((CMenu *)jlong_to_ptr(menuObject)) menu] retain];
251
JNI_COCOA_EXIT(env);
252
253
return ptr_to_jlong(nsMenu);
254
}
255
256