factor/extra/ui/cocoa/cocoa.factor

125 lines
3.3 KiB
Factor
Raw Normal View History

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.
USING: math arrays cocoa cocoa.application command-line
2007-09-20 18:09:08 -04:00
kernel memory namespaces cocoa.messages cocoa.runtime
cocoa.subclassing cocoa.pasteboard cocoa.types cocoa.windows
cocoa.classes cocoa.application sequences system ui ui.backend
ui.clipboards ui.gadgets ui.gadgets.worlds ui.cocoa.views
2007-11-24 23:57:37 -05:00
core-foundation threads ;
2007-09-20 18:09:08 -04:00
IN: ui.cocoa
2008-03-19 15:25:53 -04:00
TUPLE: handle view window ;
C: <handle> handle
2008-04-02 20:44:01 -04:00
SINGLETON: cocoa-ui-backend
2007-09-20 18:09:08 -04:00
SYMBOL: stop-after-last-window?
: event-loop? ( -- ? )
stop-after-last-window? get-global
[ windows get-global empty? not ] [ t ] if ;
: event-loop ( -- )
event-loop? [
[
2008-02-25 17:48:11 -05:00
[ NSApp do-events ui-wait ] ui-try
2007-09-20 18:09:08 -04:00
] with-autorelease-pool event-loop
] when ;
TUPLE: pasteboard handle ;
C: <pasteboard> pasteboard
M: pasteboard clipboard-contents
pasteboard-handle pasteboard-string ;
M: pasteboard set-clipboard-contents
pasteboard-handle set-pasteboard-string ;
: init-clipboard ( -- )
NSPasteboard -> generalPasteboard <pasteboard>
clipboard set-global
<clipboard> selection set-global ;
: world>NSRect ( world -- NSRect )
dup world-loc first2 rot rect-dim first2 <NSRect> ;
: gadget-window ( world -- )
[
dup <FactorView>
dup rot world>NSRect <ViewWindow>
dup install-window-delegate
over -> release
2008-03-19 15:25:53 -04:00
<handle>
2007-09-20 18:09:08 -04:00
] keep set-world-handle ;
M: cocoa-ui-backend set-title ( string world -- )
2008-03-19 15:25:53 -04:00
world-handle handle-window swap <NSString> -> setTitle: ;
2007-09-20 18:09:08 -04:00
: enter-fullscreen ( world -- )
2008-03-19 15:25:53 -04:00
world-handle handle-view
NSScreen -> mainScreen
f -> enterFullScreenMode:withOptions:
drop ;
: exit-fullscreen ( world -- )
2008-03-19 15:25:53 -04:00
world-handle handle-view f -> exitFullScreenModeWithOptions: ;
M: cocoa-ui-backend set-fullscreen* ( ? world -- )
swap [ enter-fullscreen ] [ exit-fullscreen ] if ;
M: cocoa-ui-backend fullscreen* ( world -- ? )
2008-03-19 15:25:53 -04:00
world-handle handle-view -> isInFullScreenMode zero? not ;
2007-09-20 18:09:08 -04:00
: auto-position ( world -- )
dup world-loc { 0 0 } = [
2008-03-19 15:25:53 -04:00
world-handle handle-window -> center
2007-09-20 18:09:08 -04:00
] [
drop
] if ;
M: cocoa-ui-backend (open-window) ( world -- )
2007-09-20 18:09:08 -04:00
dup gadget-window
dup auto-position
2008-03-19 15:25:53 -04:00
world-handle handle-window f -> makeKeyAndOrderFront: ;
2007-09-20 18:09:08 -04:00
M: cocoa-ui-backend (close-window) ( handle -- )
2008-03-19 15:25:53 -04:00
handle-window -> release ;
M: cocoa-ui-backend close-window ( gadget -- )
find-world [
2008-03-19 15:25:53 -04:00
world-handle [
handle-window f -> performClose:
] when*
] when* ;
2008-02-21 00:13:31 -05:00
M: cocoa-ui-backend raise-window* ( world -- )
2007-09-20 18:09:08 -04:00
world-handle [
2008-03-19 15:25:53 -04:00
handle-window dup f -> orderFront: -> makeKeyWindow
2007-09-20 18:09:08 -04:00
NSApp 1 -> activateIgnoringOtherApps:
] when* ;
M: cocoa-ui-backend select-gl-context ( handle -- )
2008-03-19 15:25:53 -04:00
handle-view -> openGLContext -> makeCurrentContext ;
2007-09-20 18:09:08 -04:00
M: cocoa-ui-backend flush-gl-context ( handle -- )
2008-03-19 15:25:53 -04:00
handle-view -> openGLContext -> flushBuffer ;
2007-09-20 18:09:08 -04:00
SYMBOL: cocoa-init-hook
M: cocoa-ui-backend ui
"UI" assert.app [
[
init-clipboard
cocoa-init-hook get [ call ] when*
start-ui
finish-launching
event-loop
] 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