diff --git a/library/cocoa/application-utils.factor b/library/cocoa/application-utils.factor index 7232de34f5..e857b052e6 100644 --- a/library/cocoa/application-utils.factor +++ b/library/cocoa/application-utils.factor @@ -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 -- ) diff --git a/library/cocoa/load.factor b/library/cocoa/load.factor index 878e2efb9f..f5cddd6078 100644 --- a/library/cocoa/load.factor +++ b/library/cocoa/load.factor @@ -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 diff --git a/library/cocoa/ui.factor b/library/cocoa/ui.factor index dd9b95b1ed..928ec351b4 100644 --- a/library/cocoa/ui.factor +++ b/library/cocoa/ui.factor @@ -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 - 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,89 +60,81 @@ 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 ; +"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 ] + } -: init-FactorView-class - "NSOpenGLView" "FactorView" { - { "drawRect:" "void" { "id" "SEL" "NSRect" } - [ - 2drop dup [openGLContext] [ - [bounds] 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 send-button-down ] - } - - { "mouseUp:" "void" { "id" "SEL" "id" } - [ 2nip send-button-up ] - } - - { "rightMouseDown:" "void" { "id" "SEL" "id" } - [ 2nip send-button-down ] - } - - { "rightMouseUp:" "void" { "id" "SEL" "id" } - [ 2nip send-button-up ] - } - - { "otherMouseDown:" "void" { "id" "SEL" "id" } - [ 2nip send-button-down ] - } - - { "otherMouseUp:" "void" { "id" "SEL" "id" } - [ 2nip send-button-up ] - } - - { "scrollWheel:" "void" { "id" "SEL" "id" } - [ 2nip send-scroll-wheel ] - } - - { "keyDown:" "void" { "id" "SEL" "id" } - [ 2nip send-key-event ] - } + { "updateFactorGadgetSize:" "void" { "id" "SEL" "id" } + [ 2drop view-dim world get set-gadget-dim ] + } + + { "acceptsFirstResponder" "bool" { "id" "SEL" } + [ 2drop 1 ] + } +} { } define-objc-class - { "updateFactorGadgetSize:" "void" { "id" "SEL" "id" } - [ 2drop world get resize-world ] - } - - { "acceptsFirstResponder" "bool" { "id" "SEL" } - [ 2drop 1 ] - } - } { } define-objc-class ; parsing +IN: objc-FactorView +DEFER: FactorView -init-FactorView-class - -USE: objc-FactorView +IN: gadgets-cocoa : ( gadget -- view ) drop @@ -187,18 +142,18 @@ USE: objc-FactorView 0 0 100 100 NSOpenGLView [defaultPixelFormat] [initWithFrame:pixelFormat:] dup 1 [setPostsBoundsChangedNotifications:] - dup 1 [setPostsFrameChangedNotifications:] ; + dup 1 [setPostsFrameChangedNotifications:] + dup "updateFactorGadgetSize:" add-resize-observer ; : ( gadget title -- window ) over rect-dim first2 0 0 2swap [ swap [setContentView:] ] 2keep - [ swap set-world-handle ] keep ; + [ swap set-world-handle ] keep + dup 1 [setAcceptsMouseMovedEvents:] + dup dup [contentView] [setInitialFirstResponder:] + dup f [makeKeyAndOrderFront:] ; -: NSViewBoundsDidChangeNotification - "NSViewBoundsDidChangeNotification" ; - -: NSViewFrameDidChangeNotification - "NSViewFrameDidChangeNotification" ; +IN: shells : ui [ @@ -207,23 +162,10 @@ USE: objc-FactorView world get ui-title - 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 ; diff --git a/library/cocoa/view-utils.factor b/library/cocoa/view-utils.factor new file mode 100644 index 0000000000..a9d036f663 --- /dev/null +++ b/library/cocoa/view-utils.factor @@ -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" ; + +: add-resize-observer ( view selector -- ) + NSViewFrameDidChangeNotification pick add-observer ; diff --git a/library/ui/hand.factor b/library/ui/hand.factor index daf4df1d15..d5843ad617 100644 --- a/library/ui/hand.factor +++ b/library/ui/hand.factor @@ -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 ; diff --git a/library/ui/paint.factor b/library/ui/paint.factor index f1e9767de1..4f38766601 100644 --- a/library/ui/paint.factor +++ b/library/ui/paint.factor @@ -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 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 ;