def stupid_stuff(class_name):

    NSObject = ObjCClass(class_name)
    print(NSObject)
    print(libobjc.object_getClassName(NSObject.ptr))

    x = NSObject.alloc()  # PYCHOK expected
    print(libobjc.object_getClassName(x.ptr))
    print('x', x)
    print('x.init', x.init)
    print('x.init()', x.init())
    print('x.objc_class', x.objc_class)
    print(x.retainCount())
    print(x.retain())
    print(x.retainCount())
    print(x.retain())
    print(x.retainCount())
    print(x.retain())
    print(x.retainCount())

    if class_name == 'NSApplication':
        # only one NSApplication allowed
        return

    y = NSObject.alloc()  # PYCHOK expected
    print('y', y)
    print('y.init', y.init)
    print('y.init()', y.init())
    print('y.objc_class', y.objc_class)
    print(y.retainCount())
    print(y.retain())
    print(y.retainCount())
Exemple #2
0
def main(timeout=None):

    # prepare and set our delegate
    app = NSApplication.sharedApplication()

    TheDelegate = ObjCClass('TheDelegate')  # the actual class

    delegate = TheDelegate.alloc().init()  # PYCHOK expected
    app.setDelegate_(delegate)
    delegate.app = app

    delegate.badge = app.dockTile()
    delegate.writer(0)

    # on a separate thread, run the scheduler
    t = threading.Thread(target=schedule, args=(delegate.writer, ))
    t.setDaemon(1)
    t.start()

    # set up the timeout
    if timeout is not None:
        try:  # PyCocoa/test
            from test import terminating
            terminating(app, timeout)
        except ImportError:
            pass

    # let her rip!-)
    app.run()  # AppHelper.runEventLoop()
def main(timeout=None):

    # prepare and set our delegate
    app = NSApplication.sharedApplication()

    TheDelegate = ObjCClass('TheDelegate')  # the actual class

    delegate = TheDelegate.alloc().init()  # PYCHOK expected
    app.setDelegate_(delegate)
    delegate.app = app

    delegate.badge = app.dockTile()
    delegate.writer(0)

    # on a separate thread, run the scheduler
    t = threading.Thread(target=schedule, args=(delegate.writer,))
    if sys.version_info[0] > 2:
        t.deamon = True
    else:  # throws DeprecationWarning
        t.setDaemon(1)
    t.start()

    # set up the timeout
    terminating(app, timeout)
    # let her rip!-)
    app.run()  # AppHelper.runEventLoop()
Exemple #4
0
        def _y(t, h):
            return (cos(t) + 1.) * h

        NSColor.whiteColor().set()
        NSRectFill(b)  # not a class

        NSColor.blackColor().set()
        for f in self.loop:
            p1 = NSPoint_t(_x(f, w), _y(f, h))
            for g in self.loop:
                p2 = NSPoint_t(_x(g, w), _y(g, h))
                NSBezierPath.strokeLineFromPoint_toPoint_(p1, p2)


_View = ObjCClass('_View')  # the actual class


class _Delegate_Implementation(object):
    _Delegate = ObjCSubclass('NSObject', '_Delegate')

    @_Delegate.method(b'@' + PyObjectEncoding)
    def init(self, app):
        self = ObjCInstance(send_super(self, 'init'))
        self.app = app
        return self

    @_Delegate.method(b'v@')
    def windowWillClose_(self, notification):
        self.app.terminate_(self)
Exemple #5
0
        print('takePyObject %r %r' % (self, pyobject))
        print('x = %d' % (self.x,))

    @MySubclass.method('v')  # return void, no args
    def dealloc(self):
        print('dealloc %r' % (self,))
        send_super(self, 'dealloc')

    @MySubclass.method('vi')  # return void, int arg
    def method(self, number):
        print('method %r %r' % (self, number))


if __name__ == '__main__':

    MySubclass = ObjCClass('MySubclass')  # the actual class

    myobject1 = MySubclass.alloc().init()
    print('after init: myobject1 = % r\n' % (myobject1,))

    myobject1.doSomething()
    myobject1.method(100)
    myobject1.takePyObject(1)

    print()

    myobject2 = MySubclass.alloc().init()
    print('after init: myobject2 = %r' % (myobject2,))

    myobject2.doSomething()
#   myobject2.doSomething()
Exemple #6
0
        if self.ratio and self.player and self.window:
            # get and maintain the aspect ratio
            # (the first player.video_get_size()
            #  call returns (0, 0), subsequent
            #  calls return (w, h) correctly)
            r = aspect_ratio(*self.player.video_get_size(0))
            if r:
                self.window.setContentAspectRatio_(NSSize_t(*r))
                self.ratio -= 1

    @_Delegate.method('v@')
    def windowWillClose_(self, notification):
        self.app.terminate_(self)  # or NSApp()...


_Delegate = ObjCClass('_Delegate')  # the actual class


def _MenuItem(label,
              action=None,
              key='',
              alt=False,
              cmd=True,
              ctrl=False,
              shift=False):
    '''New NS menu item with action and optional shortcut key.
    '''
    # <https://Developer.Apple.com/documentation/appkit/nsmenuitem/1514858-initwithtitle>
    ns = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
        NSStr(label), get_selector(action), NSStr(key))
    if key:
    print(y.retainCount())
    print(y.retain())
    print(y.retainCount())


if __name__ == '__main__':

    import sys

    if len(sys.argv) < 2:
        print('USAGE: python class_wrapper4.py <Obj-C Class>')
        exit(1)

    class_name = sys.argv[1]

    MySubclass = ObjCClass('MySubclass')
    print(MySubclass)
    x = MySubclass.alloc().init()
    print(x)

    x.doSomething()
    x.doSomething()
    x.doSomething()

    print(len(ObjCInstance._objc_cache))
    x.release()
    del x
    print(len(ObjCInstance._objc_cache))

    stupid_stuff(class_name)
#   run_window()
Exemple #8
0
# -*- coding: utf-8 -*-

from pycocoa import Dict, isNone, NSMain, NSStr, \
                    ns2Type, ObjCClass, Str, type2NS

__version__ = '19.09.27'

# get PyCocoa-internal _ObjCBase class
_ObjCBase = tuple(_ for _ in ObjCClass.mro() if _.__name__ == '_ObjCBase')


def _strepr(o):
    n = o.__class__.__name__
    t = str(o) if isinstance(o, _ObjCBase) else repr(
        o)  # .lstrip('<').rstrip('>')
    if not t.startswith(n):
        t = '%s(%s)' % (n, t)
    return t


if __name__ == '__main__':

    import sys

    try:
        # see pycocoa.nstypes.nsBundleRename
        b = NSMain.Bundle
        assert (not isNone(b))

        print('%s: %s' % ('b', _strepr(b)))