Don't center all windows on Cocoa
							parent
							
								
									4641bfff2d
								
							
						
					
					
						commit
						4c7405f353
					
				| 
						 | 
					@ -1,6 +1,6 @@
 | 
				
			||||||
! Copyright (C) 2009 Slava Pestov.
 | 
					! Copyright (C) 2009 Slava Pestov.
 | 
				
			||||||
! See http://factorcode.org/license.txt for BSD license.
 | 
					! See http://factorcode.org/license.txt for BSD license.
 | 
				
			||||||
USING: alien.c-types alien.syntax kernel layouts ;
 | 
					USING: alien.c-types alien.syntax kernel layouts math ;
 | 
				
			||||||
IN: core-graphics.types
 | 
					IN: core-graphics.types
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<< cell 4 = "float" "double" ? "CGFloat" typedef >>
 | 
					<< cell 4 = "float" "double" ? "CGFloat" typedef >>
 | 
				
			||||||
| 
						 | 
					@ -59,7 +59,10 @@ C-STRUCT: CGRect
 | 
				
			||||||
    [ set-CGRect-x ] keep ;
 | 
					    [ set-CGRect-x ] keep ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: CGRect-x-y ( alien -- origin-x origin-y )
 | 
					: CGRect-x-y ( alien -- origin-x origin-y )
 | 
				
			||||||
    [ CGRect-x ] keep CGRect-y ;
 | 
					    [ CGRect-x ] [ CGRect-y ] bi ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					: CGRect-top-left ( alien -- x y )
 | 
				
			||||||
 | 
					    [ CGRect-x ] [ [ CGRect-y ] [ CGRect-h ] bi + ] bi ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
C-STRUCT: CGAffineTransform
 | 
					C-STRUCT: CGAffineTransform
 | 
				
			||||||
    { "CGFloat" "a" }
 | 
					    { "CGFloat" "a" }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,12 +3,12 @@
 | 
				
			||||||
USING: accessors math arrays assocs cocoa cocoa.application
 | 
					USING: accessors math arrays assocs cocoa cocoa.application
 | 
				
			||||||
command-line kernel memory namespaces cocoa.messages
 | 
					command-line kernel memory namespaces cocoa.messages
 | 
				
			||||||
cocoa.runtime cocoa.subclassing cocoa.pasteboard cocoa.types
 | 
					cocoa.runtime cocoa.subclassing cocoa.pasteboard cocoa.types
 | 
				
			||||||
cocoa.windows cocoa.classes cocoa.nibs sequences system ui
 | 
					cocoa.windows cocoa.classes cocoa.nibs sequences ui
 | 
				
			||||||
ui.backend ui.clipboards ui.gadgets ui.gadgets.worlds
 | 
					ui.backend ui.clipboards ui.gadgets ui.gadgets.worlds
 | 
				
			||||||
ui.backend.cocoa.views core-foundation core-foundation.run-loop
 | 
					ui.backend.cocoa.views core-foundation core-foundation.run-loop
 | 
				
			||||||
core-graphics.types threads math.rectangles fry libc
 | 
					core-graphics.types threads math.rectangles fry libc
 | 
				
			||||||
generalizations alien.c-types cocoa.views
 | 
					generalizations alien.c-types cocoa.views
 | 
				
			||||||
combinators io.thread ;
 | 
					combinators io.thread locals ;
 | 
				
			||||||
IN: ui.backend.cocoa
 | 
					IN: ui.backend.cocoa
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TUPLE: handle ;
 | 
					TUPLE: handle ;
 | 
				
			||||||
| 
						 | 
					@ -36,14 +36,17 @@ M: pasteboard set-clipboard-contents
 | 
				
			||||||
    <clipboard> selection set-global ;
 | 
					    <clipboard> selection set-global ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: world>NSRect ( world -- NSRect )
 | 
					: world>NSRect ( world -- NSRect )
 | 
				
			||||||
    [ window-loc>> ] [ dim>> ] bi [ first2 ] bi@ <CGRect> ;
 | 
					    [ 0 0 ] dip dim>> first2 <CGRect> ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: gadget-window ( world -- )
 | 
					: auto-position ( window loc -- )
 | 
				
			||||||
    dup <FactorView>
 | 
					    dup { 0 0 } = [
 | 
				
			||||||
    2dup swap world>NSRect <ViewWindow>
 | 
					        drop
 | 
				
			||||||
    [ [ -> release ] [ install-window-delegate ] bi* ]
 | 
					        windows get [ -> center ] [
 | 
				
			||||||
    [ <window-handle> ] 2bi
 | 
					            peek second window-loc>>
 | 
				
			||||||
    >>handle drop ;
 | 
					            dupd first2 <CGPoint> -> cascadeTopLeftFromPoint:
 | 
				
			||||||
 | 
					            -> setFrameTopLeftPoint:
 | 
				
			||||||
 | 
					        ] if-empty
 | 
				
			||||||
 | 
					    ] [ first2 <CGPoint> -> setFrameTopLeftPoint: ] if ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
