Show
Ignore:
Timestamp:
03/02/09 15:53:04 (5 years ago)
Author:
shuber
Message:

* fixed several issues with GraphicsWindowCocoa?, correct window-placement + resizing,
* first cut of PixelBufferCocoa?

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/branches/osg-cocoa-dev/src/osgViewer/GraphicsWindowCocoa.mm

    r9831 r9836  
    131131 
    132132 
    133 /** remaps a native os x keycode to a GUIEventAdapter-keycode */ 
     133// ---------------------------------------------------------------------------------------------------------- 
     134// remapCocoaKey 
     135// ---------------------------------------------------------------------------------------------------------- 
    134136static unsigned int remapCocoaKey(unsigned int key, bool pressedOnKeypad = false) 
    135137{ 
     
    140142 
    141143// ---------------------------------------------------------------------------------------------------------- 
     144// Cocoa uses a coordinate system where its origin is in the bottom left corner,  
     145// osg and quartz uses top left for the origin 
     146// 
     147// these 2 methods convets rects between the different coordinate systems 
     148// ---------------------------------------------------------------------------------------------------------- 
     149 
     150static NSRect convertFromQuartzCoordinates(osgViewer::GraphicsWindowCocoa* win,const NSRect& rect)   
     151{ 
     152    NSRect frame = [[win->getWindow() screen] frame]; 
     153     
     154    float y = frame.size.height - rect.origin.y - rect.size.height; 
     155    return NSMakeRect(rect.origin.x, y, rect.size.width, rect.size.height); 
     156} 
     157 
     158static NSRect convertToQuartzCoordinates(osgViewer::GraphicsWindowCocoa* win,const NSRect& rect) 
     159{ 
     160    NSRect frame = [[win->getWindow() screen] frame]; 
     161     
     162    float y = frame.size.height - (rect.origin.y + rect.size.height); 
     163    return NSMakeRect(rect.origin.x, y, rect.size.width, rect.size.height); 
     164} 
     165 
     166#pragma mark CocoaAppDelegate 
     167 
     168// ---------------------------------------------------------------------------------------------------------- 
    142169// the app-delegate, handling quit-requests 
    143170// ---------------------------------------------------------------------------------------------------------- 
     
    154181{ 
    155182    s_quit_requested = true; 
    156     std::cout << "quit requested " << std::endl; 
     183    // std::cout << "quit requested " << std::endl; 
    157184    return NSTerminateNow; 
    158185} 
     
    160187@end 
    161188 
    162  
    163  
    164 // ---------------------------------------------------------------------------------------------------------- 
    165 // GraphicsWindowCocoaWindow 
     189#pragma mark GraphicsWindowCocoaWindow 
     190 
     191// ---------------------------------------------------------------------------------------------------------- 
     192// GraphicsWindowCocoaWindow, implements canBecomeKeyWindow + canBecomeMainWindow 
    166193// ---------------------------------------------------------------------------------------------------------- 
    167194 
     
    190217@end 
    191218 
     219#pragma mark GraphicsWindowCocoaGLView 
     220 
     221 
    192222// ---------------------------------------------------------------------------------------------------------- 
    193223// GraphicsWindowCocoaGLView 
    194 // ---------------------------------------------------------------------------------------------------------- 
    195  
     224// custom NSOpenGLView-class handling mouse- and keyboard-events, forwarding them to the EventQueue 
     225// some code borrowed from the example osgCocoaViewer from E.Wing 
     226// ---------------------------------------------------------------------------------------------------------- 
    196227 
    197228@interface GraphicsWindowCocoaGLView : NSOpenGLView 
     
    587618 
    588619 
     620 
    589621@end 
    590622 
     
    606638- (id)initWith: (osgViewer::GraphicsWindowCocoa*) win; 
    607639- (void)windowDidMove:(NSNotification *)notification; 
     640- (void)windowDidResize:(NSNotification *)notification; 
    608641- (BOOL)windowShouldClose:(id)window; 
     642- (void)updateWindowBounds; 
    609643 
    610644@end 
     645 
    611646 
    612647@implementation GraphicsWindowCocoaDelegate 
     
    619654} 
    620655 
     656 
    621657- (void)windowDidMove:(NSNotification *)notification 
     658{ 
     659    [self updateWindowBounds]; 
     660} 
     661 
     662- (void)windowDidResize:(NSNotification *)notification 
     663{ 
     664    [self updateWindowBounds]; 
     665} 
     666 
     667-(void)updateWindowBounds 
    622668{ 
    623669    if (_inDidMove) return; 
    624670    _inDidMove = true; 
     671     
    625672    GraphicsWindowCocoaWindow* nswin = _win->getWindow(); 
    626673    NSRect bounds = [nswin contentRectForFrameRect: [nswin frame] ]; 
    627     std::cout << "windowdidmove: " << bounds.origin.x << " " << bounds.origin.y << " " << bounds.size.width << " " << bounds.size.height << std::endl; 
     674     
     675    // convert to quartz-coordinate-system 
     676    bounds = convertToQuartzCoordinates(_win, bounds); 
     677     
     678    // std::cout << "windowdidmove: " << bounds.origin.x << " " << bounds.origin.y << " " << bounds.size.width << " " << bounds.size.height << std::endl; 
    628679    
    629680    _win->resized(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height); 
     
    641692 
    642693 
    643 #pragma mark GraphicsWindowCocoa 
     694#pragma mark CocoaWindowAdapter 
    644695 
    645696 
     
    662713    { 
    663714        NSRect nsrect = [_win->getWindow() frame]; 
    664  
     715        nsrect = convertToQuartzCoordinates(_win.get(), nsrect); 
     716         
    665717        rect.origin.x = nsrect.origin.x; 
    666718        rect.origin.y = nsrect.origin.y; 
     
    674726}; 
    675727 
     728#pragma mark GraphicsWindowCocoa 
     729 
     730 
     731 
     732// ---------------------------------------------------------------------------------------------------------- 
     733// init 
     734// ---------------------------------------------------------------------------------------------------------- 
    676735 
    677736void GraphicsWindowCocoa::init() 
     
    687746 
    688747 
     748// ---------------------------------------------------------------------------------------------------------- 
     749// setupNSWindow 
     750// sets up the NSWindow, adds delegates, etc 
     751// ---------------------------------------------------------------------------------------------------------- 
    689752 
    690753void GraphicsWindowCocoa::setupNSWindow(NSWindow* win) 
     
    693756    [win setReleasedWhenClosed:NO]; 
    694757        [win setDisplaysWhenScreenProfileChanges:YES];   
    695         [win setDelegate: [[GraphicsWindowCocoaDelegate alloc] initWith: this] ]; 
    696          
     758    GraphicsWindowCocoaDelegate* delegate = [[GraphicsWindowCocoaDelegate alloc] initWith: this]; 
     759    [win setDelegate: delegate ]; 
     760    //[delegate autorelease]; 
     761             
    697762    [win makeKeyAndOrderFront:nil]; 
    698763    [win setAcceptsMouseMovedEvents: YES]; 
     
    701766 
    702767 
     768// ---------------------------------------------------------------------------------------------------------- 
     769// realizeImplementation, creates the window + context 
     770// ---------------------------------------------------------------------------------------------------------- 
    703771 
    704772bool GraphicsWindowCocoa::realizeImplementation() 
     
    716784    } 
    717785     
     786    DarwinWindowingSystemInterface* wsi = dynamic_cast<DarwinWindowingSystemInterface*>(osg::GraphicsContext::getWindowingSystemInterface()); 
     787    int screenLeft(0), screenTop(0); 
     788    if (wsi) { 
     789         
     790        wsi->getScreenTopLeft((*_traits), screenLeft, screenTop); 
     791        _traits->y += screenTop; 
     792        _traits->x += screenLeft; 
     793    } 
    718794     
    719795        NSRect rect = NSMakeRect(_traits->x, _traits->y, _traits->width, _traits->height); 
     796     
    720797        _window = [[GraphicsWindowCocoaWindow alloc] initWithContentRect: rect styleMask: style backing: NSBackingStoreBuffered defer: NO]; 
    721798         
    722799    if (!_window) { 
    723         osg::notify(osg::WARN) << "GraphicsWindowCarbon::realizeImplementation :: could not create window" << std::endl; 
     800        osg::notify(osg::WARN) << "GraphicsWindowCocoa::realizeImplementation :: could not create window" << std::endl; 
    724801        return false; 
    725802    } 
    726803     
     804    rect = convertFromQuartzCoordinates(this,  rect); 
     805    [_window setFrameOrigin: rect.origin]; 
    727806          
    728807        NSOpenGLPixelFormatAttribute attr[32]; 
     
    778857     
    779858    if (!_context) { 
    780         osg::notify(osg::WARN) << "GraphicsWindowCarbon::realizeImplementation :: could not create context" << std::endl; 
     859        osg::notify(osg::WARN) << "GraphicsWindowCocoa::realizeImplementation :: could not create context" << std::endl; 
    781860        return false; 
    782861    } 
     
    810889 
    811890 
    812  
    813  
    814 /** Close the graphics context. */ 
     891// ---------------------------------------------------------------------------------------------------------- 
     892// closeImplementation 
     893// ---------------------------------------------------------------------------------------------------------- 
    815894void GraphicsWindowCocoa::closeImplementation() 
    816895{ 
     
    826905} 
    827906 
    828 /** Make this graphics context current.*/ 
     907 
     908// ---------------------------------------------------------------------------------------------------------- 
     909// makeCurrentImplementation 
     910// ---------------------------------------------------------------------------------------------------------- 
     911 
    829912bool GraphicsWindowCocoa:: makeCurrentImplementation() 
    830913{ 
     
    834917 
    835918 
    836 /** Release the graphics context.*/ 
     919// ---------------------------------------------------------------------------------------------------------- 
     920// releaseContextImplementation 
     921// ---------------------------------------------------------------------------------------------------------- 
     922 
    837923bool GraphicsWindowCocoa::releaseContextImplementation() 
    838924{ 
     
    842928 
    843929 
    844 /** Swap the front and back buffers.*/ 
     930// ---------------------------------------------------------------------------------------------------------- 
     931// swapBuffersImplementation 
     932// ---------------------------------------------------------------------------------------------------------- 
     933 
    845934void GraphicsWindowCocoa::swapBuffersImplementation() 
    846935{ 
     
    849938 
    850939 
    851 /** Check to see if any events have been generated.*/ 
     940// ---------------------------------------------------------------------------------------------------------- 
     941// checkEvents 
     942// process all pending events  
     943// ---------------------------------------------------------------------------------------------------------- 
    852944void GraphicsWindowCocoa::checkEvents() 
    853945{ 
     
    885977 
    886978 
    887 /** Set the window's position and size.*/ 
    888 bool GraphicsWindowCocoa::setWindowRectangleImplementation(int x, int y, int width, int height) 
    889 { 
    890     std::cout << "GraphicsWindowCocoa :: realizeImplementation not implemented yet " << std::endl; 
    891          
    892     MenubarController::instance()->update(); 
    893      
    894     return true; 
    895 } 
    896  
    897  
    898 /** Set Window decoration.*/ 
     979 
     980// ---------------------------------------------------------------------------------------------------------- 
     981// setWindowDecorationImplementation 
     982// 
     983// unfortunately there's no way to change the decoration of a window, so we create an new one  
     984// and swap the content 
     985// ---------------------------------------------------------------------------------------------------------- 
     986 
    899987bool GraphicsWindowCocoa::setWindowDecorationImplementation(bool flag) 
    900988{ 
    901989    if (!_realized) return false; 
    902990         
    903     // unfortunately there's no way to change the decoration of a window, so we create an new one and swap the content 
    904991    unsigned int style(NSBorderlessWindowMask); 
    905992     
     
    9241011        [_window makeKeyAndOrderFront: nil]; 
    9251012    } 
    926  
    9271013     
    9281014        return true; 
     
    9301016 
    9311017 
    932 /** Get focus.*/ 
     1018// ---------------------------------------------------------------------------------------------------------- 
     1019// grabFocus 
     1020// ---------------------------------------------------------------------------------------------------------- 
    9331021void GraphicsWindowCocoa::grabFocus() 
    9341022{ 
     
    9371025 
    9381026 
    939 /** Get focus on if the pointer is in this window.*/ 
     1027// ---------------------------------------------------------------------------------------------------------- 
     1028// grabFocusIfPointerInWindow 
     1029// ---------------------------------------------------------------------------------------------------------- 
    9401030void GraphicsWindowCocoa::grabFocusIfPointerInWindow() 
    9411031{ 
    942         std::cout << "GraphicsWindowCocoa :: grabFocusIfPointerInWindow not implemented yet " << std::endl; 
    943 } 
    944  
     1032        osg::notify(osg::INFO) << "GraphicsWindowCocoa :: grabFocusIfPointerInWindow not implemented yet " << std::endl; 
     1033} 
     1034 
     1035 
     1036// ---------------------------------------------------------------------------------------------------------- 
     1037// resizedImplementation 
     1038// ---------------------------------------------------------------------------------------------------------- 
    9451039 
    9461040void GraphicsWindowCocoa::resizedImplementation(int x, int y, int width, int height) 
     
    9481042        GraphicsContext::resizedImplementation(x, y, width, height); 
    9491043     
    950     [_window setContentSize: NSMakeSize(width, height)]; 
    9511044    [_context update]; 
    952     //[_window setFrameTopLeftPoint: NSMakePoint(x,y)];    
    9531045    MenubarController::instance()->update(); 
    954     std::cout << "GraphicsWindowCocoa :: resizedImplementation not implemented yet " << std::endl; 
    955 } 
    956  
     1046} 
     1047 
     1048 
     1049// ---------------------------------------------------------------------------------------------------------- 
     1050// setWindowRectangleImplementation 
     1051// ---------------------------------------------------------------------------------------------------------- 
     1052bool GraphicsWindowCocoa::setWindowRectangleImplementation(int x, int y, int width, int height) 
     1053{ 
     1054    
     1055    NSRect rect = NSMakeRect(x,y,width, height); 
     1056    rect = convertFromQuartzCoordinates(this, rect); 
     1057     
     1058    [_window setFrame: [NSWindow frameRectForContentRect: rect styleMask: [_window styleMask]] display: YES]; 
     1059    [_context update]; 
     1060    MenubarController::instance()->update(); 
     1061     
     1062    return true; 
     1063} 
     1064 
     1065 
     1066 
     1067// ---------------------------------------------------------------------------------------------------------- 
     1068// setWindowName 
     1069// ---------------------------------------------------------------------------------------------------------- 
    9571070 
    9581071void GraphicsWindowCocoa::setWindowName (const std::string & name) 
     
    9661079} 
    9671080 
     1081 
     1082// ---------------------------------------------------------------------------------------------------------- 
     1083// useCursor 
     1084// ---------------------------------------------------------------------------------------------------------- 
    9681085 
    9691086void GraphicsWindowCocoa::useCursor(bool cursorOn) 
     
    9941111 
    9951112 
     1113// ---------------------------------------------------------------------------------------------------------- 
     1114// setCursor 
     1115// ---------------------------------------------------------------------------------------------------------- 
     1116 
    9961117void GraphicsWindowCocoa::setCursor(MouseCursor mouseCursor) 
    9971118{ 
    998         std::cout << "GraphicsWindowCocoa :: setCursor not implemented yet " << std::endl; 
    999 } 
     1119        switch (mouseCursor)  
     1120    { 
     1121 
     1122        case NoCursor: 
     1123            [NSCursor hide]; 
     1124            break; 
     1125     
     1126        case LeftArrowCursor: 
     1127            [[NSCursor arrowCursor] set]; 
     1128            break; 
     1129         
     1130        case TextCursor: 
     1131            [[NSCursor IBeamCursor] set]; 
     1132            break; 
     1133             
     1134        case CrosshairCursor: 
     1135            [[NSCursor crosshairCursor] set]; 
     1136            break; 
     1137         
     1138        default: 
     1139            osg::notify(osg::INFO) << "GraphicsWindowCocoa::setCursor :: unsupported MouseCursor: " << mouseCursor << std::endl;     
     1140    } 
     1141} 
     1142 
     1143 
     1144// ---------------------------------------------------------------------------------------------------------- 
     1145// setVSync 
     1146// ---------------------------------------------------------------------------------------------------------- 
    10001147 
    10011148void GraphicsWindowCocoa::setVSync(bool f)  
     
    10061153 
    10071154 
     1155// ---------------------------------------------------------------------------------------------------------- 
     1156// d'tor 
     1157// ---------------------------------------------------------------------------------------------------------- 
     1158 
    10081159GraphicsWindowCocoa::~GraphicsWindowCocoa()  
    10091160{ 
     
    10121163 
    10131164 
    1014  
     1165#pragma mark CocoaWindowingSystemInterface 
     1166 
     1167// ---------------------------------------------------------------------------------------------------------- 
     1168// CocoaWindowingSystemInterface 
     1169// ---------------------------------------------------------------------------------------------------------- 
    10151170 
    10161171struct CocoaWindowingSystemInterface : public DarwinWindowingSystemInterface {