def getSymbol(nsevent):
    symbol = keymap.get(nsevent.keyCode(), None)
    if symbol is not None:
        return symbol

    chars = cocoapy.cfstring_to_string(nsevent.charactersIgnoringModifiers())
    if chars:
        return charmap.get(chars[0].upper(), None)

    return None
示例#2
0
文件: cocoa.py 项目: zzkklep/thbattle
def _get_file_name(panel, title, filters):
    panel.setCanChooseFiles_(True)
    panel.setCanChooseDirectories_(False)
    panel.setAllowsMultipleSelection_(False)
    panel.setTitle_(get_NSString(title))
    
    filters = [get_NSString(f[1][2:]) for f in filters]
    panel.setAllowedFileTypes_(_get_nsarray(filters))
    if panel.runModal():
        return cfstring_to_string(panel.URL().path())
示例#3
0
    def add_font_data(cls, data):
        # Create a cgFont with the data.  There doesn't seem to be a way to
        # register a font loaded from memory such that the operating system will
        # find it later.  So instead we just store the cgFont in a table where
        # it can be found by our __init__ method.
        # Note that the iOS CTFontManager *is* able to register graphics fonts,
        # however this method is missing from CTFontManager on MacOS 10.6
        dataRef = c_void_p(cf.CFDataCreate(None, data, len(data)))
        provider = c_void_p(quartz.CGDataProviderCreateWithCFData(dataRef))
        cgFont = c_void_p(quartz.CGFontCreateWithDataProvider(provider))

        cf.CFRelease(dataRef)
        quartz.CGDataProviderRelease(provider)

        # Create a template CTFont from the graphics font so that we can get font info.
        ctFont = c_void_p(
            ct.CTFontCreateWithGraphicsFont(cgFont, 1, None, None))

        # Get info about the font to use as key in our font table.
        string = c_void_p(ct.CTFontCopyFamilyName(ctFont))
        familyName = str(cocoapy.cfstring_to_string(string))
        cf.CFRelease(string)

        string = c_void_p(ct.CTFontCopyFullName(ctFont))
        fullName = str(cocoapy.cfstring_to_string(string))
        cf.CFRelease(string)

        traits = ct.CTFontGetSymbolicTraits(ctFont)
        cf.CFRelease(ctFont)

        # Store font in table. We store it under both its family name and its
        # full name, since its not always clear which one will be looked up.
        if familyName not in cls._loaded_CGFont_table:
            cls._loaded_CGFont_table[familyName] = {}
        cls._loaded_CGFont_table[familyName][traits] = cgFont

        if fullName not in cls._loaded_CGFont_table:
            cls._loaded_CGFont_table[fullName] = {}
        cls._loaded_CGFont_table[fullName][traits] = cgFont
    def __init__(self,
                 name,
                 size,
                 bold=False,
                 italic=False,
                 stretch=False,
                 dpi=None):
        # assert type(bold) is bool, "Only a boolean value is supported for bold in the current font renderer."
        # assert type(italic) is bool, "Only a boolean value is supported for bold in the current font renderer."

        if stretch:
            warnings.warn(
                "The current font render does not support stretching.")

        super().__init__()

        name = name or 'Helvetica'

        # I don't know what is the right thing to do here.
        dpi = dpi or 96
        size = size * dpi / 72.0

        # Construct traits value.
        traits = 0
        if bold:
            traits |= cocoapy.kCTFontBoldTrait
        if italic:
            traits |= cocoapy.kCTFontItalicTrait

        name = str(name)
        # First see if we can find an appropriate font from our table of loaded fonts.
        cgFont = self._lookup_font_with_family_and_traits(name, traits)
        if cgFont:
            # Use cgFont from table to create a CTFont object with the specified size.
            self.ctFont = c_void_p(
                ct.CTFontCreateWithGraphicsFont(cgFont, size, None, None))
        else:
            # Create a font descriptor for given name and traits and use it to create font.
            descriptor = self._create_font_descriptor(name, traits)
            self.ctFont = c_void_p(
                ct.CTFontCreateWithFontDescriptor(descriptor, size, None))

            cf.CFRelease(descriptor)
            assert self.ctFont, "Couldn't load font: " + name

        string = c_void_p(ct.CTFontCopyFamilyName(self.ctFont))
        self._family_name = str(cocoapy.cfstring_to_string(string))
        cf.CFRelease(string)

        self.ascent = int(math.ceil(ct.CTFontGetAscent(self.ctFont)))
        self.descent = -int(math.ceil(ct.CTFontGetDescent(self.ctFont)))
示例#5
0
    def getBitsPerPixel(self, cgmode):
        # from /System/Library/Frameworks/IOKit.framework/Headers/graphics/IOGraphicsTypes.h
        IO8BitIndexedPixels = "PPPPPPPP"
        IO16BitDirectPixels = "-RRRRRGGGGGBBBBB"
        IO32BitDirectPixels = "--------RRRRRRRRGGGGGGGGBBBBBBBB"

        cfstring = c_void_p(quartz.CGDisplayModeCopyPixelEncoding(cgmode))
        pixelEncoding = cfstring_to_string(cfstring)
        cf.CFRelease(cfstring)

        if pixelEncoding == IO8BitIndexedPixels: return 8
        if pixelEncoding == IO16BitDirectPixels: return 16
        if pixelEncoding == IO32BitDirectPixels: return 32
        return 0
示例#6
0
 def performKeyEquivalent_(self, nsevent):
     # Let arrow keys and certain function keys pass through the responder
     # chain so that the textview can handle on_text_motion events.
     modifierFlags = nsevent.modifierFlags()
     if modifierFlags & cocoapy.NSNumericPadKeyMask:
         return False
     if modifierFlags & cocoapy.NSFunctionKeyMask:
         ch = cocoapy.cfstring_to_string(nsevent.charactersIgnoringModifiers())
         if ch in (cocoapy.NSHomeFunctionKey, cocoapy.NSEndFunctionKey,
                   cocoapy.NSPageUpFunctionKey, cocoapy.NSPageDownFunctionKey):
             return False
     # Send the key equivalent to the main menu to perform menu items.
     NSApp = cocoapy.ObjCClass('NSApplication').sharedApplication()
     NSApp.mainMenu().performKeyEquivalent_(nsevent)
     # Indicate that we've handled the event so system won't beep.
     return True
 def insertText_(self, text):
     text = cfstring_to_string(text)
     self.setString_(self.empty_string)
     # Don't send control characters (tab, newline) as on_text events.
     if unicodedata.category(text[0]) != 'Cc':
         self._window.dispatch_event("on_text", text)