M: cocoa-ui-backend set-title ( string world -- )
 | 
					M: cocoa-ui-backend set-title ( string world -- )
 | 
				
			||||||
    handle>> window>> swap <NSString> -> setTitle: ;
 | 
					    handle>> window>> swap <NSString> -> setTitle: ;
 | 
				
			||||||
| 
						 | 
					@ -63,17 +66,16 @@ M: cocoa-ui-backend set-fullscreen* ( ? world -- )
 | 
				
			||||||
M: cocoa-ui-backend fullscreen* ( world -- ? )
 | 
					M: cocoa-ui-backend fullscreen* ( world -- ? )
 | 
				
			||||||
    handle>> view>> -> isInFullScreenMode zero? not ;
 | 
					    handle>> view>> -> isInFullScreenMode zero? not ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: auto-position ( world -- )
 | 
					M:: cocoa-ui-backend (open-window) ( world -- )
 | 
				
			||||||
    dup window-loc>> { 0 0 } = [
 | 
					    world dim>> <FactorView> :> view
 | 
				
			||||||
        handle>> window>> -> center
 | 
					    view world world>NSRect <ViewWindow> :> window
 | 
				
			||||||
    ] [
 | 
					    view -> release
 | 
				
			||||||
        drop
 | 
					    window world window-loc>> auto-position
 | 
				
			||||||
    ] if ;
 | 
					    world view register-window
 | 
				
			||||||
 | 
					    world window save-position
 | 
				
			||||||
M: cocoa-ui-backend (open-window) ( world -- )
 | 
					    window install-window-delegate
 | 
				
			||||||
    dup gadget-window
 | 
					    view window <window-handle> world (>>handle)
 | 
				
			||||||
    dup auto-position
 | 
					    window f -> makeKeyAndOrderFront: ;
 | 
				
			||||||
    handle>> window>> f -> makeKeyAndOrderFront: ;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
M: cocoa-ui-backend (close-window) ( handle -- )
 | 
					M: cocoa-ui-backend (close-window) ( handle -- )
 | 
				
			||||||
    window>> -> release ;
 | 
					    window>> -> release ;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -365,10 +365,11 @@ CLASS: {
 | 
				
			||||||
    -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 <int>
 | 
					    -> openGLContext -> CGLContextObj NSOpenGLCPSwapInterval 1 <int>
 | 
				
			||||||
    CGLSetParameter drop ;
 | 
					    CGLSetParameter drop ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
: <FactorView> ( world -- view )
 | 
					: <FactorView> ( dim -- view )
 | 
				
			||||||
    FactorView over dim>> <GLView>
 | 
					    FactorView swap <GLView> [ sync-refresh-to-screen ] keep ;
 | 
				
			||||||
    [ sync-refresh-to-screen ] keep
 | 
					
 | 
				
			||||||
    [ register-window ] keep ;
 | 
					: save-position ( world window -- )
 | 
				
			||||||
 | 
					    -> frame CGRect-top-left 2array >>window-loc drop ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
CLASS: {
 | 
					CLASS: {
 | 
				
			||||||
    { +superclass+ "NSObject" }
 | 
					    { +superclass+ "NSObject" }
 | 
				
			||||||
| 
						 | 
					@ -377,10 +378,7 @@ CLASS: {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
{ "windowDidMove:" "void" { "id" "SEL" "id" }
 | 
					{ "windowDidMove:" "void" { "id" "SEL" "id" }
 | 
				
			||||||
    [
 | 
					    [
 | 
				
			||||||
        2nip -> object
 | 
					        2nip -> object [ -> contentView window ] keep save-position
 | 
				
			||||||
        [ -> contentView window ]
 | 
					 | 
				
			||||||
        [ window-content-rect CGRect-x-y 2array ] bi
 | 
					 | 
				
			||||||
        >>window-loc drop
 | 
					 | 
				
			||||||
    ]
 | 
					    ]
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue