Ejemplo n.º 1
0
    def test_class_ivars(self):
        """An Objective-C class can have instance variables."""

        class Ivars(NSObject):
            object = objc_ivar(objc_id)
            int = objc_ivar(c_int)
            rect = objc_ivar(NSRect)

        ivars = Ivars.alloc().init()

        set_ivar(ivars, 'object', at('foo').ptr)
        set_ivar(ivars, 'int', c_int(12345))
        set_ivar(ivars, 'rect', NSMakeRect(12, 34, 56, 78))

        s = ObjCInstance(get_ivar(ivars, 'object'))
        self.assertEqual(str(s), 'foo')

        i = get_ivar(ivars, 'int')
        self.assertEqual(i.value, 12345)

        r = get_ivar(ivars, 'rect')
        self.assertEqual(r.origin.x, 12)
        self.assertEqual(r.origin.y, 34)
        self.assertEqual(r.size.width, 56)
        self.assertEqual(r.size.height, 78)
Ejemplo n.º 2
0
    def create(self):
        # OSX origin is bottom left of screen, and the screen might be
        # offset relative to other screens. Adjust for this.
        screen = NSScreen.mainScreen.visibleFrame
        position = NSMakeRect(
            screen.origin.x + self.interface.position[0],
            screen.size.height + screen.origin.y - self.interface.position[1] -
            self.interface._size[1], self.interface._size[0],
            self.interface._size[1])

        mask = NSTitledWindowMask
        if self.interface.closeable:
            mask |= NSClosableWindowMask

        if self.interface.resizeable:
            mask |= NSResizableWindowMask

        if self.interface.minimizable:
            mask |= NSMiniaturizableWindowMask

        self.native = NSWindow.alloc().initWithContentRect(
            position,
            styleMask=mask,
            backing=NSBackingStoreBuffered,
            defer=False)
        self.native.setFrame(position, display=True, animate=False)
        self.native.interface = self.interface
        self.native.impl = self

        self.delegate = WindowDelegate.alloc().init()
        self.delegate.interface = self.interface
        self.delegate.impl = self

        self.native.setDelegate_(self.delegate)
Ejemplo n.º 3
0
 def computeRect_(self, input: NSRect) -> NSRect:
     results['rect'] = True
     sup = send_super(__class__, self, 'computeRect:', input, restype=NSRect, argtypes=[NSRect])
     return NSMakeRect(
         input.origin.y + self.value, sup.origin.x,
         input.size.height + self.value, sup.size.width
     )
Ejemplo n.º 4
0
    def test_class_nonobject_properties(self):
        """An Objective-C class can have properties of non-object types."""
        class Properties(NSObject):
            object = objc_property(ObjCInstance)
            int = objc_property(c_int)
            rect = objc_property(NSRect)

        properties = Properties.alloc().init()

        properties.object = at('foo')
        properties.int = 12345
        properties.rect = NSMakeRect(12, 34, 56, 78)

        self.assertEqual(properties.object, 'foo')
        self.assertEqual(properties.int, 12345)

        r = properties.rect
        self.assertEqual(r.origin.x, 12)
        self.assertEqual(r.origin.y, 34)
        self.assertEqual(r.size.width, 56)
        self.assertEqual(r.size.height, 78)
Ejemplo n.º 5
0
    def test_interface_return_struct(self):
        "An ObjC protocol implementation that returns values by struct can be defined in Python."

        results = {}
        Thing = ObjCClass("Thing")

        class StructReturnHandler(Thing):
            @objc_method
            def initWithValue_(self, value):
                self.value = value
                return self

            @objc_method
            def computeSize_(self, input: NSSize) -> NSSize:
                results['size'] = True
                sup = send_super(__class__,
                                 self,
                                 'computeSize:',
                                 input,
                                 restype=NSSize,
                                 argtypes=[NSSize])
                return NSSize(input.width + self.value, sup.height)

            @objc_method
            def computeRect_(self, input: NSRect) -> NSRect:
                results['rect'] = True
                sup = send_super(__class__,
                                 self,
                                 'computeRect:',
                                 input,
                                 restype=NSRect,
                                 argtypes=[NSRect])
                return NSMakeRect(input.origin.y + self.value, sup.origin.x,
                                  input.size.height + self.value,
                                  sup.size.width)

            # Register a second method returning NSSize. Don't
            # have to use it - just have to register that it exists.
            @objc_method
            def origin(self) -> NSSize:
                return NSSize(0, 0)

        # Create two handler instances so we can check the right one
        # is being invoked.
        handler1 = StructReturnHandler.alloc().initWithValue_(5)
        handler2 = StructReturnHandler.alloc().initWithValue_(10)

        outSize = handler1.computeSize(NSSize(20, 30))
        self.assertEqual(outSize.width, 25)
        self.assertEqual(outSize.height, 90)
        self.assertTrue(results.get('size'))

        outRect = handler2.computeRect(NSMakeRect(10, 20, 30, 40))
        self.assertEqual(outRect.origin.x, 30)
        self.assertEqual(outRect.origin.y, 110)
        self.assertEqual(outRect.size.width, 50)
        self.assertEqual(outRect.size.height, 60)
        self.assertTrue(results.get('rect'))

        # Invoke a method through an interface.
        Example = ObjCClass("Example")
        obj = Example.alloc().init()

        # Test the base class directly
        thing1 = Thing.alloc().init()
        obj.thing = thing1
        outSize = obj.testThing(10)
        self.assertEqual(outSize.width, 0)
        self.assertEqual(outSize.height, 30)

        # Test the python handler
        obj.thing = handler1
        outSize = obj.testThing(15)
        self.assertEqual(outSize.width, 5)
        self.assertEqual(outSize.height, 45)