- Timestamp:
- 03/02/09 15:53:04 (4 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/branches/osg-cocoa-dev/src/osgViewer/GraphicsWindowCocoa.mm
r9831 r9836 131 131 132 132 133 /** remaps a native os x keycode to a GUIEventAdapter-keycode */ 133 // ---------------------------------------------------------------------------------------------------------- 134 // remapCocoaKey 135 // ---------------------------------------------------------------------------------------------------------- 134 136 static unsigned int remapCocoaKey(unsigned int key, bool pressedOnKeypad = false) 135 137 { … … 140 142 141 143 // ---------------------------------------------------------------------------------------------------------- 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 150 static 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 158 static 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 // ---------------------------------------------------------------------------------------------------------- 142 169 // the app-delegate, handling quit-requests 143 170 // ---------------------------------------------------------------------------------------------------------- … … 154 181 { 155 182 s_quit_requested = true; 156 std::cout << "quit requested " << std::endl;183 // std::cout << "quit requested " << std::endl; 157 184 return NSTerminateNow; 158 185 } … … 160 187 @end 161 188 162 163 164 // ---------------------------------------------------------------------------------------------------------- 165 // GraphicsWindowCocoaWindow 189 #pragma mark GraphicsWindowCocoaWindow 190 191 // ---------------------------------------------------------------------------------------------------------- 192 // GraphicsWindowCocoaWindow, implements canBecomeKeyWindow + canBecomeMainWindow 166 193 // ---------------------------------------------------------------------------------------------------------- 167 194 … … 190 217 @end 191 218 219 #pragma mark GraphicsWindowCocoaGLView 220 221 192 222 // ---------------------------------------------------------------------------------------------------------- 193 223 // 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 // ---------------------------------------------------------------------------------------------------------- 196 227 197 228 @interface GraphicsWindowCocoaGLView : NSOpenGLView … … 587 618 588 619 620 589 621 @end 590 622 … … 606 638 - (id)initWith: (osgViewer::GraphicsWindowCocoa*) win; 607 639 - (void)windowDidMove:(NSNotification *)notification; 640 - (void)windowDidResize:(NSNotification *)notification; 608 641 - (BOOL)windowShouldClose:(id)window; 642 - (void)updateWindowBounds; 609 643 610 644 @end 645 611 646 612 647 @implementation GraphicsWindowCocoaDelegate … … 619 654 } 620 655 656 621 657 - (void)windowDidMove:(NSNotification *)notification 658 { 659 [self updateWindowBounds]; 660 } 661 662 - (void)windowDidResize:(NSNotification *)notification 663 { 664 [self updateWindowBounds]; 665 } 666 667 -(void)updateWindowBounds 622 668 { 623 669 if (_inDidMove) return; 624 670 _inDidMove = true; 671 625 672 GraphicsWindowCocoaWindow* nswin = _win->getWindow(); 626 673 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; 628 679 629 680 _win->resized(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height); … … 641 692 642 693 643 #pragma mark GraphicsWindowCocoa694 #pragma mark CocoaWindowAdapter 644 695 645 696 … … 662 713 { 663 714 NSRect nsrect = [_win->getWindow() frame]; 664 715 nsrect = convertToQuartzCoordinates(_win.get(), nsrect); 716 665 717 rect.origin.x = nsrect.origin.x; 666 718 rect.origin.y = nsrect.origin.y; … … 674 726 }; 675 727 728 #pragma mark GraphicsWindowCocoa 729 730 731 732 // ---------------------------------------------------------------------------------------------------------- 733 // init 734 // ---------------------------------------------------------------------------------------------------------- 676 735 677 736 void GraphicsWindowCocoa::init() … … 687 746 688 747 748 // ---------------------------------------------------------------------------------------------------------- 749 // setupNSWindow 750 // sets up the NSWindow, adds delegates, etc 751 // ---------------------------------------------------------------------------------------------------------- 689 752 690 753 void GraphicsWindowCocoa::setupNSWindow(NSWindow* win) … … 693 756 [win setReleasedWhenClosed:NO]; 694 757 [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 697 762 [win makeKeyAndOrderFront:nil]; 698 763 [win setAcceptsMouseMovedEvents: YES]; … … 701 766 702 767 768 // ---------------------------------------------------------------------------------------------------------- 769 // realizeImplementation, creates the window + context 770 // ---------------------------------------------------------------------------------------------------------- 703 771 704 772 bool GraphicsWindowCocoa::realizeImplementation() … … 716 784 } 717 785 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 } 718 794 719 795 NSRect rect = NSMakeRect(_traits->x, _traits->y, _traits->width, _traits->height); 796 720 797 _window = [[GraphicsWindowCocoaWindow alloc] initWithContentRect: rect styleMask: style backing: NSBackingStoreBuffered defer: NO]; 721 798 722 799 if (!_window) { 723 osg::notify(osg::WARN) << "GraphicsWindowC arbon::realizeImplementation :: could not create window" << std::endl;800 osg::notify(osg::WARN) << "GraphicsWindowCocoa::realizeImplementation :: could not create window" << std::endl; 724 801 return false; 725 802 } 726 803 804 rect = convertFromQuartzCoordinates(this, rect); 805 [_window setFrameOrigin: rect.origin]; 727 806 728 807 NSOpenGLPixelFormatAttribute attr[32]; … … 778 857 779 858 if (!_context) { 780 osg::notify(osg::WARN) << "GraphicsWindowC arbon::realizeImplementation :: could not create context" << std::endl;859 osg::notify(osg::WARN) << "GraphicsWindowCocoa::realizeImplementation :: could not create context" << std::endl; 781 860 return false; 782 861 } … … 810 889 811 890 812 813 814 / ** Close the graphics context. */891 // ---------------------------------------------------------------------------------------------------------- 892 // closeImplementation 893 // ---------------------------------------------------------------------------------------------------------- 815 894 void GraphicsWindowCocoa::closeImplementation() 816 895 { … … 826 905 } 827 906 828 /** Make this graphics context current.*/ 907 908 // ---------------------------------------------------------------------------------------------------------- 909 // makeCurrentImplementation 910 // ---------------------------------------------------------------------------------------------------------- 911 829 912 bool GraphicsWindowCocoa:: makeCurrentImplementation() 830 913 { … … 834 917 835 918 836 /** Release the graphics context.*/ 919 // ---------------------------------------------------------------------------------------------------------- 920 // releaseContextImplementation 921 // ---------------------------------------------------------------------------------------------------------- 922 837 923 bool GraphicsWindowCocoa::releaseContextImplementation() 838 924 { … … 842 928 843 929 844 /** Swap the front and back buffers.*/ 930 // ---------------------------------------------------------------------------------------------------------- 931 // swapBuffersImplementation 932 // ---------------------------------------------------------------------------------------------------------- 933 845 934 void GraphicsWindowCocoa::swapBuffersImplementation() 846 935 { … … 849 938 850 939 851 /** Check to see if any events have been generated.*/ 940 // ---------------------------------------------------------------------------------------------------------- 941 // checkEvents 942 // process all pending events 943 // ---------------------------------------------------------------------------------------------------------- 852 944 void GraphicsWindowCocoa::checkEvents() 853 945 { … … 885 977 886 978 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 899 987 bool GraphicsWindowCocoa::setWindowDecorationImplementation(bool flag) 900 988 { 901 989 if (!_realized) return false; 902 990 903 // unfortunately there's no way to change the decoration of a window, so we create an new one and swap the content904 991 unsigned int style(NSBorderlessWindowMask); 905 992 … … 924 1011 [_window makeKeyAndOrderFront: nil]; 925 1012 } 926 927 1013 928 1014 return true; … … 930 1016 931 1017 932 /** Get focus.*/ 1018 // ---------------------------------------------------------------------------------------------------------- 1019 // grabFocus 1020 // ---------------------------------------------------------------------------------------------------------- 933 1021 void GraphicsWindowCocoa::grabFocus() 934 1022 { … … 937 1025 938 1026 939 /** Get focus on if the pointer is in this window.*/ 1027 // ---------------------------------------------------------------------------------------------------------- 1028 // grabFocusIfPointerInWindow 1029 // ---------------------------------------------------------------------------------------------------------- 940 1030 void GraphicsWindowCocoa::grabFocusIfPointerInWindow() 941 1031 { 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 // ---------------------------------------------------------------------------------------------------------- 945 1039 946 1040 void GraphicsWindowCocoa::resizedImplementation(int x, int y, int width, int height) … … 948 1042 GraphicsContext::resizedImplementation(x, y, width, height); 949 1043 950 [_window setContentSize: NSMakeSize(width, height)];951 1044 [_context update]; 952 //[_window setFrameTopLeftPoint: NSMakePoint(x,y)];953 1045 MenubarController::instance()->update(); 954 std::cout << "GraphicsWindowCocoa :: resizedImplementation not implemented yet " << std::endl; 955 } 956 1046 } 1047 1048 1049 // ---------------------------------------------------------------------------------------------------------- 1050 // setWindowRectangleImplementation 1051 // ---------------------------------------------------------------------------------------------------------- 1052 bool 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 // ---------------------------------------------------------------------------------------------------------- 957 1070 958 1071 void GraphicsWindowCocoa::setWindowName (const std::string & name) … … 966 1079 } 967 1080 1081 1082 // ---------------------------------------------------------------------------------------------------------- 1083 // useCursor 1084 // ---------------------------------------------------------------------------------------------------------- 968 1085 969 1086 void GraphicsWindowCocoa::useCursor(bool cursorOn) … … 994 1111 995 1112 1113 // ---------------------------------------------------------------------------------------------------------- 1114 // setCursor 1115 // ---------------------------------------------------------------------------------------------------------- 1116 996 1117 void GraphicsWindowCocoa::setCursor(MouseCursor mouseCursor) 997 1118 { 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 // ---------------------------------------------------------------------------------------------------------- 1000 1147 1001 1148 void GraphicsWindowCocoa::setVSync(bool f) … … 1006 1153 1007 1154 1155 // ---------------------------------------------------------------------------------------------------------- 1156 // d'tor 1157 // ---------------------------------------------------------------------------------------------------------- 1158 1008 1159 GraphicsWindowCocoa::~GraphicsWindowCocoa() 1009 1160 { … … 1012 1163 1013 1164 1014 1165 #pragma mark CocoaWindowingSystemInterface 1166 1167 // ---------------------------------------------------------------------------------------------------------- 1168 // CocoaWindowingSystemInterface 1169 // ---------------------------------------------------------------------------------------------------------- 1015 1170 1016 1171 struct CocoaWindowingSystemInterface : public DarwinWindowingSystemInterface {
