factor/library/cocoa/ui.factor

174 lines
4.3 KiB
Factor
Raw Normal View History

2006-03-12 23:21:01 -05:00
! Copyright (C) 2006 Slava Pestov.
! See http://factor.sf.net/license.txt for BSD license.
USING: alien arrays cocoa freetype gadgets gadgets-layouts
2006-03-15 00:24:00 -05:00
gadgets-listener hashtables io kernel lists math namespaces objc
2006-03-15 00:57:02 -05:00
objc-NSApplication objc-NSEvent objc-NSMenu
objc-NSNotificationCenter objc-NSObject objc-NSOpenGLContext
objc-NSOpenGLView objc-NSView objc-NSWindow opengl prettyprint
sequences threads walker ;
2006-03-14 21:09:25 -05:00
IN: gadgets
: redraw-world ( gadgets -- )
world-handle [contentView] 1 [setNeedsDisplay:] ;
2006-03-12 23:21:01 -05:00
IN: gadgets-cocoa
! Cocoa backend for Factor UI
2006-03-15 00:24:00 -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
: mouse-location ( window -- loc )
dup [contentView] [
swap [mouseLocationOutsideOfEventStream] f
[convertPoint:fromView:]
dup NSPoint-x swap NSPoint-y
] keep [frame] NSRect-h swap - 0 3array ;
: send-mouse-moved ( -- )
world get world-handle mouse-location move-hand ;
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 ;
: event>binding ( event -- binding )
dup [modifierFlags] modifier swap [keyCode] key-codes
[ add >list ] [ drop f ] if* ;
: send-key-event ( event -- )
dup event>binding
[ hand get hand-focus handle-gesture ] [ t ] if*
[ [characters] CF>string send-user-input ] [ drop ] if ;
2006-03-14 21:09:25 -05:00
"NSOpenGLView" "FactorView" {
{ "drawRect:" "void" { "id" "SEL" "NSRect" }
[
2drop dup [openGLContext] [
view-dim init-gl world get draw-gadget
] with-gl-context
]
}
{ "mouseMoved:" "void" { "id" "SEL" "id" }
[ 3drop send-mouse-moved ]
}
{ "mouseDragged:" "void" { "id" "SEL" "id" }
[ 3drop send-mouse-moved ]
}
{ "rightMouseDragged:" "void" { "id" "SEL" "id" }
[ 3drop send-mouse-moved ]
}
{ "otherMouseDragged:" "void" { "id" "SEL" "id" }
[ 3drop 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" }
[ 2nip send-key-event ]
}
2006-03-12 23:21:01 -05:00
{ "updateFactorGadgetSize:" "void" { "id" "SEL" "id" }
[ 2drop view-dim world get set-gadget-dim ]
}
{ "acceptsFirstResponder" "bool" { "id" "SEL" }
[ 2drop 1 ]
}
} { } define-objc-class
2006-03-12 23:21:01 -05:00
IN: objc-FactorView
DEFER: FactorView
IN: gadgets-cocoa
2006-03-12 23:21:01 -05:00
: <FactorView> ( gadget -- view )
2006-03-14 21:09:25 -05:00
drop
2006-03-12 23:21:01 -05:00
FactorView [alloc]
0 0 100 100 <NSRect> NSOpenGLView [defaultPixelFormat]
2006-03-15 00:57:02 -05:00
[initWithFrame:pixelFormat:]
dup 1 [setPostsBoundsChangedNotifications:]
dup 1 [setPostsFrameChangedNotifications:]
dup "updateFactorGadgetSize:" add-resize-observer ;
2006-03-12 23:21:01 -05:00
2006-03-13 00:41:59 -05:00
: <FactorWindow> ( gadget title -- window )
over rect-dim first2 0 0 2swap <NSRect> <NSWindow>
2006-03-14 21:09:25 -05:00
[ swap <FactorView> [setContentView:] ] 2keep
[ swap set-world-handle ] keep
dup 1 [setAcceptsMouseMovedEvents:]
dup dup [contentView] [setInitialFirstResponder:]
dup f [makeKeyAndOrderFront:] ;
2006-03-15 00:57:02 -05:00
IN: shells
2006-03-15 00:57:02 -05:00
2006-03-14 21:09:25 -05:00
: ui
2006-03-13 00:41:59 -05:00
[
2006-03-14 21:09:25 -05:00
[
init-world
world get ui-title <FactorWindow>
listener-application
NSApplication [sharedApplication] [finishLaunching]
2006-03-14 21:09:25 -05:00
event-loop
] with-cocoa
] with-freetype ;
2006-03-13 01:12:26 -05:00
2006-03-14 21:09:25 -05:00
ui