2008-03-19 15:25:53 -04:00
|
|
|
! Copyright (C) 2006, 2008 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-12-05 02:49:46 -05:00
|
|
|
USING: accessors math arrays assocs cocoa cocoa.application
|
2008-07-10 21:32:17 -04:00
|
|
|
command-line kernel memory namespaces cocoa.messages
|
|
|
|
cocoa.runtime cocoa.subclassing cocoa.pasteboard cocoa.types
|
2008-12-09 22:16:37 -05:00
|
|
|
cocoa.windows cocoa.classes sequences system
|
2008-07-10 21:32:17 -04:00
|
|
|
ui ui.backend ui.clipboards ui.gadgets ui.gadgets.worlds
|
2008-12-08 22:30:10 -05:00
|
|
|
ui.cocoa.views core-foundation threads math.geometry.rect fry
|
2008-12-09 00:00:47 -05:00
|
|
|
libc generalizations alien.c-types cocoa.views combinators ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: ui.cocoa
|
|
|
|
|
2008-12-08 22:30:10 -05:00
|
|
|
TUPLE: handle ;
|
|
|
|
TUPLE: window-handle < handle view window ;
|
|
|
|
TUPLE: offscreen-handle < handle context buffer ;
|
2008-03-19 15:25:53 -04:00
|
|
|
|
2008-12-08 22:30:10 -05:00
|
|
|
C: <window-handle> window-handle
|
|
|
|
C: <offscreen-handle> offscreen-handle
|
2008-03-19 15:25:53 -04:00
|
|
|
|
2008-04-02 20:44:01 -04:00
|
|
|
SINGLETON: cocoa-ui-backend
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-08 17:58:13 -04:00
|
|
|
M: cocoa-ui-backend do-events ( -- )
|
2008-11-28 01:02:02 -05:00
|
|
|
[ NSApp '[ _ do-event ] loop ui-wait ] with-autorelease-pool ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
TUPLE: pasteboard handle ;
|
|
|
|
|
|
|
|
C: <pasteboard> pasteboard
|
|
|
|
|
|
|
|
M: pasteboard clipboard-contents
|
2008-09-01 19:57:12 -04:00
|
|
|
handle>> pasteboard-string ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: pasteboard set-clipboard-contents
|
2008-09-01 19:57:12 -04:00
|
|
|
handle>> set-pasteboard-string ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: init-clipboard ( -- )
|
|
|
|
NSPasteboard -> generalPasteboard <pasteboard>
|
|
|
|
clipboard set-global
|
|
|
|
<clipboard> selection set-global ;
|
|
|
|
|
|
|
|
: world>NSRect ( world -- NSRect )
|
2008-11-30 18:47:29 -05:00
|
|
|
[ window-loc>> ] [ dim>> ] bi [ first2 ] bi@ <NSRect> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: gadget-window ( world -- )
|
2008-11-30 18:47:29 -05:00
|
|
|
dup <FactorView>
|
|
|
|
2dup swap world>NSRect <ViewWindow>
|
2008-12-08 22:30:10 -05:00
|
|
|
[ [ -> release ] [ install-window-delegate ] bi* ]
|
|
|
|
[ <window-handle> ] 2bi
|
2008-11-30 18:47:29 -05:00
|
|
|
>>handle drop ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
M: cocoa-ui-backend set-title ( string world -- )
|
2008-09-01 20:02:44 -04:00
|
|
|
handle>> window>> swap <NSString> -> setTitle: ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-02-09 03:17:24 -05:00
|
|
|
: enter-fullscreen ( world -- )
|
2008-09-01 20:02:44 -04:00
|
|
|
handle>> view>>
|
2008-03-19 15:25:53 -04:00
|
|
|
NSScreen -> mainScreen
|
|
|
|
f -> enterFullScreenMode:withOptions:
|
|
|
|
drop ;
|
2008-02-09 03:17:24 -05:00
|
|
|
|
|
|
|
: exit-fullscreen ( world -- )
|
2008-09-01 20:02:44 -04:00
|
|
|
handle>> view>> f -> exitFullScreenModeWithOptions: ;
|
2008-02-09 03:17:24 -05:00
|
|
|
|
2008-02-11 02:53:20 -05:00
|
|
|
M: cocoa-ui-backend set-fullscreen* ( ? world -- )
|
2008-02-09 03:17:24 -05:00
|
|
|
swap [ enter-fullscreen ] [ exit-fullscreen ] if ;
|
|
|
|
|
2008-02-11 02:53:20 -05:00
|
|
|
M: cocoa-ui-backend fullscreen* ( world -- ? )
|
2008-09-01 20:02:44 -04:00
|
|
|
handle>> view>> -> isInFullScreenMode zero? not ;
|
2008-02-09 03:17:24 -05:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
: auto-position ( world -- )
|
2008-07-10 21:32:17 -04:00
|
|
|
dup window-loc>> { 0 0 } = [
|
2008-09-01 20:02:44 -04:00
|
|
|
handle>> window>> -> center
|
2007-09-20 18:09:08 -04:00
|
|
|
] [
|
|
|
|
drop
|
|
|
|
] if ;
|
|
|
|
|
2007-11-24 15:41:27 -05:00
|
|
|
M: cocoa-ui-backend (open-window) ( world -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
dup gadget-window
|
|
|
|
dup auto-position
|
2008-09-01 20:02:44 -04:00
|
|
|
handle>> window>> f -> makeKeyAndOrderFront: ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2007-11-24 15:41:27 -05:00
|
|
|
M: cocoa-ui-backend (close-window) ( handle -- )
|
2008-09-01 19:57:12 -04:00
|
|
|
window>> -> release ;
|
2007-11-24 15:41:27 -05:00
|
|
|
|
|
|
|
M: cocoa-ui-backend close-window ( gadget -- )
|
|
|
|
find-world [
|
2008-09-01 20:02:44 -04:00
|
|
|
handle>> [
|
2008-09-01 19:57:12 -04:00
|
|
|
window>> f -> performClose:
|
2008-03-19 15:25:53 -04:00
|
|
|
] when*
|
2007-11-24 15:41:27 -05:00
|
|
|
] when* ;
|
|
|
|
|
2008-02-21 00:13:31 -05:00
|
|
|
M: cocoa-ui-backend raise-window* ( world -- )
|
2008-09-01 20:02:44 -04:00
|
|
|
handle>> [
|
2008-09-01 19:57:12 -04:00
|
|
|
window>> dup f -> orderFront: -> makeKeyWindow
|
2007-09-20 18:09:08 -04:00
|
|
|
NSApp 1 -> activateIgnoringOtherApps:
|
|
|
|
] when* ;
|
|
|
|
|
2008-12-08 22:30:10 -05:00
|
|
|
: pixel-size ( pixel-format -- size )
|
|
|
|
0 <int> [ NSOpenGLPFAColorSize 0 -> getValues:forAttribute:forVirtualScreen: ]
|
|
|
|
keep *int -3 shift ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-12-08 22:30:10 -05:00
|
|
|
: offscreen-buffer ( world pixel-format -- alien w h pitch )
|
|
|
|
[ dim>> first2 ] [ pixel-size ] bi*
|
2008-12-09 00:00:47 -05:00
|
|
|
{ [ * * malloc ] [ 2drop ] [ drop nip ] [ nip * ] } 3cleave ;
|
2008-12-08 22:30:10 -05:00
|
|
|
|
|
|
|
: gadget-offscreen-context ( world -- context buffer )
|
2008-12-09 12:22:23 -05:00
|
|
|
NSOpenGLPFAOffScreen 1array <PixelFormat>
|
|
|
|
[ nip NSOpenGLContext -> alloc swap f -> initWithFormat:shareContext: dup ]
|
|
|
|
[ offscreen-buffer ] 2bi
|
2008-12-09 00:00:47 -05:00
|
|
|
4 npick [ -> setOffScreen:width:height:rowbytes: ] dip ;
|
2008-12-08 22:30:10 -05:00
|
|
|
|
|
|
|
M: cocoa-ui-backend (open-offscreen-buffer) ( world -- )
|
|
|
|
dup gadget-offscreen-context <offscreen-handle> >>handle drop ;
|
|
|
|
|
|
|
|
M: cocoa-ui-backend (close-offscreen-buffer) ( handle -- )
|
|
|
|
[ context>> -> release ]
|
|
|
|
[ buffer>> free ] bi ;
|
|
|
|
|
2008-12-09 13:07:57 -05:00
|
|
|
GENERIC: (gl-context) ( handle -- context )
|
|
|
|
M: window-handle (gl-context) view>> -> openGLContext ;
|
|
|
|
M: offscreen-handle (gl-context) context>> ;
|
2008-12-08 22:30:10 -05:00
|
|
|
|
|
|
|
M: handle select-gl-context ( handle -- )
|
2008-12-09 13:07:57 -05:00
|
|
|
(gl-context) -> makeCurrentContext ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-12-08 22:30:10 -05:00
|
|
|
M: handle flush-gl-context ( handle -- )
|
2008-12-09 13:07:57 -05:00
|
|
|
(gl-context) -> flushBuffer ;
|
|
|
|
|
2008-12-10 09:49:50 -05:00
|
|
|
M: cocoa-ui-backend offscreen-pixels ( world -- alien w h )
|
|
|
|
[ handle>> buffer>> ] [ dim>> first2 neg ] ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-06-05 23:06:38 -04:00
|
|
|
M: cocoa-ui-backend beep ( -- )
|
|
|
|
NSBeep ;
|
|
|
|
|
2008-12-05 02:49:46 -05:00
|
|
|
CLASS: {
|
|
|
|
{ +superclass+ "NSObject" }
|
|
|
|
{ +name+ "FactorApplicationDelegate" }
|
|
|
|
}
|
|
|
|
|
|
|
|
{ "applicationDidFinishLaunching:" "void" { "id" "SEL" "id" }
|
|
|
|
[ 3drop event-loop ]
|
|
|
|
} ;
|
|
|
|
|
|
|
|
: install-app-delegate ( -- )
|
|
|
|
NSApp FactorApplicationDelegate install-delegate ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
SYMBOL: cocoa-init-hook
|
|
|
|
|
2008-12-08 17:02:31 -05:00
|
|
|
cocoa-init-hook global [
|
|
|
|
[ "MiniFactor.nib" load-nib install-app-delegate ] or
|
|
|
|
] change-at
|
2008-12-05 02:49:46 -05:00
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
M: cocoa-ui-backend ui
|
|
|
|
"UI" assert.app [
|
|
|
|
[
|
|
|
|
init-clipboard
|
2008-12-05 02:49:46 -05:00
|
|
|
cocoa-init-hook get call
|
2007-09-20 18:09:08 -04:00
|
|
|
start-ui
|
2008-12-05 02:49:46 -05:00
|
|
|
NSApp -> run
|
2007-09-20 18:09:08 -04:00
|
|
|
] ui-running
|
|
|
|
] with-cocoa ;
|
|
|
|
|
2008-04-02 20:44:01 -04:00
|
|
|
cocoa-ui-backend ui-backend set-global
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
[ running.app? "ui" "listener" ? ] main-vocab-hook set-global
|