def __init__(self, title='PyCocao', raiser=False, **kwds): '''New L{App}. @keyword title: App name or title (C{str}). @keyword raiser: Throw exceptions for silent errors (C{bool}). @keyword kwds: Optional, additional keyword arguments. @raise RuntimeError: Duplicate L{App}s. ''' if _Globals.App: raise RuntimeError('%s already exists' % (_Globals.App, )) _Globals.App = self if raiser: _Globals.raiser = raiser self.NS = NSMain.Application # add a method to set the app's title self.NS.setTitle_ = nsBundleRename # pool = NSAutoreleasePool.alloc().init() # created by NSApplication self.title = title if kwds: # optional, additional attributes super(App, self).__init__(**kwds) self.NSdelegate = retain(NSApplicationDelegate.alloc().init(self))
def __new__(cls, ustr): '''New L{NSStr} wrapping C{NS[Constant]String}. @param ustr: The string value (C{str} or C{unicode}). @return: The string (L{NSStr}). ''' self = super(NSStr, cls).__new__(cls, cfString(ustr)) self._str = bytes2str(ustr) # L{ObjCinstance} caches ObjC objects using the # objc_ptr.value as key, which may or may not # create in a singleton for each string value, # retaining strings seems to help singletons. return self if len(ustr) > 1 else retain(self)
def __init__(self, title='Main', frame=None, excl=0, auto=False, **kwds): '''Create a new L{Window}. @keyword title: Window title (C{str}). @keyword frame: Window frame (L{Rect}, L{NSRect_t}, L{NSRect4_t}, or None). @keyword excl: Window styles to exclude (L{WindowStyle}C{.attribute}). @keyword auto: Release window resource when closed (C{bool}). @keyword kwds: Optional, additional keyword arguments. @raise WindowError: Unique C{Id} exists. ''' self._frame = Screen(0.5) if frame is None else Rect(frame) self._ratio = self._frame.width, self._frame.height self.NS = NSWindow.alloc( ).initWithContentRect_styleMask_backing_defer_( self.frame.NS, WindowStyle.Typical ^ excl, # PYCHOK expected NSBackingStoreBuffered, NO) self.title = bytes2str(title) self.front(True) if kwds: super(Window, self).__init__(**kwds) # XXX self.NS.setIdentifier_(int2NS(id(self))) self._NSuniqID = u = self.NS.uniqueID() if u in _Globals.Windows: raise WindowError('%s %r exists: %r' % ('.uniqueID', u, _Globals.Windows[u])) _Globals.Windows[u] = self if _Globals.App and not self.app: self.app = _Globals.App if auto: self.NS.setReleasedWhenClosed_(YES) self._auto = True self.NSdelegate = retain(NSWindowDelegate.alloc().init(self))
def Screen(self): '''Get the C{NSScreen.mainScreen}. ''' if self._Screen is None: _NSMain._Screen = retain(NSScreen.alloc().init().mainScreen()) return self._Screen
def TableColumn(self): '''Get a blank C{NSTableColumn}. ''' if self._TableColumn is None: _NSMain._TableColumn = retain(NSTableColumn.alloc().init()) return self._TableColumn
def Null(self): '''Get the C{NSNull}. ''' if self._Null is None: _NSMain._Null = retain(NSNull.alloc().init()) return self._Null
def PrintInfo(self): '''Get the C{NSPrintInfo}. ''' if self._PrintInfo is None: _NSMain._PrintInfo = retain(NSPrintInfo.sharedPrintInfo()) return self._PrintInfo
def FontManager(self): '''Get the C{NSFontManager.sharedFontManager}. ''' if self._FontManager is None: _NSMain._FontManager = retain(NSFontManager.sharedFontManager()) return self._FontManager
def LayoutManager(self): '''Get the C{NSLayoutManager}. ''' if self._LayoutManager is None: _NSMain._LayoutManager = retain(NSLayoutManager.alloc().init()) return self._LayoutManager
def BundleName(self): '''Get the C{NS/CFBundleName}. ''' if self._BundleName is None: _NSMain._BundleName = retain(NSStr('CFBundleName')) return self._BundleName
def Bundle(self): '''Get the C{NSBundle.mainBundle}. ''' if self._Bundle is None: _NSMain._Bundle = retain(NSBundle.mainBundle()) return self._Bundle
def BooleanYES(self): '''Get C{NSBoolean(YES)}. ''' if self._BooleanYES is None: _NSMain._BooleanYES = retain(NSBoolean(YES)) return self._BooleanYES
def BooleanNO(self): '''Get C{NSBoolean(NO)}. ''' if self._BooleanNO is None: _NSMain._BooleanNO = retain(NSBoolean(NO)) return self._BooleanNO
def Application(self): '''Get the C{NSApplication.sharedApplication}. ''' if self._Application is None: _NSMain._Application = retain(NSApplication.sharedApplication()) return self._Application