Moved many parts of Cocoa backend into backend-independent UI code

release
slava 2006-03-15 06:20:59 +00:00
parent 80857c7a36
commit b9e823362f
6 changed files with 138 additions and 140 deletions

View File

@ -1,9 +1,9 @@
! Copyright (C) 2006 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
IN: cocoa
USING: alien errors gadgets io kernel namespaces
USING: alien errors gadgets io kernel namespaces objc
objc-NSApplication objc-NSAutoreleasePool objc-NSException
objc-NSObject objc-NSView threads ;
objc-NSNotificationCenter objc-NSObject objc-NSView threads ;
: with-autorelease-pool ( quot -- )
NSAutoreleasePool [new] slip [release] ; inline
@ -34,6 +34,10 @@ objc-NSObject objc-NSView threads ;
NSApplication [sharedApplication] do-events world-step
] with-autorelease-pool 10 sleep event-loop ;
: add-observer ( observer selector name object -- )
>r >r >r >r NSNotificationCenter [defaultCenter] r> r>
sel_registerName r> r> [addObserver:selector:name:object:] ;
IN: errors
: objc-error. ( alien -- )

View File

@ -9,6 +9,7 @@ USING: compiler io parser sequences words ;
"/library/cocoa/init-cocoa.factor"
"/library/cocoa/application-utils.factor"
"/library/cocoa/window-utils.factor"
"/library/cocoa/view-utils.factor"
} [
run-resource
] each

View File

@ -15,42 +15,10 @@ IN: gadgets
IN: gadgets-cocoa
! Cocoa backend for Factor UI
: init-gl ( rect -- )
0.0 0.0 0.0 0.0 glClearColor
{ 1.0 0.0 0.0 0.0 } gl-color
GL_COLOR_BUFFER_BIT glClear
GL_PROJECTION glMatrixMode
glLoadIdentity
GL_MODELVIEW glMatrixMode
glLoadIdentity
{ 0 0 0 } over NSRect-w pick NSRect-h 0 3array <rect>
clip set
dup NSRect-w over NSRect-h 0 0 2swap glViewport
dup NSRect-w swap NSRect-h >r >r 0 r> r> 0 gluOrtho2D
GL_SMOOTH glShadeModel
GL_BLEND glEnable
GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
GL_SCISSOR_TEST glEnable
GL_MODELVIEW glMatrixMode ;
: with-gl-context ( context quot -- )
swap
[ [makeCurrentContext] call glFlush ] keep
[flushBuffer] ; inline
: button ( event -- n )
#! Cocoa -> Factor UI button mapping
[buttonNumber] H{ { 0 1 } { 2 2 } { 1 3 } } hash ;
: send-button-down ( event -- )
update-clicked
button dup hand get hand-buttons push
[ button-down ] button-gesture ;
: send-button-up ( event -- )
button dup hand get hand-buttons delete
[ button-up ] button-gesture ;
: mouse-location ( window -- loc )
dup [contentView] [
swap [mouseLocationOutsideOfEventStream] f
@ -61,11 +29,6 @@ IN: gadgets-cocoa
: send-mouse-moved ( -- )
world get world-handle mouse-location move-hand ;
: send-scroll-wheel ( event -- )
[deltaY] 0 >
[ wheel-up ] [ wheel-down ] ?
hand get hand-clicked handle-gesture drop ;
: modifiers
{
{ "SHIFT" HEX: 10000 }
@ -97,25 +60,16 @@ IN: gadgets-cocoa
dup [modifierFlags] modifier swap [keyCode] key-codes
[ add >list ] [ drop f ] if* ;
: send-user-input ( event -- )
[characters] CF>string dup empty?
[ hand get hand-focus user-input ] unless drop ;
: send-key-event ( event -- )
dup event>binding
[ hand get hand-focus handle-gesture ] [ t ] if*
[ send-user-input ] [ drop ] if ;
[ [characters] CF>string send-user-input ] [ drop ] if ;
: resize-world ( world -- )
>r [bounds] dup NSRect-w swap NSRect-h 0 3array r>
set-gadget-dim ;
: init-FactorView-class
"NSOpenGLView" "FactorView" {
{ "drawRect:" "void" { "id" "SEL" "NSRect" }
[
2drop dup [openGLContext] [
[bounds] init-gl world get draw-gadget
view-dim init-gl world get draw-gadget
] with-gl-context
]
}
@ -137,31 +91,31 @@ IN: gadgets-cocoa
}
{ "mouseDown:" "void" { "id" "SEL" "id" }
[ 2nip send-button-down ]
[ 2nip button send-button-down ]
}
{ "mouseUp:" "void" { "id" "SEL" "id" }
[ 2nip send-button-up ]
[ 2nip button send-button-up ]
}
{ "rightMouseDown:" "void" { "id" "SEL" "id" }
[ 2nip send-button-down ]
[ 2nip button send-button-down ]
}
{ "rightMouseUp:" "void" { "id" "SEL" "id" }
[ 2nip send-button-up ]
[ 2nip button send-button-up ]
}
{ "otherMouseDown:" "void" { "id" "SEL" "id" }
[ 2nip send-button-down ]
[ 2nip button send-button-down ]
}
{ "otherMouseUp:" "void" { "id" "SEL" "id" }
[ 2nip send-button-up ]
[ 2nip button send-button-up ]
}
{ "scrollWheel:" "void" { "id" "SEL" "id" }
[ 2nip send-scroll-wheel ]
[ 2nip [deltaY] 0 > send-scroll-wheel ]
}
{ "keyDown:" "void" { "id" "SEL" "id" }
@ -169,17 +123,18 @@ IN: gadgets-cocoa
}
{ "updateFactorGadgetSize:" "void" { "id" "SEL" "id" }
[ 2drop world get resize-world ]
[ 2drop view-dim world get set-gadget-dim ]
}
{ "acceptsFirstResponder" "bool" { "id" "SEL" }
[ 2drop 1 ]
}
} { } define-objc-class ; parsing
} { } define-objc-class
init-FactorView-class
IN: objc-FactorView
DEFER: FactorView
USE: objc-FactorView
IN: gadgets-cocoa
: <FactorView> ( gadget -- view )
drop
@ -187,18 +142,18 @@ USE: objc-FactorView
0 0 100 100 <NSRect> NSOpenGLView [defaultPixelFormat]
[initWithFrame:pixelFormat:]
dup 1 [setPostsBoundsChangedNotifications:]
dup 1 [setPostsFrameChangedNotifications:] ;
dup 1 [setPostsFrameChangedNotifications:]
dup "updateFactorGadgetSize:" add-resize-observer ;
: <FactorWindow> ( gadget title -- window )
over rect-dim first2 0 0 2swap <NSRect> <NSWindow>
[ swap <FactorView> [setContentView:] ] 2keep
[ swap set-world-handle ] keep ;
[ swap set-world-handle ] keep
dup 1 [setAcceptsMouseMovedEvents:]
dup dup [contentView] [setInitialFirstResponder:]
dup f [makeKeyAndOrderFront:] ;
: NSViewBoundsDidChangeNotification
"NSViewBoundsDidChangeNotification" <NSString> ;
: NSViewFrameDidChangeNotification
"NSViewFrameDidChangeNotification" <NSString> ;
IN: shells
: ui
[
@ -207,23 +162,10 @@ USE: objc-FactorView
world get ui-title <FactorWindow>
dup 1 [setAcceptsMouseMovedEvents:]
dup dup [contentView] [setInitialFirstResponder:]
NSNotificationCenter [defaultCenter]
over [contentView]
"updateFactorGadgetSize:" sel_registerName
NSViewFrameDidChangeNotification
pick
[addObserver:selector:name:object:]
dup f [makeKeyAndOrderFront:]
[contentView] [openGLContext] [makeCurrentContext]
listener-application
NSApplication [sharedApplication] [finishLaunching]
event-loop
] with-cocoa
] with-freetype ;

