diff options
Diffstat (limited to 'src/newt/native')
-rw-r--r-- | src/newt/native/MacWindow.m | 178 | ||||
-rw-r--r-- | src/newt/native/NewtMacWindow.h | 25 | ||||
-rw-r--r-- | src/newt/native/NewtMacWindow.m | 144 |
3 files changed, 234 insertions, 113 deletions
diff --git a/src/newt/native/MacWindow.m b/src/newt/native/MacWindow.m index aa1385801..70170e525 100644 --- a/src/newt/native/MacWindow.m +++ b/src/newt/native/MacWindow.m @@ -44,25 +44,6 @@ #import <stdio.h> -// For some reason, rightMouseDown isn't automatically being passed -// from the NSView to the containing NSWindow - -@interface NewtView : NSView -{ -} - -@end - -@implementation NewtView -- (void) rightMouseDown: (NSEvent*) theEvent -{ - NSResponder* next = [self nextResponder]; - if (next != nil) { - [next rightMouseDown: theEvent]; - } -} -@end - NSString* jstringToNSString(JNIEnv* env, jstring jstr) { const jchar* jstrChars = (*env)->GetStringChars(env, jstr, NULL); @@ -81,6 +62,32 @@ void setFrameTopLeftPoint(NSWindow* win, jint x, jint y) [win setFrameTopLeftPoint: pt]; } +static NewtView * changeContentView(JNIEnv *env, jobject javaWindowObject, NSWindow *win, NewtView *newView) { + NSView* oldNSView = [win contentView]; + NewtView* oldView = NULL; + + if(NULL!=oldNSView) { + if([oldNSView isInFullScreenMode]) { + // FIXME: Available >= 10.5 - Makes the taskbar disapear + [oldNSView exitFullScreenModeWithOptions: NULL]; + } + if( [oldNSView isMemberOfClass:[NewtView class]] ) { + oldView = (NewtView *) oldNSView; + + jobject globJavaWindowObject = [oldView getJavaWindowObject]; + (*env)->DeleteGlobalRef(env, globJavaWindowObject); + [oldView setJavaWindowObject: NULL]; + } + } + if(NULL!=newView) { + jobject globJavaWindowObject = (*env)->NewGlobalRef(env, javaWindowObject); + [newView setJavaWindowObject: globJavaWindowObject]; + } + [win setContentView: newView]; + + return oldView; +} + /* * Class: com_sun_javafx_newt_macosx_MacWindow * Method: initIDs @@ -117,41 +124,61 @@ JNIEXPORT jboolean JNICALL Java_com_sun_javafx_newt_macosx_MacWindow_initIDs /* * Class: com_sun_javafx_newt_macosx_MacWindow - * Method: createWindow - * Signature: (IIIIIIZ)J + * Method: createWindow0 + * Signature: (IIIIZIIIJ)J */ -JNIEXPORT jlong JNICALL Java_com_sun_javafx_newt_macosx_MacWindow_createWindow - (JNIEnv *env, jobject jthis, jint x, jint y, jint w, jint h, jint styleMask, jint bufferingType, jboolean deferCreation) +JNIEXPORT jlong JNICALL Java_com_sun_javafx_newt_macosx_MacWindow_createWindow0 + (JNIEnv *env, jobject jthis, jint x, jint y, jint w, jint h, jboolean fullscreen, jint styleMask, + jint bufferingType, jint screen_idx, jlong jview) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; NSRect rect = NSMakeRect(x, y, w, h); - jobject windowObj = (*env)->NewGlobalRef(env, jthis); + + NSArray *screens = [NSScreen screens]; + if(screen_idx<0) screen_idx=0; + if(screen_idx>=[screens count]) screen_idx=0; + NSScreen *screen = (NSScreen *) [screens objectAtIndex: screen_idx]; + + if (fullscreen) { + styleMask = NSBorderlessWindowMask; + NSRect rect = [screen frame]; + w = (jint) (rect.size.width); + h = (jint) (rect.size.height); + } // Allocate the window NSWindow* window = [[[NewtMacWindow alloc] initWithContentRect: rect styleMask: (NSUInteger) styleMask backing: (NSBackingStoreType) bufferingType - defer: YES - javaWindowObject: windowObj] retain]; - - // If the window is undecorated, assume we want the possibility of - // a shaped window, so make it non-opaque and the background color clear - if ((styleMask & NSTitledWindowMask) == 0) { - [window setOpaque: NO]; - [window setBackgroundColor: [NSColor clearColor]]; + screen: screen] retain]; + + if (fullscreen) { + [window setOpaque: YES]; + } else { + // If the window is undecorated, assume we want the possibility of + // a shaped window, so make it non-opaque and the background color clear + if ((styleMask & NSTitledWindowMask) == 0) { + [window setOpaque: NO]; + [window setBackgroundColor: [NSColor clearColor]]; + } } // Immediately re-position the window based on an upper-left coordinate system setFrameTopLeftPoint(window, x, y); - // Allocate an NSView - NSView* view = [[NewtView alloc] initWithFrame: rect]; - // specify we want mouse-moved events [window setAcceptsMouseMovedEvents:YES]; + // Use given NewtView or allocate an NewtView if NULL + NewtView* view = (0==jview)? [[NewtView alloc] initWithFrame: rect] : (NewtView*) ((intptr_t) jview) ; + // Set the content view - [window setContentView: view]; + (void) changeContentView(env, jthis, window, view); + + if(fullscreen) { + // FIXME: Available >= 10.5 - Makes the taskbar disapear + [view enterFullScreenMode: screen withOptions:NULL]; + } // Set the next responder to be the window so that we can forward // right mouse button down events @@ -245,23 +272,37 @@ JNIEXPORT void JNICALL Java_com_sun_javafx_newt_macosx_MacWindow_dispatchMessage NSEvent* event = NULL; NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; - NewtMacWindow* nwwin = (NewtMacWindow*) ((intptr_t) window); - [nwwin setJNIEnv: env]; +NS_DURING + + NSWindow* win = (NSWindow *) ((intptr_t) window); + + if(NULL != win) { + NewtView* view = (NewtView *) [win contentView]; + [view setJNIEnv: env]; + + do { + // FIXME: ignoring event mask for the time being + event = [NSApp nextEventMatchingMask: NSAnyEventMask + untilDate: [NSDate distantPast] + inMode: NSDefaultRunLoopMode + dequeue: YES]; + if (event != NULL) { + win = (NSWindow*) [event window]; + view = (NewtView *) [win contentView]; + [view setJNIEnv: env]; + + [NSApp sendEvent: event]; + } + } while (event != NULL); + } + +NS_HANDLER + + // just ignore it .. + +NS_ENDHANDLER - do { - // FIXME: ignoring event mask for the time being - event = [NSApp nextEventMatchingMask: NSAnyEventMask - untilDate: [NSDate distantPast] - inMode: NSDefaultRunLoopMode - dequeue: YES]; - if (event != NULL) { - NewtMacWindow* nwwin = (NewtMacWindow*) [event window]; - [nwwin setJNIEnv: env]; - [NSApp sendEvent: event]; - } - } while (event != NULL); - // [nwwin setJNIEnv: NULL]; [pool release]; } @@ -282,6 +323,25 @@ JNIEXPORT jlong JNICALL Java_com_sun_javafx_newt_macosx_MacWindow_contentView /* * Class: com_sun_javafx_newt_macosx_MacWindow + * Method: changeContentView + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_com_sun_javafx_newt_macosx_MacWindow_changeContentView + (JNIEnv *env, jobject jthis, jlong window, jlong jview) +{ + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + NSWindow* win = (NewtMacWindow*) ((intptr_t) window); + NewtView* newView = (NewtView *) ((intptr_t) jview); + + NewtView* oldView = changeContentView(env, jthis, win, newView); + + [pool release]; + + return oldView; +} + +/* + * Class: com_sun_javafx_newt_macosx_MacWindow * Method: setContentSize * Signature: (JII)V */ @@ -315,14 +375,14 @@ JNIEXPORT void JNICALL Java_com_sun_javafx_newt_macosx_MacWindow_setFrameTopLeft * Signature: (I)I */ JNIEXPORT jint JNICALL Java_com_sun_javafx_newt_macosx_MacWindow_getScreenWidth - (JNIEnv *env, jclass clazz, jint sidx) + (JNIEnv *env, jclass clazz, jint screen_idx) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; NSArray *screens = [NSScreen screens]; - if(sidx<0) sidx=0; - if(sidx>=[screens count]) sidx=0; - NSScreen *screen = (NSScreen *) [screens objectAtIndex: sidx]; + if(screen_idx<0) screen_idx=0; + if(screen_idx>=[screens count]) screen_idx=0; + NSScreen *screen = (NSScreen *) [screens objectAtIndex: screen_idx]; NSRect rect = [screen frame]; [pool release]; @@ -336,14 +396,14 @@ JNIEXPORT jint JNICALL Java_com_sun_javafx_newt_macosx_MacWindow_getScreenWidth * Signature: (I)I */ JNIEXPORT jint JNICALL Java_com_sun_javafx_newt_macosx_MacWindow_getScreenHeight - (JNIEnv *env, jclass clazz, jint sidx) + (JNIEnv *env, jclass clazz, jint screen_idx) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; NSArray *screens = [NSScreen screens]; - if(sidx<0) sidx=0; - if(sidx>=[screens count]) sidx=0; - NSScreen *screen = (NSScreen *) [screens objectAtIndex: sidx]; + if(screen_idx<0) screen_idx=0; + if(screen_idx>=[screens count]) screen_idx=0; + NSScreen *screen = (NSScreen *) [screens objectAtIndex: screen_idx]; NSRect rect = [screen frame]; [pool release]; diff --git a/src/newt/native/NewtMacWindow.h b/src/newt/native/NewtMacWindow.h index fc8a1e4f3..09f9ffbd2 100644 --- a/src/newt/native/NewtMacWindow.h +++ b/src/newt/native/NewtMacWindow.h @@ -34,7 +34,7 @@ #import <AppKit/AppKit.h> #import "jni.h" -@interface NewtMacWindow : NSWindow +@interface NewtView : NSView { jobject javaWindowObject; @@ -42,15 +42,28 @@ JNIEnv* env; } -+ (BOOL) initNatives: (JNIEnv*) env forClass: (jobject) clazz; - -/* Set and cleared during event dispatching cycle */ +/* Set during event dispatching cycle */ - (void) setJNIEnv: (JNIEnv*) env; +- (JNIEnv*) getJNIEnv; + +/* Register or deregister (NULL) the java Window object, + ie, if NULL, no events are send */ +- (void) setJavaWindowObject: (jobject) javaWindowObj; +- (jobject) getJavaWindowObject; + +- (void) rightMouseDown: (NSEvent*) theEvent; + +@end + +@interface NewtMacWindow : NSWindow +{ +} + ++ (BOOL) initNatives: (JNIEnv*) env forClass: (jobject) clazz; - (id) initWithContentRect: (NSRect) contentRect styleMask: (NSUInteger) windowStyle backing: (NSBackingStoreType) bufferingType - defer: (BOOL) deferCreation - javaWindowObject: (jobject) javaWindowObj; + screen:(NSScreen *)screen; @end diff --git a/src/newt/native/NewtMacWindow.m b/src/newt/native/NewtMacWindow.m index 94837359d..62c6a6e25 100644 --- a/src/newt/native/NewtMacWindow.m +++ b/src/newt/native/NewtMacWindow.m @@ -36,6 +36,35 @@ #import "KeyEvent.h" #import "MouseEvent.h" +@implementation NewtView +- (void) setJNIEnv: (JNIEnv*) theEnv +{ + env = theEnv; +} +- (JNIEnv*) getJNIEnv +{ + return env; +} + +- (void) setJavaWindowObject: (jobject) javaWindowObj +{ + javaWindowObject = javaWindowObj; +} + +- (jobject) getJavaWindowObject +{ + return javaWindowObject; +} + +- (void) rightMouseDown: (NSEvent*) theEvent +{ + NSResponder* next = [self nextResponder]; + if (next != nil) { + [next rightMouseDown: theEvent]; + } +} +@end + static jmethodID sendMouseEventID = NULL; static jmethodID sendKeyEventID = NULL; static jmethodID sizeChangedID = NULL; @@ -61,22 +90,16 @@ static jmethodID windowDestroyedID = NULL; return NO; } -- (void) setJNIEnv: (JNIEnv*) theEnv -{ - env = theEnv; -} - - (id) initWithContentRect: (NSRect) contentRect styleMask: (NSUInteger) windowStyle backing: (NSBackingStoreType) bufferingType - defer: (BOOL) deferCreation - javaWindowObject: (jobject) javaWindowObj + screen:(NSScreen *)screen { id res = [super initWithContentRect: contentRect styleMask: windowStyle backing: bufferingType - defer: deferCreation]; - javaWindowObject = javaWindowObj; + defer: YES + screen: screen]; // Why is this necessary? Without it we don't get any of the // delegate methods like resizing and window movement. [self setDelegate: self]; @@ -110,20 +133,23 @@ static jint mods2JavaMods(NSUInteger mods) - (void) sendKeyEvent: (NSEvent*) event eventType: (jint) evType { + NSView* nsview = [self contentView]; + if( ! [nsview isMemberOfClass:[NewtView class]] ) { + return; + } + NewtView* view = (NewtView *) nsview; + jobject javaWindowObject = [view getJavaWindowObject]; + JNIEnv* env = [view getJNIEnv]; + if (env==NULL || javaWindowObject == NULL) { + return; + } + int i; jint keyCode = (jint) [event keyCode]; NSString* chars = [event charactersIgnoringModifiers]; int len = [chars length]; jint javaMods = mods2JavaMods([event modifierFlags]); - if (env == NULL) { - return; - } - - if (javaWindowObject == NULL) { - return; - } - for (i = 0; i < len; i++) { // Note: the key code in the NSEvent does not map to anything we can use jchar keyChar = (jchar) [chars characterAtIndex: i]; @@ -145,6 +171,17 @@ static jint mods2JavaMods(NSUInteger mods) - (void) sendMouseEvent: (NSEvent*) event eventType: (jint) evType { + NSView* nsview = [self contentView]; + if( ! [nsview isMemberOfClass:[NewtView class]] ) { + return; + } + NewtView* view = (NewtView *) nsview; + jobject javaWindowObject = [view getJavaWindowObject]; + JNIEnv* env = [view getJNIEnv]; + if (env==NULL || javaWindowObject == NULL) { + return; + } + jint javaMods = mods2JavaMods([event modifierFlags]); NSRect frameRect = [self frame]; NSRect contentRect = [self contentRectForFrameRect: frameRect]; @@ -179,14 +216,6 @@ static jint mods2JavaMods(NSUInteger mods) break; } - if (env == NULL) { - return; - } - - if (javaWindowObject == NULL) { - return; - } - (*env)->CallVoidMethod(env, javaWindowObject, sendMouseEventID, evType, javaMods, (jint) location.x, @@ -259,17 +288,20 @@ static jint mods2JavaMods(NSUInteger mods) - (void)windowDidResize: (NSNotification*) notification { - NSRect frameRect = [self frame]; - NSRect contentRect = [self contentRectForFrameRect: frameRect]; - - if (env == NULL) { + NSView* nsview = [self contentView]; + if( ! [nsview isMemberOfClass:[NewtView class]] ) { return; } - - if (javaWindowObject == NULL) { + NewtView* view = (NewtView *) nsview; + jobject javaWindowObject = [view getJavaWindowObject]; + JNIEnv* env = [view getJNIEnv]; + if (env==NULL || javaWindowObject == NULL) { return; } + NSRect frameRect = [self frame]; + NSRect contentRect = [self contentRectForFrameRect: frameRect]; + (*env)->CallVoidMethod(env, javaWindowObject, sizeChangedID, (jint) contentRect.size.width, (jint) contentRect.size.height); @@ -277,20 +309,23 @@ static jint mods2JavaMods(NSUInteger mods) - (void)windowDidMove: (NSNotification*) notification { + NSView* nsview = [self contentView]; + if( ! [nsview isMemberOfClass:[NewtView class]] ) { + return; + } + NewtView* view = (NewtView *) nsview; + jobject javaWindowObject = [view getJavaWindowObject]; + JNIEnv* env = [view getJNIEnv]; + if (env==NULL || javaWindowObject == NULL) { + return; + } + NSRect rect = [self frame]; NSScreen* menuBarScreen = NULL; NSScreen* screen = NULL; NSRect screenRect; NSPoint pt; - if (env == NULL) { - return; - } - - if (javaWindowObject == NULL) { - return; - } - // FIXME: unclear whether this works correctly in multiple monitor situations screen = [self screen]; screenRect = [screen visibleFrame]; @@ -302,25 +337,35 @@ static jint mods2JavaMods(NSUInteger mods) - (void)windowWillClose: (NSNotification*) notification { - if (env == NULL) { + NSView* nsview = [self contentView]; + if( ! [nsview isMemberOfClass:[NewtView class]] ) { return; } - - if (javaWindowObject == NULL) { + NewtView* view = (NewtView *) nsview; + jobject javaWindowObject = [view getJavaWindowObject]; + JNIEnv* env = [view getJNIEnv]; + if (env==NULL || javaWindowObject == NULL) { return; } (*env)->CallVoidMethod(env, javaWindowObject, windowDestroyNotifyID); // Will be called by Window.java (*env)->CallVoidMethod(env, javaWindowObject, windowDestroyedID); + + // EOL .. + (*env)->DeleteGlobalRef(env, javaWindowObject); + [view setJavaWindowObject: NULL]; } - (void) windowDidBecomeKey: (NSNotification *) notification { - if (env == NULL) { + NSView* nsview = [self contentView]; + if( ! [nsview isMemberOfClass:[NewtView class]] ) { return; } - - if (javaWindowObject == NULL) { + NewtView* view = (NewtView *) nsview; + jobject javaWindowObject = [view getJavaWindowObject]; + JNIEnv* env = [view getJNIEnv]; + if (env==NULL || javaWindowObject == NULL) { return; } @@ -329,11 +374,14 @@ static jint mods2JavaMods(NSUInteger mods) - (void) windowDidResignKey: (NSNotification *) notification { - if (env == NULL) { + NSView* nsview = [self contentView]; + if( ! [nsview isMemberOfClass:[NewtView class]] ) { return; } - - if (javaWindowObject == NULL) { + NewtView* view = (NewtView *) nsview; + jobject javaWindowObject = [view getJavaWindowObject]; + JNIEnv* env = [view getJNIEnv]; + if (env==NULL || javaWindowObject == NULL) { return; } |