2009-01-07 21:56:09 -05:00
|
|
|
! Copyright (C) 2006, 2009 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-05-01 13:56:52 -04:00
|
|
|
USING: accessors alien.c-types arrays assocs classes cocoa
|
|
|
|
cocoa.application cocoa.classes cocoa.messages cocoa.nibs
|
|
|
|
cocoa.pasteboard cocoa.runtime cocoa.subclassing cocoa.types
|
|
|
|
cocoa.views cocoa.windows combinators command-line
|
|
|
|
core-foundation core-foundation.run-loop core-graphics
|
|
|
|
core-graphics.types destructors fry generalizations io.thread
|
|
|
|
kernel libc literals locals math math.rectangles memory
|
|
|
|
namespaces sequences specialized-arrays.int threads ui
|
|
|
|
ui.backend ui.backend.cocoa.views ui.clipboards ui.gadgets
|
2009-05-01 17:33:49 -04:00
|
|
|
ui.gadgets.worlds ui.pixel-formats ui.pixel-formats.private
|
|
|
|
ui.private words.symbol ;
|
2009-01-26 01:36:37 -05:00
|
|
|
IN: ui.backend.cocoa
|
2007-09-20 18:09:08 -04:00
|
|
|
|
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
|
|
|
|
2009-05-01 21:07:14 -04:00
|
|
|
PIXEL-FORMAT-ATTRIBUTE-TABLE: NSOpenGLPFA { } H{
|
2009-05-01 10:09:38 -04:00
|
|
|
{ double-buffered { $ NSOpenGLPFADoubleBuffer } }
|
|
|
|
{ stereo { $ NSOpenGLPFAStereo } }
|
|
|
|
{ offscreen { $ NSOpenGLPFAOffScreen } }
|
|
|
|
{ fullscreen { $ NSOpenGLPFAFullScreen } }
|
|
|
|
{ windowed { $ NSOpenGLPFAWindow } }
|
|
|
|
{ accelerated { $ NSOpenGLPFAAccelerated } }
|
2009-05-07 20:47:26 -04:00
|
|
|
{ software-rendered { $ NSOpenGLPFARendererID $ kCGLRendererGenericFloatID } }
|
2009-05-01 10:09:38 -04:00
|
|
|
{ backing-store { $ NSOpenGLPFABackingStore } }
|
|
|
|
{ multisampled { $ NSOpenGLPFAMultisample } }
|
|
|
|
{ supersampled { $ NSOpenGLPFASupersample } }
|
|
|
|
{ sample-alpha { $ NSOpenGLPFASampleAlpha } }
|
|
|
|
{ color-float { $ NSOpenGLPFAColorFloat } }
|
|
|
|
{ color-bits { $ NSOpenGLPFAColorSize } }
|
|
|
|
{ alpha-bits { $ NSOpenGLPFAAlphaSize } }
|
|
|
|
{ accum-bits { $ NSOpenGLPFAAccumSize } }
|
|
|
|
{ depth-bits { $ NSOpenGLPFADepthSize } }
|
|
|
|
{ stencil-bits { $ NSOpenGLPFAStencilSize } }
|
|
|
|
{ aux-buffers { $ NSOpenGLPFAAuxBuffers } }
|
|
|
|
{ sample-buffers { $ NSOpenGLPFASampleBuffers } }
|
|
|
|
{ samples { $ NSOpenGLPFASamples } }
|
|
|
|
}
|
|
|
|
|
|
|
|
M: cocoa-ui-backend (make-pixel-format)
|
2009-05-02 13:31:33 -04:00
|
|
|
nip >NSOpenGLPFA-int-array
|
2009-05-01 10:09:38 -04:00
|
|
|
NSOpenGLPixelFormat -> alloc swap -> initWithAttributes: ;
|
|
|
|
|
|
|
|
M: cocoa-ui-backend (free-pixel-format)
|
2009-05-02 13:31:33 -04:00
|
|
|
handle>> -> release ;
|
2009-05-01 10:09:38 -04:00
|
|
|
|
|
|
|
M: cocoa-ui-backend (pixel-format-attribute)
|
2009-05-02 13:31:33 -04:00
|
|
|
[ handle>> ] [ >NSOpenGLPFA ] bi*
|
2009-05-01 17:33:49 -04:00
|
|
|
[ drop f ]
|
2009-05-01 10:09:38 -04:00
|
|
|
[ first 0 <int> [ swap 0 -> getValues:forAttribute:forVirtualScreen: ] keep *int ]
|
2009-05-01 17:33:49 -04:00
|
|
|
if-empty ;
|
2009-05-01 10:09:38 -04:00
|
|
|
|
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 )
|
2009-02-17 20:26:32 -05:00
|
|
|
[ 0 0 ] dip dim>> first2 <CGRect> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2009-02-17 20:26:32 -05:00
|
|
|
: auto-position ( window loc -- )
|
2009-04-13 04:16:57 -04:00
|
|
|
#! Note: if this is the initial window, the length of the windows
|
|
|
|
#! vector should be 1, since (open-window) calls auto-position
|
|
|
|
#! after register-window.
|
2009-02-17 20:26:32 -05:00
|
|
|
dup { 0 0 } = [
|
|
|
|
drop
|
2009-04-13 04:16:57 -04:00
|
|
|
windows get length 1 <= [ -> center ] [
|
|
|
|
windows get peek second window-loc>>
|
2009-02-17 20:26:32 -05:00
|
|
|
dupd first2 <CGPoint> -> cascadeTopLeftFromPoint:
|
|
|
|
-> setFrameTopLeftPoint:
|
2009-04-13 04:16:57 -04:00
|
|
|
] if
|
2009-02-17 20:26:32 -05:00
|
|
|
] [ first2 <CGPoint> -> setFrameTopLeftPoint: ] if ;
|
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
|
|
|
|
2009-02-17 20:26:32 -05:00
|
|
|
M:: cocoa-ui-backend (open-window) ( world -- )
|
2009-05-01 13:56:52 -04:00
|
|
|
world [ [ dim>> ] dip <FactorView> ]
|
|
|
|
with-world-pixel-format :> view
|
2009-02-17 20:26:32 -05:00
|
|
|
view world world>NSRect <ViewWindow> :> window
|
|
|
|
view -> release
|
|
|
|
world view register-window
|
2009-04-10 07:19:46 -04:00
|
|
|
window world window-loc>> auto-position
|
2009-02-17 20:26:32 -05:00
|
|
|
world window save-position
|
|
|
|
window install-window-delegate
|
|
|
|
view window <window-handle> world (>>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
|
|
|
|
2009-05-08 16:07:15 -04:00
|
|
|
M: cocoa-ui-backend (grab-input) ( handle -- )
|
|
|
|
0 CGAssociateMouseAndMouseCursorPosition drop
|
|
|
|
CGMainDisplayID CGDisplayHideCursor drop
|
|
|
|
window>> -> frame CGRect>rect rect-center
|
|
|
|
first2 <CGPoint> CGWarpMouseCursorPosition drop ;
|
|
|
|
|
|
|
|
M: cocoa-ui-backend (ungrab-input) ( handle -- )
|
|
|
|
drop
|
|
|
|
CGMainDisplayID CGDisplayShowCursor drop
|
|
|
|
1 CGAssociateMouseAndMouseCursorPosition drop ;
|
|
|
|
|
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 )
|
2009-05-01 10:09:38 -04:00
|
|
|
color-bits pixel-format-attribute -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
|
|
|
|
2009-05-01 10:09:38 -04:00
|
|
|
:: gadget-offscreen-context ( world -- context buffer )
|
2009-05-01 21:07:14 -04:00
|
|
|
world [
|
|
|
|
nip :> pf
|
2009-05-01 10:09:38 -04:00
|
|
|
NSOpenGLContext -> alloc pf handle>> f -> initWithFormat:shareContext:
|
2009-05-01 13:56:52 -04:00
|
|
|
dup world pf offscreen-buffer
|
|
|
|
4 npick [ -> setOffScreen:width:height:rowbytes: ] dip
|
2009-05-01 21:07:14 -04:00
|
|
|
] with-world-pixel-format ;
|
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 )
|
2008-12-10 10:28:33 -05:00
|
|
|
[ handle>> buffer>> ] [ dim>> first2 neg ] bi ;
|
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" }
|
|
|
|
}
|
|
|
|
|
2008-12-13 00:58:28 -05:00
|
|
|
{ "applicationDidUpdate:" "void" { "id" "SEL" "id" }
|
|
|
|
[ 3drop reset-run-loop ]
|
2008-12-05 02:49:46 -05:00
|
|
|
} ;
|
|
|
|
|
|
|
|
: install-app-delegate ( -- )
|
|
|
|
NSApp FactorApplicationDelegate install-delegate ;
|
|
|
|
|
2007-09-20 18:09:08 -04:00
|
|
|
SYMBOL: cocoa-init-hook
|
|
|
|
|
2009-02-10 17:16:12 -05:00
|
|
|
cocoa-init-hook [
|
|
|
|
[ "MiniFactor.nib" load-nib install-app-delegate ]
|
|
|
|
] initialize
|
2008-12-05 02:49:46 -05:00
|
|
|
|
2009-01-07 21:56:09 -05:00
|
|
|
M: cocoa-ui-backend (with-ui)
|
2007-09-20 18:09:08 -04:00
|
|
|
"UI" assert.app [
|
|
|
|
[
|
|
|
|
init-clipboard
|
2009-03-16 04:01:47 -04:00
|
|
|
cocoa-init-hook get call( -- )
|
2007-09-20 18:09:08 -04:00
|
|
|
start-ui
|
2008-12-13 00:58:28 -05:00
|
|
|
f io-thread-running? set-global
|
|
|
|
init-thread-timer
|
|
|
|
reset-run-loop
|
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
|
|
|
|
2009-01-07 21:56:09 -05:00
|
|
|
[ running.app? "ui.tools" "listener" ? ] main-vocab-hook set-global
|