View File

@ -0,0 +1,17 @@
! Copyright (C) 2006 Slava Pestov
! See http://factorcode.org/license.txt for BSD license.
IN: cocoa
USING: arrays kernel objc-NSOpenGLContext objc-NSView opengl ;
: with-gl-context ( context quot -- )
swap
[ [makeCurrentContext] call glFlush ] keep
[flushBuffer] ; inline
: view-dim [bounds] dup NSRect-w swap NSRect-h 0 3array ;
: NSViewFrameDidChangeNotification
"NSViewFrameDidChangeNotification" <NSString> ;
: add-resize-observer ( view selector -- )
NSViewFrameDidChangeNotification pick add-observer ;

View File

@ -1,5 +1,5 @@
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
! Copyright (C) 2005, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
IN: gadgets
USING: kernel math namespaces sequences ;
@ -21,6 +21,19 @@ C: hand ( -- hand )
swap hand get hand-clicked 3dup >r add r> handle-gesture
[ nip handle-gesture drop ] [ 3drop ] if ;
: send-button-down ( event -- )
update-clicked
dup hand get hand-buttons push
[ button-down ] button-gesture ;
: send-button-up ( event -- )
dup hand get hand-buttons delete
[ button-up ] button-gesture ;
: send-scroll-wheel ( up/down -- )
[ wheel-up ] [ wheel-down ] ?
hand get hand-clicked handle-gesture drop ;
: drag-gesture ( -- )
#! Send a gesture like [ drag 2 ]; if nobody handles it,
#! send [ drag ].
@ -33,6 +46,10 @@ C: hand ( -- hand )
[ motion ] over hand-gadget handle-gesture drop
hand-buttons empty? [ drag-gesture ] unless ;
: send-user-input ( string -- )
dup empty?
[ hand get hand-focus user-input ] unless drop ;
: each-gesture ( gesture seq -- )
[ handle-gesture* drop ] each-with ;

View File

@ -1,10 +1,27 @@
! Copyright (C) 2005 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
! Copyright (C) 2005, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: alien arrays freetype gadgets-layouts generic hashtables
io kernel lists math namespaces opengl sequences strings
styles vectors ;
IN: gadgets
: init-gl ( dim -- )
0.0 0.0 0.0 0.0 glClearColor
{ 1.0 0.0 0.0 0.0 } gl-color
GL_COLOR_BUFFER_BIT glClear
GL_PROJECTION glMatrixMode
glLoadIdentity
GL_MODELVIEW glMatrixMode
glLoadIdentity
{ 0 0 0 } over <rect> clip set
dup first2 0 0 2swap glViewport
0 swap first2 0 gluOrtho2D
GL_SMOOTH glShadeModel
GL_BLEND glEnable
GL_SRC_ALPHA GL_ONE_MINUS_SRC_ALPHA glBlendFunc
GL_SCISSOR_TEST glEnable
GL_MODELVIEW glMatrixMode ;
GENERIC: draw-gadget* ( gadget -- )
M: gadget draw-gadget* ( gadget -- ) drop ;