Exemplo n.º 1
0
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:
        mask = 0
        if alt:
            mask |= NSAlternateKeyMask
        if cmd:
            mask |= NSCommandKeyMask
        if ctrl:
            mask |= NSControlKeyMask
        if shift:
            mask |= NSShiftKeyMask  # NSAlphaShiftKeyMask
        if mask:
            ns.setKeyEquivalentModifierMask_(mask)
    return ns
Exemplo n.º 2
0
def save_panel():
    panel = NSSavePanel.savePanel()

#   panel.setTitleHidden_(False)  # "does nothing now"
    panel.setPrompt_(NSStr('Prompt...'))
    panel.setTitle_(NSStr('Title...'))
    panel.setCanChooseDirectories_(True)
    panel.setCanChooseFiles_(True)
#   panel.allowedFileTypes(?)

    panel.setNameFieldLabel_(NSStr('NameFieldLabel...'))
    panel.setNameFieldStringValue_(NSStr('NameFieldValue...'))

    panel.setDirectory_(NSStr('/Users'))
    panel.setTagNames_(py2NS('tagA tagB tagC'.split()))
    panel.setShowsTagField_(True)

    panel.orderFrontRegardless()

    if panel.runModal():  # == runModalForDirectory_file_(None, None)
        return ' or '.join((nsString2str(panel.directory()),
                            nsString2str(panel.filename()),
                          # nsString2str(panel.URL()),
                            nsString2str(panel.URL().path())))
    return None
Exemplo n.º 3
0
    def applicationDidFinishLaunching_(self, notification):
        statusbar = NSStatusBar.systemStatusBar()
        statusitem = statusbar.statusItemWithLength_(NSVariableStatusItemLength)
#       statusitem.setHighlightMode_(1)
#       statusitem.setToolTip_(NSStr('Example'))
#       statusitem.setTitle_(NSStr('Example'))

        menu = NSMenu.alloc().init()
        menuitem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
                   NSStr('Quit'), get_selector('terminate:'), NSStr(''))
        menu.addItem_(menuitem)
        statusitem.setMenu_(menu)
Exemplo n.º 4
0
def _Window2(title=_argv0, fraction=0.5):
    '''Create the main NS window and the drawable NS view.
    '''
    frame = NSScreen.alloc().init().mainScreen().frame()
    if 0.1 < fraction < 1.0:
        # use the lower left quarter of the screen size as frame
        w = int(frame.size.width * fraction + 0.5)
        h = int(frame.size.height * w / frame.size.width)
        frame = NSRect4_t(frame.origin.x + 10, frame.origin.y + 10, w, h)

    window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
        frame,
        NSWindowStyleMaskUsual,  # PYCHOK expected
        NSBackingStoreBuffered,
        False)  # or 0
    window.setTitle_(NSStr(title))

    # create the drawable_nsobject NSView for vlc.py, see vlc.MediaPlayer.set_nsobject()
    # for an alternate NSView object with protocol VLCOpenGLVideoViewEmbedding
    # <https://StackOverflow.com/questions/11562587/create-nsview-directly-from-code>
    # <https://GitHub.com/ariabuckles/pyobjc-framework-Cocoa/blob/master/Examples/AppKit/DotView/DotView.py>
    view = NSView.alloc().initWithFrame_(frame)
    window.setContentView_(view)
    # force the video/window aspect ratio, adjusted
    # above when the window is/has been resized
    window.setContentAspectRatio_(frame.size)

    window.makeKeyAndOrderFront_(None)
    return window, view
Exemplo n.º 5
0
def create_window(title=''):
    frame = NSMakeRect(10, 100, 500, 100)
    window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
        frame, NSWindowStyleMaskUsual, NSBackingStoreBuffered, 0)
    window.setTitle_(NSStr(title))
    window.makeKeyAndOrderFront_(None)
    return window
Exemplo n.º 6
0
def main(timeout=None):
    # Create a new application instance ...
    app = NSApplication.sharedApplication()
    # ... and create its delgate.  Note the use of the
    # Objective C constructors below, because Delegate
    # is a subclass of an Objective C class, NSObject
    delegate = _Delegate.alloc().init(app)
    # Tell the application which delegate object to use.
    app.setDelegate_(delegate)

    # Now we can can start to create the window ...
    frame = NSMakeRect(10, 10, 600, 300)
    # (Don't worry about these parameters for the moment. They just
    # specify the type of window, its size and position etc)
    window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
        frame, NSWindowStyleMaskUsual, NSBackingStoreBuffered, False)  # or 0
    # tell it which delegate object to use (here it happens
    # to be the same delegate as the application is using),
    # otherwise method .windowWillClose_ will not be called
    window.setDelegate_(delegate)
    # set some properties. Unicode strings are preferred.
    window.setTitle_(NSStr('Delegated - Close window to Quit'))
    # All set.  Now we can show the window
    window.orderFrontRegardless()

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

    # ... and start the application
    app.run()  # .runEventLoop()
