[futurebasic] HICocoaView in FB

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : October 2007 : Group Archive : Group : All Groups

From: Robert Purves <listrp@...>
Date: Wed, 31 Oct 2007 23:50:23 +1300
Apple's Carbon HIToolbox release notes for OS X 10.5 announce that "a  
new HIView, HICocoaView, allows wrapping an arbitrary NSView into an  
HIView so that the NSView can be used inside a Carbon window."

I've prepared a small demo to show how this new tool assists the  
integration of Cocoa into Carbon. The demo gives a fully functioning  
WebView in a Carbon window.
<http://4toc.com/fb4/HICocoaView folder.zip>  [6 KB download]

To run the demo, you'll need:
- FBtoC 1a118 or later <http://4toc.com/fb4/FBtoC_Preview1a118.zip>
- OS X 10.5 (Leopard)
- a working internet connection (because the WebView is blank until it  
connects to www.apple.com)

Those who can't run the demo may yet find some points of interest in  
the code, such as the use of FBtoC's '#if def _PASSTHROUGH' syntax to  
wrap a handful of Objective C statements.

'-----------------------
<declarations snipped.>

local fn BuildWindow
'~'1
dim as IBNibRef   @ nib
dim as WindowRef  @ w
dim as HIViewRef  @ myHICocoaView
dim as HIViewID     kMyHICocoaViewID
dim as CFStringRef  urlText
dim myWebView as ^WebView

#if def _PASSTHROUGH // ObjC
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; //  
standard Cocoa-in-Carbon
myWebView = [[WebView alloc] init];
#endif

urlText = fn CFSTR( "http://www.apple.com" )

long if ( fn CreateNibReference( fn CFSTR( "main" ), @nib ) ==  
_noErr ) // matching the nib
  long if ( fn CreateWindowFromNib( nib, fn CFSTR( "Window" ), @w ) ==  
_noErr ) // matching the nib
   kMyHICocoaViewID.signature = _"Test": kMyHICocoaViewID.id = 1 //  
matching the nib
   ShowWindow( w )
   call HIViewFindByID( fn HIViewGetRoot( w),  
kMyHICocoaViewID.signature, kMyHICocoaViewID.id, @myHICocoaView )
   long if ( myHICocoaView  )
    call HICocoaViewSetView( myHICocoaView, #myWebView )
    #if def _PASSTHROUGH // ObjC
    [[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL: 
[NSURL URLWithString:(NSString*)urlText]]];
    #endif
   end if
   call SetUserFocusWindow( w )
  end if
  DisposeNibReference( nib )
end if

#if def _PASSTHROUGH // ObjC
[myWebView release];
[pool release]; // standard Cocoa-in-Carbon
#endif

end fn

// main program
call NSApplicationLoad() // standard Cocoa-in-Carbon
fn BuildWindow
RunApplicationEventLoop()
'--------------

Robert P.