📄 hiwebview.m
字号:
return 1; else return kControlNoPart;}//----------------------------------------------------------------------------------// GetRegion//----------------------------------------------------------------------------------//static OSStatusGetRegion( HIWebView* inView, ControlPartCode inPart, RgnHandle outRgn ){ OSStatus err = eventNotHandledErr; if ( inPart == -3 ) // kControlOpaqueMetaPart: { if ( [inView->fWebView isOpaque] ) { HIRect bounds; Rect temp; HIViewGetBounds( inView->fViewRef, &bounds ); temp.top = (SInt16)bounds.origin.y; temp.left = (SInt16)bounds.origin.x; temp.bottom = (SInt16)CGRectGetMaxY( bounds ); temp.right = (SInt16)CGRectGetMaxX( bounds ); RectRgn( outRgn, &temp ); err = noErr; } } return err;}static WindowRefGetWindowRef( HIWebView* inView ){ return GetControlOwner( inView->fViewRef );}//----------------------------------------------------------------------------------// Click//----------------------------------------------------------------------------------//static OSStatusClick(HIWebView* inView, EventRef inEvent){ NSEvent *kitEvent = WKCreateNSEventWithCarbonClickEvent(inEvent, GetWindowRef(inView)); if (!inView->fIsComposited) StartUpdateObserver(inView); [inView->fKitWindow sendEvent:kitEvent]; if (!inView->fIsComposited) StopUpdateObserver(inView); [kitEvent release]; return noErr;}//----------------------------------------------------------------------------------// MouseUp//----------------------------------------------------------------------------------//static OSStatusMouseUp( HIWebView* inView, EventRef inEvent ){ NSEvent* kitEvent = WKCreateNSEventWithCarbonEvent(inEvent); [inView->fKitWindow sendEvent:kitEvent]; [kitEvent release]; return noErr;}//----------------------------------------------------------------------------------// MouseMoved//----------------------------------------------------------------------------------//static OSStatusMouseMoved( HIWebView* inView, EventRef inEvent ){ NSEvent *kitEvent = WKCreateNSEventWithCarbonMouseMoveEvent(inEvent, inView->fKitWindow); [inView->fKitWindow sendEvent:kitEvent]; [kitEvent release]; return noErr;}//----------------------------------------------------------------------------------// MouseDragged//----------------------------------------------------------------------------------//static OSStatusMouseDragged( HIWebView* inView, EventRef inEvent ){ NSEvent* kitEvent = WKCreateNSEventWithCarbonEvent(inEvent); [inView->fKitWindow sendEvent:kitEvent]; [kitEvent release]; return noErr;}//----------------------------------------------------------------------------------// MouseDragged//----------------------------------------------------------------------------------//static OSStatusMouseWheelMoved( HIWebView* inView, EventRef inEvent ){ NSEvent* kitEvent = WKCreateNSEventWithCarbonEvent(inEvent); [inView->fKitWindow sendEvent:kitEvent]; [kitEvent release]; return noErr;}//----------------------------------------------------------------------------------// ContextMenuClick//----------------------------------------------------------------------------------//static OSStatusContextMenuClick( HIWebView* inView, EventRef inEvent ){ NSView *webView = inView->fWebView; NSWindow *window = [webView window]; // Get the point out of the event. HIPoint point; GetEventParameter(inEvent, kEventParamMouseLocation, typeHIPoint, NULL, sizeof(point), NULL, &point); HIViewConvertPoint(&point, inView->fViewRef, NULL); // Flip the Y coordinate, since Carbon is flipped relative to the AppKit. NSPoint location = NSMakePoint(point.x, [window frame].size.height - point.y); // Make up an event with the point and send it to the window. NSEvent *kitEvent = [NSEvent mouseEventWithType:NSRightMouseDown location:location modifierFlags:0 timestamp:GetEventTime(inEvent) windowNumber:[window windowNumber] context:0 eventNumber:0 clickCount:1 pressure:0]; [inView->fKitWindow sendEvent:kitEvent]; return noErr;}//----------------------------------------------------------------------------------// GetKind//----------------------------------------------------------------------------------//static ControlKindGetKind(){ const ControlKind kMyKind = { 'appl', 'wbvw' }; return kMyKind;}//----------------------------------------------------------------------------------// BoundsChanged//----------------------------------------------------------------------------------//static voidBoundsChanged( HIWebView* inView, UInt32 inOptions, const HIRect* inOriginalBounds, const HIRect* inCurrentBounds ){ if ( inView->fWebView ) { SyncFrame( inView ); }}//----------------------------------------------------------------------------------// OwningWindowChanged//----------------------------------------------------------------------------------//static voidOwningWindowChanged( HIWebView* view, WindowRef oldWindow, WindowRef newWindow ){ if ( newWindow ){ WindowAttributes attrs; OSStatus err = GetWindowProperty(newWindow, NSAppKitPropertyCreator, NSCarbonWindowPropertyTag, sizeof(NSWindow *), NULL, &view->fKitWindow); if ( err != noErr ) { const EventTypeSpec kWindowEvents[] = { { kEventClassWindow, kEventWindowClosed }, { kEventClassMouse, kEventMouseMoved }, { kEventClassMouse, kEventMouseUp }, { kEventClassMouse, kEventMouseDragged }, { kEventClassMouse, kEventMouseWheelMoved }, { kEventClassKeyboard, kEventRawKeyDown }, { kEventClassKeyboard, kEventRawKeyRepeat }, { kEventClassKeyboard, kEventRawKeyUp }, { kEventClassControl, kEventControlClick }, }; view->fKitWindow = [[CarbonWindowAdapter alloc] initWithCarbonWindowRef: newWindow takingOwnership: NO disableOrdering:NO carbon:YES]; SetWindowProperty(newWindow, NSAppKitPropertyCreator, NSCarbonWindowPropertyTag, sizeof(NSWindow *), &view->fKitWindow); InstallWindowEventHandler( newWindow, WindowHandler, GetEventTypeCount( kWindowEvents ), kWindowEvents, newWindow, NULL ); } [[view->fKitWindow contentView] addSubview:view->fWebView]; GetWindowAttributes( newWindow, &attrs ); view->fIsComposited = ( ( attrs & kWindowCompositingAttribute ) != 0 ); SyncFrame( view ); } else { // Be sure to detach the cocoa view, too. if ( view->fWebView ) [view->fWebView removeFromSuperview]; view->fKitWindow = NULL; // break the ties that bind }}//-------------------------------------------------------------------------------------// WindowHandler//-------------------------------------------------------------------------------------// Redirect mouse events to the views beneath them. This is required for WebKit to work// properly. We install it once per window. We also tap into window close to release// the NSWindow that shadows our Carbon window.//static OSStatusWindowHandler( EventHandlerCallRef inCallRef, EventRef inEvent, void* inUserData ){ WindowRef window = (WindowRef)inUserData; OSStatus result = eventNotHandledErr; switch( GetEventClass( inEvent ) ) { case kEventClassControl: { switch( GetEventKind( inEvent ) ) { case kEventControlClick: { CarbonWindowAdapter *kitWindow; OSStatus err; err = GetWindowProperty( window, NSAppKitPropertyCreator, NSCarbonWindowPropertyTag, sizeof(NSWindow *), NULL, &kitWindow); // We must be outside the HIWebView, relinquish focus. [kitWindow relinquishFocus]; } break; } } break; case kEventClassKeyboard: { NSWindow* kitWindow; OSStatus err; NSEvent* kitEvent; // if the first responder in the kit window is something other than the // window, we assume a subview of the webview is focused. we must send // the event to the window so that it goes through the kit's normal TSM // logic, and -- more importantly -- allows any delegates associated // with the first responder to have a chance at the event. err = GetWindowProperty( window, NSAppKitPropertyCreator, NSCarbonWindowPropertyTag, sizeof(NSWindow *), NULL, &kitWindow); if ( err == noErr ) { NSResponder* responder = [kitWindow firstResponder]; if ( responder != kitWindow ) { kitEvent = WKCreateNSEventWithCarbonEvent(inEvent); [kitWindow sendEvent:kitEvent]; [kitEvent release]; result = noErr; } } } break; case kEventClassWindow: { NSWindow* kitWindow; OSStatus err; err = GetWindowProperty( window, NSAppKitPropertyCreator, NSCarbonWindowPropertyTag, sizeof(NSWindow *), NULL, &kitWindow); if ( err == noErr ) { [kitWindow _removeWindowRef]; [kitWindow close]; } result = noErr; } break; case kEventClassMouse: switch (GetEventKind(inEvent)) { case kEventMouseMoved: { Point where; GetEventParameter(inEvent, kEventParamMouseLocation, typeQDPoint, NULL, sizeof(Point), NULL, &where); WindowRef temp; FindWindow(where, &temp); if (temp == window) { Rect bounds; GetWindowBounds(window, kWindowStructureRgn, &bounds); where.h -= bounds.left; where.v -= bounds.top; SetEventParameter(inEvent, kEventParamWindowRef, typeWindowRef, sizeof(WindowRef), &window); SetEventParameter(inEvent, kEventParamWindowMouseLocation, typeQDPoint, sizeof(Point), &where); OSStatus err = noErr; HIViewRef view = NULL; err = HIViewGetViewForMouseEvent(HIViewGetRoot(window), inEvent, &view); if (err == noErr && view && HIObjectIsOfClass((HIObjectRef)view, kHIWebViewClassID)) result = SendEventToEventTargetWithOptions(inEvent, HIObjectGetEventTarget((HIObjectRef)view), kEventTargetDontPropagate); } } break; case kEventMouseUp: case kEventMouseDragged: case kEventMouseWheelMoved: { OSStatus err = noErr; HIViewRef view = NULL; err = HIViewGetViewForMouseEvent(HIViewGetRoot(window), inEvent, &view); if (err == noErr && view && HIObjectIsOfClass((HIObjectRef)view, kHIWebViewClassID)) result = SendEventToEventTargetWithOptions(inEvent, HIObjectGetEventTarget((HIObjectRef)view), kEventTargetDontPropagate); } break; } break; } return result;}//----------------------------------------------------------------------------------// SyncFrame//----------------------------------------------------------------------------------//static voidSyncFrame( HIWebView* inView ){ HIViewRef parent = HIViewGetSuperview( inView->fViewRef ); if ( parent ) { if ( inView->fIsComposited ) { HIRect frame; HIRect parentBounds; NSPoint origin; HIViewGetFrame( inView->fViewRef, &frame ); HIViewGetBounds( parent, &parentBounds ); origin.x = frame.origin.x; origin.y = parentBounds.size.height - CGRectGetMaxY( frame );// printf( "syncing to (%g %g) (%g %g)\n", origin.x, origin.y,// frame.size.width, frame.size.height ); [inView->fWebView setFrameOrigin: origin]; [inView->fWebView setFrameSize: *(NSSize*)&frame.size]; } else { GrafPtr port = GetWindowPort( GetControlOwner( inView->fViewRef ) ); PixMapHandle portPix = GetPortPixMap( port ); Rect bounds; HIRect rootFrame; HIRect frame;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -