Post by Roelf Toxopeushi Mike,
looking forward!
Curious how you do the objective C calls. I had a go and figured
it could be encapsulated in an OOF like syntax, perhaps.
Yes, it's something like that. The code is due to the amazing
Nao Sacrada so I don't pretend to understand the whole thing
myself, but basically we have a mechanism where we create classes
that mimic the NSxxx classes and have the correct initial ivars
so the Obj-C runtime does the right things with it. Then
Obj-C method calls use "|" instead of ":". Of course we can't
intersperse parameters with the selectors, but just put them on
the stack in the usual Mops way.
But to give you a better idea, I'll just include some code which
creates a Web browser!
Any more questions, it would be better to put straight to Nao.
Cheers, Mike.
==============================
(* ********************************************************
WebKitTest
********************************************************* *)
need cocoaCalls
syscall GetRootControl
syscall SetWindowTitleWithCFString
\ Framework calls for Carbon Webkit Utilities
Framework WebKit.framework
FrWkCall WebInitForCarbon { -- }
FrWkCall HIWebViewGetWebView { ^HIView -- ^webView }
FrWkCall HIWebViewCreate { ^view -- osstatus }
Framework Carbon.framework
FrWkCall HIViewGetBounds { ^view ^bounds -- osstatus }
FrWkCall HIViewGetFrame { ^view ^bounds -- osstatus }
FrWkCall HIViewSetFrame { ^view ^bounds -- osstatus }
FrWkCall HIViewAddSubview { ^parent ^child -- osstatus }
FrWkCall HIViewSetVisible { ^view b -- osstatus }
FrWkCall HIViewSetNeedsDisplay { ^view b -- osstatus }
FrWkCall HIViewGetNeedsDisplay { ^view -- b }
FrWkCall HIViewSetBoundsOrigin { ^view %X %y -- osstatus }
FrWkCall CFURLCreateWithString { allocator stringRef urlbase -- urlref }
\ Declarations of classes and methods in WebKit framework
@CLASS WebFrame
@CLASS WebView
@CLASS NSURLRequest
@CLASS NSDocument
+M requestWithURL| { ^nsurl -- id }
-M loadRequest| { ^request -- }
-M mainFrame { -- id }
-M goBack { -- b }
-M goForward { -- b }
-M makeTextLarger| { id -- IBAction }
-M makeTextSmaller| { id -- IBAction }
-M reload { -- }
\ -M setUIDelegate| { id -- }
\ -M UIDelegate { -- id }
-M dataSource { -- id }
:CLASS NSURLReq super{ NSMetaObj }
:m init: NSURLRequest class-id ! ;m \ In fact, it is class-init.
:m set: ( url* -- ) requestWithURL| obj-id ! ;m
;CLASS
:CLASS WebFrame_Class super{ NSMetaObj }
record
{
NSURLReq request
}
:m init:
WebFrame class-id !
init: request
;m
:m to: ( ^webframe -- ) obj-id ! ;m
:m loadit:
request @ loadRequest|
;m
:m loadrequest: ( url* -- )
set: request
loadit: self
;m
:m reload: reload ;m \ This is a Obj-C method.
:m getsource: dataSource ;m
;CLASS
:CLASS HIRECT super{ object }
\ HIRect = NSRect is composed of 4 single froats.
record
{
VAR X
VAR Y
VAR WIDTH
VAR HEIGHT
}
;CLASS
:CLASS HIVIEW super{ VAR }
:m draw: get: self Draw1Control ;m \ HIView is also Control! Hmm.
;CLASS
:CLASS WebView_CLASS super{ NSMetaObj }
record
{
WebFrame_Class myMainFrame
HIVIEW HIWEBVIEW
}
:m putmainframe: mainFrame to: myMainFrame ;m
(* :m setUIDelegate:
^base @ setUIDelegate| \ auto delegate... I don't know if it's ok.
\ Anyway, I (ns) cannot make it work yet.
;m *)
:m CreatefromHIView: ( ^HIView -- )
WebView class-id !
HIWebViewGetWebView obj-id ! \ Get NS webview from HIWebView
\ setUIDelegate: self
init: myMainframe
putmainframe: self
;m
:m loadrequest:
TEMP
{
CFString string
CFString URL \ abused again for CFRelease.
\ Maybe, we should have a CFObject superclass.
}
put: string
0 ref: string 0 CFURLCreateWithString URL !
URL @
loadrequest: myMainFrame
;m
:m reload: reload: myMainFrame ;m
:m goback: goBack drop ;m \ go viewed page back
:m goForward: goForward drop ;m \ go forward with pages.
:m contains: ( windowref -- )
TEMP{
HIRECT Bounds
HIView CONTVIEW
}
( winref ) CONTVIEW \ on compositing window,
GetRootControl drop \ ROOTControlref = HI(Content)Viewref
HIWEBVIEW HIWebViewCreate drop \ Create HIWebView
\ Set HIWebView bounds equal to ContentView bounds
get: CONTView BOUNDS HIViewGetBounds drop
get: HIWebView BOUNDS HIViewSetFrame drop
\ addview
get: contview get: HIWebView HIViewAddSubview drop
get: HIWebView dup
1 HIViewSetVisible drop
( HIWebviewRef )
CreatefromHIView: self
;m
:m enlarge: 0 makeTextLarger| drop ;m \ enlarge chars by 1 unit
:m shrink: 0 makeTextSmaller| drop ;m \ shrink chars by 1 unit
:m GetHIView: ( -- ^HIWebView ) get: HIWebView ;m
\ :m UIDelegate: UIDelegate ;m
:m draw: draw: HIWebView ;m
:m getsource: getsource: mymainframe ;m
;CLASS
-M pageTitle { -- stringref }
:CLASS NSWebDataSource super{ NSMetaObj }
:m to: obj-id ! ;m
:m getname: pageTitle ;m
;CLASS
syscall SetWindowTitleWithCFString
:CLASS WebWin super{ Window }
RECORD
{
HIRECT BOUNDS
VAR CONTVIEW
WebView_CLASS myWebView
NSWebDataSource source
VAR counter
bool redraw?
}
:m setsize:
2drop
size: portRect
getRect: self
rot -
-rot swap -
swap rot = -rot = AND ?EXIT
get: thewindowRef CONTVIEW
GetRootControl drop
get: CONTView BOUNDS HIViewGetBounds drop
GETHIVIEW: myWebView
BOUNDS HIViewSetFrame drop
set: Redraw?
;m
:m new:
new: super
WebInitforCarbon
get: thewindowRef contains: mywebview
;m
:m settitle:
getsource: mywebview ?dup 0EXIT
dup source @ = IF drop EXIT THEN
to: source
get: thewindowref getname: source SetWindowTitleWithCFString drop
;m
:m loadrequest:
loadrequest: mywebview
;m
:m getview: mywebview @ ;m
:m charsenlarge: enlarge: mywebview ;m
:m charsshrink: shrink: mywebview ;m
:m back: goback: mywebview ;m
:m forward: goforward: mywebview ;m
:m reload: reload: mywebview ;m
:m idle:
get: counter 2 <
IF
1 +: counter
ELSE
settitle: self
clear: counter
THEN
get: Redraw? 0EXIT
draw: myWebView \ redraw at idle time
clear: Redraw?
;m
;CLASS
webwin WW
rect WRect
: loadurl ( addr len -- ) loadrequest: ww ;
: CRASH bye ;
: go
WebInitForCarbon \ initialize cocoa WebKit framework for Carbon app
150 50 950 650 put: wrect \ Requires 1024*768 or more.
WRECT
" WebKit Test"
DocWind
1 19 << OR \ compositing - so that HIView structure
\ be automatically created
false false new: WW
show: ww ;
----------------------------------------------------------------
Mike Hore ***@OVE.invalid.icasolution.com.au
----------------------------------------------------------------