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 __init__(self, title='Main', screen=None, fraction=0.5, frame=None, excl=0, auto=False, **kwds): '''Create a new L{Window}. @keyword title: Window title (C{str}). @keyword screen: The screen to place the window on (C{int}) or C{None} for the current one. Use C{screen=0} for the built-in screen or C{screen=1} for the first external monitor, etc. @keyword fraction: Window size as fraction of the screen (C{float}), defining the window C{B{frame}}. @keyword frame: The window's origin and I{content} size (L{Rect}, L{NSRect_t}, L{NSRect4_t}), overriding B{C{fraction}}. @keyword excl: Window I{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. ''' if frame is None: self._frame = Frame(screen, fraction=fraction) self.NS = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_( self._frame.NS, WindowStyle.Typical ^ excl, # PYCHOK expected NSBackingStoreBuffered, NO) else: # for .tables.TableWindow self._frame = Frame(frame) self.NS = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_screen_( self._frame.NS, WindowStyle.Typical ^ excl, # PYCHOK expected NSBackingStoreBuffered, NO, Screens.Main.NS) # like .tables.TableWindow 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])) elif 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 __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 BundleName(self): '''Get the C{NS/CFBundleName}. ''' if self._BundleName is None: _NSMain._BundleName = retain(NSStr('CFBundleName')) return self._BundleName
def TableColumn(self): '''Get a blank C{NSTableColumn}. ''' if self._TableColumn is None: _NSMain._TableColumn = retain(NSTableColumn.alloc().init()) return self._TableColumn
def Screen(self): '''Get the C{NSScreen.mainScreen}. ''' if self._Screen is None: _NSMain._Screen = retain(NSScreen.alloc().init().mainScreen()) return self._Screen
def PrintInfo(self): '''Get the C{NSPrintInfo}. ''' if self._PrintInfo is None: _NSMain._PrintInfo = retain(NSPrintInfo.sharedPrintInfo()) return self._PrintInfo
def Null(self): '''Get the C{NSNull}. ''' if self._Null is None: _NSMain._Null = retain(NSNull.alloc().init()) return self._Null
def LayoutManager(self): '''Get the C{NSLayoutManager}. ''' if self._LayoutManager is None: _NSMain._LayoutManager = retain(NSLayoutManager.alloc().init()) return self._LayoutManager
def FontManager(self): '''Get the C{NSFontManager.sharedFontManager}. ''' if self._FontManager is None: _NSMain._FontManager = retain(NSFontManager.sharedFontManager()) return self._FontManager
def BooleanYES(self): '''Get C{NSBoolean(YES)}. ''' if self._BooleanYES is None: _NSMain._BooleanYES = retain(NSBoolean(YES)) return self._BooleanYES
def Bundle(self): '''Get the C{NSBundle.mainBundle}. ''' if self._Bundle is None: _NSMain._Bundle = retain(NSBundle.mainBundle()) return self._Bundle
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
class _NS(object): '''(INTERNAL) Singletons. ''' BlankCell = retain(NSStr('')) EmptyCell = retain(NSStr('-'))
def display(self, title, width=400, height=300): '''Show the table in a scrollable window. @param title: Window title (C{str}). @keyword width: Window frame width (C{int} or C{float}). @keyword height: Window frame height (C{int} or C{float}). @return: The window (L{TableWindow}). @raise ValueError: Invalid header column ":width", font ":trait" or text ":alignment". ''' f = Rect4(0, 0, width, height) v = NSTableView.alloc().initWithFrame_(f.NS) cols = [] high = 0 id2i = {} # map col.identifier to col number wide = f.width # == v.frame().size.width # <https://Developer.Apple.com/documentation/appkit/nstablecolumn> for i, h in enumerate(self._headers): # note, the identifier MUST be an NSStr (to avoid warnings) t = retain(NSStr(str(i))) c = NSTableColumn.alloc().initWithIdentifier_(t) # simply map col.identifier to I{int}, instead of frequent, # costly int(nsString2str(col.identifier())) conversions in # _NSTableViewDelegate.tableView_objectValueForTableColumn_row_ id2i[c.identifier()] = i # <https://Developer.Apple.com/documentation/appkit/nscell> h = _format(h, c) cols.append(h) c.setTitle_(release( NSStr(h))) # == c.headerCell().setStringValue_(NSStr(h)) # <https://Developer.Apple.com/documentation/uikit/nstextalignment> v.addTableColumn_(c) # increase row height 1-2 points to show (bold) descenders high = max(high, Font(c.dataCell().font()).height + 2) wide -= c.width() if wide > 0: # stretch last col to frame edge c.setWidth_(float(wide + c.width())) if high > v.rowHeight(): # adjust the row height v.setRowHeight_(high) # <https://Developer.Apple.com/library/content/documentation/ # Cocoa/Conceptual/TableView/VisualAttributes/VisualAttributes.html> v.setGridStyleMask_(NSTableViewSolidHorizontalGridLineMask | NSTableViewSolidVerticalGridLineMask) # v.setDrawsGrid_(YES) # XXX obsolete, not needed d = NSTableViewDelegate.alloc().init(cols, self._rows, id2i) v.setDelegate_(d) v.setDataSource_(d) # v.setEditing_(NSMain.NO_false) # NO v.reloadData() self.NS = retain(v) self.NSdelegate = retain(d) self._window = w = TableWindow(title, self) # v.setDelegate_(w.delegate) return w
def _nstr(col): return retain(NSStr(str(col))) if col else _NS.BlankCell