Exemple #1
0
// add an HIImageViewRef to the parentView
HIViewRef
AddImage( HIViewRef parentView )
{
	OSStatus status = paramErr;
	HIViewRef cgImageView = NULL;
	CGImageRef cgImage = NULL;
	CGDataProviderRef cgImageProvider = NULL;
	HILayoutInfo layoutInfo; 
	CFBundleRef theAppBundle = NULL;
	CFStringRef imageFileName = NULL;
	CFURLRef imageURL = NULL;
	
	HIRect parentFrame;
	
	theAppBundle = CFBundleGetMainBundle(); 
	imageFileName = CFStringCreateWithCString( NULL /*allocator*/, "rainbowpark.jpg", kCFStringEncodingASCII ); 
	imageURL = CFBundleCopyResourceURL( theAppBundle, imageFileName, NULL, NULL );

	cgImageProvider = CGDataProviderCreateWithURL(imageURL);
	
	require( cgImageProvider != NULL, FAIL );
	cgImage = CGImageCreateWithJPEGDataProvider( cgImageProvider, NULL, false, kCGRenderingIntentDefault );
	
	require( cgImage != NULL, FAIL );
	
	CGDataProviderRelease( cgImageProvider );
	CFRelease( imageFileName );
	
	HIImageViewCreate( cgImage, &cgImageView );
	CGImageRelease( cgImage);
	
	require( cgImageView != NULL, FAIL );
	
	layoutInfo.binding.left.kind = kHILayoutBindLeft;
	layoutInfo.binding.left.toView = NULL;
	layoutInfo.binding.left.offset = 0.0;
	layoutInfo.binding.top.kind = kHILayoutBindTop;
	layoutInfo.binding.top.toView = NULL;
	layoutInfo.binding.top.offset = 0.0;
	layoutInfo.binding.right.kind = kHILayoutBindRight;
	layoutInfo.binding.right.toView = NULL;
	layoutInfo.binding.right.offset = 0.0;
	layoutInfo.binding.bottom.kind = kHILayoutBindBottom;
	layoutInfo.binding.bottom.toView = NULL;
	layoutInfo.binding.bottom.offset = 0.0;
	verify_noerr( HIViewSetLayoutInfo(cgImageView, &layoutInfo) );
	verify_noerr( HIImageViewSetScaleToFit(cgImageView, true) );
	status = HIViewAddSubview(parentView, cgImageView);
	
	if( status != noErr )
	{
		CFRelease( cgImageView );
		cgImageView = NULL;
	}
	
	FAIL:
	return cgImageView;

}
SkOSWindow::SkOSWindow(void* hWnd) : fHWND(hWnd), fAGLCtx(NULL)
{
	OSStatus    result;
    WindowRef   wr = (WindowRef)hWnd;

    HIViewRef imageView, parent;
    HIViewRef rootView = HIViewGetRoot(wr);
    HIViewFindByID(rootView, kHIViewWindowContentID, &parent);
    result = HIImageViewCreate(NULL, &imageView);
	SkASSERT(result == noErr);

    result = HIViewAddSubview(parent, imageView);
	SkASSERT(result == noErr);

    fHVIEW = imageView;

    HIViewSetVisible(imageView, true);
    HIViewPlaceInSuperviewAt(imageView, 0, 0);

    if (true) {
        HILayoutInfo layout;
        layout.version = kHILayoutInfoVersionZero;
        set_bindingside(&layout.binding.left, parent, kHILayoutBindLeft);
        set_bindingside(&layout.binding.top, parent, kHILayoutBindTop);
        set_bindingside(&layout.binding.right, parent, kHILayoutBindRight);
        set_bindingside(&layout.binding.bottom, parent, kHILayoutBindBottom);
        set_axisscale(&layout.scale.x, parent);
        set_axisscale(&layout.scale.y, parent);
        set_axisposition(&layout.position.x, parent, kHILayoutPositionLeft);
        set_axisposition(&layout.position.y, rootView, kHILayoutPositionTop);
        HIViewSetLayoutInfo(imageView, &layout);
    }

    HIImageViewSetOpaque(imageView, true);
    HIImageViewSetScaleToFit(imageView, false);

	static const EventTypeSpec  gTypes[] = {
		{ kEventClassKeyboard,  kEventRawKeyDown			},
        { kEventClassKeyboard,  kEventRawKeyUp              },
		{ kEventClassMouse,		kEventMouseDown				},
		{ kEventClassMouse,		kEventMouseDragged			},
		{ kEventClassMouse,		kEventMouseMoved			},
		{ kEventClassMouse,		kEventMouseUp				},
		{ kEventClassTextInput, kEventTextInputUnicodeForKeyEvent   },
		{ kEventClassWindow,	kEventWindowBoundsChanged	},
//		{ kEventClassWindow,	kEventWindowDrawContent		},
		{ SK_MacEventClass,		SK_MacEventKind				}
	};

	EventHandlerUPP handlerUPP = NewEventHandlerUPP(SkOSWindow::EventHandler);
	int				count = SK_ARRAY_COUNT(gTypes);

	result = InstallEventHandler(GetWindowEventTarget(wr), handlerUPP,
						count, gTypes, this, nil);
	SkASSERT(result == noErr);

	gCurrOSWin = this;
	gCurrEventQ = GetCurrentEventQueue();
	gEventTarget = GetWindowEventTarget(wr);

	static bool gOnce = true;
	if (gOnce) {
		gOnce = false;
		gPrevNewHandler = set_new_handler(sk_new_handler);
	}
}