factor/library/cocoa/ui.factor

198 lines
4.7 KiB
Factor
Raw Normal View History

2006-03-12 23:21:01 -05:00
! Copyright (C) 2006 Slava Pestov.
2006-03-17 02:50:16 -05:00
! See http://factorcode.org/license.txt for BSD license.
2006-03-21 01:43:03 -05:00
IN: objc-FactorView
DEFER: FactorView
2006-03-19 15:43:40 -05:00
USING: arrays cocoa errors freetype gadgets gadgets-launchpad
2006-03-18 02:23:57 -05:00
gadgets-layouts gadgets-listener gadgets-panes hashtables kernel
lists math namespaces objc objc-NSApplication objc-NSEvent
objc-NSObject objc-NSOpenGLContext objc-NSOpenGLView objc-NSView
objc-NSWindow sequences threads ;
2006-03-14 21:09:25 -05:00
2006-03-17 02:50:16 -05:00
! Cocoa backend for Factor UI
2006-03-12 23:21:01 -05:00
IN: gadgets-cocoa
2006-03-17 02:50:16 -05:00
! Hash mapping aliens to gadgets
SYMBOL: views
H{ } clone views set-global
: view ( handle -- world ) views get hash ;
2006-03-19 01:07:36 -05:00
: mouse-location ( view event -- loc )
over >r
[locationInWindow] f [convertPoint:fromView:]
dup NSPoint-x swap NSPoint-y
r> [frame] NSRect-h swap - 0 3array ;
2006-03-14 21:09:25 -05:00
2006-03-19 01:07:36 -05:00
: send-mouse-moved ( view event -- )
over >r mouse-location r> view move-hand ;
2006-03-17 02:50:16 -05:00
: button ( event -- n )
#! Cocoa -> Factor UI button mapping
[buttonNumber] H{ { 0 1 } { 2 2 } { 1 3 } } hash ;
2006-03-14 21:09:25 -05:00
2006-03-15 00:24:00 -05:00
: modifiers
{
{ "SHIFT" HEX: 10000 }
{ "CTRL" HEX: 40000 }
{ "ALT" HEX: 80000 }
{ "META" HEX: 100000 }
} ;
: key-codes
H{
{ 36 "RETURN" }
{ 48 "TAB" }
{ 51 "BACKSPACE" }
{ 115 "HOME" }
{ 117 "DELETE" }
{ 119 "END" }
{ 123 "LEFT" }
{ 124 "RIGHT" }
{ 125 "DOWN" }
{ 126 "UP" }
} hash ;
: modifier ( mod -- seq )
modifiers
[ second swap bitand 0 > ] subset-with
[ first ] map ;
: key-code ( event -- string )
dup [keyCode] key-codes
[ ] [ [charactersIgnoringModifiers] CF>string ] ?if ;
2006-03-15 00:24:00 -05:00
: event>binding ( event -- binding )
dup [modifierFlags] modifier swap key-code
2006-03-15 00:24:00 -05:00
[ add >list ] [ drop f ] if* ;
2006-03-19 01:07:36 -05:00
: send-key-event ( view event -- )
>r view world-focus r> dup event>binding
[ pick handle-gesture ] [ t ] if*
[ [characters] CF>string swap user-input ] [ 2drop ] if ;
2006-03-14 21:09:25 -05:00
"NSOpenGLView" "FactorView" {
{ "drawRect:" "void" { "id" "SEL" "NSRect" }
[ 2drop view draw-world ]
}
{ "mouseMoved:" "void" { "id" "SEL" "id" }
2006-03-19 01:07:36 -05:00
[ nip send-mouse-moved ]
}
{ "mouseDragged:" "void" { "id" "SEL" "id" }
2006-03-19 01:07:36 -05:00
[ nip send-mouse-moved ]
}
{ "rightMouseDragged:" "void" { "id" "SEL" "id" }
2006-03-19 01:07:36 -05:00
[ nip send-mouse-moved ]
}
{ "otherMouseDragged:" "void" { "id" "SEL" "id" }
2006-03-19 01:07:36 -05:00
[ nip send-mouse-moved ]
}
{ "mouseDown:" "void" { "id" "SEL" "id" }
[ 2nip button send-button-down ]
}
{ "mouseUp:" "void" { "id" "SEL" "id" }
[ 2nip button send-button-up ]
}
{ "rightMouseDown:" "void" { "id" "SEL" "id" }
[ 2nip button send-button-down ]
}
{ "rightMouseUp:" "void" { "id" "SEL" "id" }
[ 2nip button send-button-up ]
}
{ "otherMouseDown:" "void" { "id" "SEL" "id" }
[ 2nip button send-button-down ]
}
{ "otherMouseUp:" "void" { "id" "SEL" "id" }
[ 2nip button send-button-up ]
}
{ "scrollWheel:" "void" { "id" "SEL" "id" }
[ 2nip [deltaY] 0 > send-scroll-wheel ]
}
{ "keyDown:" "void" { "id" "SEL" "id" }
2006-03-19 01:07:36 -05:00
[ nip send-key-event ]
}
2006-03-12 23:21:01 -05:00
{ "updateFactorGadgetSize:" "void" { "id" "SEL" "id" }
2006-03-17 02:50:16 -05:00
[ 2drop dup view-dim swap view set-gadget-dim ]
}
{ "acceptsFirstResponder" "bool" { "id" "SEL" }
[ 2drop 1 ]
}
2006-03-21 02:40:16 -05:00
{ "initWithFrame:pixelFormat:" "id" { "id" "SEL" "NSRect" "id" }
2006-03-21 01:43:03 -05:00
[
2006-03-21 02:40:16 -05:00
rot drop
SUPER-> [initWithFrame:pixelFormat:]
2006-03-21 01:43:03 -05:00
dup "updateFactorGadgetSize:" add-resize-observer
]
}
{ "dealloc" "void" { "id" "SEL" }
[
drop
2006-03-21 01:43:03 -05:00
dup view close-world
dup views get remove-hash
2006-03-21 01:43:03 -05:00
dup remove-observer
SUPER-> [dealloc]
]
}
} { } define-objc-class
2006-03-12 23:21:01 -05:00
2006-03-21 01:43:03 -05:00
: register-view ( world -- )
dup world-handle views get set-hash ;
2006-03-12 23:21:01 -05:00
: <FactorView> ( gadget -- view )
2006-03-17 02:50:16 -05:00
FactorView over rect-dim <GLView>
[ over set-world-handle dup add-notify register-view ] keep ;
2006-03-12 23:21:01 -05:00
2006-03-13 00:41:59 -05:00
: <FactorWindow> ( gadget title -- window )
2006-03-21 15:07:53 -05:00
>r <FactorView> r> <ViewWindow> dup [contentView] [release] ;
2006-03-15 00:57:02 -05:00
2006-03-18 02:23:57 -05:00
IN: gadgets
: draw-handle ( handle -- )
2006-03-21 02:40:16 -05:00
1 [setNeedsDisplay:] ;
2006-03-18 02:23:57 -05:00
: in-window ( gadget status dim title -- )
>r <world> r> <FactorWindow> drop ;
: select-gl-context ( handle -- )
[openGLContext] [makeCurrentContext] ;
: flush-gl-context ( handle -- )
[openGLContext] [flushBuffer] ;
IN: shells
2006-03-15 00:57:02 -05:00
2006-03-14 21:09:25 -05:00
: ui
2006-03-19 15:43:40 -05:00
running.app? [
"The Factor UI requires you to run the supplied Factor.app." throw
] unless
2006-03-13 00:41:59 -05:00
[
2006-03-14 21:09:25 -05:00
[
2006-03-18 02:23:57 -05:00
launchpad-window
listener-window
2006-03-17 02:50:16 -05:00
finish-launching
2006-03-14 21:09:25 -05:00
event-loop
] with-cocoa
] with-freetype ;
2006-03-19 18:00:07 -05:00
IN: kernel
: default-shell running.app? "ui" "tty" ? ;