changeset 12:e0dcf6c116be

Simplified directory/package structure.
author Atul Varma <varmaa@toolness.com>
date Sun, 24 Feb 2008 11:44:34 -0600
parents 37cc1514eb00
children f33df8208c7d
files enso_osx/__init__.py enso_osx/graphics.py enso_osx/graphics/__init__.py enso_osx/graphics/transparentwindow.py enso_osx/input.py enso_osx/input_manager.py src/SConscript src/quartz_cairo_bridge.m
diffstat 8 files changed, 337 insertions(+), 341 deletions(-) [+]
line wrap: on
line diff
--- a/enso_osx/__init__.py	Sun Feb 24 11:36:47 2008 -0600
+++ b/enso_osx/__init__.py	Sun Feb 24 11:44:34 2008 -0600
@@ -1,11 +1,9 @@
 def provideInterface( name ):
     if name == "input":
-        import enso_osx.input_manager
-        return enso_osx.input_manager
+        import enso_osx.input
+        return enso_osx.input
     elif name == "graphics":
         import enso_osx.graphics
-        from enso_osx.graphics.transparentwindow import TransparentWindow
-        enso_osx.graphics.TransparentWindow = TransparentWindow
         return enso_osx.graphics
     else:
         return None
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/enso_osx/graphics.py	Sun Feb 24 11:44:34 2008 -0600
@@ -0,0 +1,144 @@
+import os
+import weakref
+import cStringIO
+
+import objc
+import AppKit
+import Foundation
+import cairo
+
+from enso_osx import quartz_cairo_bridge
+
+MAX_OPACITY = 0xff
+
+class _TransparentWindowView( AppKit.NSView ):
+    def initWithParent_( self, parent ):
+        self = super( _TransparentWindowView, self ).init()
+        if self == None:
+            return None
+        self.__parent = weakref.ref( parent )
+        return self
+
+    def drawRect_( self, rect ):
+        parent = self.__parent()
+        if not parent:
+            return
+
+        surface = parent._surface
+        if not surface:
+            return
+
+        context = AppKit.NSGraphicsContext.currentContext()
+
+        # Taken from the OS X Cocoa Drawing Guide section on
+        # "Creating a Flip Transform".
+        frameRect = self.bounds()
+        xform = AppKit.NSAffineTransform.transform()
+        xform.translateXBy_yBy_( 0.0, frameRect.size.height )
+        xform.scaleXBy_yBy_( 1.0, -1.0 )
+        xform.concat()
+
+        parent._imageRep.draw()
+
+def _convertY( y, height ):
+    """
+    Flip a y-coordinate to account for the fact that OS X has its
+    origin at the bottom-left of an image instead of the top-left, as
+    Enso expects it to be.
+    """
+
+    screenSize = getDesktopSize()
+    return screenSize[1] - y - height
+
+class TransparentWindow( object ):
+    def __init__( self, x, y, maxWidth, maxHeight ):
+        self.__x = x
+        self.__y = y
+        self.__maxWidth = maxWidth
+        self.__maxHeight = maxHeight
+        self.__width = maxWidth
+        self.__height = maxHeight
+        self._surface = None
+        self.__opacity = 0xff
+
+        rect = Foundation.NSMakeRect( self.__x,
+                                      _convertY( self.__y, self.__height ),
+                                      self.__width,
+                                      self.__height )
+        style = AppKit.NSBorderlessWindowMask
+        self.__wind = AppKit.NSWindow.alloc().initWithContentRect_styleMask_backing_defer_( rect, style, AppKit.NSBackingStoreBuffered, objc.YES )
+        self.__wind.setBackgroundColor_( AppKit.NSColor.clearColor() )
+        self.__view = _TransparentWindowView.alloc().initWithParent_( self )
+        self.__wind.setContentView_( self.__view )
+        self.__wind.setLevel_( AppKit.NSPopUpMenuWindowLevel )
+        self.__wind.setOpaque_( objc.NO )
+        self.__wind.setAlphaValue_( 1.0 )
+
+    def update( self ):
+        if self._surface:
+            self.__wind.makeKeyAndOrderFront_( objc.nil )
+            self.__view.setNeedsDisplay_( objc.YES )
+
+    def makeCairoSurface( self ):
+        if not self._surface:
+            self._imageRep = AppKit.NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(
+                None,
+                self.__maxWidth,
+                self.__maxHeight,
+                8,
+                4,
+                True,
+                False,
+                AppKit.NSCalibratedRGBColorSpace,
+                0,
+                4 * self.__maxWidth,
+                32
+                )
+            self._nsContext = AppKit.NSGraphicsContext.graphicsContextWithBitmapImageRep_( self._imageRep )
+            self._surface = quartz_cairo_bridge.cairo_surface_from_NSGraphicsContext( self._nsContext, self.__maxWidth, self.__maxHeight )
+        return self._surface
+
+    def setOpacity( self, opacity ):
+        self.__opacity = opacity
+        self.__wind.setAlphaValue_( (float(opacity)/MAX_OPACITY) * 1.0 )
+
+    def getOpacity( self ):
+        return self.__opacity
+
+    def setPosition( self, x, y ):
+        self.__x = x
+        self.__y = y
+        topLeft = Foundation.NSPoint( self.__x,
+                                      _convertY( self.__y, self.__height ) )
+        self.__wind.setFrameTopLeftPoint_( topLeft )
+
+    def getX( self ):
+        return self.__x
+
+    def getY( self ):
+        return self.__y
+
+    def setSize( self, width, height ):
+        self.__width = width
+        self.__height = height
+        rect = Foundation.NSMakeRect( self.__x,
+                                      _convertY( self.__y, self.__height ),
+                                      self.__width,
+                                      self.__height )
+        self.__wind.setFrame_display_( rect, objc.YES )
+
+    def getWidth( self ):
+        return self.__width
+
+    def getHeight( self ):
+        return self.__height
+
+    def getMaxWidth( self ):
+        return self.__maxWidth
+
+    def getMaxHeight( self ):
+        return self.__maxHeight
+
+def getDesktopSize():
+    size = AppKit.NSScreen.mainScreen().frame().size
+    return ( size.width, size.height )
--- a/enso_osx/graphics/__init__.py	Sun Feb 24 11:36:47 2008 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5 +0,0 @@
-import AppKit
-
-def getDesktopSize():
-    size = AppKit.NSScreen.mainScreen().frame().size
-    return ( size.width, size.height )
--- a/enso_osx/graphics/transparentwindow.py	Sun Feb 24 11:36:47 2008 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,141 +0,0 @@
-import os
-import weakref
-import cStringIO
-
-import objc
-import AppKit
-import Foundation
-import cairo
-
-from enso_osx.graphics import quartz_cairo_bridge
-from enso_osx.graphics import getDesktopSize as _getDesktopSize
-
-MAX_OPACITY = 0xff
-
-class _TransparentWindowView( AppKit.NSView ):
-    def initWithParent_( self, parent ):
-        self = super( _TransparentWindowView, self ).init()
-        if self == None:
-            return None
-        self.__parent = weakref.ref( parent )
-        return self
-
-    def drawRect_( self, rect ):
-        parent = self.__parent()
-        if not parent:
-            return
-
-        surface = parent._surface
-        if not surface:
-            return
-
-        context = AppKit.NSGraphicsContext.currentContext()
-
-        # Taken from the OS X Cocoa Drawing Guide section on
-        # "Creating a Flip Transform".
-        frameRect = self.bounds()
-        xform = AppKit.NSAffineTransform.transform()
-        xform.translateXBy_yBy_( 0.0, frameRect.size.height )
-        xform.scaleXBy_yBy_( 1.0, -1.0 )
-        xform.concat()
-
-        parent._imageRep.draw()
-
-def _convertY( y, height ):
-    """
-    Flip a y-coordinate to account for the fact that OS X has its
-    origin at the bottom-left of an image instead of the top-left, as
-    Enso expects it to be.
-    """
-
-    screenSize = _getDesktopSize()
-    return screenSize[1] - y - height
-
-class TransparentWindow( object ):
-    def __init__( self, x, y, maxWidth, maxHeight ):
-        self.__x = x
-        self.__y = y
-        self.__maxWidth = maxWidth
-        self.__maxHeight = maxHeight
-        self.__width = maxWidth
-        self.__height = maxHeight
-        self._surface = None
-        self.__opacity = 0xff
-
-        rect = Foundation.NSMakeRect( self.__x,
-                                      _convertY( self.__y, self.__height ),
-                                      self.__width,
-                                      self.__height )
-        style = AppKit.NSBorderlessWindowMask
-        self.__wind = AppKit.NSWindow.alloc().initWithContentRect_styleMask_backing_defer_( rect, style, AppKit.NSBackingStoreBuffered, objc.YES )
-        self.__wind.setBackgroundColor_( AppKit.NSColor.clearColor() )
-        self.__view = _TransparentWindowView.alloc().initWithParent_( self )
-        self.__wind.setContentView_( self.__view )
-        self.__wind.setLevel_( AppKit.NSPopUpMenuWindowLevel )
-        self.__wind.setOpaque_( objc.NO )
-        self.__wind.setAlphaValue_( 1.0 )
-
-    def update( self ):
-        if self._surface:
-            self.__wind.makeKeyAndOrderFront_( objc.nil )
-            self.__view.setNeedsDisplay_( objc.YES )
-
-    def makeCairoSurface( self ):
-        if not self._surface:
-            self._imageRep = AppKit.NSBitmapImageRep.alloc().initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bitmapFormat_bytesPerRow_bitsPerPixel_(
-                None,
-                self.__maxWidth,
-                self.__maxHeight,
-                8,
-                4,
-                True,
-                False,
-                AppKit.NSCalibratedRGBColorSpace,
-                0,
-                4 * self.__maxWidth,
-                32
-                )
-            self._nsContext = AppKit.NSGraphicsContext.graphicsContextWithBitmapImageRep_( self._imageRep )
-            self._surface = quartz_cairo_bridge.cairo_surface_from_NSGraphicsContext( self._nsContext, self.__maxWidth, self.__maxHeight )
-        return self._surface
-
-    def setOpacity( self, opacity ):
-        self.__opacity = opacity
-        self.__wind.setAlphaValue_( (float(opacity)/MAX_OPACITY) * 1.0 )
-
-    def getOpacity( self ):
-        return self.__opacity
-
-    def setPosition( self, x, y ):
-        self.__x = x
-        self.__y = y
-        topLeft = Foundation.NSPoint( self.__x,
-                                      _convertY( self.__y, self.__height ) )
-        self.__wind.setFrameTopLeftPoint_( topLeft )
-
-    def getX( self ):
-        return self.__x
-
-    def getY( self ):
-        return self.__y
-
-    def setSize( self, width, height ):
-        self.__width = width
-        self.__height = height
-        rect = Foundation.NSMakeRect( self.__x,
-                                      _convertY( self.__y, self.__height ),
-                                      self.__width,
-                                      self.__height )
-        self.__wind.setFrame_display_( rect, objc.YES )
-
-    def getWidth( self ):
-        return self.__width
-
-    def getHeight( self ):
-        return self.__height
-
-    def getMaxWidth( self ):
-        return self.__maxWidth
-
-    def getMaxHeight( self ):
-        return self.__maxHeight
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/enso_osx/input.py	Sun Feb 24 11:44:34 2008 -0600
@@ -0,0 +1,189 @@
+import objc
+import Foundation
+import AppKit
+
+# Timer interval in seconds.
+_TIMER_INTERVAL = 0.010
+
+# Timer interval in milliseconds.
+_TIMER_INTERVAL_IN_MS = int( _TIMER_INTERVAL * 1000 )
+
+KEYCODE_CAPITAL = -1
+KEYCODE_SPACE = 49
+KEYCODE_LSHIFT = -1
+KEYCODE_RSHIFT = -1
+KEYCODE_LCONTROL = -1
+KEYCODE_RCONTROL = -1
+KEYCODE_LWIN = -1
+KEYCODE_RWIN = -1
+KEYCODE_RETURN = 36
+KEYCODE_ESCAPE = 53
+KEYCODE_TAB = 48
+KEYCODE_BACK = 51
+KEYCODE_DOWN = 125
+KEYCODE_UP = 126
+
+EVENT_KEY_UP = 0
+EVENT_KEY_DOWN = 1
+EVENT_KEY_QUASIMODE = 2
+
+KEYCODE_QUASIMODE_START = 0
+KEYCODE_QUASIMODE_END = 1
+KEYCODE_QUASIMODE_CANCEL = 2
+
+_inputManager = None
+
+def get():
+    return _inputManager
+
+class _Timer( Foundation.NSObject ):
+    def initWithCallback_( self, callback ):
+        self = super( _Timer, self ).init()
+        if self == None:
+            return None
+        self.__callback = callback
+        return self
+
+    def onTimer( self ):
+        self.__callback()
+
+class _KeyListener( Foundation.NSObject ):
+    def initWithCallback_( self, callback ):
+        self = super( _KeyListener, self ).init()
+        if self == None:
+            return None
+        self.__callback = callback
+        return self
+
+    def onNotification( self, notification ):
+        #print "notification received: %s" % notification.name()
+        userInfo = notification.userInfo()
+        eventDict = {}
+        for key in userInfo:
+            eventDict[key] = userInfo.objectForKey_(key)
+        self.__callback( eventDict )
+
+    def register( self ):
+        self.__center = Foundation.NSDistributedNotificationCenter.defaultCenter()
+        self.__center.addObserver_selector_name_object_(
+            self,
+            self.onNotification,
+            u"KeyNotifier_msg",
+            u"KeyNotifier"
+            )
+
+    def unregister( self ):
+        self.__center.removeObserver_( self )
+
+class InputManager( object ):
+    def __init__( self, quasimodeKeycode ):
+        global _inputManager
+
+        self.__shouldStop = False
+        self.__mouseEventsEnabled = False
+        _inputManager = self
+        self.__qmKeycodes = [0, 0, 0]
+        self.__isModal = False
+        self.__inQuasimode = False
+
+    def __timerCallback( self ):
+        self.onTick( _TIMER_INTERVAL_IN_MS )
+
+    def __keyCallback( self, info ):
+        if info["event"] == "quasimodeStart":
+            self.onKeypress( EVENT_KEY_QUASIMODE,
+                             KEYCODE_QUASIMODE_START )
+        elif info["event"] == "quasimodeEnd":
+            self.onKeypress( EVENT_KEY_QUASIMODE,
+                             KEYCODE_QUASIMODE_END )
+        elif info["event"] == "someKey":
+            self.onSomeKey()
+        elif info["event"] in ["keyUp", "keyDown"]:
+            keycode = info["keycode"]
+            if info["event"] == "keyUp":
+                eventType = EVENT_KEY_UP
+            else:
+                eventType = EVENT_KEY_DOWN
+            self.onKeypress( eventType, keycode )
+        else:
+            print "Don't know what to do with event: %s" % info
+
+    def run( self ):
+        print "Entering InputManager.run()"
+
+        app = AppKit.NSApplication.sharedApplication()
+
+        timer = _Timer.alloc().initWithCallback_( self.__timerCallback )
+        signature = timer.methodSignatureForSelector_( timer.onTimer )
+        invocation = Foundation.NSInvocation.invocationWithMethodSignature_(
+            signature
+            )
+        invocation.setSelector_( timer.onTimer )
+        invocation.setTarget_( timer )
+
+        Foundation.NSTimer.scheduledTimerWithTimeInterval_invocation_repeats_(
+            _TIMER_INTERVAL,
+            invocation,
+            objc.YES
+            )
+
+        keyListener = _KeyListener.alloc().initWithCallback_(
+            self.__keyCallback
+            )
+        keyListener.register()
+
+        try:
+            self.onInit()
+            while not self.__shouldStop:
+                #print "Waiting for event."
+                event = app.nextEventMatchingMask_untilDate_inMode_dequeue_(
+                    0xffff,
+                    Foundation.NSDate.distantFuture(),
+                    AppKit.NSDefaultRunLoopMode,
+                    objc.YES
+                    )
+                if event:
+                    app.sendEvent_( event )
+        finally:
+            keyListener.unregister()
+
+        print "Exiting InputManager.run()"
+
+    def stop( self ):
+        self.__shouldStop = True
+
+    def enableMouseEvents( self, isEnabled ):
+        self.__mouseEventsEnabled = isEnabled
+
+    def onKeypress( self, eventType, vkCode ):
+        pass
+
+    def onSomeKey( self ):
+        pass
+
+    def onSomeMouseButton( self ):
+        pass
+
+    def onExitRequested( self ):
+        pass
+
+    def onMouseMove( self, x, y ):
+        pass
+
+    def getQuasimodeKeycode( self, quasimodeKeycode ):
+        return self.__qmKeycodes[quasimodeKeycode]
+
+    def setQuasimodeKeycode( self, quasimodeKeycode, keycode ):
+        self.__qmKeycodes[quasimodeKeycode] = keycode
+
+    def setModality( self, isModal ):
+        self.__isModal = isModal
+
+    def setCapsLockMode( self, isCapsLockEnabled ):
+        pass
+
+    def onTick( self, msPassed ):
+        pass
+
+    def onInit( self ):
+        pass
--- a/enso_osx/input_manager.py	Sun Feb 24 11:36:47 2008 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,189 +0,0 @@
-import objc
-import Foundation
-import AppKit
-
-# Timer interval in seconds.
-_TIMER_INTERVAL = 0.010
-
-# Timer interval in milliseconds.
-_TIMER_INTERVAL_IN_MS = int( _TIMER_INTERVAL * 1000 )
-
-KEYCODE_CAPITAL = -1
-KEYCODE_SPACE = 49
-KEYCODE_LSHIFT = -1
-KEYCODE_RSHIFT = -1
-KEYCODE_LCONTROL = -1
-KEYCODE_RCONTROL = -1
-KEYCODE_LWIN = -1
-KEYCODE_RWIN = -1
-KEYCODE_RETURN = 36
-KEYCODE_ESCAPE = 53
-KEYCODE_TAB = 48
-KEYCODE_BACK = 51
-KEYCODE_DOWN = 125
-KEYCODE_UP = 126
-
-EVENT_KEY_UP = 0
-EVENT_KEY_DOWN = 1
-EVENT_KEY_QUASIMODE = 2
-
-KEYCODE_QUASIMODE_START = 0
-KEYCODE_QUASIMODE_END = 1
-KEYCODE_QUASIMODE_CANCEL = 2
-
-_inputManager = None
-
-def get():
-    return _inputManager
-
-class _Timer( Foundation.NSObject ):
-    def initWithCallback_( self, callback ):
-        self = super( _Timer, self ).init()
-        if self == None:
-            return None
-        self.__callback = callback
-        return self
-
-    def onTimer( self ):
-        self.__callback()
-
-class _KeyListener( Foundation.NSObject ):
-    def initWithCallback_( self, callback ):
-        self = super( _KeyListener, self ).init()
-        if self == None:
-            return None
-        self.__callback = callback
-        return self
-
-    def onNotification( self, notification ):
-        #print "notification received: %s" % notification.name()
-        userInfo = notification.userInfo()
-        eventDict = {}
-        for key in userInfo:
-            eventDict[key] = userInfo.objectForKey_(key)
-        self.__callback( eventDict )
-
-    def register( self ):
-        self.__center = Foundation.NSDistributedNotificationCenter.defaultCenter()
-        self.__center.addObserver_selector_name_object_(
-            self,
-            self.onNotification,
-            u"KeyNotifier_msg",
-            u"KeyNotifier"
-            )
-
-    def unregister( self ):
-        self.__center.removeObserver_( self )
-
-class InputManager( object ):
-    def __init__( self, quasimodeKeycode ):
-        global _inputManager
-
-        self.__shouldStop = False
-        self.__mouseEventsEnabled = False
-        _inputManager = self
-        self.__qmKeycodes = [0, 0, 0]
-        self.__isModal = False
-        self.__inQuasimode = False
-
-    def __timerCallback( self ):
-        self.onTick( _TIMER_INTERVAL_IN_MS )
-
-    def __keyCallback( self, info ):
-        if info["event"] == "quasimodeStart":
-            self.onKeypress( EVENT_KEY_QUASIMODE,
-                             KEYCODE_QUASIMODE_START )
-        elif info["event"] == "quasimodeEnd":
-            self.onKeypress( EVENT_KEY_QUASIMODE,
-                             KEYCODE_QUASIMODE_END )
-        elif info["event"] == "someKey":
-            self.onSomeKey()
-        elif info["event"] in ["keyUp", "keyDown"]:
-            keycode = info["keycode"]
-            if info["event"] == "keyUp":
-                eventType = EVENT_KEY_UP
-            else:
-                eventType = EVENT_KEY_DOWN
-            self.onKeypress( eventType, keycode )
-        else:
-            print "Don't know what to do with event: %s" % info
-
-    def run( self ):
-        print "Entering InputManager.run()"
-
-        app = AppKit.NSApplication.sharedApplication()
-
-        timer = _Timer.alloc().initWithCallback_( self.__timerCallback )
-        signature = timer.methodSignatureForSelector_( timer.onTimer )
-        invocation = Foundation.NSInvocation.invocationWithMethodSignature_(
-            signature
-            )
-        invocation.setSelector_( timer.onTimer )
-        invocation.setTarget_( timer )
-
-        Foundation.NSTimer.scheduledTimerWithTimeInterval_invocation_repeats_(
-            _TIMER_INTERVAL,
-            invocation,
-            objc.YES
-            )
-
-        keyListener = _KeyListener.alloc().initWithCallback_(
-            self.__keyCallback
-            )
-        keyListener.register()
-
-        try:
-            self.onInit()
-            while not self.__shouldStop:
-                #print "Waiting for event."
-                event = app.nextEventMatchingMask_untilDate_inMode_dequeue_(
-                    0xffff,
-                    Foundation.NSDate.distantFuture(),
-                    AppKit.NSDefaultRunLoopMode,
-                    objc.YES
-                    )
-                if event:
-                    app.sendEvent_( event )
-        finally:
-            keyListener.unregister()
-
-        print "Exiting InputManager.run()"
-
-    def stop( self ):
-        self.__shouldStop = True
-
-    def enableMouseEvents( self, isEnabled ):
-        self.__mouseEventsEnabled = isEnabled
-
-    def onKeypress( self, eventType, vkCode ):
-        pass
-
-    def onSomeKey( self ):
-        pass
-
-    def onSomeMouseButton( self ):
-        pass
-
-    def onExitRequested( self ):
-        pass
-
-    def onMouseMove( self, x, y ):
-        pass
-
-    def getQuasimodeKeycode( self, quasimodeKeycode ):
-        return self.__qmKeycodes[quasimodeKeycode]
-
-    def setQuasimodeKeycode( self, quasimodeKeycode, keycode ):
-        self.__qmKeycodes[quasimodeKeycode] = keycode
-
-    def setModality( self, isModal ):
-        self.__isModal = isModal
-
-    def setCapsLockMode( self, isCapsLockEnabled ):
-        pass
-
-    def onTick( self, msPassed ):
-        pass
-
-    def onInit( self ):
-        pass
--- a/src/SConscript	Sun Feb 24 11:36:47 2008 -0600
+++ b/src/SConscript	Sun Feb 24 11:44:34 2008 -0600
@@ -61,7 +61,7 @@
     target = ["quartz_cairo_bridge.so"],
     )
 
-qcbEnv.Install( "#enso_osx/graphics", quartzCairoBridge )
+qcbEnv.Install( "#enso_osx", quartzCairoBridge )
 
 # key notifier
 
--- a/src/quartz_cairo_bridge.m	Sun Feb 24 11:36:47 2008 -0600
+++ b/src/quartz_cairo_bridge.m	Sun Feb 24 11:44:34 2008 -0600
@@ -58,7 +58,7 @@
 PyMODINIT_FUNC
 initquartz_cairo_bridge( void )
 {
-    Py_InitModule( "enso_osx.graphics.quartz_cairo_bridge",
+    Py_InitModule( "enso_osx.quartz_cairo_bridge",
                    quartz_cairo_bridge_methods );
 
     Pycairo_IMPORT;