factor/basis/ui/backend/cocoa/cocoa.factor

230 lines
7.6 KiB
Factor
Raw Normal View History

! Copyright (C) 2006, 2009 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors alien.c-types alien.data arrays assocs classes
cocoa cocoa.application cocoa.classes cocoa.messages cocoa.nibs
2009-05-01 13:56:52 -04:00
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.bitwise math.rectangles
memory namespaces sequences threads ui colors ui.backend
ui.backend.cocoa.views ui.clipboards ui.gadgets
ui.gadgets.worlds ui.pixel-formats ui.pixel-formats.private
ui.private words.symbol ;
IN: ui.backend.cocoa
2007-09-20 18:09:08 -04:00
2016-03-30 10:31:13 -04:00
TUPLE: window-handle view window ;
2008-03-19 15:25:53 -04:00
C: <window-handle> window-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
CONSTANT: attrib-table H{
{ double-buffered { $ NSOpenGLPFADoubleBuffer } }
{ stereo { $ NSOpenGLPFAStereo } }
{ offscreen { $ NSOpenGLPFAOffScreen } }
{ fullscreen { $ NSOpenGLPFAFullScreen } }
{ windowed { $ NSOpenGLPFAWindow } }
{ accelerated { $ NSOpenGLPFAAccelerated } }
{ software-rendered {
$ NSOpenGLPFARendererID
$ kCGLRendererGenericFloatID }
}
{ 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)
nip { } attrib-table pixel-format-attributes>int-array
NSOpenGLPixelFormat send: alloc swap send: \initWithAttributes: ;
M: cocoa-ui-backend (free-pixel-format)
handle>> send: release ;
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 send: generalPasteboard <pasteboard>
2007-09-20 18:09:08 -04:00
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 -- )
2015-09-08 19:15:10 -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
2018-01-27 11:38:35 -05:00
worlds get-global length 1 <= [ send: center ] [
2018-01-22 16:17:23 -05:00
worlds get-global last second window-loc>>
2018-01-27 11:38:35 -05:00
dupd first2 <CGPoint> send: \cascadeTopLeftFromPoint:
send: \setFrameTopLeftPoint:
2009-04-13 04:16:57 -04:00
] if
] [ first2 <CGPoint> send: \setFrameTopLeftPoint: ] if ;
2007-09-20 18:09:08 -04:00
M: cocoa-ui-backend set-title ( string world -- )
handle>> window>> swap <NSString> send: \setTitle: ;
2007-09-20 18:09:08 -04:00
: enter-fullscreen ( world -- )
2008-09-01 20:02:44 -04:00
handle>> view>>
NSScreen send: mainScreen
f send: \enterFullScreenMode:withOptions:
2008-03-19 15:25:53 -04:00
drop ;
: exit-fullscreen ( world -- )
handle>>
[ view>> f send: \exitFullScreenModeWithOptions: ]
[ [ window>> ] [ view>> ] bi send: \makeFirstResponder: drop ] bi ;
M: cocoa-ui-backend (set-fullscreen) ( world ? -- )
[ enter-fullscreen ] [ exit-fullscreen ] if ;
M: cocoa-ui-backend (fullscreen?) ( world -- ? )
handle>> view>> send: isInFullScreenMode zero? not ;
! XXX: Until someone tests OSX with a tiling window manager,
! dialog-window is the same as normal-title-window
CONSTANT: window-control>styleMask
H{
{ close-button $ NSClosableWindowMask }
{ minimize-button $ NSMiniaturizableWindowMask }
{ maximize-button 0 }
{ resize-handles $ NSResizableWindowMask }
{ small-title-bar $[ NSTitledWindowMask NSUtilityWindowMask bitor ] }
{ textured-background $ NSTexturedBackgroundWindowMask }
{ normal-title-bar $ NSTitledWindowMask }
{ dialog-window $ NSTitledWindowMask }
}
: world>styleMask ( world -- n )
2009-06-18 12:41:34 -04:00
window-controls>> window-control>styleMask symbols>flags ;
: make-context-transparent ( view -- )
send: openGLContext
0 int <ref> NSOpenGLCPSurfaceOpacity send: \setValues:forParameter: ;
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
world window-controls>> textured-background swap member-eq?
2009-09-25 10:42:09 -04:00
[ view make-context-transparent ] when
view world [ world>NSRect ] [ world>styleMask ] bi <ViewWindow> :> window
view send: release
2009-02-17 20:26:32 -05:00
world view register-window
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 send: \makeKeyAndOrderFront:
2016-03-30 10:31:13 -04:00
t world active?<< ;
2007-09-20 18:09:08 -04:00
M: cocoa-ui-backend (close-window) ( handle -- )
[
view>> dup send: isInFullScreenMode zero?
[ drop ]
[ f send: \exitFullScreenModeWithOptions: ] if
] [ window>> send: release ] bi ;
2009-05-08 16:07:15 -04:00
M: cocoa-ui-backend (grab-input) ( handle -- )
0 CGAssociateMouseAndMouseCursorPosition drop
CGMainDisplayID CGDisplayHideCursor drop
window>> send: frame CGRect>rect rect-center
NSScreen send: screens 0 send: \objectAtIndex: send: frame CGRect-h
[ drop first ] [ swap second - ] 2bi <CGPoint>
[ GetCurrentButtonState zero? not ] [ yield ] while
CGWarpMouseCursorPosition drop ;
2009-05-08 16:07:15 -04:00
M: cocoa-ui-backend (ungrab-input) ( handle -- )
drop
CGMainDisplayID CGDisplayShowCursor drop
1 CGAssociateMouseAndMouseCursorPosition drop ;
M: cocoa-ui-backend close-window ( gadget -- )
find-world [
2008-09-01 20:02:44 -04:00
handle>> [
window>> send: close
2008-03-19 15:25:53 -04:00
] when*
] 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>> [
window>> dup f send: \orderFront: send: makeKeyWindow
NSApp 1 send: \activateIgnoringOtherApps:
2007-09-20 18:09:08 -04:00
] when* ;
2016-03-30 10:31:13 -04:00
M: window-handle select-gl-context ( handle -- )
view>> send: openGLContext send: makeCurrentContext ;
2016-03-30 10:31:13 -04:00
M: window-handle flush-gl-context ( handle -- )
view>> send: openGLContext send: flushBuffer ;
M: cocoa-ui-backend beep ( -- )
NSBeep ;
2016-03-30 10:31:13 -04:00
M: cocoa-ui-backend resize-window
[ handle>> window>> ] [ first2 ] bi* <CGSize> send: \setContentSize: ;
2012-11-25 17:21:24 -05:00
M: cocoa-ui-backend system-alert
NSAlert send: alloc send: init send: autorelease [
{
[ swap <NSString> send: \setInformativeText: ]
[ swap <NSString> send: \setMessageText: ]
[ "OK" <NSString> send: \addButtonWithTitle: drop ]
[ send: runModal drop ]
} cleave
] [ 2drop ] if* ;
<CLASS: FactorApplicationDelegate < NSObject
COCOA-METHOD: void applicationDidUpdate: id obj [ reset-thread-timer ] ;
COCOA-METHOD: char applicationShouldTerminateAfterLastWindowClosed: id app [
ui-stop-after-last-window? get 1 0 ?
] ;
;CLASS>
: install-app-delegate ( -- )
NSApp FactorApplicationDelegate install-delegate ;
INITIALIZED-SYMBOL: cocoa-startup-hook [
[ "MiniFactor.nib" load-nib install-app-delegate ]
]
M: cocoa-ui-backend (with-ui)
2007-09-20 18:09:08 -04:00
"UI" assert.app [
2018-02-18 12:34:48 -05:00
init-clipboard
cocoa-startup-hook get call( -- )
start-ui
stop-io-thread
init-thread-timer
reset-thread-timer
NSApp send: run
NSApp send: run
2007-09-20 18:09:08 -04:00
] 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
M: cocoa-ui-backend ui-backend-available?
running.app? ;