def NSview(self, ns_view):
     '''Set this window's view (C{NSView...}).
     '''
     if not isNone(ns_view):
         isObjCInstanceOf(ns_view, NSScrollView, NSView, name='ns_view')
         self.NS.setContentView_(ns_view)
     self._NSview = ns_view
Example #2
0
 def items(self):
     '''Yield the key, value pairs, like C{dict.items}.
     '''
     for key, k in nsIter2(self.NS.allKeys()):
         v = self.NS.objectForKey_(k)
         if isNone(v):  # missing key?
             self._NS_KeyError(key, k)
         yield key, ns2Type(v)
Example #3
0
 def NS(self, ns):
     '''Set the ObjC instance (C{NS...}).
     '''
     if not isNone(ns):  # see also .nstypes.nsOf
         isinstanceOf(ns, ObjCInstance, c_struct_t, ObjC_t, name='ns')
     elif isinstanceOf(self.NS, ObjCInstance):
         # self.NS.release()
         pass
     self._NS = ns
Example #4
0
    def resize(self, size):
        '''Get this font in an other point size.

           @keyword size: Desired point size (C{int}).

           @return: The other or this font (L{Font}).

           @see: L{Font}C{.__init__} for errors raised.
        '''
        ns = None
        if size == self.size:
            return self
        elif size:
            ns = self.NS.fontWithSize_(size)
            if isNone(ns):
                ns = None
        return Font(ns or self.family,
                    size=size,
                    traits=self.traits,
                    weight=self.weight)
Example #5
0
    def __init__(self, family_or_font, size=0, traits=0, weight=5):
        '''New L{Font}.

           @param family_or_font: Generic font name (C{str}, L{Str}, L{NSStr})
                                  like "Times" or "Helvetica" or a L{Font},
                                  C{NSFont} or C{NSFontDescriptor} instance.
           @keyword size: Desired point size (C{int}), zero for any.
           @keyword traits: Desired font traits (C{str} or C{FontTrait}C{s mask}).
           @keyword weigth: Desired book weight (C{int}) in range 0..15, where
                            0=light, 5=regular, 9=bold and 15=heavy.

           @raise FontError: No such I{family_or_font}.

           @raise FontTraitError: Mutually exclusive I{traits}.

           @raise TypeError: Invalid I{family_or_font}.

           @raise ValueError: Invalid I{weight}.

           @note: The new L{Font} may not exhibit the desired I{traits}
                  and I{weight}.  The I{weight} is ignored if I{traits}
                  include C{FontTrait.Bold}, both I{traits} and I{weight}
                  are ignored if I{family_or_font} is C{NSFontDescriptor}.

           @see: Function L{fontsof} to obtain all available fonts of
                 a particular font family.
        '''
        if isinstance(family_or_font, Str):
            ns, py = family_or_font.NS, str(family_or_font)
        elif isinstance(family_or_font, _ByteStrs):
            ns, py = release(NSStr(family_or_font)), bytes2str(family_or_font)
        elif isinstance(family_or_font, NSStr):
            ns, py = family_or_font, nsString2str(family_or_font)
#       elif isObjCInstanceOf(family_or_font, NSFontDescriptor):
# <https://Developer.Apple.com/documentation/appkit/nsfont/1525386-init>
# ignore traits and weight
#           ns, py = NSFont.alloc().init_(family_or_font, size), None
        elif isObjCInstanceOf(family_or_font, NSFont, name='family_or_font'):
            ns, py = family_or_font, None
            if size == 0:
                size = ns.pointSize()
            if traits == 0:
                traits = NSMain.FontManager.traitsOfFont_(ns)
            if not (size == ns.pointSize()
                    and traits == NSMain.FontManager.traitsOfFont_(ns)):
                ns = ns.familyName()
                py = nsString2str(ns)

        if py is not None:
            # <https://Developer.Apple.com/documentation/appkit/
            #        nsfontmanager/1462332-fontwithfamily>
            self._traits = _traitsin(traits)
            self._weight = _weightin(weight)
            ns = NSMain.FontManager.fontWithFamily_traits_weight_size_(
                ns, self._traits, self._weight, size)
            if isNone(ns):
                self._family = py
                self._size = flint(size)
                raise FontError('no such %s: %s' % ('font', self._argstr()))

        self._NS = ns  # _RO
        # <https://Developer.Apple.com/library/content/documentation/
        #  TextFonts/Conceptual/CocoaTextArchitecture/FontHandling/FontHandling.html>
        self._family = nsString2str(ns.familyName())
        self._height = flint(
            NSMain.LayoutManager.defaultLineHeightForFont_(ns) + 1)
        self._name = nsString2str(ns.fontName())
        self._size = flint(ns.pointSize())
        # traits not always reflect actual traits
        self._traits = NSMain.FontManager.traitsOfFont_(ns) or 0
        # update with the family traits, if any
        self._traits |= _traitsin(self._family, raiser=False)
        if ns.isFixedPitch() and not self.isMonoSpace:
            self._traits |= FontTrait.MonoSpace
        self._weight = NSMain.FontManager.weightOfFont_(ns)
Example #6
0
 def _NS_get3(self, key):
     k = type2NS(key)
     v = self.NS.objectForKey_(k)  # nil for missing key
     return k, v, (missing if isNone(v) else ns2Type(v))
Example #7
0
 def NSdelegate(self, delegate):
     '''Set the class' delegate.
     '''
     if not isNone(delegate):
         isinstanceOf(delegate, ObjCInstance, name='delegate')  # XXXX ????
         self.NS.setDelegate_(delegate)