Exemplo n.º 7
0
def new_menu_item(label, action=None, key='',
                  alt=False, cmd=True, ctrl=False, shift=False):
    # <http://developer.apple.com/documentation/appkit/nsmenuitem/1514858-initwithtitle>
    item = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
        NSStr(label), get_selector(action), NSStr(key))
    if key:
        mask = 0
        if alt:
            mask |= NSAlternateKeyMask
        if cmd:
            mask |= NSCommandKeyMask
        if ctrl:
            mask |= NSControlKeyMask
        if shift:
            mask |= NSShiftKeyMask  # NSAlphaShiftKeyMask
        if mask:
            item.setKeyEquivalentModifierMask_(mask)
    return item
Exemplo n.º 8
0
def create_window():
    window = send_message('NSWindow', 'alloc')
    window = send_message(window, 'initWithContentRect:styleMask:backing:defer:',
                          NSMakeRect(10, 500, 600, 300),  # frame
                          NSWindowStyleMaskUsual,
                          NSBackingStoreBuffered,
                          0)  # or False
    send_message(window, 'setTitle:', NSStr("Window - Select Quit from Dock menu"))
    send_message(window, 'makeKeyAndOrderFront:', None)
    return window
Exemplo n.º 9
0
 def toggle_(self, notification):
     # toggle between Pause and Play
     if self.player.is_playing():
         # note, .pause() pauses and un-pauses the video,
         # .stop() stops the video and blanks the window
         self.player.pause()
         t = 'Play'
     else:
         self.player.play()
         t = 'Pause'
     self.NSitem.setTitle_(NSStr(t))
Exemplo n.º 10
0
def open_panel():
    panel = NSOpenPanel.openPanel()

    panel.setPrompt_(NSStr('Set prompt...'))
#   panel.setResolvesAliases_(True)
    panel.setCanChooseDirectories_(True)
    panel.setCanChooseFiles_(True)
#   panel.allowedFileTypes(?)

    if panel.runModal():
        return nsString2str(panel.URL().path())
    return None
Exemplo n.º 11
0
 def init(self, app, player, title, video):
     self = ObjCInstance(send_super(self, 'init'))
     #       self = ObjCInstance(send_message('NSObject', 'alloc'))
     self.app = app
     self.NSitem = None  # Play/Pause toggle
     self.player = player
     self.ratio = 2
     self.title = title  # app name, top-level menu title
     self.video = video  # window banner
     self.window = None
     nsBundleRename(NSStr(title))  # top-level menu title
     return self
Exemplo n.º 12
0
def run_window():

    app = NSApplication.sharedApplication()
    #   pool = NSAutoreleasePool.alloc().init()  # PYCHOK expected

    window = NSWindow.alloc()
    window.initWithContentRect_styleMask_backing_defer_(
        NSRect4_t(100, 100, 300, 300), NSWindowStyleMaskUsual,
        NSBackingStoreBuffered, False)
    window.setTitle_(NSStr("Class Window"))
    window.makeKeyAndOrderFront_(None)

    app.run()
Exemplo n.º 13
0
def create_menu(name='', app=None):
    menubar = NSMenu.alloc().init()
    appMenuItem = NSMenuItem.alloc().init()
    menubar.addItem_(appMenuItem)
    appMenu = NSMenu.alloc().init()

    fullItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
        NSStr('Full Screen'), get_selector('enterFullScreenMode:'), NSStr('f'))
    fullItem.setKeyEquivalentModifierMask_(NSControlKeyMask)  # Ctrl-Cmd-F
    appMenu.addItem_(fullItem)

    appMenu.addItem_(NSMenuItem.separatorItem())

    hideItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
        NSStr('Hide ' + name), get_selector('hide:'), NSStr('h'))
    appMenu.addItem_(hideItem)

    otherItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
        NSStr('Hide Others'), get_selector('hideOtherApplications:'),
        NSStr('h'))
    otherItem.setKeyEquivalentModifierMask_(NSAlternateKeyMask)  # Alt-Cmd-H
    appMenu.addItem_(otherItem)

    showItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
        NSStr('Show All'), get_selector('unhideAllApplications:'), NSStr(''))
    appMenu.addItem_(showItem)

    appMenu.addItem_(NSMenuItem.separatorItem())

    quitItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
        NSStr('Quit ' + name), get_selector('terminate:'), NSStr('q'))
    appMenu.addItem_(quitItem)

    appMenuItem.setSubmenu_(appMenu)

    if app:
        app.setMainMenu_(menubar)
    return menubar
Exemplo n.º 14
0
def main(timeout=None):

    app = NSApplication.sharedApplication()

    frame = NSMakeRect(10, 10, 500, 400)
    window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_(
        frame, NSWindowStyleMaskUsual, NSBackingStoreBuffered, False)
    window.setTitle_(NSStr('Drawing - Close window to Quit'))

    view = _View.alloc().initWithFrame_(frame, 10)
    window.setContentView_(view)

    delegate = _Delegate.alloc().init(app)
    window.setDelegate_(delegate)

    window.display()
    window.orderFrontRegardless()

    # set up the timeout
    terminating(app, timeout)
    app.run()
Exemplo n.º 15
0
 def writer(self, s):
     self.badge.setBadgeLabel_(NSStr(str(s)))