factor/library/cocoa/ui.factor

252 lines
6.3 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
IN: objc-FactorUIWindowDelegate
DEFER: FactorUIWindowDelegate
2006-03-21 01:43:03 -05:00
USING: alien arrays cocoa errors freetype gadgets
gadgets-launchpad gadgets-layouts gadgets-listener gadgets-panes
2006-05-15 01:01:47 -04:00
hashtables kernel 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
2006-04-01 19:48:17 -05:00
: purge-views ( hash -- hash )
global [
views [ [ drop expired? not ] hash-subset ] change
] bind ;
2006-03-17 02:50:16 -05:00
: 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-23 17:46:35 -05:00
: button&loc ( view event -- button# loc )
dup button -rot mouse-location ;
2006-03-15 00:24:00 -05:00
: modifiers
{
2006-05-18 22:01:38 -04:00
{ S+ HEX: 10000 }
{ C+ HEX: 40000 }
{ A+ HEX: 80000 }
{ M+ HEX: 100000 }
2006-03-15 00:24:00 -05:00
} ;
: key-codes
H{
{ 36 "RETURN" }
{ 48 "TAB" }
{ 51 "BACKSPACE" }
{ 115 "HOME" }
{ 117 "DELETE" }
{ 119 "END" }
{ 123 "LEFT" }
{ 124 "RIGHT" }
{ 125 "DOWN" }
{ 126 "UP" }
} ;
2006-03-15 00:24:00 -05:00
: key-code ( event -- string )
dup [keyCode] key-codes hash
[ ] [ [charactersIgnoringModifiers] CF>string ] ?if ;
2006-05-22 21:55:28 -04:00
: event>gesture ( event -- modifiers keycode )
dup [modifierFlags] modifiers modifier swap key-code ;
2006-03-15 00:24:00 -05:00
2006-05-22 21:55:28 -04:00
: send-key-event ( view event quot -- )
>r event>gesture r> call swap view world-focus
handle-gesture ; inline
: send-user-input ( view event -- )
[characters] CF>string swap view world-focus user-input ;
: send-key-down-event ( view event -- )
2dup [ <key-down> ] send-key-event
[ send-user-input ] [ 2drop ] if ;
: send-key-up-event ( view event -- )
[ <key-up> ] send-key-event ;
2006-03-14 21:09:25 -05:00
2006-03-23 17:46:35 -05:00
: send-button-down$ ( view event -- )
over >r button&loc r> view send-button-down ;
: send-button-up$ ( view event -- )
over >r button&loc r> view send-button-up ;
: send-wheel$ ( view event -- )
[ [deltaY] 0 > ] 2keep mouse-location rot view send-wheel ;
"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" }
2006-03-23 17:46:35 -05:00
[ nip send-button-down$ ]
}
{ "mouseUp:" "void" { "id" "SEL" "id" }
2006-03-23 17:46:35 -05:00
[ nip send-button-up$ ]
}
{ "rightMouseDown:" "void" { "id" "SEL" "id" }
2006-03-23 17:46:35 -05:00
[ nip send-button-down$ ]
}
{ "rightMouseUp:" "void" { "id" "SEL" "id" }
2006-03-23 17:46:35 -05:00
[ nip send-button-up$ ]
}
{ "otherMouseDown:" "void" { "id" "SEL" "id" }
2006-03-23 17:46:35 -05:00
[ nip send-button-down$ ]
}
{ "otherMouseUp:" "void" { "id" "SEL" "id" }
2006-03-23 17:46:35 -05:00
[ nip send-button-up$ ]
}
{ "scrollWheel:" "void" { "id" "SEL" "id" }
2006-03-23 17:46:35 -05:00
[ nip send-wheel$ ]
}
{ "keyDown:" "void" { "id" "SEL" "id" }
2006-05-22 21:55:28 -04:00
[ nip send-key-down-event ]
}
{ "keyUp:" "void" { "id" "SEL" "id" }
[ nip send-key-up-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
: window-root-gadget-pref-dim [contentView] view pref-dim ;
: frame-rect-for-window-content-rect ( window rect -- rect )
swap [styleMask] NSWindow -rot [frameRectForContentRect:styleMask:] ;
: content-rect-for-window-frame-rect ( window rect -- rect )
swap [styleMask] NSWindow -rot [contentRectForFrameRect:styleMask:] ;
: window-content-rect ( window -- rect )
dup [frame] content-rect-for-window-frame-rect ;
"NSObject" "FactorUIWindowDelegate" {
{ "windowWillUseStandardFrame:defaultFrame:" "NSRect" { "id" "SEL" "id" "NSRect" }
[
2006-05-18 22:01:38 -04:00
drop 2nip
dup window-content-rect NSRect-x-far-y
pick window-root-gadget-pref-dim first2
<far-y-NSRect>
frame-rect-for-window-content-rect
]
}
} { } define-objc-class
: install-window-delegate ( window -- )
FactorUIWindowDelegate [alloc] [init] [setDelegate:] ;
2006-03-18 02:23:57 -05:00
IN: gadgets
: redraw-world ( handle -- )
world-handle 1 [setNeedsDisplay:] ;
2006-03-18 02:23:57 -05:00
2006-03-24 22:58:03 -05:00
: open-window* ( world title -- )
>r <FactorView> r> <ViewWindow>
dup install-window-delegate
[contentView] [release] ;
2006-03-18 02:23:57 -05:00
: 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-23 17:46:35 -05:00
init-ui
2006-04-01 19:48:17 -05:00
purge-views
default-main-menu
2006-03-18 02:23:57 -05:00
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" ? ;