├── tests ├── config.nims ├── tcore_foundation.nim ├── tblocks.nim ├── tfsstream.nim ├── tarrays.nim ├── timpl_class.nim └── tobjc_runtime.nim ├── darwin ├── app_kit │ ├── nstext.nim │ ├── nstextview.nim │ ├── nscontrol.nim │ ├── nscolor.nim │ ├── nsdragginginfo.nim │ ├── nsresponder.nim │ ├── nspanel.nim │ ├── nswindowcontroller.nim │ ├── nsfont.nim │ ├── nscell.nim │ ├── nsimageview.nim │ ├── nsdragoperation.nim │ ├── nsscreen.nim │ ├── nsopenglview.nim │ ├── nstextfield.nim │ ├── nsopenpanel.nim │ ├── nseventmask.nim │ ├── nsview.nim │ ├── nssavepanel.nim │ ├── nsmenu.nim │ ├── nsopengl.nim │ ├── nspasteboard.nim │ ├── nsbutton.nim │ ├── nswindow.nim │ ├── nsalert.nim │ ├── nsapplication.nim │ ├── nscursor.nim │ ├── nsimage.nim │ └── nsevent.nim ├── foundation │ ├── nscoder.nim │ ├── nsdecimal_number.nim │ ├── nslocale.nim │ ├── nsnotification.nim │ ├── nsautoreleasepool.nim │ ├── nsurl.nim │ ├── nsurlrequest.nim │ ├── nsprocessinfo.nim │ ├── nsrunloop.nim │ ├── nsenumerator.nim │ ├── nsbundle.nim │ ├── nsgeometry.nim │ ├── nserror.nim │ ├── nsnumberformatter.nim │ ├── nsdata.nim │ ├── nsurlresponse.nim │ ├── nsdate.nim │ ├── nsstream.nim │ ├── nspath_utilities.nim │ ├── nsnumber.nim │ ├── nsstring.nim │ ├── nsarray.nim │ ├── nsdictionary.nim │ └── nsset.nim ├── core_foundation │ ├── cferror.nim │ ├── cfurl.nim │ ├── cfpropertylist.nim │ ├── cfstream.nim │ ├── cfrunloop.nim │ ├── cfpreferences.nim │ ├── cfnumber.nim │ ├── cfdictionary.nim │ ├── cfstring.nim │ ├── cfbase.nim │ └── cfarray.nim ├── quartz_core.nim ├── ui_kit.nim ├── core_video │ ├── cvreturn.nim │ ├── cvbase.nim │ └── cvdisplay_link.nim ├── opengl │ └── cgltypes.nim ├── quartz_core │ ├── cametal_layer.nim │ └── calayer.nim ├── web_kit │ ├── wknavigation.nim │ ├── wknavigationdelegate.nim │ ├── wkusercontentcontroller.nim │ ├── wkscriptmessage.nim │ ├── wkframeinfo.nim │ ├── wkopenpanelparameters.nim │ ├── wkuserscript.nim │ ├── wksecurityorigin.nim │ ├── wknavigationresponse.nim │ ├── wkpreferences.nim │ ├── wkwebviewconfiguration.nim │ └── wkwebview.nim ├── core_text.nim ├── ui_kit │ ├── uiview.nim │ ├── uidevice.nim │ └── uiscreen.nim ├── objc │ ├── nsobject.nim │ ├── blocks.nim │ └── runtime.nim ├── core_graphics.nim ├── store_kit.nim ├── core_graphics │ ├── cgbitmap_context.nim │ ├── cgcolor_space.nim │ ├── cggeometry.nim │ ├── cgfont.nim │ ├── cgaffine_transform.nim │ └── cgcontext.nim ├── core_foundation.nim ├── core_text │ ├── ctfont_descriptor.nim │ ├── ctfont_manager.nim │ └── ctfont.nim ├── store_kit │ ├── skpayment.nim │ ├── skrequest.nim │ ├── skproduct.nim │ ├── skpayment_queue.nim │ ├── skproducts_request.nim │ └── skpayment_transaction.nim ├── web_kit.nim ├── foundation.nim ├── app_kit.nim └── core_services │ └── fsstream.nim ├── .gitignore ├── darwin.nimble ├── README.md ├── .travis.yml ├── LICENSE └── .github └── workflows └── action.yml /tests/config.nims: -------------------------------------------------------------------------------- 1 | 2 | switch("path", "$projectDir/..") 3 | -------------------------------------------------------------------------------- /darwin/app_kit/nstext.nim: -------------------------------------------------------------------------------- 1 | import ./nsview 2 | type NSText* = ptr object of NSView -------------------------------------------------------------------------------- /darwin/app_kit/nstextview.nim: -------------------------------------------------------------------------------- 1 | import ./nstext 2 | type NSTextView* = ptr object of NSText -------------------------------------------------------------------------------- /darwin/app_kit/nscontrol.nim: -------------------------------------------------------------------------------- 1 | import ./nsview 2 | 3 | type 4 | NSControl* = ptr object of NSView -------------------------------------------------------------------------------- /darwin/foundation/nscoder.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | type NSCoder* = ptr object of NSObject -------------------------------------------------------------------------------- /darwin/app_kit/nscolor.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type 4 | NSColor* = ptr object of NSObject -------------------------------------------------------------------------------- /darwin/core_foundation/cferror.nim: -------------------------------------------------------------------------------- 1 | import cfbase 2 | 3 | type 4 | CFError* = ptr object of CFObject 5 | -------------------------------------------------------------------------------- /darwin/quartz_core.nim: -------------------------------------------------------------------------------- 1 | import quartz_core / [ calayer, cametal_layer ] 2 | export calayer, cametal_layer 3 | -------------------------------------------------------------------------------- /darwin/ui_kit.nim: -------------------------------------------------------------------------------- 1 | import ui_kit / [ uiscreen, uidevice, uiview ] 2 | export uiscreen, uidevice, uiview 3 | -------------------------------------------------------------------------------- /darwin/app_kit/nsdragginginfo.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | type 3 | NSDraggingInfo* = ptr object of NSObject -------------------------------------------------------------------------------- /darwin/app_kit/nsresponder.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type 4 | NSResponder* = ptr object of NSObject -------------------------------------------------------------------------------- /darwin/core_video/cvreturn.nim: -------------------------------------------------------------------------------- 1 | 2 | type 3 | CVReturn* = int32 4 | 5 | const 6 | kCVReturnSuccess* = 0 7 | -------------------------------------------------------------------------------- /darwin/opengl/cgltypes.nim: -------------------------------------------------------------------------------- 1 | 2 | type 3 | CGLContextObj* = ptr object 4 | CGLPixelFormatObj* = ptr object 5 | -------------------------------------------------------------------------------- /darwin/quartz_core/cametal_layer.nim: -------------------------------------------------------------------------------- 1 | import ./calayer 2 | 3 | type 4 | CAMetalLayer* = ptr object of CALayer -------------------------------------------------------------------------------- /darwin/web_kit/wknavigation.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type 4 | WKNavigation* = ptr object of NSObject -------------------------------------------------------------------------------- /tests/tcore_foundation.nim: -------------------------------------------------------------------------------- 1 | import ../darwin/core_foundation 2 | 3 | doAssert($CFStringCreate("hello") == "hello") 4 | -------------------------------------------------------------------------------- /darwin/app_kit/nspanel.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ./nswindow 3 | 4 | type 5 | NSPanel* = ptr object of NSWindow -------------------------------------------------------------------------------- /darwin/app_kit/nswindowcontroller.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type 4 | NSWindowController* = ptr object of NSObject -------------------------------------------------------------------------------- /darwin/core_text.nim: -------------------------------------------------------------------------------- 1 | import core_text / [ctfont_manager, ctfont, ctfont_descriptor] 2 | export ctfont_manager, ctfont, ctfont_descriptor 3 | -------------------------------------------------------------------------------- /darwin/foundation/nsdecimal_number.nim: -------------------------------------------------------------------------------- 1 | 2 | # import ../objc/runtime 3 | import nsnumber 4 | 5 | type NSDecimalNumber* = ptr object of NSNumber 6 | -------------------------------------------------------------------------------- /darwin/foundation/nslocale.nim: -------------------------------------------------------------------------------- 1 | 2 | import ../objc/runtime 3 | 4 | type NSLocale* = ptr object of NSObject 5 | 6 | proc currencySymbol*(l: NSLocale): NSString {.objc.} 7 | -------------------------------------------------------------------------------- /darwin/foundation/nsnotification.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type NSNotification* = ptr object of NSObject 4 | 5 | proc `object`*(self: NSNotification): ID {.objc.} 6 | -------------------------------------------------------------------------------- /darwin/ui_kit/uiview.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../quartz_core/calayer 3 | 4 | type UIView* = ptr object of NSObject 5 | 6 | proc layer*(v: UIView): CALayer {.objc: "layer".} 7 | -------------------------------------------------------------------------------- /darwin/web_kit/wknavigationdelegate.nim: -------------------------------------------------------------------------------- 1 | type WKNavigationActionPolicy* = enum 2 | WKNavigationActionPolicyCancel 3 | WKNavigationActionPolicyAllow 4 | WKNavigationActionPolicyDownload 5 | -------------------------------------------------------------------------------- /darwin/foundation/nsautoreleasepool.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime] 2 | 3 | type 4 | NSAutoreleasePool* = ptr object of NSObject 5 | 6 | proc drain*(self: NSAutoreleasePool) {.objc: "drain".} 7 | -------------------------------------------------------------------------------- /darwin/quartz_core/calayer.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../core_graphics/cgcontext 3 | 4 | type CALayer* = ptr object of NSObject 5 | 6 | proc render*(l: CALayer, c: CGContext) {.objc: "renderInContext:".} 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | nimcache/ 2 | * 3 | !**/ 4 | !*.* 5 | .DS_Store 6 | !.gitattributes 7 | !.gitignore 8 | !readme.md 9 | !.gitkeep 10 | !*.nim 11 | !*.nims 12 | !*.nimble 13 | !*.h 14 | !*.css 15 | !*.html 16 | !*/ 17 | *.out -------------------------------------------------------------------------------- /darwin/objc/nsobject.nim: -------------------------------------------------------------------------------- 1 | import ./runtime 2 | 3 | export NSObject 4 | 5 | proc performSelectorOnMainThread*(self: NSObject, s: SEL, o: NSObject, wait: bool) {.objc: "performSelectorOnMainThread:withObject:waitUntilDone:".} 6 | -------------------------------------------------------------------------------- /darwin/core_graphics.nim: -------------------------------------------------------------------------------- 1 | import core_graphics / [cggeometry, cgfont, cgcolor_space, cgcontext, cgbitmap_context, cgaffine_transform] 2 | export cggeometry, cgfont, cgcolor_space, cgcontext, cgbitmap_context, cgaffine_transform 3 | -------------------------------------------------------------------------------- /darwin/store_kit.nim: -------------------------------------------------------------------------------- 1 | import store_kit/[skproducts_request, skrequest, skproduct, skpayment, skpayment_queue, skpayment_transaction] 2 | export skproducts_request, skrequest, skproduct, skpayment, skpayment_queue, skpayment_transaction 3 | -------------------------------------------------------------------------------- /darwin/app_kit/nsfont.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../core_graphics/cggeometry 3 | type 4 | NSFont* = ptr object of NSObject 5 | 6 | proc systemFontOfSize*(self: typedesc[NSFont], size: CGFloat): NSFont {.objc: "systemFontOfSize:".} -------------------------------------------------------------------------------- /darwin/foundation/nsurl.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type NSURL* = ptr object of NSObject 4 | 5 | proc URLWithString*(self: typedesc[NSURL], str: NSString): NSURL {.objc: "URLWithString:".} 6 | proc path*(self: NSURL): NSString {.objc.} 7 | -------------------------------------------------------------------------------- /darwin.nimble: -------------------------------------------------------------------------------- 1 | # Package 2 | 3 | version = "0.1.1" 4 | author = "Yuriy Glukhov" 5 | description = "Collection of bindings to MacOS and iOS APIs" 6 | license = "MIT" 7 | 8 | # Dependencies 9 | 10 | requires "nim >= 0.17.0" 11 | -------------------------------------------------------------------------------- /darwin/foundation/nsurlrequest.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ./nsurl 3 | 4 | type 5 | NSURLRequest* = ptr object of NSObject 6 | 7 | proc requestWithURL*(self: typedesc[NSURLRequest], url: NSURL): NSURLRequest {.objc: "requestWithURL:".} 8 | -------------------------------------------------------------------------------- /darwin/core_graphics/cgbitmap_context.nim: -------------------------------------------------------------------------------- 1 | import cgcontext, cgcolor_space 2 | 3 | proc CGBitmapContextCreate*(data: pointer, 4 | width, height, bitsPerComponent, bytesPerRow: csize, 5 | space: CGColorSpace, bitmapInfo: uint32): CGContext {.importc.} 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # darwin ![Build Status](https://github.com/yglukhov/darwin/workflows/build/badge.svg) 2 | Bindings to MacOS/iOS frameworks 3 | 4 | This repo is supposed to be a collection of bindings to MacOS and iOS native frameworks. Contributions are welcome! :) 5 | -------------------------------------------------------------------------------- /darwin/foundation/nsprocessinfo.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type 4 | NSProcessInfo* = ptr object of NSObject 5 | 6 | proc processInfo*(self: typedesc[NSProcessInfo]): NSProcessInfo {.objc.} 7 | proc processName*(self: NSProcessInfo): NSString {.objc.} 8 | -------------------------------------------------------------------------------- /darwin/core_foundation.nim: -------------------------------------------------------------------------------- 1 | import core_foundation / [cfbase, cfarray, cfdictionary, cfstring, cfnumber, cfurl, cferror, cfstream, cfpropertylist, cfpreferences] 2 | export cfbase, cfarray, cfdictionary, cfstring, cfnumber, cfurl, cferror, cfstream, cfpropertylist, cfpreferences 3 | -------------------------------------------------------------------------------- /darwin/core_graphics/cgcolor_space.nim: -------------------------------------------------------------------------------- 1 | import darwin / core_foundation / [cfbase] 2 | 3 | type 4 | CGColorSpace* = ptr object of CFObject 5 | 6 | proc CGColorSpaceCreateDeviceGray*(): CGColorSpace {.importc.} 7 | proc CGColorSpaceCreateDeviceRGB*(): CGColorSpace {.importc.} 8 | -------------------------------------------------------------------------------- /darwin/web_kit/wkusercontentcontroller.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ./wkuserscript 3 | 4 | type 5 | WKUserContentController* = ptr object of NSObject 6 | 7 | proc addUserScript*(self: WKUserContentController, userScript: WKUserScript): void {.objc: "addUserScript:".} 8 | -------------------------------------------------------------------------------- /darwin/app_kit/nscell.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type 4 | NSCell* = ptr object of NSObject 5 | NSImageScaling* {.size: sizeof(uint).} = enum 6 | NSImageScaleProportionallyDown 7 | NSImageScaleAxesIndependently 8 | NSImageScaleNone 9 | NSImageScaleProportionallyUpOrDown -------------------------------------------------------------------------------- /darwin/core_text/ctfont_descriptor.nim: -------------------------------------------------------------------------------- 1 | import darwin / core_foundation / [cfbase] 2 | 3 | type 4 | CTFontDescriptor* = ptr object of CFObject 5 | CTFontOrientation* {.size: sizeof(uint32).} = enum 6 | kCTFontOrientationDefault = 0 7 | kCTFontOrientationHorizontal = 1 8 | kCTFontOrientationVertical = 2 9 | -------------------------------------------------------------------------------- /darwin/store_kit/skpayment.nim: -------------------------------------------------------------------------------- 1 | 2 | 3 | import ../objc/runtime 4 | import skproduct 5 | 6 | type SKPayment* = ptr object of NSObject 7 | 8 | proc withProduct*(t: typedesc[SKPayment], p: SKProduct): SKPayment {.objc: "paymentWithProduct:".} 9 | proc applicationUsername*(p: SKPayment): NSString {.objc: "applicationUsername".} 10 | -------------------------------------------------------------------------------- /darwin/store_kit/skrequest.nim: -------------------------------------------------------------------------------- 1 | 2 | import ../objc/runtime 3 | 4 | type SKRequest* = ptr object of NSObject 5 | 6 | proc setDelegate*(r: SKRequest, delegate: NSObject) {.objc: "setDelegate:".} 7 | proc delegate*(r: SKRequest): NSObject {.objc.} 8 | proc start*(r: SKRequest) {.objc.} 9 | proc cancel*(r: SKRequest) {.objc.} 10 | -------------------------------------------------------------------------------- /darwin/web_kit/wkscriptmessage.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime] 2 | import ./wkwebview 3 | 4 | type 5 | WKScriptMessage* = ptr object of NSObject 6 | 7 | proc name*(self: WKScriptMessage): NSString {.objc: "name".} 8 | 9 | proc body*(self: WKScriptMessage): ID {.objc.} 10 | 11 | proc webView*(self: WKScriptMessage): WKWebView {.objc: "webView".} 12 | -------------------------------------------------------------------------------- /darwin/app_kit/nsimageview.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ./nscontrol 3 | import ./nsimage 4 | import ./nscell 5 | 6 | type 7 | NSImageView* = ptr object of NSControl 8 | 9 | proc setImage*(self: NSImageView, image: NSImage) {.objc: "setImage:".} 10 | proc setImageScaling*(self: NSImageView, scaling: NSImageScaling) {.objc: "setImageScaling:".} 11 | -------------------------------------------------------------------------------- /darwin/web_kit/wkframeinfo.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime] 2 | import ./wksecurityorigin 3 | 4 | type 5 | WKFrameInfo* = ptr object of NSObject 6 | 7 | # Check if the frame is the main frame 8 | proc isMainFrame*(self: WKFrameInfo): BOOL {.objc: "isMainFrame".} 9 | 10 | # Get the security origin of the frame 11 | proc securityOrigin*(self: WKFrameInfo): WKSecurityOrigin {.objc: "securityOrigin".} 12 | -------------------------------------------------------------------------------- /darwin/app_kit/nsdragoperation.nim: -------------------------------------------------------------------------------- 1 | type 2 | NSDragOperation* {.size: sizeof(uint).} = enum 3 | NSDragOperationEvery = uint.high 4 | NSDragOperationNone = 0 5 | NSDragOperationCopy = 1 6 | NSDragOperationLink = 2 7 | NSDragOperationGeneric = 4 8 | NSDragOperationPrivate = 8 9 | NSDragOperationAll_Obsolete = 15 10 | NSDragOperationMove = 16 11 | NSDragOperationDelete = 32 -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | os: 2 | - osx 3 | 4 | before_script: 5 | - export PATH=$HOME/.nimble/bin:$PATH 6 | - mkdir -p $HOME/.nimble/bin 7 | - curl -sSfL "https://github.com/dom96/choosenim/releases/download/v0.2.2/choosenim-0.2.2_macosx_amd64" -o "$HOME/.nimble/bin/choosenim" 8 | - chmod +x "$HOME/.nimble/bin/choosenim" 9 | - choosenim devel 10 | - nimble install -y 11 | 12 | script: 13 | - nimble test 14 | -------------------------------------------------------------------------------- /darwin/foundation/nsrunloop.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import nsdate 3 | 4 | type 5 | NSRunLoop* = ptr object of NSObject 6 | NSRunLoopMode* = NSString 7 | 8 | var NSDefaultRunLoopMode* {.importc.} : NSRunLoopMode 9 | 10 | proc current*(t: typedesc[NSRunLoop]): NSRunLoop {.objc: "currentRunLoop".} 11 | proc run*(s: NSRunLoop) {.objc.} 12 | proc runUntilDate*(s: NSRunLoop, date: NSDate) {.objc: "runUntilDate:".} 13 | -------------------------------------------------------------------------------- /darwin/foundation/nsenumerator.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type NSEnumeratorAbstract* = ptr object of NSObject 4 | type NSEnumerator*[T] = ptr object of NSEnumeratorAbstract 5 | 6 | proc objcClass(t: typedesc[NSEnumeratorAbstract]): auto {.inline.} = objcClass("NSEnumerator") 7 | 8 | proc nextObject*(e: NSEnumeratorAbstract): NSObject {.objc.} 9 | proc nextObject*[T](e: NSEnumerator[T]): T = cast[T](nextObject(cast[NSEnumeratorAbstract](e))) -------------------------------------------------------------------------------- /darwin/web_kit/wkopenpanelparameters.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime] 2 | 3 | type 4 | WKOpenPanelParameters* = ptr object of NSObject 5 | 6 | # Check if the panel allows selecting multiple files 7 | proc allowsMultipleSelection*(self: WKOpenPanelParameters): BOOL {.objc: "allowsMultipleSelection".} 8 | 9 | # Check if the panel allows selecting directories 10 | proc allowsDirectories*(self: WKOpenPanelParameters): BOOL {.objc: "allowsDirectories".} 11 | -------------------------------------------------------------------------------- /darwin/web_kit/wkuserscript.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type 4 | WKUserScript* = ptr object of NSObject 5 | WKUserScriptInjectionTime* {.size: sizeof(uint32).} = enum 6 | AtDocumentStart = 0 7 | AtDocumentEnd = 1 8 | 9 | proc initWithSource*(self: WKUserScript, source: NSString, injectionTime: WKUserScriptInjectionTime, forMainFrameOnly: BOOL): WKUserScript {.objc: "initWithSource:injectionTime:forMainFrameOnly:", discardable.} 10 | -------------------------------------------------------------------------------- /darwin/store_kit/skproduct.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../foundation/[nsdecimal_number, nslocale] 3 | 4 | type SKProduct* = ptr object of NSObject 5 | 6 | proc localizedDescription*(p: SKProduct): NSString {.objc.} 7 | proc localizedTitle*(p: SKProduct): NSString {.objc.} 8 | proc priceLocale*(p: SKProduct): NSLocale {.objc.} 9 | proc price*(p: SKProduct): NSDecimalNumber {.objc.} 10 | proc productIdentifier*(p: SKProduct): NSString {.objc.} 11 | -------------------------------------------------------------------------------- /darwin/web_kit.nim: -------------------------------------------------------------------------------- 1 | import web_kit / [wkwebview, wkwebviewconfiguration, wknavigation, 2 | wkusercontentcontroller, wkuserscript, wkpreferences, wknavigationdelegate, 3 | wknavigationresponse, wkopenpanelparameters, wkframeinfo, wkscriptmessage] 4 | export wkwebview, wkwebviewconfiguration, wknavigation, wkusercontentcontroller, 5 | wkuserscript, wkpreferences, wknavigationdelegate, wknavigationresponse, 6 | wkopenpanelparameters, wkframeinfo, wkscriptmessage 7 | -------------------------------------------------------------------------------- /darwin/web_kit/wksecurityorigin.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime] 2 | 3 | type 4 | WKSecurityOrigin* = ptr object of NSObject 5 | 6 | # Get the host of the security origin 7 | proc host*(self: WKSecurityOrigin): NSString {.objc: "host".} 8 | 9 | # Get the protocol of the security origin 10 | proc protocol*(self: WKSecurityOrigin): NSString {.objc: "protocol".} 11 | 12 | # Get the port of the security origin 13 | proc port*(self: WKSecurityOrigin): int {.objc: "port".} 14 | -------------------------------------------------------------------------------- /darwin/web_kit/wknavigationresponse.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime] 2 | import ../foundation/[nsurlresponse, nsurl] 3 | import ./wkwebview 4 | 5 | type 6 | WKNavigationResponse* = ptr object of NSObject 7 | 8 | # Properties 9 | proc canShowMIMEType*(self: WKNavigationResponse): BOOL {.objc: "canShowMIMEType".} 10 | proc isForMainFrame*(self: WKNavigationResponse): BOOL {.objc: "forMainFrame".} 11 | proc response*(self: WKNavigationResponse): NSURLResponse {.objc: "response".} 12 | -------------------------------------------------------------------------------- /darwin/foundation/nsbundle.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type 4 | NSBundle* = ptr object of NSObject 5 | 6 | proc mainBundle*(self: typedesc[NSBundle]): NSBundle {.objc: "mainBundle" .} 7 | proc bundlePath*(self: NSBundle): NSString {.objc: "bundlePath".} 8 | proc pathForResource*(self: NSBundle, name: NSString, ext: NSString): NSString {.objc: "pathForResource:ofType:".} 9 | proc objectForInfoDictionaryKey*(self: NSBundle, key: NSString): NSObject {.objc: "objectForInfoDictionaryKey:".} -------------------------------------------------------------------------------- /darwin/store_kit/skpayment_queue.nim: -------------------------------------------------------------------------------- 1 | 2 | import ../objc/runtime 3 | import skpayment, skpayment_transaction 4 | 5 | type SKPaymentQueue* = ptr object of NSObject 6 | 7 | proc defaultQueue*(t: typedesc[SKPaymentQueue]): SKPaymentQueue {.objc.} 8 | proc add*(q: SKPaymentQueue, p: SKPayment) {.objc: "addPayment:".} 9 | proc finish*(q: SKPaymentQueue, t: SKPaymentTransaction) {.objc: "finishTransaction:".} 10 | proc addTransactionObserver*(q: SKPaymentQueue, a: NSObject) {.objc: "addTransactionObserver:".} 11 | -------------------------------------------------------------------------------- /darwin/ui_kit/uidevice.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type UIDevice* = ptr object of NSObject 4 | 5 | proc currentDevice*(n: typedesc[UIDevice]): UIDevice {.objc: "currentDevice".} 6 | proc name*(d: UIDevice): NSString {.objc: "name".} 7 | proc model*(d: UIDevice): NSString {.objc: "model".} 8 | proc localizedModel*(d: UIDevice): NSString {.objc: "localizedModel".} 9 | proc systemName*(d: UIDevice): NSString {.objc: "systemName".} 10 | proc systemVersion*(d: UIDevice): NSString {.objc: "systemVersion".} 11 | -------------------------------------------------------------------------------- /darwin/ui_kit/uiscreen.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../core_graphics/cggeometry 3 | 4 | type UIScreen* = ptr object of NSObject 5 | 6 | proc mainScreen*(n: typedesc[UIScreen]): UIScreen {.objc: "mainScreen".} 7 | proc mainScreen*(): UIScreen {.inline.} = UIScreen.mainScreen() 8 | proc nativeScale(s: UIScreen): CGFloat {.objc: "nativeScale".} 9 | proc scaleFactor*(s: UIScreen): CGFloat = 10 | if s.respondsToSelector("nativeScale"): 11 | result = s.nativeScale() 12 | else: 13 | result = 1 14 | -------------------------------------------------------------------------------- /darwin/core_text/ctfont_manager.nim: -------------------------------------------------------------------------------- 1 | import darwin / core_foundation / [ cfurl, cferror ] 2 | 3 | type 4 | CTFontManagerScope* {.size: sizeof(uint32).} = enum 5 | kCTFontManagerScopeNone = 0, 6 | kCTFontManagerScopeProcess = 1, 7 | kCTFontManagerScopeUser = 2, # not supported in iOS 8 | kCTFontManagerScopeSession = 3 # not supported in iOS 9 | 10 | proc CTFontManagerRegisterFontsForURL*(fontURL: CFURL, scope: CTFontManagerScope, error: ptr CFError): bool {.importc.} 11 | -------------------------------------------------------------------------------- /darwin/web_kit/wkpreferences.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type 4 | WKPreferences* = ptr object of NSObject 5 | 6 | proc setDeveloperExtrasEnabled*(self: WKPreferences, a: BOOL) {.objc: "_setDeveloperExtrasEnabled:".} 7 | proc setFullScreenEnabled*(self: WKPreferences, a: BOOL) {.objc: "_setFullScreenEnabled:".} 8 | proc setJavaScriptCanAccessClipboard*(self: WKPreferences, a: BOOL) {.objc: "_setJavaScriptCanAccessClipboard:".} 9 | proc setDOMPasteAllowed*(self: WKPreferences, a: BOOL) {.objc: "_setDOMPasteAllowed:".} 10 | -------------------------------------------------------------------------------- /darwin/foundation/nsgeometry.nim: -------------------------------------------------------------------------------- 1 | 2 | import ../core_graphics/cggeometry 3 | 4 | type 5 | NSPoint* = CGPoint 6 | NSRect* = CGRect 7 | NSSize* = CGSize 8 | 9 | proc NSMakePoint*(x, y: CGFloat): NSPoint = 10 | result.x = x 11 | result.y = y 12 | 13 | proc NSMakeSize*(w, h: CGFloat): NSSize = 14 | result.width = w 15 | result.height = h 16 | 17 | proc NSMakeRect*(x, y, w, h: CGFloat): NSRect = 18 | result.origin.x = x 19 | result.origin.y = y 20 | result.size.width = w 21 | result.size.height = h 22 | -------------------------------------------------------------------------------- /darwin/app_kit/nsscreen.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../core_graphics/cggeometry 3 | 4 | type NSScreen* = ptr object of NSObject 5 | 6 | proc mainScreen(n: typedesc[NSScreen]): NSScreen {.objc: "mainScreen".} 7 | proc mainScreen*(): NSScreen {.inline.} = NSScreen.mainScreen() 8 | 9 | proc backingScaleFactor(s: NSScreen): CGFloat {.objc: "backingScaleFactor".} 10 | proc scaleFactor*(s: NSScreen): CGFloat = 11 | if s.respondsToSelector("backingScaleFactor"): 12 | result = s.backingScaleFactor() 13 | else: 14 | result = 1 15 | -------------------------------------------------------------------------------- /darwin/app_kit/nsopenglview.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../foundation / [ nsgeometry ] 3 | import ./[nsview, nsopengl] 4 | 5 | type 6 | NSOpenGLView* = ptr object of NSView 7 | 8 | proc initWithFrame*(self: NSOpenGLView, frame: NSRect, pixelFormat: NSOpenGLPixelFormat): NSOpenGLView {.objc: "initWithFrame:pixelFormat:".} 9 | 10 | proc setWantsBestResolutionOpenGLSurface*(self: NSOpenGLView, flag: bool) {.objc: "setWantsBestResolutionOpenGLSurface:".} 11 | proc openGLContext*(self: NSOpenGLView): NSOpenGLContext {.objc.} 12 | proc reshape*(self: NSOpenGLView) {.objc.} 13 | -------------------------------------------------------------------------------- /darwin/store_kit/skproducts_request.nim: -------------------------------------------------------------------------------- 1 | 2 | import ../objc/runtime 3 | import ../foundation/[nsset, nsarray] 4 | import skrequest, skproduct 5 | 6 | type 7 | SKProductsRequest* = ptr object of SKRequest 8 | SKProductsResponse* = ptr object of NSObject 9 | 10 | proc init*(r: SKProductsRequest, productIds: NSSet[NSString]): SKProductsRequest {.objc: "initWithProductIdentifiers:".} 11 | 12 | 13 | proc products*(r: SKProductsResponse): NSArray[SKProduct] {.objc: "products".} 14 | proc invalidProductIdentifiers*(r: SKProductsResponse): NSArray[NSString] {.objc: "invalidProductIdentifiers".} 15 | -------------------------------------------------------------------------------- /darwin/core_video/cvbase.nim: -------------------------------------------------------------------------------- 1 | type 2 | CVSMPTETime* = object 3 | subframes*: int16 4 | subframeDivisor*: int16 5 | counter*: uint32 6 | typ*: uint32 7 | flags*: uint32 8 | hours*: int16 9 | minutes*: int16 10 | seconds*: int16 11 | frames*: int16 12 | 13 | CVTimeStamp* = object 14 | version*: uint32 15 | videoTimeScale*: uint32 16 | videoTime*: int64 17 | hostTime*: uint64 18 | rateScalar*: cdouble 19 | videoRefreshPeriod*: int64 20 | smpteTime*: CVSMPTETime 21 | flags*: uint64 22 | reserved: uint64 23 | 24 | CVOptionFlags* = uint64 25 | -------------------------------------------------------------------------------- /darwin/web_kit/wkwebviewconfiguration.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ./wkusercontentcontroller 3 | import ./wkpreferences 4 | type 5 | WKWebViewConfiguration* = ptr object of NSObject 6 | 7 | proc newWKWebViewConfiguration*(a: typedesc[WKWebViewConfiguration]): WKWebViewConfiguration{.objc: "new".} 8 | 9 | proc userContentController*(self: WKWebViewConfiguration): WKUserContentController {.objc: "userContentController".} 10 | proc setUserContentController*(self: WKWebViewConfiguration, controller: WKUserContentController): void {.objc: "setUserContentController:".} 11 | proc preferences*(self: WKWebViewConfiguration): WKPreferences {.objc.} -------------------------------------------------------------------------------- /darwin/core_graphics/cggeometry.nim: -------------------------------------------------------------------------------- 1 | 2 | when sizeof(pointer) == 8: 3 | type CGFloat* = cdouble 4 | else: 5 | type CGFloat* = cfloat 6 | 7 | 8 | type 9 | CGPoint* {.bycopy.} = object 10 | x*: CGFloat 11 | y*: CGFloat 12 | 13 | CGSize* {.bycopy.} = object 14 | width*: CGFloat 15 | height*: CGFloat 16 | 17 | CGVector* {.bycopy.} = object 18 | dx*: CGFloat 19 | dy*: CGFloat 20 | 21 | CGRect* {.bycopy.} = object 22 | origin*: CGPoint 23 | size*: CGSize 24 | 25 | proc CGPointMake*(x, y: CGFloat): CGPoint {.inline.} = 26 | result.x = x 27 | result.y = y 28 | -------------------------------------------------------------------------------- /darwin/store_kit/skpayment_transaction.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import skpayment 3 | 4 | type 5 | SKPaymentTransaction* = ptr object of NSObject 6 | SKPaymentTransactionState* {.size: sizeof(uint).} = enum 7 | purchasing 8 | purchased 9 | failed 10 | restored 11 | deferred 12 | 13 | proc payment*(t: SKPaymentTransaction): SKPayment {.objc.} 14 | proc state*(t: SKPaymentTransaction): SKPaymentTransactionState {.objc: "transactionState".} 15 | proc identifier*(t: SKPaymentTransaction): NSString {.objc: "transactionIdentifier".} 16 | proc original*(t: SKPaymentTransaction): SKPaymentTransaction {.objc: "originalTransaction".} 17 | -------------------------------------------------------------------------------- /tests/tblocks.nim: -------------------------------------------------------------------------------- 1 | import ../darwin/objc/blocks 2 | 3 | #### 4 | proc testBlock() = 5 | var v = 5 6 | let bl = toBlock() do(a: int): 7 | v = v + a 8 | bl.call(123) 9 | doAssert(v == 128) 10 | 11 | testBlock() 12 | 13 | #### 14 | type MyBlock = proc(a:int): void 15 | proc testBlock2(cb: Block[MyBlock]) = 16 | cb.call(123) 17 | 18 | var v = 5 19 | let bl = toBlock() do(a: int): 20 | v = v + a 21 | testBlock2(bl) 22 | doAssert(v == 128) 23 | 24 | #### 25 | type MyBlock2 = proc(): void 26 | 27 | proc testBlock3(cb: Block[MyBlock2]) = 28 | cb.call() 29 | 30 | var v2 = 5 31 | let bl2 = toBlock() do(): 32 | v2 = v2 + 1 33 | testBlock3(bl2) 34 | doAssert(v2 == 6) 35 | -------------------------------------------------------------------------------- /darwin/app_kit/nstextfield.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ./nscontrol 3 | import ../foundation/nsgeometry 4 | import ./nsfont 5 | import ./nscolor 6 | 7 | type 8 | NSTextField* = ptr object of NSControl 9 | 10 | proc setStringValue*(self: NSTextField, s: NSString) {.objc: "setStringValue:".} 11 | proc setEditable*(self: NSTextField, b: BOOL) {.objc: "setEditable:".} 12 | proc setBezeled*(self: NSTextField, b: BOOL) {.objc: "setBezeled:".} 13 | proc setDrawsBackground*(self: NSTextField, b: BOOL) {.objc: "setDrawsBackground:".} 14 | proc setFont*(self: NSTextField, f: NSFont) {.objc: "setFont:".} 15 | proc setTextColor*(self: NSTextField, f: NSColor) {.objc: "setTextColor:".} 16 | -------------------------------------------------------------------------------- /darwin/app_kit/nsopenpanel.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ./nssavepanel 3 | import ../foundation/[nsarray, nsurl] 4 | 5 | type 6 | NSOpenPanel* = ptr object of NSSavePanel 7 | 8 | proc openPanel*(self: typedesc[NSOpenPanel]): NSOpenPanel {.objc.} 9 | proc setCanChooseFiles*(self: NSOpenPanel, b: BOOL) {.objc: "setCanChooseFiles:".} 10 | proc setCanChooseDirectories*(self: NSOpenPanel, b: BOOL) {.objc: "setCanChooseDirectories:".} 11 | proc setResolvesAliases*(self: NSOpenPanel, b: BOOL) {.objc: "setResolvesAliases:".} 12 | proc setAllowsMultipleSelection*(self: NSOpenPanel, b: BOOL) {.objc: "setAllowsMultipleSelection:".} 13 | proc URLs*(self: NSOpenPanel): NSArray[NSURL] {.objc.} 14 | -------------------------------------------------------------------------------- /darwin/core_foundation/cfurl.nim: -------------------------------------------------------------------------------- 1 | import cfbase, cfstring 2 | 3 | type 4 | CFURL* = ptr object of CFObject 5 | CFURLPathStyle* = enum 6 | kCFURLPOSIXPathStyle = 0 7 | kCFURLWindowsPathStyle = 2 8 | 9 | 10 | proc CFURLGetTypeID*(): CFTypeID {.importc.} 11 | 12 | proc CFURLCreateWithBytes*(allocator: CFAllocator, URLBytes: pointer, length: CFIndex, encoding: CFStringEncoding, baseURL: CFURL): CFURL {.importc.} 13 | proc CFURLCreateWithString*(allocator: CFAllocator, URLString: CFString, baseURL: CFURL): CFURL {.importc.} 14 | proc CFURLCreateWithFileSystemPath*(allocator: CFAllocator, filePath: CFString, pathStyle: CFURLPathStyle, isDirectory: Boolean): CFURL {.importc.} 15 | -------------------------------------------------------------------------------- /darwin/app_kit/nseventmask.nim: -------------------------------------------------------------------------------- 1 | type 2 | NSEventMask* {.size: sizeof(uint64).} = enum 3 | LeftMouseDown = 1 shl 1, 4 | LeftMouseUp = 1 shl 2, 5 | RightMouseDown = 1 shl 3, 6 | RightMouseUp = 1 shl 4, 7 | MouseMoved = 1 shl 5, 8 | LeftMouseDragged = 1 shl 6, 9 | RightMouseDragged = 1 shl 7, 10 | KeyDown = 1 shl 10, 11 | KeyUp = 1 shl 11, 12 | FlagsChanged = 1 shl 12, 13 | ScrollWheel = 1 shl 22, 14 | TabletPoint = 1 shl 23, 15 | TabletProximity = 1 shl 24, 16 | OtherMouseDown = 1 shl 25, 17 | OtherMouseUp = 1 shl 26, 18 | OtherMouseDragged = 1 shl 27 19 | -------------------------------------------------------------------------------- /darwin/foundation/nserror.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../foundation/[nsdictionary] 3 | 4 | type NSError* = ptr object of NSObject 5 | 6 | proc localizedDescription*(self: NSError): NSString {.objc: "localizedDescription".} 7 | proc localizedFailureReason*(self: NSError): NSString {.objc: "localizedFailureReason".} 8 | proc localizedRecoverySuggestion*(self: NSError): NSString {.objc: "localizedRecoverySuggestion".} 9 | proc code*(self: NSError): NSInteger {.objc.} 10 | proc domain*(self: NSError): NSString {.objc.} 11 | proc userInfo*(self: NSError): NSDictionary {.objc.} 12 | proc errorWithDomain*(domain: NSString, code: NSInteger, userInfo: NSDictionary): NSError {.objc: "errorWithDomain:code:userInfo:".} 13 | -------------------------------------------------------------------------------- /tests/tfsstream.nim: -------------------------------------------------------------------------------- 1 | import std / [unittest, strutils] 2 | 3 | import darwin / core_services / fsstream 4 | 5 | test "fstream enums": 6 | 7 | check cast[FSEventStreamEventFlags]({FSEventStreamEventFlag.MustScanSubDirs}).uint32.toHex() == "00000001" 8 | check cast[FSEventStreamEventFlags]({MustScanSubDirs}).uint32.toHex() == "00000001" 9 | check cast[FSEventStreamEventFlags]({ItemIsFile}).uint32.toHex() == "00010000" 10 | check cast[FSEventStreamEventFlags]({ItemIsDir}).uint32.toHex() == "00020000" 11 | check cast[FSEventStreamEventFlags]({ItemFinderInfoMod}).uint32.toHex() == "00002000" 12 | check cast[FSEventStreamEventFlags]({ItemIsSymlink}).uint32.toHex() == "00040000" 13 | -------------------------------------------------------------------------------- /darwin/core_graphics/cgfont.nim: -------------------------------------------------------------------------------- 1 | import darwin / core_foundation / [cfbase, cfstring] 2 | import cggeometry 3 | 4 | type 5 | CGFont* = ptr object of CFObject 6 | CGFontIndex* = distinct uint16 7 | CGGlyph* = distinct CGFontIndex 8 | 9 | proc CGFontCreateWithFontName*(name: CFString): CGFont {.importc.} 10 | proc getGlyphAdvances*(font: CGFont, glyphs: ptr CGGlyph, count: csize, advances: ptr cint): bool {.importc: "CGFontGetGlyphAdvances".} 11 | proc getGlyphBBoxes*(font: CGFont, glyphs: ptr CGGlyph, count: csize, bboxes: ptr CGRect): bool {.importc: "CGFontGetGlyphBBoxes".} 12 | 13 | proc getAscent*(font: CGFont): cint {.importc: "CGFontGetAscent".} 14 | proc getDescent*(font: CGFont): cint {.importc: "CGFontGetDescent".} 15 | -------------------------------------------------------------------------------- /darwin/foundation.nim: -------------------------------------------------------------------------------- 1 | import foundation / [nsstring, nsgeometry, nsarray, nsdata, nsset, nsenumerator, 2 | nserror, nsdate, nsdictionary, nsnumber, nsdecimal_number, nslocale, 3 | nspath_utilities, nsprocessinfo, nsurl, nsurlrequest, nscoder, nsbundle, 4 | nsrunloop, nsstream, nsnotification, nsurlresponse, nsautoreleasepool] 5 | export nsstring, nsgeometry, nsarray, nsdata, nsset, nsenumerator, nserror, 6 | nsdate, nsdictionary, nsnumber, nsdecimal_number, nslocale, 7 | nspath_utilities, nsprocessinfo, nsurl, nsurlrequest, nscoder, nsbundle, 8 | nsrunloop, nsstream, nsnotification, nsurlresponse, nsautoreleasepool 9 | 10 | import objc/runtime 11 | export NSObject, NSLog, isKindOfClass, retain, release, alloc, init 12 | -------------------------------------------------------------------------------- /darwin/foundation/nsnumberformatter.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../foundation/[nsnumber, nslocale] 3 | 4 | const kCFNumberFormatterCurrencyStyle = 2 5 | const NSNumberFormatterBehavior10_4* = 1040 6 | 7 | type NSNumberFormatter* = ptr object of NSObject 8 | 9 | type NSNumberFormatterStyle* = enum 10 | NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle 11 | 12 | proc stringFromNumber*(f: NSNumberFormatter, p: NSNumber): NSString {.objc: "stringFromNumber:".} 13 | 14 | proc setFormatterBehavior*(formatter: NSNumberFormatter): NSString {.objc.} 15 | proc setNumberStyle*(formatter: NSNumberFormatter, 16 | style: NSNumberFormatterStyle) {.objc:"setNumberStyle:".} 17 | proc setLocale*(formatter: NSNumberFormatter, locale: NSLocale) {.objc:"setLocale:".} 18 | -------------------------------------------------------------------------------- /darwin/core_foundation/cfpropertylist.nim: -------------------------------------------------------------------------------- 1 | import cfbase, cfstream, cferror 2 | 3 | export CFPropertyList 4 | 5 | type 6 | CFPropertyListMutabilityOptions* {.size: sizeof(uint).} = enum 7 | kCFPropertyListImmutable = 0 8 | kCFPropertyListMutableContainers = 1 9 | kCFPropertyListMutableContainersAndLeaves = 2 10 | 11 | CFPropertyListFormat* {.size: sizeof(uint).} = enum 12 | kCFPropertyListOpenStepFormat = 1 13 | kCFPropertyListXMLFormat_v1_0 = 100 14 | kCFPropertyListBinaryFormat_v1_0 = 200 15 | 16 | proc CFPropertyListCreateWithStream*(alloc: CFAllocator, stream: CFReadStream, streamLength: CFIndex, options: CFPropertyListMutabilityOptions, format: ptr CFPropertyListFormat, error: ptr CFError): CFPropertyList {.importc.} 17 | 18 | -------------------------------------------------------------------------------- /darwin/core_foundation/cfstream.nim: -------------------------------------------------------------------------------- 1 | import cfbase, cfurl 2 | 3 | type 4 | CFReadStream* = ptr object of CFObject 5 | CFWriteStream* = ptr object of CFObject 6 | 7 | proc CFReadStreamGetTypeID*(): CFTypeID {.importc.} 8 | proc CFWriteStreamGetTypeID*(): CFTypeID {.importc.} 9 | 10 | proc CFReadStreamCreateWithBytesNoCopy*(alloc: CFAllocator, bytes: pointer, length: CFIndex, bytesDeallocator: CFAllocator): CFReadStream {.importc.} 11 | proc CFReadStreamCreateWithFile*(alloc: CFAllocator, fileURL: CFURL): CFReadStream {.importc.} 12 | 13 | proc CFWriteStreamCreateWithFile*(alloc: CFAllocator, fileURL: CFURL): CFReadStream {.importc.} 14 | 15 | proc CFReadStreamOpen(stream: CFReadStream): Boolean {.importc.} 16 | 17 | proc open*(s: CFReadStream): bool {.inline.} = bool CFReadStreamOpen(s) 18 | -------------------------------------------------------------------------------- /darwin/foundation/nsdata.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type NSData* = ptr object of NSObject 4 | 5 | proc length*(p: NSData): int {.objc: "length".} 6 | proc getBytes*(self: NSData, buffer: pointer, length: int) {.objc: "getBytes:length:".} 7 | proc withBytes(n: typedesc[NSData], bytes: pointer, length: int): NSData {.objc: "dataWithBytes:length:".} 8 | 9 | proc dataWithBytes*(bytes: cstring, length: int): NSData {.inline.} = 10 | NSData.withBytes(bytes, length) 11 | 12 | proc emptyData*(n: typedesc[NSData]): NSData {.objc: "data".} 13 | 14 | proc withBytes*(T: typedesc[NSData], bytes: openarray[byte]): T {.inline.} = 15 | if bytes.len == 0: 16 | T.emptyData() 17 | else: 18 | T.withBytes(unsafeAddr bytes[0], bytes.len) 19 | 20 | proc len*(p: NSData): int {.inline.} = p.length 21 | -------------------------------------------------------------------------------- /darwin/app_kit.nim: -------------------------------------------------------------------------------- 1 | import foundation 2 | import app_kit / [nsscreen, nsview, nsevent, nscursor, nspasteboard, nswindow, 3 | nsapplication, nsmenu, nsresponder, nstext, nstextview, nswindowcontroller, 4 | nsdragoperation, nsdragginginfo, nsalert, nsimage, nscell, nscontrol, 5 | nsfont, nstextfield, nscolor, nsimageview, nspanel, nssavepanel, 6 | nsopenpanel, nsbutton, nseventmask] 7 | export foundation, nsscreen, nsview, nsevent, nscursor, nspasteboard, nswindow, 8 | nsapplication, nsmenu, nsresponder, nstext, nstextview, nswindowcontroller, 9 | nsdragoperation, nsdragginginfo, nsalert, nsimage, nscell, nscontrol, 10 | nsfont, nstextfield, nscolor, nsimageview, nspanel, nssavepanel, 11 | nsopenpanel, nsbutton, nseventmask 12 | 13 | {.passL: "-framework AppKit".} 14 | -------------------------------------------------------------------------------- /darwin/core_foundation/cfrunloop.nim: -------------------------------------------------------------------------------- 1 | import cfbase 2 | 3 | type 4 | CFRunLoop* = ptr object of CFObject 5 | CFRunLoopMode* = ptr object of CFString 6 | 7 | CFRunLoopRunResult* {.size: sizeof(int32).} = enum 8 | kCFRunLoopRunFinished = 1 9 | kCFRunLoopRunStopped = 2 10 | kCFRunLoopRunTimedOut = 3 11 | kCFRunLoopRunHandledSource = 4 12 | 13 | let 14 | kCFRunLoopDefaultMode* {.importc, extern: "kCFRunLoopDefaultMode".}: CFRunLoopMode 15 | kCFRunLoopCommonModes* {.importc, extern: "kCFRunLoopCommonModes".}: CFRunLoopMode 16 | 17 | proc CFRunLoopGetCurrent*(): CFRunLoop {.importc.} 18 | proc runInMode*(mode: CFRunLoopMode, seconds: CFTimeInterval, returnAfterSourceHandled: bool): CFRunLoopRunResult {.importc: "CFRunLoopRunInMode".} 19 | proc stop*(loop: CFRunLoop) {.importc: "CFRunLoopStop".} 20 | -------------------------------------------------------------------------------- /darwin/foundation/nsurlresponse.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime] 2 | import ../foundation/[nsstring, nsurl] 3 | 4 | type 5 | NSURLResponse* = ptr object of NSObject 6 | 7 | # Properties 8 | 9 | # Get the expected content length of the response 10 | proc expectedContentLength*(self: NSURLResponse): int64 {.objc: "expectedContentLength".} 11 | 12 | # Get the MIME type of the response 13 | proc MIMEType*(self: NSURLResponse): NSString {.objc: "MIMEType".} 14 | 15 | # Get the suggested filename for the response 16 | proc suggestedFilename*(self: NSURLResponse): NSString {.objc: "suggestedFilename".} 17 | 18 | # Get the URL for the response 19 | proc URL*(self: NSURLResponse): NSURL {.objc: "URL".} 20 | 21 | # Get the text encoding name of the response 22 | proc textEncodingName*(self: NSURLResponse): NSString {.objc: "textEncodingName".} 23 | -------------------------------------------------------------------------------- /darwin/core_graphics/cgaffine_transform.nim: -------------------------------------------------------------------------------- 1 | import cggeometry 2 | 3 | type CGAffineTransform* = object 4 | a*, b*, c*, d*: CGFloat 5 | tx*, ty*: CGFloat 6 | 7 | var 8 | CGAffineTransformIdentityAux {.importc: "CGAffineTransformIdentity".}: CGAffineTransform 9 | 10 | template CGAffineTransformIdentity*: CGAffineTransform = 11 | let a = CGAffineTransformIdentityAux; a 12 | 13 | proc translate*(t: CGAffineTransform, tx, ty: CGFloat): CGAffineTransform {.importc: "CGAffineTransformTranslate".} 14 | proc scale*(t: CGAffineTransform, sx, sy: CGFloat): CGAffineTransform {.importc: "CGAffineTransformScale".} 15 | proc rotate*(t: CGAffineTransform, angle: CGFloat): CGAffineTransform {.importc: "CGAffineTransformRotate".} 16 | proc invert*(t: CGAffineTransform): CGAffineTransform {.importc: "CGAffineTransformInvert".} 17 | proc concat*(t1, t2: CGAffineTransform): CGAffineTransform {.importc: "CGAffineTransformConcat".} 18 | -------------------------------------------------------------------------------- /darwin/foundation/nsdate.nim: -------------------------------------------------------------------------------- 1 | import times 2 | import ../objc/runtime 3 | 4 | 5 | type NSTimeInterval* = cdouble 6 | type NSDate* = ptr object of NSObject 7 | 8 | 9 | proc withTimeIntervalSinceNow*(t: typedesc[NSDate], sec: NSTimeInterval): NSDate {.objc: "dateWithTimeIntervalSinceNow:".} 10 | proc withTimeInterval*(t: typedesc[NSDate], sec: NSTimeInterval, sinceDate: NSDate): NSDate {.objc: "dateWithTimeInterval:sinceDate:".} 11 | proc withTimeIntervalSinceReferenceDate*(t: typedesc[NSDate], sec: NSTimeInterval): NSDate {.objc: "dateWithTimeIntervalSinceReferenceDate:".} 12 | proc withTimeIntervalSince1970*(t: typedesc[NSDate], sec: NSTimeInterval): NSDate {.objc: "dateWithTimeIntervalSince1970:".} 13 | 14 | proc timeIntervalSince1970*(d: NSDate): float {.objc.} 15 | 16 | converter toTime*(d: NSDate): Time = 17 | when declared(fromUnixFloat): 18 | fromUnixFloat(d.timeIntervalSince1970()) 19 | else: 20 | fromSeconds(d.timeIntervalSince1970()) 21 | -------------------------------------------------------------------------------- /tests/tarrays.nim: -------------------------------------------------------------------------------- 1 | import ../darwin/foundation 2 | import ../darwin/objc/blocks 3 | 4 | let a = NSString("hello") 5 | let b = NSString("bye") 6 | let arr = arrayWithObjects(a, b) 7 | doAssert($arr[0] == "hello") 8 | doAssert(arr.len == 2) 9 | 10 | proc test() = 11 | var elems = newSeq[string]() 12 | let bl = toBlock() do(o: NSString, idx: uint, stop: var bool): 13 | elems.add($o) 14 | 15 | arr.enumerateObjectsUsingBlock(bl) 16 | bl.release() 17 | doAssert(elems == @["hello", "bye"]) 18 | 19 | let bl2 = toBlock() do(o: NSString, idx: uint, stop: var bool): 20 | # Here we test that the block created as of the same exact 21 | # type as above despite this callback being not closure 22 | discard 23 | 24 | arr.enumerateObjectsUsingBlock(bl2) 25 | bl2.release() 26 | 27 | elems = @[] 28 | arr.enumerateObjectsUsingBlock() do(o: NSString, idx: uint, stop: var bool): 29 | elems.add($o) 30 | doAssert(elems == @["hello", "bye"]) 31 | 32 | test() 33 | -------------------------------------------------------------------------------- /darwin/foundation/nsstream.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import nsdata, nsrunloop 3 | 4 | type 5 | NSStream* = ptr object of NSObject 6 | NSInputStream* = ptr object of NSStream 7 | NSOutputStream* = ptr object of NSStream 8 | 9 | NSStreamEvent* {.size: sizeof(uint).} = enum 10 | NSStreamEventNone = 0 11 | NSStreamEventOpenCompleted = 1 shl 0 12 | NSStreamEventHasBytesAvailable = 1 shl 1 13 | NSStreamEventHasSpaceAvailable = 1 shl 2 14 | NSStreamEventErrorOccurred = 1 shl 3 15 | NSStreamEventEndEncountered = 1 shl 4 16 | 17 | proc setDelegate*(s: NSStream, d: NSObject) {.objc: "setDelegate:".} 18 | proc delegate*(s: NSStream): NSObject {.objc.} 19 | proc open*(s: NSStream) {.objc.} 20 | proc close*(s: NSStream) {.objc.} 21 | 22 | proc scheduleInRunLoop*(s: NSStream, rl: NSRunLoop, forMode: NSRunLoopMode) {.objc: "scheduleInRunLoop:forMode:".} 23 | 24 | proc withData*(t: typedesc[NSInputStream], d: NSData): NSInputStream {.objc: "inputStreamWithData:".} 25 | -------------------------------------------------------------------------------- /darwin/core_video/cvdisplay_link.nim: -------------------------------------------------------------------------------- 1 | import ../core_foundation/[cfbase] 2 | import ../opengl/[cgltypes] 3 | import ./cvreturn, ./cvbase 4 | 5 | export cvreturn, cvbase 6 | 7 | type 8 | CVDisplayLink* = ptr object of CFObject 9 | CVDisplayLinkOutputCallback* = proc(displayLink: CVDisplayLink, inNow, inOutputTime: ptr CVTimeStamp, flagsIn: CVOptionFlags, flagsOut: var CVOptionFlags, userInfo: pointer) {.cdecl.} 10 | 11 | 12 | proc CVDisplayLinkCreateWithActiveCGDisplays*(l: ptr CVDisplayLink): CVReturn {.importc.} 13 | proc setOutputCallback*(l: CVDisplayLink, cb: CVDisplayLinkOutputCallback, userInfo: pointer): CVReturn {.importc: "CVDisplayLinkSetOutputCallback".} 14 | proc setCurrentCGDisplayFromOpenGLContext*(l: CVDisplayLink, cglContext: CGLContextObj, cglPixelFormat: CGLPixelFormatObj): CVReturn {.importc: "CVDisplayLinkSetCurrentCGDisplayFromOpenGLContext".} 15 | proc start*(l: CVDisplayLink): CVReturn {.importc: "CVDisplayLinkStart".} 16 | proc stop*(l: CVDisplayLink): CVReturn {.importc: "CVDisplayLinkStop".} 17 | -------------------------------------------------------------------------------- /darwin/foundation/nspath_utilities.nim: -------------------------------------------------------------------------------- 1 | import ./nsarray 2 | import ./nsstring 3 | 4 | type 5 | NSSearchPathDomain* {.pure, size: sizeof(uint).} = enum 6 | user 7 | local 8 | network 9 | system 10 | 11 | NSSearchPathDirectory* {.pure, size: sizeof(uint).} = enum 12 | application = 1 13 | demoApplication 14 | developerApplication 15 | adminApplication 16 | library 17 | developer 18 | user 19 | documentation 20 | document 21 | coreService 22 | autosavedInformation 23 | desktop = 12 24 | caches = 13 25 | applicationSupport = 14 26 | downloads 27 | inputMethods 28 | movies 29 | music 30 | pictures 31 | printerDescription 32 | sharedPublic 33 | preferencePanes 34 | applicationScripts 35 | itemReplacement 36 | allApplications = 100 37 | allLibraries = 101 38 | trash 39 | 40 | proc NSSearchPathForDirectoriesInDomains*(d: NSSearchPathDirectory, domains: set[NSSearchPathDomain], expandTilde: bool): NSArray[NSString] {.importc.} 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Yuriy Glukhov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /tests/timpl_class.nim: -------------------------------------------------------------------------------- 1 | # import darwin/objc/class_impl 2 | import darwin/objc/runtime 3 | import darwin/foundation 4 | 5 | # objcClass MyDelegate of NSObject: 6 | # proc handleEvent(self: MyDelegate, s: NSStream, ev: NSStreamEvent) {.objc: "stream:handleEvent:".} 7 | 8 | var handleCalled = false 9 | 10 | # Declare a class 11 | type 12 | MyDelegate = ptr object of NSObject 13 | 14 | # Implement a method 15 | proc handleEvent(self: MyDelegate, cmd: SEL, s: NSStream, ev: NSStreamEvent) {.cdecl.} = 16 | handleCalled = true 17 | 18 | # Register class implementation 19 | let dClass = allocateClassPair(getClass("NSObject"), "MyDelegate", 0) 20 | doAssert(dClass != nil) 21 | discard addMethod(dClass, sel_registerName("stream:handleEvent:"), handleEvent) 22 | registerClassPair(dClass) 23 | 24 | # Instantiate 25 | let d = cast[MyDelegate](createInstance(dClass, 0)) 26 | 27 | # Test 28 | let s = NSinputStream.withData(NSData.withBytes([1.byte, 2, 3, 4])) 29 | s.setDelegate(d) 30 | s.scheduleInRunLoop(NSRunLoop.current(), NSDefaultRunLoopMode) 31 | s.open() 32 | NSRunLoop.current().runUntilDate(NSDate.withTimeIntervalSinceNow(0.1)) 33 | doAssert(handleCalled) 34 | -------------------------------------------------------------------------------- /.github/workflows/action.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - README.md 7 | pull_request: 8 | paths-ignore: 9 | - README.md 10 | 11 | jobs: 12 | build: 13 | runs-on: ${{ matrix.os }} 14 | strategy: 15 | matrix: 16 | os: 17 | # - macOS-latest 18 | - macOS-13 19 | version: 20 | # - version-2-0 21 | - devel 22 | steps: 23 | - name: Checkout 24 | uses: actions/checkout@v4 25 | 26 | - name: Cache choosenim 27 | id: cache-choosenim 28 | uses: actions/cache@v4 29 | with: 30 | path: ~/.choosenim 31 | key: ${{ runner.os }}-choosenim-${{ matrix.version}} 32 | 33 | - name: Cache nimble 34 | id: cache-nimble 35 | uses: actions/cache@v4 36 | with: 37 | path: ~/.nimble 38 | key: ${{ runner.os }}-nimble-${{ matrix.version}}-${{ hashFiles('*.nimble') }} 39 | 40 | - uses: alaviss/setup-nim@0.1.1 41 | with: 42 | path: 'nim' 43 | version: ${{ matrix.version }} 44 | 45 | - name: Install Packages 46 | run: nimble install -d -y 47 | 48 | - name: test 49 | run: nimble test 50 | -------------------------------------------------------------------------------- /darwin/core_text/ctfont.nim: -------------------------------------------------------------------------------- 1 | import darwin / core_foundation / [cfbase] 2 | import darwin / core_graphics / [cggeometry, cgaffine_transform, cgfont] 3 | 4 | import ctfont_descriptor 5 | 6 | type 7 | CTFont* = ptr object of CFObject 8 | 9 | proc CTFontCreateWithName*(name: CFString, size: CGFloat, transform: ptr CGAffineTransform): CTFont {.importc.} 10 | proc getGlyphsForCharacters*(font: CTFont, characters: ptr UniChar, glyphs: ptr CGGlyph, count: CFIndex): bool {.importc: "CTFontGetGlyphsForCharacters".} 11 | 12 | proc getAscent*(font: CTFont): CGFloat {.importc: "CTFontGetAscent".} 13 | proc getDescent*(font: CTFont): CGFloat {.importc: "CTFontGetDescent".} 14 | proc getSize*(font: CTFont): CGFloat {.importc: "CTFontGetSize".} 15 | 16 | proc getBoundingRectsForGlyphs*(font: CTFont, orientation: CTFontOrientation, glyphs: ptr CGGlyph, boundingRects: ptr CGRect, count: CFIndex): CGRect {.importc: "CTFontGetBoundingRectsForGlyphs".} 17 | 18 | proc getAdvancesForGlyphs*(font: CTFont, orientation: CTFontOrientation, glyphs: ptr CGGlyph, advances: ptr CGSize, count: CFIndex): cdouble {.importc: "CTFontGetAdvancesForGlyphs".} 19 | 20 | proc copyGraphicsFont*(font: CTFont, attributes: CTFontDescriptor): CGFont {.importc: "CTFontCopyGraphicsFont".} 21 | -------------------------------------------------------------------------------- /darwin/core_foundation/cfpreferences.nim: -------------------------------------------------------------------------------- 1 | import cfbase, cfstring 2 | 3 | var 4 | anyApplication {.importc: "kCFPreferencesAnyApplication".}: CFString 5 | currentApplication {.importc: "kCFPreferencesCurrentApplication".}: CFString 6 | anyHost {.importc: "kCFPreferencesAnyHost".}: CFString 7 | currentHost {.importc: "kCFPreferencesCurrentHost".}: CFString 8 | anyUser {.importc: "kCFPreferencesAnyUser".}: CFString 9 | currentUser {.importc: "kCFPreferencesCurrentUser".}: CFString 10 | 11 | template kCFPreferencesAnyApplication*: CFString = 12 | let a = anyApplication; a 13 | template kCFPreferencesCurrentApplication*: CFString = 14 | let a = currentApplication; a 15 | template kCFPreferencesAnyHost*: CFString = 16 | let a = anyHost; a 17 | template kCFPreferencesCurrentHost*: CFString = 18 | let a = currentHost; a 19 | template kCFPreferencesAnyUser*: CFString = 20 | let a = anyUser; a 21 | template kCFPreferencesCurrentUser*: CFString = 22 | let a = currentUser; a 23 | 24 | proc CFPreferencesCopyAppValue*(key, appId: CFString): CFPropertyList {.importc.} 25 | proc CFPreferencesAppSynchronize*(appId: CFString): Boolean {.importc.} 26 | proc CFPreferencesSetAppValue*(key: CFString, value: CFPropertyList, appId: CFString) {.importc.} 27 | -------------------------------------------------------------------------------- /darwin/web_kit/wkwebview.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime, blocks] 2 | import ../core_graphics/cggeometry 3 | import ./wkwebviewconfiguration 4 | import ../foundation/[nsurl, nsurlrequest, nserror] 5 | import ./wknavigation 6 | 7 | when defined(ios): 8 | import ../ui_kit/uiview 9 | type WKWebView* = ptr object of UIView 10 | else: 11 | import ../app_kit/nsview 12 | type WKWebView* = ptr object of NSView 13 | 14 | proc initWithFrame*(self: WKWebView, frame: CGRect): WKWebView {.objc: "initWithFrame:".} 15 | 16 | proc initWithFrameAndConfiguration*(self: WKWebView, frame: CGRect, conf: WKWebViewConfiguration): WKWebView {.objc: "initWithFrame:configuration:".} 17 | 18 | proc loadHTMLString*(self: WKWebView, str: NSString, baseURL: NSURL): WKNavigation {.objc: "loadHTMLString:baseURL:" , discardable.} 19 | 20 | proc loadRequest*(self: WKWebView, request: NSURLRequest): WKNavigation {.objc: "loadRequest:", discardable.} 21 | 22 | proc configuration*(self: WKWebView): WKWebViewConfiguration {.objc: "configuration".} 23 | 24 | proc evaluateJavaScript*(self: WKWebView, javaScriptString: NSString, completionHandler: Block[proc (res: ID; err: NSError)]) {.objc: "evaluateJavaScript:completionHandler:".} 25 | 26 | proc setNavigationDelegate*(s: WKWebview, d: NSObject) {.objc: "setNavigationDelegate:".} 27 | -------------------------------------------------------------------------------- /darwin/app_kit/nsview.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../foundation / [ nsgeometry ] 3 | import ../quartz_core / [ calayer ] 4 | 5 | type 6 | NSView* = ptr object of NSObject 7 | NSAutoresizingMaskOptions* {.size: sizeof(uint).} = enum 8 | NSViewNotSizable = 0 9 | NSViewMinXMargin = 1 10 | NSViewWidthSizable = 2 11 | NSViewMaxXMargin = 4 12 | NSViewMinYMargin = 8 13 | NSViewHeightSizable = 16 14 | NSViewMaxYMargin = 32 15 | 16 | proc convertPointFromView*(v: NSView, point: NSPoint, fromView: NSView): NSPoint {.objc: "convertPoint:fromView:".} 17 | proc convertPointToView*(v: NSView, point: NSPoint, toView: NSView): NSPoint {.objc: "convertPoint:toView:".} 18 | proc convertRectToBacking*(v: NSView, rect: NSRect): NSRect {.objc: "convertRectToBacking:".} 19 | proc frame*(v: NSView): NSRect {.objc: "frame".} 20 | proc bounds*(v: NSView): NSRect {.objc: "bounds".} 21 | proc initWithFrame*(self: NSView, rect: NSRect): NSView {.objc: "initWithFrame:".} 22 | proc addSubview*(self: NSView, sub: NSView) {.objc: "addSubview:".} 23 | proc setAutoresizingMask*(self: NSView, opts: NSAutoresizingMaskOptions) {.objc: "setAutoresizingMask:".} 24 | proc setWantsLayer*(s: NSView, l: bool) {.objc: "setWantsLayer:".} 25 | proc setLayer*(s: NSView, l: CALayer) {.objc: "setLayer:".} 26 | proc setNeedsDisplay*(s: NSView, d: bool) {.objc: "setNeedsDisplay:".} 27 | -------------------------------------------------------------------------------- /darwin/app_kit/nssavepanel.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime, blocks] 2 | import ./nspanel 3 | import ../foundation/[nsurl] 4 | import ./nswindow 5 | 6 | type 7 | NSSavePanel* = ptr object of NSPanel 8 | 9 | proc savePanel*(self: typedesc[NSSavePanel]): NSSavePanel {.objc.} 10 | proc beginWithCompletionHandler*(self: NSSavePanel, handler: Block[proc (r: int)]) {.objc: "beginWithCompletionHandler:".} 11 | proc setCanCreateDirectories*(self: NSSavePanel, b: BOOL) {.objc: "setCanCreateDirectories:".} 12 | proc setCanSelectHiddenExtension*(self: NSSavePanel, b: BOOL) {.objc: "setCanSelectHiddenExtension:".} 13 | proc setShowsHiddenFiles*(self: NSSavePanel, b: BOOL) {.objc: "setShowsHiddenFiles:".} 14 | proc setExtensionHidden*(self: NSSavePanel, b: BOOL) {.objc: "setExtensionHidden:".} 15 | proc setExpanded*(self: NSSavePanel, b: BOOL) {.objc: "setExpanded:".} 16 | proc isExpanded*(self: NSSavePanel, b: BOOL) {.objc.} 17 | proc isExtensionHidden*(self: NSSavePanel, b: BOOL) {.objc.} 18 | proc setNameFieldLabel*(self: NSSavePanel, b: NSString) {.objc: "setNameFieldLabel:".} 19 | proc setNameFieldStringValue*(self: NSSavePanel, b: NSString) {.objc: "setNameFieldStringValue:".} 20 | proc URL*(self: NSSavePanel): NSURL {.objc.} 21 | proc beginSheetModalForWindow*(self: NSSavePanel, window: NSWindow, handler: Block[proc (r: int)]) {.objc: "beginSheetModalForWindow:completionHandler:".} 22 | proc setTreatsFilePackagesAsDirectories*(self: NSSavePanel, b: BOOL) {.objc: "setTreatsFilePackagesAsDirectories:".} 23 | -------------------------------------------------------------------------------- /darwin/core_graphics/cgcontext.nim: -------------------------------------------------------------------------------- 1 | import darwin / core_foundation / [cfbase] 2 | import cgfont, cggeometry, cgaffine_transform 3 | 4 | type 5 | CGContext* = ptr object of CFObject 6 | 7 | proc setFont*(c: CGContext, font: CGFont) {.importc: "CGContextSetFont".} 8 | proc setFontSize*(c: CGContext, size: CGFloat) {.importc: "CGContextSetFontSize".} 9 | 10 | proc setTextPosition*(c: CGContext, x, y: CGFloat) {.importc: "CGContextSetTextPosition".} 11 | proc getTextPosition*(c: CGContext): CGPoint {.importc: "CGContextGetTextPosition".} 12 | 13 | proc setGrayFillColor*(c: CGContext, gray, alpha: CGFloat) {.importc: "CGContextSetGrayFillColor".} 14 | proc setGrayStrokeColor*(c: CGContext, gray, alpha: CGFloat) {.importc: "CGContextSetGrayStrokeColor".} 15 | 16 | proc setRGBFillColor*(c: CGContext, r, g, b, a: CGFloat) {.importc: "CGContextSetRGBFillColor".} 17 | proc setRGBStrokeColor*(c: CGContext, r, g, b, a: CGFloat) {.importc: "CGContextSetRGBStrokeColor".} 18 | 19 | proc setCMYKFillColor*(c: CGContext, cy, m, y, k, a: CGFloat) {.importc: "CGContextSetCMYKFillColor".} 20 | proc setCMYKStrokeColor*(c: CGContext, cy, m, y, k, a: CGFloat) {.importc: "CGContextSetCMYKStrokeColor".} 21 | 22 | proc showGlyphsAtPositions*(c: CGContext, glyphs: ptr CGGlyph, positions: ptr CGPoint, count: csize) {.importc: "CGContextShowGlyphsAtPositions".} 23 | 24 | proc scaleCTM*(c: CGContext, sx, sy: CGFloat) {.importc: "CGContextScaleCTM".} 25 | proc translateCTM*(c: CGContext, tx, ty: CGFloat) {.importc: "CGContextTranslateCTM".} 26 | proc rotateCTM*(c: CGContext, angle: CGFloat) {.importc: "CGContextRotateCTM".} 27 | proc concatCTM*(c: CGContext, transform: CGAffineTransform) {.importc: "CGContextConcatCTM".} 28 | proc getCTM*(c: CGContext): CGAffineTransform {.importc: "CGContextGetCTM".} 29 | -------------------------------------------------------------------------------- /darwin/foundation/nsnumber.nim: -------------------------------------------------------------------------------- 1 | import strutils 2 | import ../objc/runtime 3 | 4 | 5 | type NSNumber* = ptr object of NSObject 6 | 7 | 8 | type NSNumberTypes* = enum 9 | ntChar = "c" 10 | ntInt = "i" 11 | ntShort = "s" 12 | ntLong = "l" 13 | ntLongLong = "q" 14 | ntUnsignedChar = "C" 15 | ntUnsignedInt = "I" 16 | ntUnsignedShort = "S" 17 | ntUnsignedLong = "L" 18 | ntUnsignedLongLong = "Q" 19 | ntFloat = "f" 20 | ntDouble = "d" 21 | ntBool = "B" 22 | 23 | 24 | proc withBool*(t: typedesc[NSNumber], b: bool): NSNumber {.objc: "numberWithBool:".} 25 | proc withDouble*(t: typedesc[NSNumber], f: float64): NSNumber {.objc: "numberWithDouble:".} 26 | proc withFloat*(t: typedesc[NSNumber], f: float32): NSNumber {.objc: "numberWithFloat:".} 27 | proc withInt*(t: typedesc[NSNumber], i: int16): NSNumber {.objc: "numberWithInt:".} 28 | proc withInt*(t: typedesc[NSNumber], i: int32): NSNumber {.objc: "numberWithInt:".} 29 | proc withLong*(t: typedesc[NSNumber], i: int32): NSNumber {.objc: "numberWithLong:".} 30 | proc withLongLong*(t: typedesc[NSNumber], i: int64): NSNumber {.objc: "numberWithLongLong:".} 31 | proc withShort*(t: typedesc[NSNumber], i: int16): NSNumber {.objc: "numberWithShort:".} 32 | 33 | 34 | proc boolValue*(n: NSNumber): bool {.objc.} 35 | proc doubleValue*(n: NSNumber): float64 {.objc.} 36 | proc floatValue*(n: NSNumber): float32 {.objc.} 37 | proc intValue*(n: NSNumber): int32 {.objc.} 38 | proc longValue*(n: NSNumber): int32 {.objc.} 39 | proc longLongValue*(n: NSNumber): int64 {.objc.} 40 | proc shortValue*(n: NSNumber): int16 {.objc.} 41 | proc stringValue*(n: NSNumber): NSString {.objc.} 42 | 43 | proc objCType*(n: NSNumber): cstring {.objc.} 44 | proc numberType*(n: NSNumber): NSNumberTypes = 45 | result = parseEnum[NSNumberTypes]($n.objCType()) 46 | -------------------------------------------------------------------------------- /darwin/app_kit/nsmenu.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ./nsevent 3 | 4 | type 5 | NSMenu* = ptr object of NSObject 6 | NSMenuItem* = ptr object of NSObject 7 | 8 | proc initWithTitle*(self: NSMenu, str: NSString): NSMenu {.objc: "initWithTitle:", discardable.} 9 | 10 | proc initWithTitle*(self: NSMenuItem, str: NSString, action: SEL, key: NSString): NSMenuItem {.objc: "initWithTitle:action:keyEquivalent:", discardable.} 11 | 12 | proc setSubmenu*(self: NSMenuItem, menu: NSMenu) {.objc: "setSubmenu:".} 13 | 14 | proc addItem*(self: NSMenu, item: NSMenuItem) {.objc: "addItem:".} 15 | proc addItem*(self: NSMenu, title: NSString, action: SEL, key: NSString): NSMenuItem {.objc: "addItemWithTitle:action:keyEquivalent:".} 16 | proc numberOfItems*(self: NSMenu): NSInteger {.objc.} 17 | proc itemAtIndex*(self: NSMenu, idx: NSInteger): NSMenuItem {.objc: "itemAtIndex:".} 18 | 19 | 20 | proc setKeyEquivalentModifierMask*(self: NSMenuItem, mask: NSEventModifierFlags) {.objc: "setKeyEquivalentModifierMask:".} 21 | 22 | proc separatorItem*(self: typedesc[NSMenuItem]): NSMenuItem {.objc.} 23 | 24 | proc setTarget*(self: NSMenuItem, target: Id) {.objc: "setTarget:".} 25 | 26 | proc setAction*(self: NSMenuItem, action: SEL) {.objc: "setAction:".} 27 | 28 | proc setEnabled*(self: NSMenuItem, enabled: BOOL) {.objc: "setEnabled:".} 29 | 30 | proc title*(self: NSMenuItem): NSString {.objc: "title".} 31 | 32 | proc setTitle*(self: NSMenuItem, title: NSString) {.objc: "setTitle:".} 33 | 34 | proc setSeparatorItem*(self: NSMenuItem, b: BOOL): NSMenuItem {.objc: "setSeparatorItem:".} 35 | 36 | proc isSeparatorItem*(self: NSMenuItem): BOOL {.objc: "isSeparatorItem".} 37 | 38 | proc keyEquivalent*(self: NSMenuItem): NSString {.objc: "keyEquivalent".} 39 | 40 | proc hasSubmenu*(self: NSMenuItem): BOOL {.objc: "hasSubmenu".} 41 | -------------------------------------------------------------------------------- /darwin/app_kit/nsopengl.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../opengl/[cgltypes] 3 | 4 | type 5 | NSOpenGLPixelFormat* = ptr object of NSObject 6 | NSOpenGLContext* = ptr object of NSObject 7 | 8 | const 9 | NSOpenGLPFAAllRenderers* = 1.uint32 10 | NSOpenGLPFATripleBuffer* = 3.uint32 11 | NSOpenGLPFADoubleBuffer* = 5.uint32 12 | NSOpenGLPFAAuxBuffers* = 7.uint32 13 | NSOpenGLPFAColorSize* = 8.uint32 14 | NSOpenGLPFAAlphaSize* = 11.uint32 15 | NSOpenGLPFADepthSize* = 12.uint32 16 | NSOpenGLPFAStencilSize* = 13.uint32 17 | NSOpenGLPFAAccumSize* = 14.uint32 18 | NSOpenGLPFAMinimumPolicy* = 51.uint32 19 | NSOpenGLPFAMaximumPolicy* = 52.uint32 20 | NSOpenGLPFASampleBuffers* = 55.uint32 21 | NSOpenGLPFASamples* = 56.uint32 22 | NSOpenGLPFAAuxDepthStencil* = 57.uint32 23 | NSOpenGLPFAColorFloat* = 58.uint32 24 | NSOpenGLPFAMultisample* = 59.uint32 25 | NSOpenGLPFASupersample* = 60.uint32 26 | NSOpenGLPFASampleAlpha* = 61.uint32 27 | NSOpenGLPFARendererID* = 70.uint32 28 | NSOpenGLPFANoRecovery* = 72.uint32 29 | NSOpenGLPFAAccelerated* = 73.uint32 30 | NSOpenGLPFAClosestPolicy* = 74.uint32 31 | NSOpenGLPFABackingStore* = 76.uint32 32 | NSOpenGLPFAScreenMask* = 84.uint32 33 | NSOpenGLPFAAllowOfflineRenderers* = 96.uint32 34 | NSOpenGLPFAAcceleratedCompute* = 97.uint32 35 | NSOpenGLPFAOpenGLProfile* = 99.uint32 36 | NSOpenGLPFAVirtualScreenCount* = 128.uint32 37 | 38 | proc initWithAttributes*(self: NSOpenGLPixelFormat, attrs: ptr uint32): NSOpenGLPixelFormat {.objc: "initWithAttributes:".} 39 | proc cglPixelFormatObj*(self: NSOpenGLPixelFormat): CGLPixelFormatObj {.importc: "CGLPixelFormatObj".} 40 | 41 | proc flushBuffer*(self: NSOpenGLContext) {.objc.} 42 | proc makeCurrentContext*(self: NSOpenGLContext) {.objc.} 43 | proc cglContextObj*(self: NSOpenGLContext): CGLContextObj {.objc: "CGLContextObj".} 44 | -------------------------------------------------------------------------------- /tests/tobjc_runtime.nim: -------------------------------------------------------------------------------- 1 | import darwin/objc/runtime 2 | import darwin/foundation/nsgeometry 3 | 4 | type 5 | NSNumber = ptr object of NSObject 6 | NSValue = ptr object of NSObject 7 | 8 | proc valueWithRect(n: typedesc[NSValue], d: NSRect): NSValue {.objc: "valueWithRect:".} 9 | proc valueWithPoint(n: typedesc[NSValue], d: NSPoint): NSValue {.objc: "valueWithPoint:".} 10 | proc rectValue(n: NSValue): NSRect {.objc.} 11 | proc pointValue(n: NSValue): NSPoint {.objc.} 12 | proc description(n: NSValue): NSString {.objc.} 13 | 14 | 15 | proc numberWithDouble(n: typedesc[NSNumber], d: float): NSNumber {.objc: "numberWithDouble:".} 16 | proc numberWithFloat(n: typedesc[NSNumber], d: cfloat): NSNumber {.objc: "numberWithFloat:".} 17 | 18 | proc doubleValue(n: NSNumber): float {.objc: "doubleValue".} 19 | proc floatValue(n: NSNumber): cfloat {.objc: "floatValue".} 20 | 21 | proc UTF8String(n: NSString): cstring {.objc: "UTF8String".} 22 | 23 | 24 | proc alloc*[T](o: typedesc[T]): T {.objc: "alloc".} 25 | 26 | proc initWithUTF8String(o: NSString, str: cstring): NSString {.objc: "initWithUTF8String:".} 27 | 28 | let a = NSString.alloc().initWithUTF8String("This is a test!") 29 | 30 | let n = NSNumber.numberWithDouble(123.456) 31 | let nf = NSNumber.numberWithFloat(123.456) 32 | 33 | doAssert(n.doubleValue > 123 and n.doubleValue < 124) 34 | doAssert(nf.floatValue > 123 and nf.floatValue < 124) 35 | doAssert($a.UTF8String == "This is a test!") 36 | 37 | block: 38 | let v = NSValue.valueWithRect(NSMakeRect(1, 2, 3, 4)) 39 | doAssert($v.description.UTF8String == "NSRect: {{1, 2}, {3, 4}}") 40 | doAssert(v.rectValue == NSMakeRect(1, 2, 3, 4)) 41 | 42 | block: 43 | let v = NSValue.valueWithPoint(NSMakePoint(1, 2)) 44 | doAssert($v.description.UTF8String == "NSPoint: {1, 2}") 45 | doAssert(v.pointValue == NSMakePoint(1, 2)) 46 | 47 | 48 | a.release() 49 | -------------------------------------------------------------------------------- /darwin/foundation/nsstring.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | export NSString 4 | 5 | 6 | type NSStringEncoding* = enum 7 | NSASCIIStringEncoding = 1 8 | NSNEXTSTEPStringEncoding = 2 9 | NSJapaneseEUCStringEncoding = 3 10 | NSUTF8StringEncoding = 4 11 | NSISOLatin1StringEncoding = 5 12 | NSSymbolStringEncoding = 6 13 | NSNonLossyASCIIStringEncoding = 7 14 | NSShiftJISStringEncoding = 8 15 | NSISOLatin2StringEncoding = 9 16 | NSUnicodeStringEncoding = 10 17 | NSWindowsCP1251StringEncoding = 11 18 | NSWindowsCP1252StringEncoding = 12 19 | NSWindowsCP1253StringEncoding = 13 20 | NSWindowsCP1254StringEncoding = 14 21 | NSWindowsCP1250StringEncoding = 15 22 | NSISO2022JPStringEncoding = 21 23 | NSMacOSRomanStringEncoding = 30 24 | NSUTF32StringEncoding = 0x8c000100 25 | NSUTF16BigEndianStringEncoding = 0x90000100 26 | NSUTF16LittleEndianStringEncoding = 0x94000100 27 | NSUTF32BigEndianStringEncoding = 0x98000100 28 | NSUTF32LittleEndianStringEncoding = 0x9c000100 29 | const NSUTF16StringEncoding* = NSStringEncoding.NSUnicodeStringEncoding 30 | 31 | proc UTF8String*(s: NSString): cstring {.objc: "UTF8String".} 32 | 33 | proc isEqualToString*(s1, s2: NSString): bool {.objc: "isEqualToString:".} 34 | 35 | template `==`*(s1, s2: NSString): bool = s1.isEqualToString(s2) 36 | 37 | proc withUTF8String*(n: typedesc[NSString], s: cstring): NSString {.objc: "stringWithUTF8String:".} 38 | proc stringWithNSString*(n: NSString): string = $n.UTF8String 39 | proc stringByAppendingString*(s: NSString, a: NSString): NSString {.objc: "stringByAppendingString:".} 40 | proc hasSuffix*(s: NSString, a: NSString): BOOL {.objc: "hasSuffix:".} 41 | 42 | converter toNSString*(s: string): NSString = NSString.withUTF8String(s) 43 | converter nsstringtostring*(s: NSString): string = stringWithNSString(s) 44 | template `$`*(s: NSString): string = nsstringtostring(s) 45 | 46 | 47 | proc description*(o: NSObject): NSString {.objc.} 48 | template `$`*(o: NSObject): string = stringWithNSString(o.description) 49 | proc `@`*(s: string): NSString = NSString(s) 50 | -------------------------------------------------------------------------------- /darwin/app_kit/nspasteboard.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../foundation 3 | 4 | type 5 | NSPasteboard* = ptr object of NSObject 6 | NSPasteboardItem* = ptr object of NSObject 7 | 8 | proc clearContents*(p: NSPasteboard) {.objc: "clearContents".} 9 | proc writeObjects*(p: NSPasteboard, o: NSArray[NSPasteboardItem]) {.objc: "writeObjects:".} 10 | proc pasteboardItems*(p: NSPasteboard): NSArray[NSPasteboardItem] {.objc: "pasteboardItems".} 11 | proc dataForType*(pi: NSPasteboard, t: NSString): NSData {.objc: "dataForType:".} 12 | 13 | proc types*(pi: NSPasteboardItem): NSArray[NSString] {.objc: "types".} 14 | proc dataForType*(pi: NSPasteboardItem, t: NSString): NSData {.objc: "dataForType:".} 15 | proc setDataForType*(self: NSPasteboardItem, data: NSData, forType: NSString): bool {.objc: "setData:forType:".} 16 | 17 | proc withName*(n: typedesc[NSPasteboard], name: NSString): NSPasteboard {.objc: "pasteboardWithName:".} 18 | 19 | var NSPasteboardNameGeneral* {.importc.} : NSString 20 | var NSPasteboardNameFont* {.importc.} : NSString 21 | var NSPasteboardNameRuler* {.importc.} : NSString 22 | var NSPasteboardNameFind* {.importc.} : NSString 23 | var NSPasteboardNameDrag* {.importc.} : NSString 24 | 25 | var NSPasteboardTypeString* {.importc.} : NSString 26 | var NSPasteboardTypePDF* {.importc.} : NSString 27 | var NSPasteboardTypeTIFF* {.importc.} : NSString 28 | var NSPasteboardTypePNG* {.importc.} : NSString 29 | var NSPasteboardTypeRTF* {.importc.} : NSString 30 | var NSPasteboardTypeRTFD* {.importc.} : NSString 31 | var NSPasteboardTypeHTML* {.importc.} : NSString 32 | var NSPasteboardTypeTabularText* {.importc.} : NSString 33 | var NSPasteboardTypeFont* {.importc.} : NSString 34 | var NSPasteboardTypeRuler* {.importc.} : NSString 35 | var NSPasteboardTypeColor* {.importc.} : NSString 36 | var NSPasteboardTypeSound* {.importc.} : NSString 37 | var NSPasteboardTypeMultipleTextSelection* {.importc.} : NSString 38 | var NSPasteboardTypeFindPanelSearchOptions* {.importc.} : NSString 39 | 40 | # Deprecated 41 | var NSGeneralPboard* {.importc.} : NSString 42 | var NSFontPboard* {.importc.} : NSString 43 | var NSRulerPboard* {.importc.} : NSString 44 | var NSFindPboard* {.importc.} : NSString 45 | var NSDragPboard* {.importc.} : NSString 46 | -------------------------------------------------------------------------------- /darwin/app_kit/nsbutton.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime] 2 | import ../foundation/[nsstring] 3 | import ./nscontrol 4 | import ../app_kit/[nsimage, nsview] 5 | 6 | type 7 | NSButton* = ptr object of NSControl 8 | 9 | # Enum for NSButton type 10 | type 11 | NSButtonType* {.size: sizeof(uint).} = enum 12 | NSButtonTypeMomentaryLight = 0, 13 | NSButtonTypePush = 1, 14 | NSButtonTypeToggle = 2, 15 | NSButtonTypeSwitch = 3, 16 | NSButtonTypeRadio = 4, 17 | NSButtonTypeMomentaryChange = 5, 18 | NSButtonTypeOnOff = 6, 19 | NSButtonTypeAccelerator = 7, 20 | NSButtonTypeMultiLevelAccelerator = 8 21 | 22 | # Enum for NSControlState 23 | type 24 | NSControlStateValue* {.size: sizeof(int).} = enum 25 | NSControlStateValueMixed = -1, 26 | NSControlStateValueOff = 0, 27 | NSControlStateValueOn = 1 28 | 29 | # Button title and image 30 | proc title*(self: NSButton): NSString {.objc: "title".} 31 | proc setTitle*(self: NSButton, title: NSString) {.objc: "setTitle:".} 32 | proc alternateTitle*(self: NSButton): NSString {.objc: "alternateTitle".} 33 | proc setAlternateTitle*(self: NSButton, title: NSString) {.objc: "setAlternateTitle:".} 34 | proc image*(self: NSButton): NSImage {.objc: "image".} 35 | proc setImage*(self: NSButton, image: NSImage) {.objc: "setImage:".} 36 | proc alternateImage*(self: NSButton): NSImage {.objc: "alternateImage".} 37 | proc setAlternateImage*(self: NSButton, image: NSImage) {.objc: "setAlternateImage:".} 38 | 39 | # Button state and type 40 | proc state*(self: NSButton): NSControlStateValue {.objc: "state".} 41 | proc setState*(self: NSButton, state: NSControlStateValue) {.objc: "setState:".} 42 | proc buttonType*(self: NSButton): NSButtonType {.objc: "buttonType".} 43 | proc setButtonType*(self: NSButton, buttonType: NSButtonType) {.objc: "setButtonType:".} 44 | 45 | # Bezel style 46 | proc bezelStyle*(self: NSButton): NSInteger {.objc: "bezelStyle".} 47 | proc setBezelStyle*(self: NSButton, style: NSInteger) {.objc: "setBezelStyle:".} 48 | 49 | # Target-action mechanism 50 | proc setTarget*(self: NSButton, target: NSObject) {.objc: "setTarget:".} 51 | proc setAction*(self: NSButton, action: SEL) {.objc: "setAction:".} 52 | 53 | # Button properties 54 | proc setEnabled*(self: NSButton, enabled: BOOL) {.objc: "setEnabled:".} 55 | proc isEnabled*(self: NSButton): BOOL {.objc: "isEnabled".} 56 | -------------------------------------------------------------------------------- /darwin/core_foundation/cfnumber.nim: -------------------------------------------------------------------------------- 1 | import cfbase 2 | 3 | type 4 | CFNumber* = ptr object of CFPropertyList 5 | CFBoolean* = ptr object of CFPropertyList 6 | 7 | CFNumberType* {.size: sizeof(uint).} = enum 8 | # Fixed-width types 9 | kCFNumberSInt8Type = 1, 10 | kCFNumberSInt16Type = 2, 11 | kCFNumberSInt32Type = 3, 12 | kCFNumberSInt64Type = 4, 13 | kCFNumberFloat32Type = 5, 14 | kCFNumberFloat64Type = 6, # 64-bit IEEE 754 15 | # Basic C types 16 | kCFNumberCharType = 7, 17 | kCFNumberShortType = 8, 18 | kCFNumberIntType = 9, 19 | kCFNumberLongType = 10, 20 | kCFNumberLongLongType = 11, 21 | kCFNumberFloatType = 12, 22 | kCFNumberDoubleType = 13, 23 | # Other 24 | kCFNumberCFIndexType = 14, 25 | kCFNumberNSIntegerType = 15, 26 | kCFNumberCGFloatType = 16, 27 | 28 | var 29 | boolTrue {.importc: "kCFBooleanTrue".}: CFBoolean 30 | boolFalse {.importc: "kCFBooleanFalse".}: CFBoolean 31 | 32 | template kCFBooleanTrue*: CFBoolean = 33 | let a = boolTrue; a 34 | template kCFBooleanFalse*: CFBoolean = 35 | let a = boolFalse; a 36 | 37 | proc CFNumberGetTypeID*(): CFTypeID {.importc.} 38 | proc CFBooleanGetTypeID*(): CFTypeID {.importc.} 39 | 40 | proc CFNumberCreate*(allocator: CFAllocator, typ: CFNumberType, value: pointer): CFNumber {.importc.} 41 | proc CFNumberCreate*(v: int64): CFNumber {.inline.} = 42 | CFNumberCreate(kCFAllocatorDefault, kCFNumberLongLongType, unsafeAddr v) 43 | proc CFNumberCreate*(v: cdouble): CFNumber {.inline.} = 44 | CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, unsafeAddr v) 45 | 46 | proc CFBooleanGetValue(boolean: CFBoolean): Boolean {.importc.} 47 | proc CFNumberIsFloatType(n: CFNumber): Boolean {.importc.} 48 | proc CFNumberGetValue(number: CFNumber, theType: CFNumberType, valuePtr: pointer): Boolean {.importc.} 49 | 50 | proc getType*(number: CFNumber): CFNumberType {.importc: "CFNumberGetType".} 51 | proc value*(b: CFBoolean): bool {.inline.} = bool CFBooleanGetValue(b) 52 | proc isFloatType*(n: CFNumber): bool {.inline.} = bool CFNumberIsFloatType(n) 53 | 54 | proc getFloat*(n: CFNumber): float = 55 | var d: cdouble 56 | discard CFNumberGetValue(n, kCFNumberDoubleType, addr d) 57 | result = d 58 | 59 | proc getInt64*(n: CFNumber): int64 = 60 | discard CFNumberGetValue(n, kCFNumberSInt64Type, addr result) 61 | 62 | proc toCFBoolean*(b: bool): CFBoolean {.inline.} = 63 | if b: kCFBooleanTrue else: kCFBooleanFalse 64 | -------------------------------------------------------------------------------- /darwin/app_kit/nswindow.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | import ../core_graphics/cggeometry 3 | import ../foundation / [ nsgeometry ] 4 | import nsview 5 | import ./nscolor 6 | 7 | type 8 | NSWindow* = ptr object of NSObject 9 | NSWindowStyleMask* {.size: sizeof(uint).} = enum 10 | NSWindowStyleMaskBorderless = 0 11 | NSWindowStyleMaskTitled = 1 shl 0 12 | NSWindowStyleMaskClosable = 1 shl 1 13 | NSWindowStyleMaskMiniaturizable = 1 shl 2 14 | NSWindowStyleMaskResizable = 1 shl 3 15 | NSWindowStyleMaskUtilityWindow = 1 shl 4 16 | NSWindowStyleMaskDocModalWindow = 1 shl 6 17 | NSWindowStyleMaskNonactivatingPanel = 1 shl 7 18 | NSWindowStyleMaskUnifiedTitleAndToolbar = 1 shl 12 19 | NSWindowStyleMaskHUDWindow = 1 shl 13 20 | NSWindowStyleMaskFullScreen = 1 shl 14 21 | NSWindowStyleMaskFullSizeContentView = 1 shl 15 22 | NSBackingStoreType* {.size: sizeof(uint).} = enum 23 | NSBackingStoreBuffered = 2 24 | 25 | proc `or`*(a,b:NSWindowStyleMask):cint = a.cint or b.cint 26 | proc `or`*(a:cint,b:NSWindowStyleMask):cint = a or b.cint 27 | 28 | proc backingScaleFactor(s: NSWindow): CGFloat {.objc: "backingScaleFactor".} 29 | proc scaleFactor*(s: NSWindow): CGFloat = 30 | if s.respondsToSelector("backingScaleFactor"): 31 | result = s.backingScaleFactor() 32 | else: 33 | result = 1 34 | 35 | proc initWithContentRect*(s: NSWindow, contentRect: NSRect, styleMask: NSWindowStyleMask, backing: NSBackingStoreType, `defer`: BOOL): NSWindow {.objc: "initWithContentRect:styleMask:backing:defer:", discardable.} 36 | proc setTitle*(s: NSWindow, title: NSString) {.objc: "setTitle:".} 37 | proc setContentView*(s: NSWindow, view: NSView) {.objc: "setContentView:".} 38 | proc center*(s: NSWindow) {.objc.} 39 | proc frame*(s: NSWindow): NSRect {.objc.} 40 | proc setFrame*(s: NSWindow, rect: NSRect, display: BOOL) {.objc: "setFrame:display:".} 41 | proc contentRectForFrameRect*(s: NSWindow, r: NSRect): NSRect {.objc: "contentRectForFrameRect:".} 42 | proc contentView*(s: NSWindow): NSView {.objc.} 43 | proc orderFrontRegardless*(s: NSWindow) {.objc.} 44 | proc setReleasedWhenClosed*(s: NSWindow, b: BOOL) {.objc: "setReleasedWhenClosed:".} 45 | proc setBackgroundColor*(s: NSWindow, c: NSColor) {.objc: "setBackgroundColor:".} 46 | proc makeKeyAndOrderFront*(s: NSWindow, sender: ID) {.objc: "makeKeyAndOrderFront:".} 47 | proc setDelegate*(s: NSWindow, d: NSObject) {.objc: "setDelegate:".} 48 | -------------------------------------------------------------------------------- /darwin/app_kit/nsalert.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime, blocks] 2 | import ../foundation/[nsstring, nsdictionary, nsarray] 3 | import ../app_kit/[nsimage, nsview, nswindow] 4 | import ./nsbutton 5 | 6 | const 7 | NSModalResponseOK* = 1 8 | NSModalResponseCancel* = 0 9 | NSModalResponseContinue* = -1002 10 | NSModalResponseStop* = -1000 11 | NSModalResponseAbort* = -1001 12 | NSAlertFirstButtonReturn* = 1000 13 | NSAlertSecondButtonReturn* = 1001 14 | NSAlertThirdButtonReturn* = 1002 15 | 16 | type 17 | NSAlert* = ptr object of NSObject 18 | NSAlertStyle* {.size: sizeof(uint).} = enum 19 | NSAlertStyleWarning = 0 20 | NSAlertStyleInformational = 1 21 | NSAlertStyleCritical = 2 22 | 23 | # Properties 24 | proc messageText*(self: NSAlert): NSString {.objc: "messageText".} 25 | proc setMessageText*(self: NSAlert, text: NSString) {.objc: "setMessageText:".} 26 | proc informativeText*(self: NSAlert): NSString {.objc: "informativeText".} 27 | proc setInformativeText*(self: NSAlert, text: NSString) {.objc: "setInformativeText:".} 28 | proc alertStyle*(self: NSAlert): NSAlertStyle {.objc: "alertStyle".} 29 | proc setAlertStyle*(self: NSAlert, style: NSAlertStyle) {.objc: "setAlertStyle:".} 30 | proc showsHelp*(self: NSAlert): BOOL {.objc: "showsHelp".} 31 | proc setShowsHelp*(self: NSAlert, show: BOOL) {.objc: "setShowsHelp:".} 32 | proc helpAnchor*(self: NSAlert): NSString {.objc: "helpAnchor".} 33 | proc setHelpAnchor*(self: NSAlert, anchor: NSString) {.objc: "setHelpAnchor:".} 34 | 35 | # Buttons 36 | proc addButtonWithTitle*(self: NSAlert, title: NSString) {.objc: "addButtonWithTitle:".} 37 | proc buttons*(self: NSAlert): NSArray[NSButton] {.objc: "buttons".} 38 | 39 | # Optional icon 40 | proc setIcon*(self: NSAlert, icon: NSImage) {.objc: "setIcon:".} 41 | proc icon*(self: NSAlert): NSImage {.objc: "icon".} 42 | 43 | # Optional accessory view 44 | proc setAccessoryView*(self: NSAlert, view: NSView) {.objc: "setAccessoryView:".} 45 | proc accessoryView*(self: NSAlert): NSView {.objc: "accessoryView".} 46 | 47 | # Suppression button 48 | proc showsSuppressionButton*(self: NSAlert): BOOL {.objc: "showsSuppressionButton".} 49 | proc setShowsSuppressionButton*(self: NSAlert, show: BOOL) {.objc: "setShowsSuppressionButton:".} 50 | proc suppressionButton*(self: NSAlert): NSButton {.objc: "suppressionButton".} 51 | 52 | # Modal presentation 53 | proc runModal*(self: NSAlert): int {.objc: "runModal", discardable.} 54 | proc beginSheetModalForWindow*(self: NSAlert, window: NSWindow, handler: Block[proc (r: int)]) {.objc: "beginSheetModalForWindow:completionHandler:".} 55 | 56 | # Access user dictionary 57 | proc userInfo*(self: NSAlert): NSDictionary {.objc: "userInfo".} 58 | 59 | # Handling custom help actions 60 | proc setHelpHandler*(self: NSAlert, handler: Block[proc]): void {.objc: "setHelpHandler:".} 61 | -------------------------------------------------------------------------------- /darwin/app_kit/nsapplication.nim: -------------------------------------------------------------------------------- 1 | import ./nsresponder 2 | import ./nsmenu 3 | import ../objc/runtime 4 | import ./nsevent 5 | import ./nswindow 6 | 7 | type 8 | NSApplication* = ptr object of NSResponder 9 | NSApplicationActivationPolicy* {.size: sizeof(uint).} = enum 10 | NSApplicationActivationPolicyRegular 11 | NSApplicationActivationPolicyAccessory 12 | NSApplicationActivationPolicyProhibited 13 | NSModalSession* = ptr object of NSObject 14 | 15 | var NSApp* {.importc.}: pointer 16 | 17 | proc sharedApplication*(self: typedesc[NSApplication]): NSApplication {.objc.} 18 | 19 | proc setMainMenu*(self: NSApplication, menu: NSMenu) {.objc: "setMainMenu:".} 20 | proc mainMenu*(self: NSApplication): NSMenu {.objc.} 21 | 22 | proc setServicesMenu*(self: NSApplication, menu: NSMenu) {.objc: "setServicesMenu:".} 23 | proc servicesMenu*(self: NSApplication): NSMenu {.objc.} 24 | 25 | proc setWindowsMenu*(self: NSApplication, menu: NSMenu) {.objc: "setWindowsMenu:".} 26 | proc windowsMenu*(self: NSApplication): NSMenu {.objc.} 27 | 28 | 29 | proc setActivationPolicy*(self: NSApplication, policy: NSApplicationActivationPolicy): BOOL {.objc: "setActivationPolicy:", discardable.} 30 | 31 | proc activateIgnoringOtherApps*(self: NSApplication, ignoreOtherApps: BOOL) {.objc: "activateIgnoringOtherApps:", deprecated.} 32 | 33 | proc activate*(self: NSApplication) {.objc.} 34 | 35 | proc deactivate*(self: NSApplication) {.objc.} 36 | 37 | proc run*(self: NSApplication) {.objc.} 38 | 39 | proc running*(self: NSApplication): BOOL {.objc.} 40 | 41 | proc finishLaunching*(self: NSApplication) {.objc.} 42 | 43 | proc stop*(self: NSApplication, sender: ID) {.objc: "stop:".} 44 | 45 | proc terminate*(self: NSApplication, sender: ID) {.objc: "terminate:".} 46 | 47 | proc postEvent*(self: NSApplication, event: NSEvent, atStart: BOOL) {.objc: "postEvent:atStart:".} 48 | 49 | # Stop the modal with a specific code 50 | proc stopModalWithCode*(self: NSApplication, code: int) {.objc: "stopModalWithCode:".} 51 | 52 | # Run modal for a specific window 53 | proc runModalForWindow*(self: NSApplication, window: NSWindow): int {.objc: "runModalForWindow:".} 54 | 55 | # End the modal session with a specific return code 56 | proc endModalSession*(self: NSApplication, session: NSModalSession) {.objc: "endModalSession:".} 57 | 58 | # Begin a new modal session for a specific window 59 | proc beginModalSessionForWindow*(self: NSApplication, window: NSWindow): NSModalSession {.objc: "beginModalSessionForWindow:".} 60 | 61 | # Run the modal session 62 | proc runModalSession*(self: NSApplication, session: NSModalSession): int {.objc: "runModalSession:".} 63 | 64 | # Stop a modal session and exit with a return code 65 | proc stopModal*(self: NSApplication) {.objc: "stopModal".} 66 | 67 | # Abort a specific modal session 68 | proc abortModal*(self: NSApplication) {.objc: "abortModal".} 69 | 70 | proc setDelegate*(s: NSApplication, d: NSObject) {.objc: "setDelegate:".} 71 | 72 | proc sendAction*(s: NSApplication, a: SEL, to: ID, `from`: ID): BOOL {.objc: "sendAction:to:from:".} -------------------------------------------------------------------------------- /darwin/app_kit/nscursor.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | type NSCursor* = ptr object of NSObject 4 | 5 | proc arrowCursor(n: typedesc[NSCursor]): NSCursor {.objc: "arrowCursor".} 6 | proc IBeamCursor(n: typedesc[NSCursor]): NSCursor {.objc: "IBeamCursor".} 7 | proc crosshairCursor(n: typedesc[NSCursor]): NSCursor {.objc: "crosshairCursor".} 8 | proc closedHandCursor(n: typedesc[NSCursor]): NSCursor {.objc: "closedHandCursor".} 9 | proc pointingHandCursor(n: typedesc[NSCursor]): NSCursor {.objc: "pointingHandCursor".} 10 | proc resizeLeftCursor(n: typedesc[NSCursor]): NSCursor {.objc: "resizeLeftCursor".} 11 | proc resizeRightCursor(n: typedesc[NSCursor]): NSCursor {.objc: "resizeRightCursor".} 12 | proc resizeLeftRightCursor(n: typedesc[NSCursor]): NSCursor {.objc: "resizeLeftRightCursor".} 13 | proc resizeUpCursor(n: typedesc[NSCursor]): NSCursor {.objc: "resizeUpCursor".} 14 | proc resizeDownCursor(n: typedesc[NSCursor]): NSCursor {.objc: "resizeDownCursor".} 15 | proc resizeUpDownCursor(n: typedesc[NSCursor]): NSCursor {.objc: "resizeUpDownCursor".} 16 | proc disappearingItemCursor(n: typedesc[NSCursor]): NSCursor {.objc: "disappearingItemCursor".} 17 | proc IBeamCursorForVerticalLayout(n: typedesc[NSCursor]): NSCursor {.objc: "IBeamCursorForVerticalLayout".} 18 | proc operationNotAllowedCursor(n: typedesc[NSCursor]): NSCursor {.objc: "operationNotAllowedCursor".} 19 | proc dragLinkCursor(n: typedesc[NSCursor]): NSCursor {.objc: "dragLinkCursor".} 20 | proc dragCopyCursor(n: typedesc[NSCursor]): NSCursor {.objc: "dragCopyCursor".} 21 | proc contextualMenuCursor(n: typedesc[NSCursor]): NSCursor {.objc: "contextualMenuCursor".} 22 | 23 | proc arrowCursor*(): NSCursor {.inline.} = NSCursor.arrowCursor() 24 | proc IBeamCursor*(): NSCursor {.inline.} = NSCursor.IBeamCursor() 25 | proc crosshairCursor*(): NSCursor {.inline.} = NSCursor.crosshairCursor() 26 | proc closedHandCursor*(): NSCursor {.inline.} = NSCursor.closedHandCursor() 27 | proc pointingHandCursor*(): NSCursor {.inline.} = NSCursor.pointingHandCursor() 28 | proc resizeLeftCursor*(): NSCursor {.inline.} = NSCursor.resizeLeftCursor() 29 | proc resizeRightCursor*(): NSCursor {.inline.} = NSCursor.resizeRightCursor() 30 | proc resizeLeftRightCursor*(): NSCursor {.inline.} = NSCursor.resizeLeftRightCursor() 31 | proc resizeUpCursor*(): NSCursor {.inline.} = NSCursor.resizeUpCursor() 32 | proc resizeDownCursor*(): NSCursor {.inline.} = NSCursor.resizeDownCursor() 33 | proc resizeUpDownCursor*(): NSCursor {.inline.} = NSCursor.resizeUpDownCursor() 34 | proc disappearingItemCursor*(): NSCursor {.inline.} = NSCursor.disappearingItemCursor() 35 | proc IBeamCursorForVerticalLayout*(): NSCursor {.inline.} = NSCursor.IBeamCursorForVerticalLayout() 36 | proc operationNotAllowedCursor*(): NSCursor {.inline.} = NSCursor.operationNotAllowedCursor() 37 | proc dragLinkCursor*(): NSCursor {.inline.} = NSCursor.dragLinkCursor() 38 | proc dragCopyCursor*(): NSCursor {.inline.} = NSCursor.dragCopyCursor() 39 | proc contextualMenuCursor*(): NSCursor {.inline.} = NSCursor.contextualMenuCursor() 40 | 41 | proc setCurrent*(c: NSCursor) {.objc: "set".} 42 | -------------------------------------------------------------------------------- /darwin/foundation/nsarray.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime, blocks] 2 | 3 | type 4 | NSArrayAbstract* = ptr object of NSObject 5 | NSMutableArrayAbstract* = ptr object of NSArrayAbstract 6 | NSArray*[T] = ptr object of NSArrayAbstract 7 | NSMutableArray*[T] = ptr object of NSArray[T] 8 | 9 | proc objcClass(t: typedesc[NSArrayAbstract]): auto {.inline.} = objcClass("NSArray") 10 | proc objcClass(t: typedesc[NSMutableArrayAbstract]): auto {.inline.} = objcClass("NSMutableArray") 11 | 12 | proc objectAtIndex*(a: NSArrayAbstract, i: int): NSObject {.objc: "objectAtIndex:".} 13 | proc withObjectsAndCount(n: typedesc[NSArrayAbstract], objs: pointer, count: int): NSArrayAbstract {.objc: "arrayWithObjects:count:".} 14 | proc withObjectsAndCount(n: typedesc[NSMutableArrayAbstract], objs: pointer, count: int): NSArrayAbstract {.objc: "arrayWithObjects:count:".} 15 | proc withCapacity(n: typedesc[NSMutableArrayAbstract], c: int): NSArrayAbstract {.objc: "arrayWithCapacity:".} 16 | proc arrayWithObjects*[T](objs: varargs[T]): NSArray[T] = 17 | cast[NSArray[T]](NSArrayAbstract.withObjectsAndCount(cast[ptr T](objs), objs.len)) 18 | 19 | proc newMutableArrayAbstract(n: typedesc[NSMutableArrayAbstract]): NSMutableArrayAbstract {.objc: "new".} 20 | 21 | proc newMutableArray*[T](): NSMutableArray[T] {.inline.} = 22 | cast[NSMutableArray[T]](newMutableArrayAbstract(NSMutableArrayAbstract)) 23 | 24 | proc mutableArrayWithCapacity*[T](capacity: int): NSMutableArray[T] {.inline.} = 25 | cast[NSMutableArray[T]](withCapacity(NSMutableArrayAbstract, capacity)) 26 | 27 | proc mutableArrayWithObjects*[T](objs: varargs[T]): NSMutableArray[T] {.inline.} = 28 | cast[NSMutableArray[T]](NSMutableArrayAbstract.withObjectsAndCount(unsafeAddr objs[0], objs.len)) 29 | 30 | proc addObject*(a: NSMutableArrayAbstract, o: NSObject) {.objc: "addObject:".} 31 | 32 | proc add*[T](a: NSMutableArray[T], o: T) {.inline.} = 33 | addObject(cast[NSMutableArrayAbstract](a), o) 34 | 35 | proc count*(a: NSArrayAbstract): int {.objc: "count".} 36 | proc len*(a: NSArrayAbstract): int {.inline.} = a.count 37 | 38 | proc setObject(a: NSMutableArrayAbstract, v: NSObject, idx: int) {.objc: "setObject:atIndexedSubscript:".} 39 | 40 | proc `[]`*[T](a: NSArray[T], idx: int): T {.inline.} = cast[T](objectAtIndex(a, idx)) 41 | proc `[]=`*[T](a: NSMutableArray[T], idx: int, v: T) {.inline.} = 42 | setObject(cast[NSMutableArrayAbstract](a), v, idx) 43 | 44 | iterator items*[T](a: NSArray[T]): T = 45 | let ln = a.len 46 | for i in 0 ..< ln: yield a[i] 47 | 48 | proc enumerateObjectsUsingBlockAbstract(a: NSArrayAbstract, blk: Block[proc(o: NSObject, idx: uint, stop: var bool)]) {.objc: "enumerateObjectsUsingBlock:".} 49 | 50 | proc enumerateObjectsUsingBlock*[T](a: NSArray[T], b: Block[proc(o: T, idx: uint, stop: var bool)]) {.inline.} = 51 | a.enumerateObjectsUsingBlockAbstract(cast[Block[proc(o: NSObject, idx: uint, stop: var bool)]](b)) 52 | 53 | proc enumerateObjectsUsingBlock*[T](a: NSArray[T], b: proc(o: T, idx: uint, stop: var bool)) = 54 | let b = toBlock(b) 55 | a.enumerateObjectsUsingBlock(b) 56 | b.release() 57 | -------------------------------------------------------------------------------- /darwin/foundation/nsdictionary.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | import nsarray 4 | 5 | 6 | type 7 | NSDictionaryAbstract* = ptr object of NSObject 8 | NSMutableDictionaryAbstract* = ptr object of NSDictionaryAbstract 9 | NSDictionary*[K, V] = ptr object of NSDictionaryAbstract 10 | NSMutableDictionary*[K, V] = ptr object of NSDictionary[K, V] 11 | 12 | 13 | proc objcClass(t: typedesc[NSDictionaryAbstract]): auto {.inline.} = objcClass("NSDictionary") 14 | proc objcClass(t: typedesc[NSMutableDictionaryAbstract]): auto {.inline.} = objcClass("NSMutableDictionary") 15 | 16 | proc newDictionaryAbstract[T](t: typedesc[T]): T {.objc: "new".} 17 | proc newDictionary*[K, V](): NSDictionary[K, V] = cast[NSDictionary[K, V]](NSDictionaryAbstract.newDictionaryAbstract()) 18 | proc newMutableDictionary*[K, V](): NSMutableDictionary[K, V] = cast[NSMutableDictionary[K, V]](NSMutableDictionaryAbstract.newDictionaryAbstract()) 19 | 20 | proc withDictionary[T](t: typedesc[T], dict: NSDictionaryAbstract): T {.objc: "dictionaryWithDictionary:".} 21 | proc dictionaryWithDictionary*[K, V](dict: NSDictionary[K, V]): NSDictionary[K, V] = cast[NSDictionary[K, V]](NSDictionaryAbstract.withDictionary(dict)) 22 | proc mutableDictionaryWithDictionary*[K, V](dict: NSDictionary[K, V]): NSMutableDictionary[K, V] = cast[NSMutableDictionary[K, V]](NSMutableDictionaryAbstract.withDictionary(dict)) 23 | 24 | proc allKeys(d: NSDictionaryAbstract): NSArrayAbstract {.objc.} 25 | proc allKeys*[K, V](d: NSDictionary[K, V]): NSArray[K] = cast[NSArray[K]](d.NSDictionaryAbstract.allKeys()) 26 | iterator keys*[K, V](d: NSDictionary[K, V]): K = 27 | for key in d.allKeys(): 28 | yield key 29 | 30 | proc allValues(d: NSDictionaryAbstract): NSArrayAbstract {.objc.} 31 | proc allValues*[K, V](d: NSDictionary[K, V]): NSArray[V] = cast[NSArray[V]](d.NSDictionaryAbstract.allValues()) 32 | iterator values*[K, V](d: NSDictionary[K, V]): K = 33 | for value in d.allValues(): 34 | yield value 35 | 36 | proc objectForKey(d: NSDictionaryAbstract, k: NSObject): NSObject {.objc: "objectForKey:".} 37 | proc objectForKey*[K, V](d: NSDictionary[K, V], k: K): V = cast[V](d.NSDictionaryAbstract.objectForKey(k)) 38 | 39 | proc `[]`*[K, V](d: NSDictionary[K, V], k: K): V = d.objectForKey(k) 40 | proc hasKey*[K, V](d: NSDictionary[K, V], k: K): bool = not d[k].isNil 41 | 42 | iterator pairs*[K, V](d: NSDictionary[K, V]): (K, V) = 43 | for k in d.allKeys(): 44 | yield (k, d[k]) 45 | 46 | proc count*(a: NSDictionaryAbstract): int {.objc: "count".} 47 | proc len*(a: NSDictionaryAbstract): int {.inline.} = a.count 48 | 49 | proc setObject(d: NSMutableDictionaryAbstract, o: NSObject, k: NSObject) {.objc: "setObject:forKey:".} 50 | proc setObject*[K, V](d: NSMutableDictionary[K, V], o: V, k: K) = cast[NSMutableDictionaryAbstract](d).setObject(o, k) 51 | proc `[]=`*[K, V](d: NSMutableDictionary[K, V], k: K, o: V) = d.setObject(o, k) 52 | 53 | proc removeObject[K, V](d: NSMutableDictionaryAbstract, k: K) {.objc: "removeObjectForKey:".} 54 | proc del*[K, V](d: NSMutableDictionary[K, V], k: K) = cast[NSMutableDictionaryAbstract](d).removeObject(k) 55 | -------------------------------------------------------------------------------- /darwin/core_services/fsstream.nim: -------------------------------------------------------------------------------- 1 | import ../core_foundation/[cfbase, cfarray, cfrunloop] 2 | 3 | type 4 | FSEventStream* = ptr object 5 | FSEventStreamEventId* = distinct culonglong 6 | 7 | FSEventStreamCreateFlags* = distinct cuint 8 | FSEventStreamCreateFlag* {.pure, size: sizeof(cuint).} = enum 9 | UseCFTypes 10 | NoDefer 11 | WatchRoot 12 | IgnoreSelf 13 | FileEvents 14 | MarkSelf 15 | FullHistory 16 | UseExtendedData 17 | WithDocID 18 | 19 | FSEventStreamEventFlags* = distinct cuint 20 | FSEventStreamEventFlag* {.pure, size: sizeof(cuint).} = enum 21 | MustScanSubDirs = 0 # 0x00000001 (1 << 0) 22 | UserDropped = 1 # 0x00000002 (1 << 1) 23 | KernelDropped = 2 # 0x00000004 (1 << 2) 24 | EventIdsWrapped = 3 # 0x00000008 (1 << 3) 25 | HistoryDone = 4 # 0x00000010 (1 << 4) 26 | RootChanged = 5 # 0x00000020 (1 << 5) 27 | Mount = 6 # 0x00000040 (1 << 6) 28 | Unmount = 7 # 0x00000080 (1 << 7) 29 | ItemCreated = 8 # 0x00000100 (1 << 8) 30 | ItemRemoved = 9 # 0x00000200 (1 << 9) 31 | ItemInodeMetaMod = 10 # 0x00000400 (1 << 10) 32 | ItemRenamed = 11 # 0x00000800 (1 << 11) 33 | ItemModified = 12 # 0x00001000 (1 << 12) 34 | ItemFinderInfoMod = 13 # 0x00002000 (1 << 13) 35 | ItemChangeOwner = 14 # 0x00004000 (1 << 14) 36 | ItemXattrMod = 15 # 0x00008000 (1 << 15) 37 | ItemIsFile = 16 # 0x00010000 (1 << 16) 38 | ItemIsDir = 17 # 0x00020000 (1 << 17) 39 | ItemIsSymlink = 18 # 0x00040000 (1 << 18) 40 | OwnEvent = 19 # 0x00080000 (1 << 19) 41 | ItemIsHardlink = 20 # 0x00100000 (1 << 20) 42 | ItemIsLastHardlink = 21 # 0x00200000 (1 << 21) 43 | ItemCloned = 22 # 0x00400000 (1 << 22) 44 | 45 | FSEventStreamContext* {.bycopy, final.} = object 46 | version*: CFIndex 47 | info*: pointer 48 | retain*: pointer 49 | release*: pointer 50 | copyDescription*: pointer 51 | 52 | FSEventStreamCallback* = proc (streamRef: FSEventStream, clientCallBackInfo: pointer, numEvents: csize_t, 53 | eventPaths: pointer, eventFlags: ptr FSEventStreamEventFlags, 54 | eventIds: ptr FSEventStreamEventId) {.cdecl.} 55 | 56 | const 57 | kFSEventStreamEventIdSinceNow* = 0xFFFFFFFFFFFFFFFF'u64 58 | 59 | kFSEventStreamCreateFlagNone*: set[FSEventStreamCreateFlag] = {} 60 | kFSEventStreamEventFlagNone*: set[FSEventStreamEventFlag] = {} 61 | 62 | # FSEvents Functions 63 | proc FSEventStreamCreate*( 64 | allocator: CFAllocator, 65 | callback: FSEventStreamCallback, 66 | context: ptr FSEventStreamContext, 67 | pathsToWatch: CFArray[CFString], 68 | sinceWhen: FSEventStreamEventId, 69 | latency: CFTimeInterval, 70 | flags: FSEventStreamCreateFlags 71 | ): FSEventStream {.importc.} ## Function to create FSStreams 72 | ## Note that `ptr FSEventStreamEventFlags` is actually a 73 | ## `set[FSEventStreamEventFlag]`, but I left them raw 74 | 75 | proc scheduleWithRunLoop*( 76 | streamRef: FSEventStream, 77 | runLoop: CFRunLoop, 78 | runLoopMode: CFRunLoopMode 79 | ) {.importc: "FSEventStreamScheduleWithRunLoop", deprecated: "use setDispatchQueue instead".} 80 | 81 | proc start*(streamRef: FSEventStream): bool {.importc: "FSEventStreamStart".} 82 | proc stop*(streamRef: FSEventStream) {.importc: "FSEventStreamStop".} 83 | proc invalidate*(streamRef: FSEventStream) {.importc: "FSEventStreamInvalidate".} 84 | proc retain*(streamRef: FSEventStream) {.importc: "FSEventStreamRetain".} 85 | proc release*(streamRef: FSEventStream) {.importc: "FSEventStreamRelease".} 86 | -------------------------------------------------------------------------------- /darwin/app_kit/nsimage.nim: -------------------------------------------------------------------------------- 1 | 2 | import ../objc/runtime 3 | import ../foundation/nsstring 4 | import ../foundation/nsurl 5 | import ../foundation/nsgeometry 6 | 7 | type 8 | NSImage* = ptr object of NSObject 9 | NSBitmapImageFileType* = enum 10 | NSBitmapImageFileTypeTIFF 11 | NSBitmapImageFileTypeBMP 12 | NSBitmapImageFileTypeGIF 13 | NSBitmapImageFileTypeJPEG 14 | NSBitmapImageFileTypePNG 15 | NSBitmapImageFileTypeJPEG2000 16 | NSBitmapImageRep* = ptr object of NSObject 17 | NSImageRep* = ptr object of NSObject 18 | NSImageName* = NSString 19 | 20 | proc initWithContentsOfFile*(self: NSImage, fileName: NSString): NSImage {.objc: "initWithContentsOfFile:".} 21 | proc initWithContentsOfURL*(self: NSImage, url: NSURL): NSImage {.objc: "initWithContentsOfURL:".} 22 | proc imageNamed*(s: typedesc[NSImage]; name: NSString): NSImage {.objc: "imageNamed:".} 23 | proc setSize*(s: NSImage; size: NSSize) {.objc: "setSize:".} 24 | 25 | var 26 | NSImageNameQuickLookTemplate* {.importc.} : NSString 27 | NSImageNameUser* {.importc.} : NSString 28 | NSImageNameUserGroup* {.importc.} : NSString 29 | NSImageNameEveryone* {.importc.} : NSString 30 | NSImageNameUserAccounts* {.importc.} : NSString 31 | NSImageNameLockLockedTemplate* {.importc.} : NSString 32 | NSImageNameLockUnlockedTemplate* {.importc.} : NSString 33 | NSImageNameGoLeftTemplate* {.importc.} : NSString 34 | NSImageNameGoRightTemplate* {.importc.} : NSString 35 | NSImageNameAddTemplate* {.importc.} : NSString 36 | NSImageNameRemoveTemplate* {.importc.} : NSString 37 | NSImageNameActionTemplate* {.importc.} : NSString 38 | NSImageNameSmartBadgeTemplate* {.importc.} : NSString 39 | NSImageNameIconViewTemplate* {.importc.} : NSString 40 | NSImageNameListViewTemplate* {.importc.} : NSString 41 | NSImageNameColumnViewTemplate* {.importc.} : NSString 42 | NSImageNameFlowViewTemplate* {.importc.} : NSString 43 | NSImageNamePathTemplate* {.importc.} : NSString 44 | NSImageNameInvalidDataFreestandingTemplate* {.importc.} : NSString 45 | NSImageNameLockLocked* {.importc.} : NSString 46 | NSImageNameLockUnlocked* {.importc.} : NSString 47 | NSImageNameStopProgressTemplate* {.importc.} : NSString 48 | NSImageNameStopProgressFreestandingTemplate* {.importc.} : NSString 49 | NSImageNameRefreshTemplate* {.importc.} : NSString 50 | NSImageNameRefreshFreestandingTemplate* {.importc.} : NSString 51 | NSImageNameBonjour* {.importc.} : NSString 52 | NSImageNameComputer* {.importc.} : NSString 53 | NSImageNameFolderBurnable* {.importc.} : NSString 54 | NSImageNameFolderSmart* {.importc.} : NSString 55 | NSImageNameFolder* {.importc.} : NSString 56 | NSImageNameNetwork* {.importc.} : NSString 57 | NSImageNameMobileMe* {.importc.} : NSString 58 | NSImageNameMultipleDocuments* {.importc.} : NSString 59 | NSImageNamePreferencesGeneral* {.importc.} : NSString 60 | NSImageNameAdvanced* {.importc.} : NSString 61 | NSImageNameInfo* {.importc.} : NSString 62 | NSImageNameFontPanel* {.importc.} : NSString 63 | NSImageNameColorPanel* {.importc.} : NSString 64 | NSImageNameUserGuest* {.importc.} : NSString 65 | NSImageNameMenuOnStateTemplate* {.importc.} : NSString 66 | NSImageNameMenuMixedStateTemplate* {.importc.} : NSString 67 | NSImageNameApplicationIcon* {.importc.} : NSString 68 | NSImageNameTrashEmpty* {.importc.} : NSString 69 | NSImageNameTrashFull* {.importc.} : NSString 70 | NSImageNameHomeTemplate* {.importc.} : NSString 71 | NSImageNameBookmarksTemplate* {.importc.} : NSString 72 | NSImageNameCaution* {.importc.} : NSString 73 | NSImageNameStatusAvailable* {.importc.} : NSString 74 | NSImageNameStatusPartiallyAvailable* {.importc.} : NSString 75 | NSImageNameStatusUnavailable* {.importc.} : NSString 76 | NSImageNameStatusNone* {.importc.} : NSString 77 | NSImageNameShareTemplate* {.importc.} : NSString 78 | NSImageNamePathArrow* {.importc.} : NSString 79 | -------------------------------------------------------------------------------- /darwin/app_kit/nsevent.nim: -------------------------------------------------------------------------------- 1 | import ../objc/[runtime, blocks] 2 | import ../core_graphics/cggeometry 3 | import ../foundation/[nsgeometry, nsstring, nsdate] 4 | import ./nseventmask 5 | 6 | type 7 | NSEvent* = ptr object of NSObject 8 | 9 | NSEventKind* = enum 10 | NSLeftMouseDown = 1, 11 | NSLeftMouseUp = 2, 12 | NSRightMouseDown = 3, 13 | NSRightMouseUp = 4, 14 | NSMouseMoved = 5, 15 | NSLeftMouseDragged = 6, 16 | NSRightMouseDragged = 7, 17 | NSMouseEntered = 8, 18 | NSMouseExited = 9, 19 | NSKeyDown = 10, 20 | NSKeyUp = 11, 21 | NSFlagsChanged = 12, 22 | NSAppKitDefined = 13, 23 | NSSystemDefined = 14, 24 | NSApplicationDefined = 15, 25 | NSPeriodic = 16, 26 | NSCursorUpdate = 17, 27 | NSEventTypeRotate = 18, 28 | NSEventTypeBeginGesture = 19, 29 | NSEventTypeEndGesture = 20 30 | NSScrollWheel = 22, 31 | NSTabletPoint = 23, 32 | NSTabletProximity = 24, 33 | NSOtherMouseDown = 25, 34 | NSOtherMouseUp = 26, 35 | NSOtherMouseDragged = 27 36 | NSEventTypeGesture = 29, 37 | NSEventTypeMagnify = 30, 38 | NSEventTypeSwipe = 31, 39 | NSEventTypeSmartMagnify = 32, 40 | NSEventTypeQuickLook = 33 41 | NSEventTypePressure = 34 42 | NSEventModifierFlags* {.size: sizeof(uint).} = enum 43 | NSEventModifierFlagCapsLock = 1 shl 16 44 | NSEventModifierFlagShift = 1 shl 17 45 | NSEventModifierFlagControl = 1 shl 18 46 | NSEventModifierFlagOption = 1 shl 19 47 | NSEventModifierFlagCommand = 1 shl 20 48 | NSEventModifierFlagNumericPad = 1 shl 21 49 | NSEventModifierFlagHelp = 1 shl 22 50 | NSEventModifierFlagFunction = 1 shl 23 51 | NSEventModifierFlagDeviceIndependentFlagsMask = 0xffff0000.uint 52 | 53 | proc kind*(e: NSEvent): NSEventKind {.objc: "type".} 54 | proc locationInWindow*(e: NSEvent): NSPoint {.objc: "locationInWindow".} 55 | 56 | # Timestamp of the event 57 | proc timestamp*(self: NSEvent): NSTimeInterval {.objc: "timestamp".} 58 | 59 | # Modifier flags (Shift, Control, Command, etc.) 60 | proc modifierFlags*(self: NSEvent): NSEventModifierFlags {.objc: "modifierFlags".} 61 | 62 | # Event window number 63 | proc windowNumber*(self: NSEvent): NSInteger {.objc: "windowNumber".} 64 | 65 | # Get the characters for a key event 66 | proc characters*(self: NSEvent): NSString {.objc: "characters".} 67 | 68 | # Get the key code for a key event 69 | proc keyCode*(self: NSEvent): uint16 {.objc: "keyCode".} 70 | 71 | # Mouse button number (for mouse-related events) 72 | proc buttonNumber*(self: NSEvent): NSInteger {.objc: "buttonNumber".} 73 | 74 | # Delta of scrolling for scroll wheel events 75 | proc scrollingDeltaX*(self: NSEvent): CGFloat {.objc: "scrollingDeltaX".} 76 | proc scrollingDeltaY*(self: NSEvent): CGFloat {.objc: "scrollingDeltaY".} 77 | 78 | # Check if the event is a key repeat 79 | proc isARepeat*(self: NSEvent): BOOL {.objc: "isARepeat".} 80 | 81 | # Check if the event is a tablet event 82 | proc isTabletEvent*(self: NSEvent): BOOL {.objc: "isTabletPointingDevice".} 83 | 84 | # Class methods for event creation (mouse event example) 85 | proc mouseEventWithType*(self: typedesc[NSEvent], eventType: NSEventKind, location: CGPoint, modifierFlags: NSUInteger, 86 | timestamp: NSTimeInterval, windowNumber: NSInteger, 87 | buttonNumber: NSInteger, clickCount: NSInteger, pressure: CGFloat): NSEvent {.objc: "mouseEventWithType:location:modifierFlags:timestamp:windowNumber:buttonNumber:clickCount:pressure:".} 88 | 89 | proc charactersIgnoringModifiers*(self: NSEvent): NSString {.objc: "charactersIgnoringModifiers".} 90 | proc addLocalMonitorForEventsMatchingMask*(self: typedesc[NSEvent], mask: NSEventMask, handler: Block[proc (e: NSEvent): NSEvent]):ID {.objc: "addLocalMonitorForEventsMatchingMask:handler:", discardable.} 91 | -------------------------------------------------------------------------------- /darwin/core_foundation/cfdictionary.nim: -------------------------------------------------------------------------------- 1 | import cfbase, cfstring, cfpropertylist 2 | 3 | type 4 | CFAbstractDictionary = ptr object of CFPropertyList # CFDictionary 5 | CFAbstractMutableDictionary = ptr object of CFAbstractDictionary # CFMutableDictionary 6 | 7 | CFDictionary*[K, V] = ptr object of CFAbstractDictionary 8 | CFMutableDictionary*[K, V] = ptr object of CFDictionary[K, V] 9 | 10 | CFDictionaryRetainCallBack* = proc(allocator: CFAllocator, value: pointer): pointer {.cdecl.} 11 | CFDictionaryReleaseCallBack* = proc(allocator: CFAllocator, value: pointer) {.cdecl.} 12 | CFDictionaryCopyDescriptionCallBack* = proc(value: pointer): CFString {.cdecl.} 13 | CFDictionaryEqualCallBack* = proc(value1, value2: pointer): Boolean {.cdecl.} 14 | CFDictionaryHashCallBack* = proc(value: pointer): CFHashCode {.cdecl.} 15 | 16 | CFDictionaryKeyCallbacks* {.byref.} = object 17 | version*: CFIndex 18 | retain*: CFDictionaryRetainCallBack 19 | release*: CFDictionaryReleaseCallBack 20 | copyDescription*: CFDictionaryCopyDescriptionCallBack 21 | equal*: CFDictionaryEqualCallBack 22 | hash*: CFDictionaryHashCallBack 23 | 24 | CFDictionaryValueCallBacks* {.byref.} = object 25 | version*: CFIndex 26 | retain*: CFDictionaryRetainCallBack 27 | release*: CFDictionaryReleaseCallBack 28 | copyDescription*: CFDictionaryCopyDescriptionCallBack 29 | equal*: CFDictionaryEqualCallBack 30 | 31 | var 32 | cfTypeKeyCallbacks {.importc: "kCFTypeDictionaryKeyCallBacks".}: CFDictionaryKeyCallBacks 33 | cfCopyStringKeyCallbacks {.importc: "kCFCopyStringDictionaryKeyCallBacks".}: CFDictionaryKeyCallBacks 34 | cfTypeValueCallbacks {.importc: "kCFTypeDictionaryValueCallBacks".}: CFDictionaryValueCallBacks 35 | 36 | template kCFTypeDictionaryKeyCallBacks*: CFDictionaryKeyCallBacks = 37 | let a = cfTypeKeyCallbacks; a 38 | template kCFCopyStringDictionaryKeyCallBacks*: CFDictionaryKeyCallBacks = 39 | let a = cfCopyStringKeyCallbacks; a 40 | template kCFTypeDictionaryValueCallBacks*: CFDictionaryValueCallBacks = 41 | let a = cfTypeValueCallbacks; a 42 | 43 | proc CFDictionaryGetTypeID*(): CFTypeID {.importc.} 44 | 45 | proc CFDictionaryCreateMutableAbstract(allocator: CFAllocator, capacity: CFIndex, keyCallBacks: CFDictionaryKeyCallBacks, valueCallBacks: CFDictionaryValueCallBacks): CFAbstractMutableDictionary {.importc: "CFDictionaryCreateMutable".} 46 | 47 | proc CFDictionaryCreateMutable*[K, V](allocator: CFAllocator, capacity: CFIndex, keyCallBacks: CFDictionaryKeyCallBacks, valueCallBacks: CFDictionaryValueCallBacks): CFMutableDictionary[K, V] {.inline.} = 48 | cast[CFMutableDictionary[K, V]](CFDictionaryCreateMutableAbstract(allocator, capacity, keyCallBacks, valueCallBacks)) 49 | 50 | proc CFDictionaryGetCount(d: CFAbstractDictionary): int {.importc.} 51 | 52 | proc CFDictionaryGetValue(d: CFAbstractDictionary, key: pointer): pointer {.importc.} 53 | 54 | proc CFDictionarySetValue(d: CFAbstractMutableDictionary, k, v: pointer) {.importc.} 55 | 56 | proc `[]`*[K, V](d: CFDictionary[K, V], k: K): V {.inline.} = 57 | cast[V](CFDictionaryGetValue(d, cast[pointer](k))) 58 | 59 | proc `[]=`*[K, V](d: CFMutableDictionary[K, V], k: K, v: V) {.inline.} = 60 | CFDictionarySetValue(cast[CFAbstractMutableDictionary](d), cast[pointer](k), cast[pointer](v)) 61 | 62 | proc len*(d: CFAbstractDictionary): int {.inline.} = 63 | CFDictionaryGetCount(d) 64 | 65 | proc CFDictionaryGetKeysAndValues(d: CFAbstractDictionary, keys, values: ptr pointer) {.importc.} 66 | 67 | proc getKeysAndValues*[K, V](d: CFDictionary[K, V], keys: var seq[K], values: var seq[V]) = 68 | let c = d.len 69 | keys.setLen(c) 70 | values.setLen(c) 71 | if c != 0: 72 | CFDictionaryGetKeysAndValues(d, cast[ptr pointer](addr keys[0]), cast[ptr pointer](addr values[0])) 73 | 74 | iterator items*[K, V](d: CFDictionary[K, V]): K = 75 | let c = d.len 76 | if c != 0: 77 | var keys = newSeq[K](c) 78 | CFDictionaryGetKeysAndValues(d, cast[ptr pointer](addr keys[0]), nil) 79 | for k in keys: yield k 80 | 81 | iterator pairs*[K, V](d: CFDictionary[K, V]): (K, V) = 82 | let c = d.len 83 | if c != 0: 84 | var keys = newSeq[K](c) 85 | var values = newSeq[V](c) 86 | CFDictionaryGetKeysAndValues(d, cast[ptr pointer](addr keys[0]), cast[ptr pointer](addr values[0])) 87 | for i in 0 ..< c: yield (keys[i], values[i]) 88 | -------------------------------------------------------------------------------- /darwin/foundation/nsset.nim: -------------------------------------------------------------------------------- 1 | import ../objc/runtime 2 | 3 | import nsarray 4 | import nsenumerator 5 | 6 | type 7 | NSSetAbstract* = ptr object of NSObject 8 | NSMutableSetAbstract* = ptr object of NSSetAbstract 9 | NSSet*[T] = ptr object of NSSetAbstract 10 | NSMutableSet*[T] = ptr object of NSSet[T] 11 | 12 | 13 | proc objcClass(t: typedesc[NSSetAbstract]): auto {.inline.} = objcClass("NSSet") 14 | proc objcClass(t: typedesc[NSMutableSetAbstract]): auto {.inline.} = objcClass("NSMutableSet") 15 | 16 | 17 | proc newSetAbstract[T](t: typedesc[T]): T {.objc: "new".} 18 | proc newSet*[T](): NSSet[T] = cast[NSSet[T]](NSSetAbstract.newSetAbstract()) 19 | proc newMutableSet*[T]: NSMutableSet[T] = cast[NSMutableSet[T]](NSMutableSetAbstract.newSetAbstract()) 20 | 21 | proc withObjectsAndCount[T](t: typedesc[T], objs: pointer, count: int): T {.objc: "setWithObjects:count:".} 22 | proc setWithObjects*[T](objs: varargs[T]): NSSet[T] = cast[NSSet[T]](NSSetAbstract.withObjectsAndCount(unsafeAddr objs[0], objs.len)) 23 | proc mutableSetWithObjects*[T](objs: varargs[T]): NSMutableSet[T] = cast[NSMutableSet[T]](NSMutableSetAbstract.withObjectsAndCount(unsafeAddr objs[0], objs.len)) 24 | 25 | proc withArray[T](t: typedesc[T], arr: NSArrayAbstract): T {.objc: "setWithArray:".} 26 | proc setWithArray*[T](arr: NSArray[T]): NSSet[T] = cast[NSSet[T]](NSSetAbstract.withArray(cast[NSArrayAbstract](arr))) 27 | proc mutableSetWithArray*[T](arr: NSArray[T]): NSMutableSet[T] = cast[NSMutableSet[T]](NSMutableSetAbstract.withArray(cast[NSArrayAbstract](arr))) 28 | 29 | proc withSet[T](t: typedesc[T], s: NSSetAbstract): T {.objc: "setWithSet:".} 30 | proc setWithSet*[T](s: NSSet[T]): NSSet[T] = cast[NSSet[T]](NSSetAbstract.withSet(cast[NSSetAbstract](s))) 31 | proc mutableSetWithSet*[T](s: NSSet[T]): NSMutableSet[T] = cast[NSMutableSet[T]](NSMutableSetAbstract.withSet(cast[NSSetAbstract](s))) 32 | 33 | proc setByAddingObjectsFromSet(s: NSSetAbstract, sa: NSSetAbstract): NSSetAbstract {.objc: "setByAddingObjectsFromSet:".} 34 | proc setByAddingObjectsFromSet*[T](s: NSSet[T], sa: NSSet[T]): NSSet[T] = cast[NSSet[T]](cast[NSSetAbstract](s).setByAddingObjectsFromSet(cast[NSSetAbstract](sa))) 35 | 36 | proc setByAddingObjectsFromArray(s: NSSetAbstract, sa: NSArrayAbstract): NSSetAbstract {.objc: "setByAddingObjectsFromArray:".} 37 | proc setByAddingObjectsFromArray*[T](s: NSSet[T], sa: NSArray[T]): NSSet[T] = cast[NSSet[T]](cast[NSSetAbstract](s).setByAddingObjectsFromArray(cast[NSArrayAbstract](sa))) 38 | 39 | proc count*(s: NSSetAbstract): int {.objc: "count".} 40 | proc len*(s: NSSetAbstract): int {.inline.} = s.count 41 | 42 | proc containsObject(s: NSSetAbstract, obj: NSObject): bool {.objc: "containsObject:".} 43 | proc contains*[T](s: NSSet[T], obj: T): bool = containsObject(cast[NSSetAbstract](s), obj) 44 | 45 | proc objectEnumerator*(s: NSSetAbstract): NSEnumeratorAbstract {.objc.} 46 | proc objectEnumerator*[T](s: NSSet[T]): NSEnumerator[T] = cast[NSEnumerator[T]](cast[NSSetAbstract](s).objectEnumerator()) 47 | 48 | iterator items*[T](s: NSSet[T]): T = 49 | var enumerator = s.objectEnumerator() 50 | while true: 51 | let obj = enumerator.nextObject() 52 | if obj.isNil: 53 | break 54 | yield obj 55 | 56 | proc setWithCapacity(t: typedesc[NSMutableSetAbstract], capacity: int): NSMutableSetAbstract {.objc: "setWithCapacity:".} 57 | proc mutableSetWithCapacity*[T](capacity: int): NSMutableSet[T] = cast[NSMutableSet[T]](NSMutableSetAbstract.setWithCapacity(capacity)) 58 | 59 | proc addObject(s: NSMutableSetAbstract, o: NSObject) {.objc: "addObject:".} 60 | proc addObject*[T](s: NSMutableSet[T], o: NSObject) = cast[NSMutableSetAbstract](s).addObject(o) 61 | proc incl*[T](s: NSMutableSet[T], o: NSObject) = s.addObject(o) 62 | 63 | proc removeObject(s: NSMutableSetAbstract, o: NSObject) {.objc: "removeObject:".} 64 | proc removeObject*[T](s: NSMutableSet[T], o: NSObject) = cast[NSMutableSetAbstract](s).removeObject(o) 65 | proc excl*[T](s: NSMutableSet[T], o: NSObject) = s.removeObject(o) 66 | 67 | proc removeAllObjects(s: NSMutableSetAbstract) {.objc.} 68 | proc removeAllObjects*[T](s: NSMutableSet[T]) = cast[NSMutableSetAbstract](s).removeAllObjects() 69 | 70 | proc addObjectsFromArray(s: NSMutableSetAbstract, a: NSArrayAbstract) {.objc: "addObjectsFromArray:".} 71 | proc addObjectsFromArray*[T](s: NSMutableSet[T], arr: NSArray[T]) = cast[NSMutableSetAbstract](s).addObjectsFromArray(cast[NSArrayAbstract](arr)) -------------------------------------------------------------------------------- /darwin/core_foundation/cfstring.nim: -------------------------------------------------------------------------------- 1 | import cfbase 2 | 3 | export CFString, CFMutableString 4 | 5 | type 6 | CFStringEncoding* = distinct uint32 7 | 8 | const 9 | kCFStringEncodingInvalidId* = CFStringEncoding(0xffffffff'u32) 10 | kCFStringEncodingMacRoman* = CFStringEncoding(0'u32) 11 | kCFStringEncodingWindowsLatin1* = CFStringEncoding(0x0500'u32) 12 | kCFStringEncodingISOLatin1* = CFStringEncoding(0x0201'u32) 13 | kCFStringEncodingNextStepLatin* = CFStringEncoding(0x0B01'u32) 14 | kCFStringEncodingASCII* = CFStringEncoding(0x0600'u32) 15 | kCFStringEncodingUnicode* = CFStringEncoding(0x0100'u32) 16 | kCFStringEncodingUTF8* = CFStringEncoding(0x08000100'u32) 17 | kCFStringEncodingNonLossyASCII* = CFStringEncoding(0x0BFF'u32) 18 | 19 | kCFStringEncodingUTF16* = CFStringEncoding(0x0100'u32) 20 | kCFStringEncodingUTF16BE* = CFStringEncoding(0x10000100'u32) 21 | kCFStringEncodingUTF16LE* = CFStringEncoding(0x14000100'u32) 22 | 23 | kCFStringEncodingUTF32* = CFStringEncoding(0x0c000100'u32) 24 | kCFStringEncodingUTF32BE* = CFStringEncoding(0x18000100'u32) 25 | kCFStringEncodingUTF32LE* = CFStringEncoding(0x1c000100'u32) 26 | 27 | proc CFStringGetTypeID*(): CFTypeID {.importc.} 28 | 29 | proc CFStringCreateWithCString*(alloc: CFAllocator, cStr: cstring, encoding: CFStringEncoding): CFString {.importc.} 30 | proc CFStringCreateWithBytes*(alloc: CFAllocator, bytes: pointer, numBytes: CFIndex, encoding: CFStringEncoding, isExternalRepresentation: Boolean): CFString {.importc.} 31 | proc CFStringCreateWithCharacters*(alloc: CFAllocator, chars: ptr UniChar, numChars: CFIndex): CFString {.importc.} 32 | 33 | proc CFStringCreate*(cStr: cstring, encoding: CFStringEncoding): CFString {.inline.} = CFStringCreateWithCString(kCFAllocatorDefault, cStr, encoding) 34 | proc CFStringCreate*(bytes: pointer, numBytes: CFIndex, encoding: CFStringEncoding, isExternalRepresentation: bool): CFString {.inline.} = CFStringCreateWithBytes(kCFAllocatorDefault, bytes, numBytes, encoding, toBoolean(isExternalRepresentation)) 35 | proc CFStringCreate*(chars: ptr UniChar, numChars: CFIndex): CFString {.inline.} = CFStringCreateWithCharacters(kCFAllocatorDefault, chars, numChars) 36 | proc CFStringCreate*(s: string): CFString {.inline.} = CFStringCreate(s, kCFStringEncodingUTF8) 37 | 38 | proc CFStringCreateWithCStringNoCopy*(alloc: CFAllocator, cStr: cstring, encoding: CFStringEncoding, contentsDeallocator: CFAllocator): CFString {.importc.} 39 | proc CFStringCreateWithBytesNoCopy*(alloc: CFAllocator, bytes: pointer, numBytes: CFIndex, encoding: CFStringEncoding, isExternalRepresentation: Boolean, contentsDeallocator: CFAllocator): CFString {.importc.} 40 | proc CFStringCreateWithCharactersNoCopy*(alloc: CFAllocator, chars: ptr UniChar, numChars: CFIndex, contentsDeallocator: CFAllocator): CFString {.importc.} 41 | 42 | proc CFStringCreateWithSubstring*(alloc: CFAllocator, str: CFString, rng: CFRange): CFString {.importc.} 43 | proc CFStringCreateCopy*(alloc: CFAllocator, theString: CFString): CFString {.importc.} 44 | 45 | proc CFStringCreateMutable*(alloc: CFAllocator, maxLength: CFIndex): CFMutableString {.importc.} 46 | proc CFStringCreateMutableCopy*(alloc: CFAllocator, maxLength: CFIndex, theString: CFString): CFMutableString {.importc.} 47 | 48 | proc len*(theString: CFString): CFIndex {.importc: "CFStringGetLength".} 49 | 50 | proc getCharacterAtIndex*(theString: CFString, idx: CFIndex): UniChar {.importc: "CFStringGetCharacterAtIndex".} 51 | proc getCharacters*(theString: CFString, rng: CFRange, buffer: ptr UniChar): UniChar {.importc: "CFStringGetCharacters".} 52 | 53 | proc getCString*(theString: CFString, buffer: cstring, bufferSize: CFIndex, encoding: CFStringEncoding): Boolean {.importc: "CFStringGetCString".} 54 | 55 | proc getCStringPtr*(theString: CFString, encoding: CFStringEncoding): cstring {.importc: "CFStringGetCStringPtr".} 56 | proc getCharactersPtr*(theString: CFString): cstring {.importc: "CFStringGetCharactersPtr".} 57 | proc getBytes*(theString: CFString, rng: CFRange, encoding: CFStringEncoding, lossByte: char, isExternalRepresentation: bool, buffer: pointer, maxBufLen: CFIndex, usedBufLen: ptr CFIndex): CFIndex {.importc: "CFStringGetBytes".} 58 | 59 | 60 | proc getIntValue*(theString: CFString): int32 {.importc: "CFStringGetIntValue".} 61 | proc getDoubleValue*(theString: CFString): cdouble {.importc: "CFStringGetDoubleValue".} 62 | 63 | proc append*(theString: CFMutableString, appendedString: CFString) {.importc: "CFStringAppend".} 64 | 65 | proc show*(obj: CFObject) {.importc: "CFShow".} 66 | proc show*(obj: CFString) {.importc: "CFShowStr".} 67 | 68 | proc `$`*(s: CFString): string = 69 | var ln: int 70 | let rng = CFRangeMake(0, s.len) 71 | discard getBytes(s, rng, kCFStringEncodingUTF8, '?', false, nil, 0, addr ln) 72 | result = newString(ln) 73 | if ln != 0: 74 | discard getBytes(s, rng, kCFStringEncodingUTF8, '?', false, addr(result[0]), ln, nil) 75 | -------------------------------------------------------------------------------- /darwin/core_foundation/cfbase.nim: -------------------------------------------------------------------------------- 1 | 2 | {.passL: "-framework CoreFoundation".} 3 | 4 | type 5 | CFTypeID* = distinct uint 6 | CFOptionFlags* = uint 7 | CFHashCode* = uint 8 | CFIndex* = int 9 | 10 | CFRange* = object 11 | location*: CFIndex 12 | length*: CFIndex 13 | 14 | CFObject* {.pure, inheritable.} = ptr object # CFTypeRef 15 | CFPropertyList* = ptr object of CFObject 16 | 17 | CFAllocator* = ptr object of CFObject 18 | 19 | CFString* = ptr object of CFPropertyList 20 | CFMutableString* = ptr object of CFString 21 | 22 | 23 | Boolean* = uint8 24 | UniChar* = uint16 25 | 26 | CFTimeInterval* = float64 ## Represents elapsed time in seconds. 27 | 28 | const kCFNotFound*: CFIndex = -1 29 | 30 | proc `==`*(a, b: CFTypeID): bool {.borrow.} 31 | 32 | proc CFRangeMake*(location, length: CFIndex): CFRange {.inline.} = 33 | result.location = location 34 | result.length = length 35 | 36 | var 37 | allocDefault {.importc: "kCFAllocatorDefault".}: CFAllocator 38 | allocSystemDefault {.importc: "kCFAllocatorSystemDefault".}: CFAllocator 39 | allocMalloc {.importc: "kCFAllocatorMalloc".}: CFAllocator 40 | allocMallocZone {.importc: "kCFAllocatorMallocZone".}: CFAllocator 41 | allocNull {.importc: "kCFAllocatorNull".}: CFAllocator 42 | allocUseContext {.importc: "kCFAllocatorUseContext".}: CFAllocator 43 | 44 | template kCFAllocatorDefault*: CFAllocator = 45 | let a = allocDefault; a 46 | template kCFAllocatorSystemDefault*: CFAllocator = 47 | let a = allocSystemDefault; a 48 | template kCFAllocatorMalloc*: CFAllocator = 49 | let a = allocMalloc; a 50 | template kCFAllocatorMallocZone*: CFAllocator = 51 | let a = allocMallocZone; a 52 | template kCFAllocatorNull*: CFAllocator = 53 | let a = allocNull; a 54 | template kCFAllocatorUseContext*: CFAllocator = 55 | let a = allocUseContext; a 56 | 57 | type 58 | CFAllocatorRetainCallBack* = proc(info: pointer): pointer {.cdecl.} 59 | CFAllocatorReleaseCallBack* = proc(info: pointer) {.cdecl.} 60 | CFAllocatorCopyDescriptionCallBack* = proc(info: pointer): CFString {.cdecl.} 61 | CFAllocatorAllocateCallBack* = proc(allocSize: CFIndex, hint: CFOptionFlags, info: pointer): pointer {.cdecl.} 62 | CFAllocatorReallocateCallBack* = proc(buf: pointer, newsize: CFIndex, hint: CFOptionFlags, info: pointer): pointer {.cdecl.} 63 | CFAllocatorDeallocateCallBack* = proc(buf: pointer, info: pointer) {.cdecl.} 64 | CFAllocatorPreferredSizeCallBack* = proc(size: CFIndex, hint: CFOptionFlags, info: pointer): CFIndex {.cdecl.} 65 | 66 | CFAllocatorContext* = object 67 | version*: CFIndex 68 | info*: pointer 69 | retain*: CFAllocatorRetainCallBack 70 | release*: CFAllocatorReleaseCallBack 71 | copyDescription*: CFAllocatorCopyDescriptionCallBack 72 | allocate*: CFAllocatorAllocateCallBack 73 | reallocate*: CFAllocatorReallocateCallBack 74 | deallocate*: CFAllocatorDeallocateCallBack 75 | preferredSize*: CFAllocatorPreferredSizeCallBack 76 | 77 | proc CFAllocatorGetTypeID*(): CFTypeID {.importc.} 78 | proc CFAllocatorSetDefault*(a: CFAllocator) {.importc.} 79 | proc CFAllocatorGetDefault*(): CFAllocator {.importc.} 80 | proc CFAllocatorCreateAux(allocator: CFAllocator, context: ptr CFAllocatorContext): CFAllocator {.importc: "CFAllocatorCreate".} 81 | proc CFAllocatorCreate*(allocator: CFAllocator, context: CFAllocatorContext): CFAllocator {.inline.} = CFAllocatorCreateAux(allocator, unsafeAddr context) 82 | 83 | proc allocate*(allocator: CFAllocator, size: CFIndex, hint: CFOptionFlags): pointer {.importc: "CFAllocatorAllocate".} 84 | proc reallocate*(allocator: CFAllocator, buf: pointer, newsize: CFIndex, hint: CFOptionFlags): pointer {.importc: "CFAllocatorReallocate".} 85 | proc deallocate*(allocator: CFAllocator, buf: pointer) {.importc: "CFAllocatorDeallocate".} 86 | 87 | proc getPreferredSizeForSize*(allocator: CFAllocator, size: CFIndex, hint: CFOptionFlags): CFIndex {.importc: "CFAllocatorGetPreferredSizeForSize".} 88 | proc getContext*(allocator: CFAllocator, context: var CFAllocatorContext) {.importc: "CFAllocatorGetContext".} 89 | 90 | proc getTypeID*(cf: CFObject): CFTypeID {.importc: "CFGetTypeID".} 91 | proc copyTypeIDDescription*(type_id: CFTypeID): CFString {.importc: "CFCopyTypeIDDescription".} 92 | 93 | proc CFRetain(cf: CFObject): CFObject {.importc.} 94 | template retain*[T: CFObject](cf: T): T = cast[T](CFRetain(cf)) 95 | proc release*(cf: CFObject) {.importc: "CFRelease".} 96 | 97 | template toBool*(b: Boolean): bool = b == 0 98 | template toBoolean*(b: bool): Boolean = Boolean(b) 99 | 100 | proc CFEqual(cf1, cf2: CFObject): Boolean {.importc.} 101 | template equal*(cf1, cf2: CFObject): bool = toBool(CFEqual(cf1, cf2)) 102 | 103 | proc hash*(cf: CFObject): CFHashCode {.importc: "CFHash".} 104 | proc copyDescription*(cf: CFObject): CFString {.importc: "CFCopyDescription".} 105 | proc getAllocator*(cf: CFObject): CFAllocator {.importc: "CFGetAllocator".} 106 | -------------------------------------------------------------------------------- /darwin/objc/blocks.nim: -------------------------------------------------------------------------------- 1 | import runtime, macros 2 | 3 | type 4 | Block*[T: proc] = ptr object of NSObject 5 | 6 | BlockLiteral[T] = object 7 | isa: pointer 8 | flags: cint 9 | reserved: cint 10 | invoke: pointer 11 | descriptor: ptr BlockDescriptor 12 | # imported variables 13 | h: ClosureHolder[T] 14 | 15 | # This wrapper is only needed because we can't GC_ref closures 16 | ClosureHolder[T] = ref object 17 | p: T 18 | 19 | BlockDescriptor = object 20 | reserved: culong 21 | size: culong 22 | copy_helper: proc(dst, src: pointer) {.cdecl.} 23 | dispose_helper: proc(src: pointer) {.cdecl.} 24 | signature: cstring 25 | 26 | # Reference: https://clang.llvm.org/docs/Block-ABI-Apple.html 27 | 28 | proc copyHelper[T](dst, src: pointer) {.cdecl.} = 29 | cast[ptr BlockLiteral[T]](dst)[] = cast[ptr BlockLiteral[T]](src)[] 30 | 31 | proc disposeHelper[T](dst: pointer) {.cdecl.} = 32 | let blk = cast[ptr BlockLiteral[T]](dst) 33 | let h = blk.h 34 | blk.h = nil 35 | GC_unref(h) 36 | 37 | proc getSignature(t: typedesc): cstring = 38 | # TODO: Should this be implemented??? 39 | "" 40 | 41 | proc stripSinkFromArgType(t: NimNode): NimNode = 42 | result = t 43 | if result.kind == nnkBracketExpr and result.len == 2 and result[0].kind == nnkSym and $result[0] == "sink": 44 | result = result[1] 45 | 46 | iterator arguments(formalParams: NimNode): tuple[idx: int, name, typ, default: NimNode] = 47 | formalParams.expectKind(nnkFormalParams) 48 | var iParam = 0 49 | for i in 1 ..< formalParams.len: 50 | let pp = formalParams[i] 51 | for j in 0 .. pp.len - 3: 52 | yield (iParam, pp[j], copyNimTree(stripSinkFromArgType(pp[^2])), pp[^1]) 53 | inc iParam 54 | 55 | macro implementInvoke(T: typedesc): untyped = 56 | let t = getTypeImpl(T)[1] 57 | 58 | let call = newCall(newDotExpr(newDotExpr(ident"blk", ident"h"), ident"p")) 59 | let params = copyNimTree(t[0]) 60 | result = newProc(ident"invoke", body = call) 61 | let newParams = newTree(nnkFormalParams, params[0]) 62 | newParams.add(newIdentDefs(ident"blk", ident"TBlock")) 63 | 64 | for _, n, t, _ in arguments(params): 65 | call.add(ident($n)) 66 | newParams.add(newIdentDefs(ident($n), t)) 67 | 68 | result.params = newParams 69 | result.addPragma(ident"cdecl") 70 | 71 | result = newTree(nnkStmtList, result) 72 | result.add quote do: 73 | result = invoke 74 | 75 | proc getInvoke(T: typedesc): pointer = 76 | type TBlock = ptr BlockLiteral[T] 77 | implementInvoke(T) 78 | 79 | proc createBlockDescriptor(T: typedesc): BlockDescriptor = 80 | BlockDescriptor( 81 | size: culong(sizeof(BlockLiteral[T])), 82 | copy_helper: copyHelper[T], 83 | dispose_helper: disposeHelper[T], 84 | signature: getSignature(T) 85 | ) 86 | 87 | proc getBlockDescriptor(T: typedesc): ptr BlockDescriptor = 88 | const bd = createBlockDescriptor(T) 89 | addr bd 90 | 91 | var NSConcreteStackBlock {.importc: "_NSConcreteStackBlock".}: ptr pointer 92 | proc copyBlock(blk: pointer): pointer {.importc: "_Block_copy".} 93 | 94 | const 95 | BLOCK_IS_NOESCAPE = (1 shl 23) 96 | 97 | BLOCK_HAS_COPY_DISPOSE = (1 shl 25) 98 | BLOCK_HAS_CTOR = (1 shl 26) 99 | BLOCK_IS_GLOBAL = (1 shl 28) 100 | BLOCK_HAS_STRET = (1 shl 29) # IFF BLOCK_HAS_SIGNATURE 101 | BLOCK_HAS_SIGNATURE = (1 shl 30) 102 | 103 | proc toBlockFromClosure[T](v: T): Block[T] = 104 | var blk = BlockLiteral[T]( 105 | isa: NSConcreteStackBlock, 106 | flags: BLOCK_HAS_COPY_DISPOSE, 107 | invoke: getInvoke(T), 108 | descriptor: getBlockDescriptor(T), 109 | h: ClosureHolder[T](p: v) 110 | ) 111 | GC_ref(blk.h) 112 | cast[Block[T]](copyBlock(addr blk)) 113 | 114 | macro convertToClosure(v: typed): untyped = 115 | let t = getTypeImpl(v) 116 | 117 | var hasClosurePragma = false 118 | for n in t.pragma: 119 | if t.kind in {nnkIdent, nnkSym} and eqIdent($t, "closure"): 120 | hasClosurePragma = true 121 | break 122 | 123 | if not hasClosurePragma: 124 | t.addPragma(ident"closure") 125 | 126 | let params = t.params 127 | let newParams = newTree(nnkFormalParams, params[0]) 128 | for _, n, t, _ in arguments(params): 129 | newParams.add(newIdentDefs(ident($n), t)) 130 | t.params = newParams 131 | 132 | result = quote do: 133 | var a: `t` = `v` 134 | toBlockFromClosure(a) 135 | 136 | proc toBlock*[T: proc](v: T): auto = 137 | ## Returns Block[T {.closure.}] 138 | convertToClosure(v) 139 | 140 | proc getInvokeType(procType: NimNode): NimNode = 141 | result = copyNimTree(getTypeImpl(procType)) 142 | let pragmas = result.pragma 143 | 144 | var i = 0 145 | while i < pragmas.len: 146 | if pragmas[i].kind in {nnkSym, nnkIdent} and eqIdent($pragmas[i], "closure"): 147 | pragmas.del(i) 148 | else: 149 | inc i 150 | 151 | result.addPragma(ident"cdecl") 152 | 153 | result.params.insert(1, newIdentDefs(ident"", ident"pointer")) 154 | 155 | macro invokeAux(b: untyped, procType: typedesc, f: pointer, args: varargs[untyped]): untyped = 156 | let procT = getTypeImpl(procType)[1] 157 | # echo treeRepr procT 158 | result = newCall(newTree(nnkCast, getInvokeType(procT), f), b) 159 | for a in args: 160 | result.add(a) 161 | # echo "invokeAux: ", repr result 162 | 163 | template call*[T](b: Block[T], args: varargs[untyped]): untyped = 164 | invokeAux(b, T, cast[ptr BlockLiteral[T]](b).invoke, args) 165 | -------------------------------------------------------------------------------- /darwin/objc/runtime.nim: -------------------------------------------------------------------------------- 1 | import macros, strutils 2 | 3 | {.passL: "-framework Foundation".} 4 | 5 | const 6 | YES* = true 7 | NO* = false 8 | 9 | type 10 | NSObject* {.pure, inheritable.} = ptr object 11 | ObjcClass* = ptr object of NSObject 12 | 13 | NSString* = ptr object of NSObject 14 | 15 | Method* = distinct pointer 16 | Ivar* = distinct pointer 17 | Category* = distinct pointer 18 | IMP* = proc(id: ID, selector: SEL): ID {.cdecl, varargs.} 19 | Protocol* = distinct pointer 20 | ID* = pointer 21 | SEL* = ptr object 22 | STR* = ptr char 23 | arith_t* = int 24 | uarith_t* = uint 25 | ptrdiff_t* = int 26 | BOOL* = bool 27 | 28 | NSInteger* = int 29 | NSUInteger* = uint 30 | 31 | objc_method_description = object 32 | name: SEL 33 | types: cstring 34 | 35 | MethodDescription* = object 36 | name*: SEL 37 | types*: string 38 | 39 | Property* = distinct pointer 40 | 41 | ObjcSuper* = object 42 | receiver*: ID 43 | superClass*: ObjcClass 44 | 45 | objc_property_attribute_t* = object 46 | name*: cstring 47 | value*: cstring 48 | 49 | PropertyAttribute* = object 50 | name*: string 51 | value*: string 52 | 53 | objc_exception_functions_t* = object 54 | version: cint 55 | throw_exc: proc(id: ID) {.cdecl.} 56 | try_enter: proc(p: pointer) {.cdecl.} 57 | try_exit: proc(p: pointer) {.cdecl.} 58 | extract: proc(p: pointer): ID {.cdecl.} 59 | match: proc(class: ObjcClass, id: ID): cint {.cdecl.} 60 | 61 | objc_AssociationPolicy* {.size: sizeof(cuint).} = enum 62 | OBJC_ASSOCIATION_ASSIGN = 0 63 | OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1 64 | OBJC_ASSOCIATION_COPY_NONATOMIC = 3 65 | OBJC_ASSOCIATION_RETAIN = 01401 66 | OBJC_ASSOCIATION_COPY = 01403 67 | 68 | proc isNil*(a: ObjcClass): bool = 69 | result = a.pointer == nil 70 | 71 | proc c_free(p: pointer) {.importc: "free", header: "".} 72 | 73 | proc class_getName(cls: ObjcClass): cstring {.cdecl, importc.} 74 | proc getName*(cls: ObjcClass): string = 75 | result = $class_getName(cls) 76 | 77 | proc `$`*(cls: ObjcClass): string = 78 | getName(cls) 79 | 80 | proc class_getSuperclass(cls: ObjcClass): ObjcClass {.cdecl, importc.} 81 | template getSuperclass*(cls: ObjcClass): untyped = 82 | class_getSuperClass(cls) 83 | 84 | proc class_isMetaClass(cls: ObjcClass): bool {.cdecl, importc.} 85 | template isMetaClass*(cls: ObjcClass): untyped = 86 | class_isMetaClass(cls) 87 | 88 | proc class_getInstanceSize(cls: ObjcClass): csize_t {.cdecl, importc.} 89 | proc getInstanceSize*(cls: ObjcClass): int = class_getInstanceSize(cls).int 90 | 91 | proc class_getInstanceVariable(cls: ObjcClass; name: cstring): Ivar {.cdecl, importc.} 92 | template getIvar*(cls: ObjcClass, name: string): untyped = 93 | class_getInstanceVariable(cls, name.cstring) 94 | 95 | proc class_getClassVariable(cls: ObjcClass; name: cstring): Ivar {.cdecl, importc.} 96 | template getClassVariable*(cls: ObjcClass; name: string): untyped = 97 | class_getClassVariable(cls, name.cstring) 98 | 99 | proc class_addIvar(cls: ObjcClass; name: cstring; size: csize_t; alignment: uint8; types: cstring): bool {.cdecl, importc.} 100 | proc addIvar*(cls: ObjcClass; name: string; size: int; alignment: int; types: string): bool = 101 | class_addIvar(cls, name.cstring, size.csize_t, alignment.uint8, types.cstring) == YES 102 | 103 | proc class_copyIvarList(cls: ObjcClass; outCount: var cuint): ptr Ivar {.cdecl, importc.} 104 | proc ivarList*(cls: ObjcClass): seq[Ivar] = 105 | var 106 | count = 0.cuint 107 | ivars = class_copyIvarList(cls, count) 108 | if count == 0: 109 | result = @[] 110 | return result 111 | result = newSeq[Ivar](count) 112 | copyMem(result[0].addr, ivars, sizeof(Ivar) * count.int) 113 | c_free(ivars) 114 | 115 | proc class_getIvarLayout*(cls: ObjcClass): ptr uint8 {.cdecl, importc.} 116 | proc class_getWeakIvarLayout*(cls: ObjcClass): ptr uint8 {.cdecl, importc.} 117 | proc class_setIvarLayout*(cls: ObjcClass; layout: ptr uint8) {.cdecl, importc.} 118 | proc class_setWeakIvarLayout*(cls: ObjcClass; layout: ptr uint8) {.cdecl, importc.} 119 | 120 | proc class_getProperty(cls: ObjcClass; name: cstring): Property {.cdecl, importc.} 121 | template getProperty*(cls: ObjcClass; name: string): untyped = 122 | class_getProperty(cls, name.cstring) 123 | proc class_copyPropertyList*(cls: ObjcClass, outCount: var cuint): ptr Property {.cdecl, importc.} 124 | proc propertyList*(cls: ObjcClass): seq[Property] = 125 | var 126 | count = 0.cuint 127 | props = class_copyPropertyList(cls, count) 128 | if count == 0: 129 | result = @[] 130 | return result 131 | result = newSeq[Property](count) 132 | copyMem(result[0].addr, props, sizeof(Property) * count.int) 133 | c_free(props) 134 | 135 | proc class_addMethod(cls: ObjcClass; name: SEL; imp: IMP; types: cstring): bool {.cdecl, importc.} 136 | template addMethod*(cls: ObjcClass; name: SEL; imp: IMP; types: string): bool = 137 | class_addMethod(cls, name, imp, types.cstring) 138 | 139 | proc class_getInstanceMethod(cls: ObjcClass; name: SEL): Method {.cdecl, importc.} 140 | template getInstanceMethod*(cls: ObjcClass; name: SEL): Method = 141 | class_getInstanceMethod(cls, name) 142 | 143 | proc class_getClassMethod(cls: ObjcClass; name: SEL): Method {.cdecl, importc.} 144 | template getClassMethod*(cls: ObjcClass; name: SEL): Method = 145 | class_getClassMethod(cls, name) 146 | 147 | proc class_copyMethodList(cls: ObjcClass; outCount: var cuint): ptr Method {.cdecl, importc.} 148 | proc methodList*(cls: ObjcClass): seq[Method] = 149 | var 150 | count = 0.cuint 151 | procs = class_copyMethodList(cls, count) 152 | if count == 0: 153 | result = @[] 154 | return result 155 | result = newSeq[Method](count) 156 | copyMem(result[0].addr, procs, sizeof(Method) * count.int) 157 | c_free(procs) 158 | 159 | proc class_replaceMethod(cls: ObjcClass; name: SEL; imp: IMP; types: cstring): IMP {.cdecl, importc.} 160 | template replaceMethod*(cls: ObjcClass; name: SEL; imp: IMP; types: string): untyped = 161 | class_replaceMethod(cls, name, imp, types.cstring) 162 | 163 | proc class_getMethodImplementation(cls: ObjcClass; name: SEL): IMP {.cdecl, importc.} 164 | template getMethodImplementation*(cls: ObjcClass; name: SEL): untyped = 165 | class_getMethodImplementation(cls, name) 166 | 167 | proc class_getMethodImplementation_stret*(cls: ObjcClass; name: SEL): IMP {.cdecl, importc.} 168 | 169 | proc class_respondsToSelector(cls: ObjcClass; sel: SEL): bool {.cdecl, importc.} 170 | template respondsToSelector*(cls: ObjcClass; sel: SEL): untyped = 171 | class_respondsToSelector(cls, sel) 172 | 173 | proc class_addProtocol(cls: ObjcClass; protocol: Protocol): bool {.cdecl, importc.} 174 | template addProtocol*(cls: ObjcClass; protocol: Protocol): untyped = 175 | class_addProtocol(cls, protocol) 176 | 177 | proc class_addProperty(cls: ObjcClass; name: cstring; 178 | attributes: ptr objc_property_attribute_t; 179 | attributeCount: cuint): bool {.cdecl, importc.} 180 | 181 | proc addProperty*(cls: ObjcClass; name: string; attributes: openArray[objc_property_attribute_t]): bool = 182 | class_addProperty(cls, name.cstring, attributes[0].unsafeAddr, attributes.len.cuint) == YES 183 | 184 | 185 | proc class_replaceProperty(cls: ObjcClass; name: cstring; 186 | attributes: ptr objc_property_attribute_t; 187 | attributeCount: cuint) {.cdecl, importc.} 188 | 189 | proc replaceProperty*(cls: ObjcClass; name: string; attributes: openArray[objc_property_attribute_t]) = 190 | class_replaceProperty(cls, name.cstring, attributes[0].unsafeAddr, attributes.len.cuint) 191 | 192 | proc class_conformsToProtocol(cls: ObjcClass; protocol: Protocol): bool {.cdecl, importc.} 193 | template conformsToProtocol*(cls: ObjcClass; protocol: Protocol): bool = 194 | class_conformsToProtocol(cls, protocol) == YES 195 | 196 | proc class_copyProtocolList(cls: ObjcClass; outCount: var cuint): ptr Protocol {.cdecl, importc.} 197 | proc protocolList*(cls: ObjcClass): seq[Protocol] = 198 | var 199 | count = 0.cuint 200 | prots = class_copyProtocolList(cls, count) 201 | if count == 0: 202 | result = @[] 203 | return result 204 | result = newSeq[Protocol](count) 205 | copyMem(result[0].addr, prots, sizeof(Protocol) * count.int) 206 | c_free(prots) 207 | 208 | proc class_getVersion(cls: ObjcClass): cint {.cdecl, importc.} 209 | template getVersion*(cls: ObjcClass): untyped = 210 | class_getVersion(cls).int 211 | 212 | proc class_setVersion(cls: ObjcClass; version: cint) {.cdecl, importc.} 213 | template setVersion*(cls: ObjcClass; version: int) = 214 | class_setVersion(cls, version.cint) 215 | 216 | proc objc_getFutureClass(name: cstring): ObjcClass {.cdecl, importc.} 217 | template getFutureClass*(name: string): untyped = 218 | objc_getFutureClass(name.cstring) 219 | 220 | proc objc_allocateClassPair(superclass: ObjcClass, name: cstring, extraBytes: csize_t): ObjcClass {.cdecl, importc.} 221 | template allocateClassPair*(superclass: ObjcClass, name: string, extraBytes: int): untyped = 222 | objc_allocateClassPair(superclass, name.cstring, extrabytes.csize_t) 223 | 224 | proc objc_disposeClassPair(cls: ObjcClass) {.cdecl, importc.} 225 | template disposeClassPair*(cls: ObjcClass) = 226 | objc_disposeClassPair(cls) 227 | 228 | proc objc_registerClassPair(cls: ObjcClass) {.cdecl, importc.} 229 | template registerClassPair*(cls: ObjcClass) = 230 | objc_registerClassPair(cls) 231 | 232 | proc objc_duplicateClass(original: ObjcClass; name: cstring; extraBytes: csize_t): ObjcClass {.cdecl, importc.} 233 | template duplicateClass*(original: ObjcClass; name: string; extraBytes: int): untyped = 234 | objc_duplicateClass(original, name.cstring, extraBytes.csize_t) 235 | 236 | proc class_createInstance(cls: ObjcClass; extraBytes: csize_t): ID {.cdecl, importc.} 237 | template createInstance*(cls: ObjcClass; extraBytes: csize_t): untyped = 238 | class_createInstance(cls, extraBytes.csize_t) 239 | 240 | proc objc_constructInstance(cls: ObjcClass; bytes: pointer): ID {.cdecl, importc.} 241 | template constructInstance*(cls: ObjcClass; bytes: pointer): untyped = 242 | objc_constructInstance(cls, bytes) 243 | 244 | proc objc_destructInstance(obj: ID): pointer {.cdecl, importc.} 245 | template destructInstance*(obj: ID): untyped = 246 | objc_destructInstance(obj) 247 | 248 | proc object_copy(obj: ID; size: csize_t): ID {.cdecl, importc.} 249 | template copy*(obj: ID; size: csize_t): untyped = 250 | object_copy(obj, size.csize_t) 251 | 252 | proc object_dispose(obj: ID): ID {.cdecl, importc.} 253 | template dispose*(obj: ID): untyped = 254 | object_dispose(obj) 255 | 256 | proc object_setInstanceVariable(obj: ID; name: cstring; value: pointer): Ivar {.cdecl, importc.} 257 | template setInstanceVariable*(obj: ID; name: string; value: pointer): untyped = 258 | object_setInstanceVariable(obj, name.cstring, value) 259 | 260 | proc object_getInstanceVariable(obj: ID; name: cstring; outValue: var pointer): Ivar {.cdecl, importc.} 261 | template getInstanceVariable*(obj: ID; name: string; outValue: var pointer): untyped = 262 | object_getInstanceVariable(obj, name.cstring, outValue) 263 | 264 | proc object_getIndexedIvars(obj: ID): pointer {.cdecl, importc.} 265 | template getIndexedIvars*(obj: ID): untyped = 266 | object_getIndexedIvars(obj) 267 | 268 | proc object_getIvar(obj: ID; ivar: Ivar): ID {.cdecl, importc.} 269 | template getIvar*(obj: ID; ivar: Ivar): untyped = 270 | object_getIvar(obj, ivar) 271 | 272 | proc object_setIvar(obj: ID; ivar: Ivar; value: ID) {.cdecl, importc.} 273 | template setIvar*(obj: ID; ivar: Ivar; value: ID) = 274 | object_setIvar(obj, ivar, value) 275 | 276 | proc object_getClassName(obj: ID): cstring {.cdecl, importc.} 277 | proc getClassName*(obj: ID): string = 278 | result = $object_getClassName(obj) 279 | 280 | proc objc_getClass(name: cstring): ObjcClass {.cdecl, importc.} 281 | template getClass*(name: string): untyped = 282 | objc_getClass(name.cstring) 283 | 284 | proc object_setClass(obj: ID; cls: ObjcClass): ObjcClass {.cdecl, importc.} 285 | template setClass*(obj: ID; cls: ObjcClass): untyped = 286 | object_setClass(obj, cls) 287 | 288 | proc objc_getClassList(buffer: ptr ObjcClass; bufferCount: cint): cint {.cdecl, importc.} 289 | proc getClassList*(): seq[ObjcClass] = 290 | let count = objc_getClassList(nil, 0.cint) 291 | if count == 0: 292 | result = @[] 293 | return result 294 | result = newSeq[ObjcClass](count) 295 | discard objc_getClassList(result[0].addr, result.len.cint) 296 | 297 | proc objc_copyClassList(outCount: var cuint): ptr ObjcClass {.cdecl, importc.} 298 | 299 | proc copyClassList*(): seq[ObjcClass] = 300 | var 301 | count = 0.cuint 302 | classes = objc_copyClassList(count) 303 | if count == 0: 304 | result = @[] 305 | return result 306 | result = newSeq[ObjcClass](count) 307 | copyMem(result[0].addr, classes, sizeof(ObjcClass) * count.int) 308 | c_free(classes) 309 | 310 | proc objc_lookUpClass(name: cstring): ObjcClass {.cdecl, importc.} 311 | template lookUpClass*(name: cstring): untyped = 312 | objc_lookUpClass(name.cstring) 313 | 314 | proc object_getClass(obj: ID): ObjcClass {.cdecl, importc.} 315 | template getClass*(obj: ID): untyped = 316 | object_getClass(obj) 317 | 318 | proc objc_getRequiredClass(name: cstring): ObjcClass {.cdecl, importc.} 319 | template getRequiredClass*(name: string): untyped = 320 | objc_getRequiredClass(name.cstring) 321 | 322 | proc objc_getMetaClass(name: cstring): ObjcClass {.cdecl, importc.} 323 | template getMetaClass*(name: string): untyped = 324 | objc_getMetaClass(name.cstring) 325 | 326 | proc ivar_getName(v: Ivar): cstring {.cdecl, importc.} 327 | template getName*(v: Ivar): untyped = 328 | $ivar_getName(v) 329 | 330 | proc `$`*(v: Ivar): string = 331 | getName(v) 332 | 333 | proc ivar_getTypeEncoding(v: Ivar): cstring {.cdecl, importc.} 334 | template getTypeEncoding*(v: Ivar): untyped = 335 | $ivar_getTypeEncoding(v) 336 | 337 | proc ivar_getOffset(v: Ivar): ptrdiff_t {.cdecl, importc.} 338 | template getOffset*(v: Ivar): untyped = 339 | ivar_getOffset(v) 340 | 341 | proc objc_setAssociatedObject(obj: ID; key: pointer; value: ID; policy: objc_AssociationPolicy) {.cdecl, importc.} 342 | template setAssociatedObject*(obj: ID; key: pointer; value: ID; policy: objc_AssociationPolicy) = 343 | objc_setAssociatedObject(obj, key, value, policy) 344 | 345 | proc objc_getAssociatedObject(obj: ID; key: pointer): ID {.cdecl, importc.} 346 | template getAssociatedObject*(obj: ID; key: pointer): untyped = 347 | objc_getAssociatedObject(obj, key) 348 | 349 | proc objc_removeAssociatedObjects(obj: ID) {.cdecl, importc.} 350 | template removeAssociatedObjects*(obj: ID) = 351 | objc_removeAssociatedObjects(obj) 352 | 353 | proc objc_msgSend*(self: ID; op: SEL): ID {.cdecl, importc, discardable, varargs.} 354 | proc objc_msgSend_fpret*(self: ID; op: SEL): cdouble {.cdecl, importc, varargs.} 355 | proc objc_msgSend_stret*(self: ID; op: SEL) {.cdecl, importc, varargs.} 356 | proc objc_msgSendSuper*(super: var ObjcSuper; op: SEL): ID {.cdecl, importc, varargs.} 357 | proc objc_msgSendSuper_stret*(super: var ObjcSuper; op: SEL) {.cdecl, importc, varargs.} 358 | proc method_invoke*(receiver: ID; m: Method): ID {.cdecl, importc, varargs.} 359 | proc method_invoke_stret*(receiver: ID; m: Method) {.cdecl, importc, varargs.} 360 | 361 | proc sel_getName*(sel: SEL): cstring {.cdecl, importc.} 362 | template getName*(sel: SEL): untyped = 363 | $sel_getName(sel) 364 | 365 | proc `$`*(sel: SEL): string = 366 | getName(sel) 367 | 368 | proc sel_registerName*(str: cstring): SEL {.cdecl, importc.} 369 | template registerName*(str: string): untyped = 370 | sel_registerName(str.cstring) 371 | 372 | proc `$$`*(str: string): SEL = 373 | sel_registerName(str.cstring) 374 | 375 | proc sel_getUid(str: cstring): SEL {.cdecl, importc.} 376 | template getUid*(str: string): untyped = 377 | sel_getUid(str.cstring) 378 | 379 | proc sel_isEqual(lhs: SEL; rhs: SEL): bool {.cdecl, importc.} 380 | template isEqual*(lhs, rhs: SEL): untyped = 381 | sel_isEqual(lhs, rhs) 382 | 383 | proc method_getName(m: Method): SEL {.cdecl, importc.} 384 | template getName*(m: Method): untyped = 385 | $method_getName(m) 386 | 387 | proc `$`*(m: Method): string = 388 | getName(m) 389 | 390 | proc method_getImplementation(m: Method): IMP {.cdecl, importc.} 391 | template getImplementation*(m: Method): untyped = 392 | method_getImplementation(m) 393 | 394 | proc method_getTypeEncoding(m: Method): cstring {.cdecl, importc.} 395 | template getTypeEncoding*(m: Method): untyped = 396 | $method_getTypeEncoding(m) 397 | 398 | proc method_copyReturnType(m: Method): cstring {.cdecl, importc.} 399 | proc copyReturnType*(m: Method): string = 400 | var ret = method_copyReturnType(m) 401 | result = $ret 402 | c_free(ret) 403 | 404 | proc method_copyArgumentType(m: Method; index: cuint): cstring {.cdecl, importc.} 405 | proc copyArgumentType*(m: Method; index: int): string = 406 | var ret = method_copyArgumentType(m, index.cuint) 407 | result = $ret 408 | c_free(ret) 409 | 410 | proc method_getReturnType(m: Method; dst: cstring; dst_len: csize_t) {.cdecl, importc.} 411 | proc getReturnType*(m: Method): string = 412 | var ret: array[100, char] 413 | method_getReturnType(m, cast[cstring](ret[0].addr), sizeof(ret).csize_t) 414 | result = $(cast[cstring](ret[0].addr)) 415 | 416 | proc method_getNumberOfArguments(m: Method): cuint {.cdecl, importc.} 417 | template getNumberOfArguments*(m: Method): untyped = 418 | method_getNumberOfArguments(m).int 419 | 420 | proc method_getArgumentType(m: Method; index: cuint; dst: cstring; dst_len: csize_t) {.cdecl, importc.} 421 | proc getArgumentType*(m: Method; index: int): string = 422 | var ret: array[100, char] 423 | method_getArgumentType(m, index.cuint, cast[cstring](ret[0].addr), sizeof(ret).csize_t) 424 | result = $(cast[cstring](ret[0].addr)) 425 | 426 | proc argumentTypes*(m: Method): seq[string] = 427 | let count = getNumberOfArguments(m) 428 | result = newSeq[string](count) 429 | if count == 0: 430 | result = @[] 431 | return result 432 | for i in 0 ..< count: 433 | result[i] = getArgumentType(m, i) 434 | 435 | proc method_getDescription(m: Method): ptr objc_method_description {.cdecl, importc.} 436 | proc getDescription*(m: Method): MethodDescription = 437 | var p = method_getDescription(m) 438 | result.name = p.name 439 | result.types = $p.types 440 | 441 | proc method_setImplementation(m: Method; imp: IMP): IMP {.cdecl, importc.} 442 | template setImplementation*(m: Method; imp: IMP): untyped = 443 | method_setImplementation(m, imp) 444 | 445 | proc method_exchangeImplementations(m1: Method; m2: Method) {.cdecl, importc.} 446 | template exchangeImplementations*(m1: Method; m2: Method) = 447 | method_exchangeImplementations(m1, m2) 448 | 449 | proc objc_copyImageNames(outCount: var cuint): cstringArray {.cdecl, importc.} 450 | proc imageNames*(): seq[string] = 451 | var 452 | count = 0.cuint 453 | images = objc_copyImageNames(count) 454 | if count == 0: 455 | result = @[] 456 | return result 457 | result = newSeq[string](count.int) 458 | for i in 0 ..< result.len: 459 | result[i] = $images[i] 460 | 461 | proc class_getImageName(cls: ObjcClass): cstring {.cdecl, importc.} 462 | template getImageName*(cls: ObjcClass): untyped = 463 | $class_getImageName(cls) 464 | 465 | proc objc_copyClassNamesForImage(image: cstring; outCount: var cuint): cstringArray {.cdecl, importc.} 466 | proc classNamesForImage*(image: string): seq[string] = 467 | var 468 | count = 0.cuint 469 | classes = objc_copyClassNamesForImage(image.cstring, count) 470 | if count == 0: 471 | result = @[] 472 | return result 473 | result = newSeq[string](count.int) 474 | for i in 0 ..< result.len: 475 | result[i] = $classes[i] 476 | 477 | proc objc_getProtocol(name: cstring): Protocol {.cdecl, importc.} 478 | template getProtocol*(name: string): untyped = 479 | objc_getProtocol(name.cstring) 480 | 481 | proc objc_copyProtocolList(outCount: var cuint): ptr Protocol {.cdecl, importc.} 482 | proc protocolList*(): seq[Protocol] = 483 | var 484 | count = 0.cuint 485 | prots = objc_copyProtocolList(count) 486 | if count == 0: 487 | result = @[] 488 | return result 489 | result = newSeq[Protocol](count.int) 490 | copyMem(result[0].addr, prots, result.len * sizeof(Protocol)) 491 | c_free(prots) 492 | 493 | proc objc_allocateProtocol(name: cstring): Protocol {.cdecl, importc.} 494 | template allocateProtocol*(name: string): untyped = 495 | objc_allocateProtocol(name.cstring) 496 | 497 | proc objc_registerProtocol*(proto: Protocol) {.cdecl, importc.} 498 | template registerProtocol*(proto: Protocol) = 499 | objc_registerProtocol(proto) 500 | 501 | proc protocol_addMethodDescription(proto: Protocol; name: SEL; types: cstring; 502 | isRequiredMethod, isInstanceMethod: bool) {.cdecl, importc.} 503 | 504 | template addMethodDescription*(proto: Protocol; name: SEL; types: string; 505 | isRequiredMethod, isInstanceMethod: bool) = 506 | protocol_addMethodDescription(proto, name, types.cstring, isRequiredMethod, isInstanceMethod) 507 | 508 | proc protocol_addProtocol(proto, addition: Protocol) {.cdecl, importc.} 509 | template addProtocol*(proto, addition: Protocol) = 510 | protocol_addProtocol(proto, addition) 511 | 512 | proc protocol_addProperty(proto: Protocol; name: cstring; 513 | attributes: ptr objc_property_attribute_t; 514 | attributeCount: cuint; isRequiredProperty: bool; 515 | isInstanceProperty: bool) {.cdecl, importc.} 516 | 517 | proc addProperty*(proto: Protocol; name: string; attributes: openArray[objc_property_attribute_t], 518 | isRequiredProperty, isInstanceProperty: bool) = 519 | protocol_addProperty(proto, name, attributes[0].unsafeAddr, attributes.len.cuint, 520 | isRequiredProperty, isInstanceProperty) 521 | 522 | proc protocol_getName(p: Protocol): cstring {.cdecl, importc.} 523 | template getName*(p: Protocol): untyped = 524 | $protocol_getName(p) 525 | 526 | proc `$`*(p: Protocol): string = 527 | getName(p) 528 | 529 | proc protocol_isEqual(proto, other: Protocol): bool {.cdecl, importc.} 530 | template isEqual*(proto, other: Protocol): untyped = 531 | protocol_isEqual(proto, other) 532 | 533 | proc protocol_copyMethodDescriptionList(p: Protocol; isRequiredMethod, isInstanceMethod: bool; 534 | outCount: var cuint): ptr objc_method_description {.cdecl, importc.} 535 | 536 | proc methodDescriptionList*(p: Protocol; isRequiredMethod, isInstanceMethod: bool): seq[MethodDescription] = 537 | type 538 | DescT = array[0..0, objc_method_description] 539 | var 540 | count = 0.cuint 541 | raw = protocol_copyMethodDescriptionList(p, isRequiredMethod, isInstanceMethod, count) 542 | descs = cast[DescT](raw) 543 | if count == 0: 544 | result = @[] 545 | return result 546 | result = newSeq[MethodDescription](count.int) 547 | for i in 0 ..< count.int: 548 | result[i] = MethodDescription(name: descs[i].name, types: $descs[i].types) 549 | c_free(raw) 550 | 551 | proc protocol_getMethodDescription(p: Protocol; aSel: SEL; 552 | isRequiredMethod, isInstanceMethod: bool): objc_method_description {.cdecl, importc.} 553 | 554 | template getMethodDescription*(p: Protocol; aSel: SEL; isRequiredMethod, isInstanceMethod: bool): untyped = 555 | protocol_getMethodDescription(p, aSel, isRequiredMethod, isInstanceMethod) 556 | 557 | proc protocol_copyPropertyList(proto: Protocol; outCount: var cuint): ptr Property {.cdecl, importc.} 558 | proc propertyList*(proto: Protocol): seq[Property] = 559 | var 560 | count = 0.cuint 561 | props = protocol_copyPropertyList(proto, count) 562 | if count == 0: 563 | result = @[] 564 | return result 565 | result = newSeq[Property](count.int) 566 | copyMem(result[0].addr, props, result.len * sizeof(Property)) 567 | c_free(props) 568 | 569 | proc protocol_getProperty(proto: Protocol; name: cstring; isRequiredProperty, isInstanceProperty: bool): Property {.cdecl, importc.} 570 | template getProperty*(proto: Protocol; name: string; isRequiredProperty, isInstanceProperty: bool): untyped = 571 | protocol_getProperty(proto, name.cstring, isRequiredProperty, isInstanceProperty) 572 | 573 | proc protocol_copyProtocolList*(proto: Protocol, outCount: var cuint): ptr Protocol {.cdecl, importc.} 574 | proc protocolList*(proto: Protocol): seq[Protocol] = 575 | var 576 | count = 0.cuint 577 | prots = protocol_copyProtocolList(proto, count) 578 | if count == 0: 579 | result = @[] 580 | return result 581 | result = newSeq[Protocol](count.int) 582 | copyMem(result[0].addr, prots, result.len * sizeof(Protocol)) 583 | c_free(prots) 584 | 585 | proc protocol_conformsToProtocol(proto, other: Protocol): bool {.cdecl, importc.} 586 | template conformsToProtocol*(proto, other: Protocol): untyped = 587 | protocol_conformsToProtocol(proto, other) 588 | 589 | proc property_getName(property: Property): cstring {.cdecl, importc.} 590 | template getName*(property: Property): untyped = 591 | $property_getName(property) 592 | 593 | proc `$`*(property: Property): string = 594 | getName(property) 595 | 596 | proc property_getAttributes(property: Property): cstring {.cdecl, importc.} 597 | template getAttributes*(property: Property): untyped = 598 | $property_getAttributes(property) 599 | 600 | proc property_copyAttributeList(property: Property; outCount: var cuint): ptr objc_property_attribute_t {.cdecl, importc.} 601 | proc attributeList*(property: Property): seq[PropertyAttribute] = 602 | type AttrT = array[0..0, objc_property_attribute_t] 603 | var 604 | count = 0.cuint 605 | raw = property_copyAttributeList(property, count) 606 | attrs = cast[AttrT](raw) 607 | if count == 0: 608 | result = @[] 609 | return result 610 | result = newSeq[PropertyAttribute](count.int) 611 | for i in 0 ..< count.int: 612 | result[i] = PropertyAttribute(name: $attrs[i].name, value: $attrs[i].value) 613 | c_free(raw) 614 | 615 | proc property_copyAttributeValue(property: Property; attributeName: cstring): cstring {.cdecl, importc.} 616 | proc attributeValue*(property: Property; attributeName: string): string = 617 | var res = property_copyAttributeValue(property, attributeName.cstring) 618 | result = $res 619 | c_free(res) 620 | 621 | proc objc_enumerationMutation(obj: ID) {.cdecl, importc.} 622 | template enumerationMutation*(obj: ID) = 623 | objc_enumerationMutation(obj) 624 | 625 | type 626 | EnumerationHandler = proc(a2: ID) {.cdecl.} 627 | 628 | proc objc_setEnumerationMutationHandler(handler: EnumerationHandler) {.cdecl, importc.} 629 | template setEnumerationMutationHandler*(handler: EnumerationHandler) = 630 | objc_setEnumerationMutationHandler(handler) 631 | 632 | proc imp_implementationWithBlock(blok: ID): IMP {.cdecl, importc.} 633 | template implementationWithBlock*(blok: ID): untyped = 634 | imp_implementationWithBlock(blok) 635 | 636 | proc imp_getBlock(anImp: IMP): ID {.cdecl, importc.} 637 | template getBlock*(anImp: IMP): untyped = 638 | imp_getBlock(anImp) 639 | 640 | proc imp_removeBlock(anImp: IMP): bool {.cdecl, importc.} 641 | template removeBlock*(anImp: IMP): untyped = 642 | imp_removeBlock(anImp) 643 | 644 | proc objc_loadWeak(location: var ID): ID {.cdecl, importc.} 645 | template loadWeak*(location: var ID): untyped = 646 | objc_loadWeak(location) 647 | 648 | proc objc_storeWeak(location: var ID; obj: ID): ID {.cdecl, importc.} 649 | template storeWeak*(location: var ID; obj: ID): untyped = 650 | objc_storeWeak(location, obj) 651 | 652 | {.push stackTrace: off.} 653 | # These procs should better be inlined, but there's a Nim bug #5945 654 | 655 | proc objcClass*(name: static[string]): ObjcClass = 656 | var c {.global.} = objc_getClass(name) 657 | return c 658 | 659 | proc objcClass*[T](t: typedesc[T]): ObjcClass {.inline.} = objcClass($T) 660 | 661 | proc getSelector(name: static[string]): SEL = 662 | var s {.global.}: SEL 663 | if pointer(s).isNil: 664 | s = sel_registerName(name) 665 | return s 666 | 667 | proc respondsToSelector*(obj: NSObject, selector: static[string]): bool = 668 | class_respondsToSelector(object_getClass(obj), sel_registerName(selector)) 669 | 670 | {.pop.} 671 | 672 | proc getArgsAndTypes(routine: NimNode): (NimNode, NimNode) = 673 | let args = newNimNode(nnkStmtList) 674 | let types = newNimNode(nnkStmtList) 675 | let params = routine.params 676 | for a in 1 ..< params.len: 677 | let p = params[a] 678 | for i in 0 .. p.len - 3: 679 | args.add(p[i]) 680 | types.add(p[^2]) 681 | result = (args, types) 682 | 683 | proc unpackPragmaParams(p1, p2: NimNode): (string, NimNode) = 684 | if p2.kind == nnkNilLit: ("", p1) else: ($p1, p2) 685 | 686 | proc guessSelectorNameFromProc(p: NimNode): string = 687 | var pName = p.name 688 | if pName.kind == nnkPostfix: 689 | pName = pName[^1] 690 | result = $pName 691 | 692 | type ObjCMsgSendFlavor = enum 693 | normal 694 | fpret 695 | stret 696 | 697 | template msgSendFlavorForRetType(retType: typedesc): ObjCMsgSendFlavor = 698 | when (retType is float | float32 | float64 | cfloat | cdouble) and hostCPU == "i386": 699 | ObjCMsgSendFlavor.fpret 700 | elif (retType is object | tuple) and sizeof(retType) > sizeof(pointer) * 2 and not defined(arm64): # TODO: sizeof check is a dangerous guess here! Please help. 701 | ObjCMsgSendFlavor.stret 702 | else: 703 | ObjCMsgSendFlavor.normal 704 | 705 | macro objcAux(flavor: static[ObjCMsgSendFlavor], firstArg: typed, name: static[string], body: untyped): untyped = 706 | var name = name 707 | 708 | let performSend = ident"performSend" 709 | 710 | let senderParams = newNimNode(nnkFormalParams) 711 | if flavor == stret: 712 | senderParams.add(ident"void") 713 | senderParams.add(newIdentDefs(ident"_", ident"pointer")) 714 | else: 715 | senderParams.add(copyNimTree(body.params[0])) 716 | senderParams.add(newIdentDefs(ident"self", bindSym"NSObject")) 717 | senderParams.add(newIdentDefs(ident"selector", bindSym"SEL")) 718 | 719 | let procTy = newTree(nnkProcTy, senderParams) 720 | procTy.add(newTree(nnkPragma, ident"cdecl", ident"gcsafe")) 721 | 722 | let objcSendProc = case flavor 723 | of fpret: bindSym"objc_msgSend_fpret" 724 | of stret: bindSym"objc_msgSend_stret" 725 | else: bindSym"objc_msgSend" 726 | 727 | let sendProc = newTree(nnkCast, procTy, objcSendProc) 728 | 729 | let castSendProc = newTree(nnkLetSection, newIdentDefs(performSend, newEmptyNode(), sendProc)) 730 | 731 | let call = newCall(performSend) 732 | 733 | let (args, argTypes) = body.getArgsAndTypes() 734 | 735 | if flavor == stret: 736 | call.add(newCall("addr", ident"result")) 737 | 738 | call.add(firstArg) 739 | 740 | if name.len == 0: 741 | name = guessSelectorNameFromProc(body) 742 | 743 | call.add(newCall(bindSym"getSelector", newLit(name))) # selector 744 | 745 | for i in 1 ..< args.len: 746 | senderParams.add(newIdentDefs(args[i], argTypes[i], newEmptyNode())) 747 | call.add(args[i]) 748 | 749 | result = newStmtList(castSendProc, call) 750 | 751 | macro objc*(name: untyped, body: untyped = nil): untyped = 752 | var (name, body) = unpackPragmaParams(name, body) 753 | var retType = body.params[0] 754 | if retType.kind == nnkEmpty: retType = ident"void" 755 | 756 | let (args, argTypes) = body.getArgsAndTypes() 757 | 758 | let firstArgTyp = argTypes[0] 759 | let isStatic = firstArgTyp.kind == nnkBracketExpr and firstArgTyp[0].kind == nnkIdent and $(firstArgTyp[0]) == "typedesc" 760 | let firstArg = if isStatic: 761 | newCall(ident"objcClass", args[0]) 762 | else: 763 | args[0] 764 | 765 | result = copyNimTree(body) 766 | result.body = newCall(bindSym"objcAux", 767 | newCall(bindSym"msgSendFlavorForRetType", retType), 768 | firstArg, 769 | newLit(name), body) 770 | result.addPragma(ident"inline") 771 | 772 | proc NSLog*(str: NSString) {.importc, varargs.} 773 | 774 | proc retainAux(o: NSObject): NSObject {.objc: "retain".} 775 | template retain*[T: NSObject](o: T): T = cast[T](retainAux(o)) 776 | proc release*(o: NSObject) {.objc.} 777 | proc alloc*[T: NSObject](n: typedesc[T]): T {.objc: "alloc".} 778 | proc autorelease*[T: NSObject](n: T): T {.objc: "autorelease", discardable.} 779 | proc initAux(v: NSObject): NSObject {.objc: "init".} 780 | proc init*[T: NSObject](v: T): T {.inline.} = cast[T](initAux(v)) 781 | 782 | proc isKindOfClass(o: NSObject, c: ObjcClass): bool {.objc: "isKindOfClass:".} 783 | proc isKindOfClass*(o: NSObject, c: typedesc): bool = o.isKindOfClass(c.objcClass()) 784 | proc encodeType*[T](t:typedesc[T]):string = 785 | # https://nshipster.com/type-encodings/ 786 | when t is char: 787 | return "c" 788 | elif t is uint8: 789 | return "C" 790 | elif t is int: 791 | return "i" 792 | elif t is uint: 793 | return "I" 794 | elif t is cshort: 795 | return "s" 796 | elif t is cushort: 797 | return "S" 798 | elif t is int32: 799 | return "l" 800 | elif t is uint32: 801 | return "L" 802 | elif t is int64: 803 | return "q" 804 | elif t is uint64: 805 | return "Q" 806 | elif t is cfloat: 807 | return "f" 808 | elif t is cdouble: 809 | return "d" 810 | elif t is bool: 811 | return "B" 812 | elif t is cstring: 813 | return "*" 814 | elif t is ObjcClass: 815 | return "#" 816 | elif t is typedesc[NSObject]: 817 | return "#" 818 | elif t is NSObject: 819 | return "@" 820 | elif t is ID: 821 | return "@" 822 | elif t is SEL: 823 | return ":" 824 | elif t is void: 825 | return "v" 826 | 827 | macro getProcEncode*(y: typed): untyped = 828 | y.expectKind {nnkSym, nnkCast} 829 | var x = if y.kind == nnkSym: y.getImpl() else: y[1].getImpl() 830 | var j = newCall(bindSym"join") 831 | let encode = bindSym"encodeType" 832 | var ab = nnkBracket.newTree() 833 | x.expectKind nnkProcDef 834 | for p in x.params: 835 | if p.kind == nnkIdentDefs: 836 | ab.add newCall(encode, newCall(ident"type", p[1])) 837 | elif p.kind == nnkEmpty: 838 | ab.add newCall(encode, ident"void") 839 | elif p.kind == nnkSym: 840 | ab.add newCall(encode, newCall(ident"type", p)) 841 | j.add ab 842 | result = nnkStaticExpr.newTree(j) 843 | 844 | template addMethod*[T](cls: ObjcClass; name: SEL; imp: T): bool = 845 | class_addMethod(cls, name, cast[IMP](imp), getProcEncode(imp)) 846 | 847 | template replaceMethod*[T](cls: ObjcClass; name: SEL; imp: T): IMP = 848 | class_replaceMethod(cls, name, cast[IMP](imp), getProcEncode(imp)) 849 | -------------------------------------------------------------------------------- /darwin/core_foundation/cfarray.nim: -------------------------------------------------------------------------------- 1 | import cfbase 2 | 3 | type 4 | CFAbstractArray = ptr object of CFPropertyList # CFArray 5 | CFAbstractMutableArray = ptr object of CFAbstractArray # CFMutableArray 6 | 7 | CFArray*[T] = ptr object of CFAbstractArray 8 | CFMutableArray*[T] = ptr object of CFArray[T] 9 | 10 | CFAraryRetainCallBack* = proc(allocator: CFAllocator, value: pointer): pointer {.cdecl.} 11 | CFAraryReleaseCallBack* = proc(allocator: CFAllocator, value: pointer) {.cdecl.} 12 | CFAraryCopyDescriptionCallBack* = proc(value: pointer): CFString {.cdecl.} 13 | CFAraryEqualCallBack* = proc(value1, value2: pointer): Boolean {.cdecl.} 14 | CFAraryHashCallBack* = proc(value: pointer): CFHashCode {.cdecl.} 15 | 16 | CFArrayCallBacks* {.byref.} = object 17 | version*: CFIndex 18 | retain*: CFAraryRetainCallBack 19 | release*: CFAraryReleaseCallBack 20 | copyDescription*: CFAraryCopyDescriptionCallBack 21 | equal*: CFAraryEqualCallBack 22 | 23 | var 24 | cfTypeCallbacks {.importc: "kCFTypeArrayCallBacks".}: CFArrayCallBacks 25 | 26 | template kCFTypeArrayCallBacks*: CFArrayCallBacks = 27 | let a = cfTypeCallbacks; a 28 | 29 | 30 | proc CFArrayGetTypeID*(): CFTypeID {.importc.} 31 | 32 | proc CFArrayCreateMutableAbstract(allocator: CFAllocator, capacity: CFIndex, callBacks: CFArrayCallBacks): CFAbstractMutableArray {.importc: "CFArrayCreateMutable".} 33 | 34 | proc CFArrayCreateMutable*[V](allocator: CFAllocator, capacity: CFIndex, callBacks: CFArrayCallBacks | pointer): CFMutableArray[V] {.inline.} = 35 | cast[CFMutableArray[V]](CFArrayCreateMutableAbstract(allocator, capacity, callBacks)) 36 | 37 | proc CFArrayGetCount(theArray: CFAbstractArray): int {.importc.} 38 | proc CFArrayGetValueAtIndex(theArray: CFAbstractArray, idx: CFIndex): pointer {.importc.} 39 | proc CFArrayAppendValue(theArray: CFAbstractMutableArray, value: pointer) {.importc.} 40 | 41 | proc CFArrayCreate*[T](allocator: CFAllocator, values: T, numValues: CFIndex, callBacks: CFArrayCallBacks | pointer): CFArray[T] {.importc: "CFArrayCreate".} 42 | proc CFArrayCreateCopy*[T](allocator: CFAllocator, theArray: CFArray[T]): CFArray[T] {.importc: "CFArrayCreateCopy".} 43 | proc len*(a: CFAbstractArray): int {.inline.} = CFArrayGetCount(a) 44 | proc `[]`*[T](a: CFArray[T], idx: int): T {.inline.} = cast[T](CFArrayGetValueAtIndex(a, idx)) 45 | proc add*[T](a: CFMutableArray[T], v: T) {.inline.} = CFArrayAppendValue(cast[CFAbstractMutableArray](a), cast[pointer](v)) 46 | 47 | iterator items*[T](a: CFArray[T]): T = 48 | let c = a.len 49 | for i in 0 ..< c: yield a[i] 50 | 51 | iterator pairs*[T](a: CFArray[T]): (int, T) = 52 | let c = a.len 53 | for i in 0 ..< c: yield (i, a[i]) 54 | 55 | # #include 56 | 57 | # CF_IMPLICIT_BRIDGING_ENABLED 58 | # CF_EXTERN_C_BEGIN 59 | 60 | # /*! 61 | # @typedef CFArrayCallBacks 62 | # Structure containing the callbacks of a CFArray. 63 | # @field version The version number of the structure type being passed 64 | # in as a parameter to the CFArray creation functions. This 65 | # structure is version 0. 66 | # @field retain The callback used to add a retain for the array on 67 | # values as they are put into the array. This callback returns 68 | # the value to store in the array, which is usually the value 69 | # parameter passed to this callback, but may be a different 70 | # value if a different value should be stored in the array. 71 | # The array's allocator is passed as the first argument. 72 | # @field release The callback used to remove a retain previously added 73 | # for the array from values as they are removed from the 74 | # array. The array's allocator is passed as the first 75 | # argument. 76 | # @field copyDescription The callback used to create a descriptive 77 | # string representation of each value in the array. This is 78 | # used by the CFCopyDescription() function. 79 | # @field equal The callback used to compare values in the array for 80 | # equality for some operations. 81 | # */ 82 | # typedef const void * (*CFArrayRetainCallBack)(CFAllocatorRef allocator, const void *value); 83 | # typedef void (*CFArrayReleaseCallBack)(CFAllocatorRef allocator, const void *value); 84 | # typedef CFStringRef (*CFArrayCopyDescriptionCallBack)(const void *value); 85 | # typedef Boolean (*CFArrayEqualCallBack)(const void *value1, const void *value2); 86 | # typedef struct { 87 | # CFIndex version; 88 | # CFArrayRetainCallBack retain; 89 | # CFArrayReleaseCallBack release; 90 | # CFArrayCopyDescriptionCallBack copyDescription; 91 | # CFArrayEqualCallBack equal; 92 | # } CFArrayCallBacks; 93 | 94 | # /*! 95 | # @constant kCFTypeArrayCallBacks 96 | # Predefined CFArrayCallBacks structure containing a set of callbacks 97 | # appropriate for use when the values in a CFArray are all CFTypes. 98 | # */ 99 | # CF_EXPORT 100 | # const CFArrayCallBacks kCFTypeArrayCallBacks; 101 | 102 | # /*! 103 | # @typedef CFArrayApplierFunction 104 | # Type of the callback function used by the apply functions of 105 | # CFArrays. 106 | # @param value The current value from the array. 107 | # @param context The user-defined context parameter given to the apply 108 | # function. 109 | # */ 110 | # typedef void (*CFArrayApplierFunction)(const void *value, void *context); 111 | 112 | # /*! 113 | # @typedef CFArrayRef 114 | # This is the type of a reference to immutable CFArrays. 115 | # */ 116 | # typedef const struct CF_BRIDGED_TYPE(NSArray) __CFArray * CFArrayRef; 117 | 118 | # /*! 119 | # @typedef CFMutableArrayRef 120 | # This is the type of a reference to mutable CFArrays. 121 | # */ 122 | # typedef struct CF_BRIDGED_MUTABLE_TYPE(NSMutableArray) __CFArray * CFMutableArrayRef; 123 | 124 | # /*! 125 | # @function CFArrayGetTypeID 126 | # Returns the type identifier of all CFArray instances. 127 | # */ 128 | # CF_EXPORT 129 | # CFTypeID CFArrayGetTypeID(void); 130 | 131 | # /*! 132 | # @function CFArrayCreate 133 | # Creates a new immutable array with the given values. 134 | # @param allocator The CFAllocator which should be used to allocate 135 | # memory for the array and its storage for values. This 136 | # parameter may be NULL in which case the current default 137 | # CFAllocator is used. If this reference is not a valid 138 | # CFAllocator, the behavior is undefined. 139 | # @param values A C array of the pointer-sized values to be in the 140 | # array. The values in the array are ordered in the same order 141 | # in which they appear in this C array. This parameter may be 142 | # NULL if the numValues parameter is 0. This C array is not 143 | # changed or freed by this function. If this parameter is not 144 | # a valid pointer to a C array of at least numValues pointers, 145 | # the behavior is undefined. 146 | # @param numValues The number of values to copy from the values C 147 | # array into the CFArray. This number will be the count of the 148 | # array. 149 | # If this parameter is negative, or greater than the number of 150 | # values actually in the value's C array, the behavior is 151 | # undefined. 152 | # @param callBacks A pointer to a CFArrayCallBacks structure 153 | # initialized with the callbacks for the array to use on each 154 | # value in the array. The retain callback will be used within 155 | # this function, for example, to retain all of the new values 156 | # from the values C array. A copy of the contents of the 157 | # callbacks structure is made, so that a pointer to a 158 | # structure on the stack can be passed in, or can be reused 159 | # for multiple array creations. If the version field of this 160 | # callbacks structure is not one of the defined ones for 161 | # CFArray, the behavior is undefined. The retain field may be 162 | # NULL, in which case the CFArray will do nothing to add a 163 | # retain to the contained values for the array. The release 164 | # field may be NULL, in which case the CFArray will do nothing 165 | # to remove the array's retain (if any) on the values when the 166 | # array is destroyed. If the copyDescription field is NULL, 167 | # the array will create a simple description for the value. If 168 | # the equal field is NULL, the array will use pointer equality 169 | # to test for equality of values. This callbacks parameter 170 | # itself may be NULL, which is treated as if a valid structure 171 | # of version 0 with all fields NULL had been passed in. 172 | # Otherwise, if any of the fields are not valid pointers to 173 | # functions of the correct type, or this parameter is not a 174 | # valid pointer to a CFArrayCallBacks callbacks structure, 175 | # the behavior is undefined. If any of the values put into the 176 | # array is not one understood by one of the callback functions 177 | # the behavior when that callback function is used is 178 | # undefined. 179 | # @result A reference to the new immutable CFArray. 180 | # */ 181 | # CF_EXPORT 182 | # CFArrayRef CFArrayCreate(CFAllocatorRef allocator, const void **values, CFIndex numValues, const CFArrayCallBacks *callBacks); 183 | 184 | # /*! 185 | # @function CFArrayCreateCopy 186 | # Creates a new immutable array with the values from the given array. 187 | # @param allocator The CFAllocator which should be used to allocate 188 | # memory for the array and its storage for values. This 189 | # parameter may be NULL in which case the current default 190 | # CFAllocator is used. If this reference is not a valid 191 | # CFAllocator, the behavior is undefined. 192 | # @param theArray The array which is to be copied. The values from the 193 | # array are copied as pointers into the new array (that is, 194 | # the values themselves are copied, not that which the values 195 | # point to, if anything). However, the values are also 196 | # retained by the new array. The count of the new array will 197 | # be the same as the given array. The new array uses the same 198 | # callbacks as the array to be copied. If this parameter is 199 | # not a valid CFArray, the behavior is undefined. 200 | # @result A reference to the new immutable CFArray. 201 | # */ 202 | # CF_EXPORT 203 | # CFArrayRef CFArrayCreateCopy(CFAllocatorRef allocator, CFArrayRef theArray); 204 | 205 | # /*! 206 | # @function CFArrayCreateMutable 207 | # Creates a new empty mutable array. 208 | # @param allocator The CFAllocator which should be used to allocate 209 | # memory for the array and its storage for values. This 210 | # parameter may be NULL in which case the current default 211 | # CFAllocator is used. If this reference is not a valid 212 | # CFAllocator, the behavior is undefined. 213 | # @param capacity A hint about the number of values that will be held 214 | # by the CFArray. Pass 0 for no hint. The implementation may 215 | # ignore this hint, or may use it to optimize various 216 | # operations. An array's actual capacity is only limited by 217 | # address space and available memory constraints). If this 218 | # parameter is negative, the behavior is undefined. 219 | # @param callBacks A pointer to a CFArrayCallBacks structure 220 | # initialized with the callbacks for the array to use on each 221 | # value in the array. A copy of the contents of the 222 | # callbacks structure is made, so that a pointer to a 223 | # structure on the stack can be passed in, or can be reused 224 | # for multiple array creations. If the version field of this 225 | # callbacks structure is not one of the defined ones for 226 | # CFArray, the behavior is undefined. The retain field may be 227 | # NULL, in which case the CFArray will do nothing to add a 228 | # retain to the contained values for the array. The release 229 | # field may be NULL, in which case the CFArray will do nothing 230 | # to remove the array's retain (if any) on the values when the 231 | # array is destroyed. If the copyDescription field is NULL, 232 | # the array will create a simple description for the value. If 233 | # the equal field is NULL, the array will use pointer equality 234 | # to test for equality of values. This callbacks parameter 235 | # itself may be NULL, which is treated as if a valid structure 236 | # of version 0 with all fields NULL had been passed in. 237 | # Otherwise, if any of the fields are not valid pointers to 238 | # functions of the correct type, or this parameter is not a 239 | # valid pointer to a CFArrayCallBacks callbacks structure, 240 | # the behavior is undefined. If any of the values put into the 241 | # array is not one understood by one of the callback functions 242 | # the behavior when that callback function is used is 243 | # undefined. 244 | # @result A reference to the new mutable CFArray. 245 | # */ 246 | # CF_EXPORT 247 | # CFMutableArrayRef CFArrayCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFArrayCallBacks *callBacks); 248 | 249 | # /*! 250 | # @function CFArrayCreateMutableCopy 251 | # Creates a new mutable array with the values from the given array. 252 | # @param allocator The CFAllocator which should be used to allocate 253 | # memory for the array and its storage for values. This 254 | # parameter may be NULL in which case the current default 255 | # CFAllocator is used. If this reference is not a valid 256 | # CFAllocator, the behavior is undefined. 257 | # @param capacity A hint about the number of values that will be held 258 | # by the CFArray. Pass 0 for no hint. The implementation may 259 | # ignore this hint, or may use it to optimize various 260 | # operations. An array's actual capacity is only limited by 261 | # address space and available memory constraints). 262 | # This parameter must be greater than or equal 263 | # to the count of the array which is to be copied, or the 264 | # behavior is undefined. If this parameter is negative, the 265 | # behavior is undefined. 266 | # @param theArray The array which is to be copied. The values from the 267 | # array are copied as pointers into the new array (that is, 268 | # the values themselves are copied, not that which the values 269 | # point to, if anything). However, the values are also 270 | # retained by the new array. The count of the new array will 271 | # be the same as the given array. The new array uses the same 272 | # callbacks as the array to be copied. If this parameter is 273 | # not a valid CFArray, the behavior is undefined. 274 | # @result A reference to the new mutable CFArray. 275 | # */ 276 | # CF_EXPORT 277 | # CFMutableArrayRef CFArrayCreateMutableCopy(CFAllocatorRef allocator, CFIndex capacity, CFArrayRef theArray); 278 | 279 | # /*! 280 | # @function CFArrayGetCount 281 | # Returns the number of values currently in the array. 282 | # @param theArray The array to be queried. If this parameter is not a valid 283 | # CFArray, the behavior is undefined. 284 | # @result The number of values in the array. 285 | # */ 286 | # CF_EXPORT 287 | # CFIndex CFArrayGetCount(CFArrayRef theArray); 288 | 289 | # /*! 290 | # @function CFArrayGetCountOfValue 291 | # Counts the number of times the given value occurs in the array. 292 | # @param theArray The array to be searched. If this parameter is not a 293 | # valid CFArray, the behavior is undefined. 294 | # @param range The range within the array to search. If the range 295 | # location or end point (defined by the location plus length 296 | # minus 1) is outside the index space of the array (0 to 297 | # N-1 inclusive, where N is the count of the array), the 298 | # behavior is undefined. If the range length is negative, the 299 | # behavior is undefined. The range may be empty (length 0). 300 | # @param value The value for which to find matches in the array. The 301 | # equal() callback provided when the array was created is 302 | # used to compare. If the equal() callback was NULL, pointer 303 | # equality (in C, ==) is used. If value, or any of the values 304 | # in the array, are not understood by the equal() callback, 305 | # the behavior is undefined. 306 | # @result The number of times the given value occurs in the array, 307 | # within the specified range. 308 | # */ 309 | # CF_EXPORT 310 | # CFIndex CFArrayGetCountOfValue(CFArrayRef theArray, CFRange range, const void *value); 311 | 312 | # /*! 313 | # @function CFArrayContainsValue 314 | # Reports whether or not the value is in the array. 315 | # @param theArray The array to be searched. If this parameter is not a 316 | # valid CFArray, the behavior is undefined. 317 | # @param range The range within the array to search. If the range 318 | # location or end point (defined by the location plus length 319 | # minus 1) is outside the index space of the array (0 to 320 | # N-1 inclusive, where N is the count of the array), the 321 | # behavior is undefined. If the range length is negative, the 322 | # behavior is undefined. The range may be empty (length 0). 323 | # @param value The value for which to find matches in the array. The 324 | # equal() callback provided when the array was created is 325 | # used to compare. If the equal() callback was NULL, pointer 326 | # equality (in C, ==) is used. If value, or any of the values 327 | # in the array, are not understood by the equal() callback, 328 | # the behavior is undefined. 329 | # @result true, if the value is in the specified range of the array, 330 | # otherwise false. 331 | # */ 332 | # CF_EXPORT 333 | # Boolean CFArrayContainsValue(CFArrayRef theArray, CFRange range, const void *value); 334 | 335 | # /*! 336 | # @function CFArrayGetValueAtIndex 337 | # Retrieves the value at the given index. 338 | # @param theArray The array to be queried. If this parameter is not a 339 | # valid CFArray, the behavior is undefined. 340 | # @param idx The index of the value to retrieve. If the index is 341 | # outside the index space of the array (0 to N-1 inclusive, 342 | # where N is the count of the array), the behavior is 343 | # undefined. 344 | # @result The value with the given index in the array. 345 | # */ 346 | # CF_EXPORT 347 | # const void *CFArrayGetValueAtIndex(CFArrayRef theArray, CFIndex idx); 348 | 349 | # /*! 350 | # @function CFArrayGetValues 351 | # Fills the buffer with values from the array. 352 | # @param theArray The array to be queried. If this parameter is not a 353 | # valid CFArray, the behavior is undefined. 354 | # @param range The range of values within the array to retrieve. If 355 | # the range location or end point (defined by the location 356 | # plus length minus 1) is outside the index space of the 357 | # array (0 to N-1 inclusive, where N is the count of the 358 | # array), the behavior is undefined. If the range length is 359 | # negative, the behavior is undefined. The range may be empty 360 | # (length 0), in which case no values are put into the buffer. 361 | # @param values A C array of pointer-sized values to be filled with 362 | # values from the array. The values in the C array are ordered 363 | # in the same order in which they appear in the array. If this 364 | # parameter is not a valid pointer to a C array of at least 365 | # range.length pointers, the behavior is undefined. 366 | # */ 367 | # CF_EXPORT 368 | # void CFArrayGetValues(CFArrayRef theArray, CFRange range, const void **values); 369 | 370 | # /*! 371 | # @function CFArrayApplyFunction 372 | # Calls a function once for each value in the array. 373 | # @param theArray The array to be operated upon. If this parameter is not 374 | # a valid CFArray, the behavior is undefined. 375 | # @param range The range of values within the array to which to apply 376 | # the function. If the range location or end point (defined by 377 | # the location plus length minus 1) is outside the index 378 | # space of the array (0 to N-1 inclusive, where N is the count 379 | # of the array), the behavior is undefined. If the range 380 | # length is negative, the behavior is undefined. The range may 381 | # be empty (length 0). 382 | # @param applier The callback function to call once for each value in 383 | # the given range in the array. If this parameter is not a 384 | # pointer to a function of the correct prototype, the behavior 385 | # is undefined. If there are values in the range which the 386 | # applier function does not expect or cannot properly apply 387 | # to, the behavior is undefined. 388 | # @param context A pointer-sized user-defined value, which is passed 389 | # as the second parameter to the applier function, but is 390 | # otherwise unused by this function. If the context is not 391 | # what is expected by the applier function, the behavior is 392 | # undefined. 393 | # */ 394 | # CF_EXPORT 395 | # void CFArrayApplyFunction(CFArrayRef theArray, CFRange range, CFArrayApplierFunction CF_NOESCAPE applier, void *context); 396 | 397 | # /*! 398 | # @function CFArrayGetFirstIndexOfValue 399 | # Searches the array for the value. 400 | # @param theArray The array to be searched. If this parameter is not a 401 | # valid CFArray, the behavior is undefined. 402 | # @param range The range within the array to search. If the range 403 | # location or end point (defined by the location plus length 404 | # minus 1) is outside the index space of the array (0 to 405 | # N-1 inclusive, where N is the count of the array), the 406 | # behavior is undefined. If the range length is negative, the 407 | # behavior is undefined. The range may be empty (length 0). 408 | # The search progresses from the smallest index defined by 409 | # the range to the largest. 410 | # @param value The value for which to find a match in the array. The 411 | # equal() callback provided when the array was created is 412 | # used to compare. If the equal() callback was NULL, pointer 413 | # equality (in C, ==) is used. If value, or any of the values 414 | # in the array, are not understood by the equal() callback, 415 | # the behavior is undefined. 416 | # @result The lowest index of the matching values in the range, or 417 | # kCFNotFound if no value in the range matched. 418 | # */ 419 | # CF_EXPORT 420 | # CFIndex CFArrayGetFirstIndexOfValue(CFArrayRef theArray, CFRange range, const void *value); 421 | 422 | # /*! 423 | # @function CFArrayGetLastIndexOfValue 424 | # Searches the array for the value. 425 | # @param theArray The array to be searched. If this parameter is not a 426 | # valid CFArray, the behavior is undefined. 427 | # @param range The range within the array to search. If the range 428 | # location or end point (defined by the location plus length 429 | # minus 1) is outside the index space of the array (0 to 430 | # N-1 inclusive, where N is the count of the array), the 431 | # behavior is undefined. If the range length is negative, the 432 | # behavior is undefined. The range may be empty (length 0). 433 | # The search progresses from the largest index defined by the 434 | # range to the smallest. 435 | # @param value The value for which to find a match in the array. The 436 | # equal() callback provided when the array was created is 437 | # used to compare. If the equal() callback was NULL, pointer 438 | # equality (in C, ==) is used. If value, or any of the values 439 | # in the array, are not understood by the equal() callback, 440 | # the behavior is undefined. 441 | # @result The highest index of the matching values in the range, or 442 | # kCFNotFound if no value in the range matched. 443 | # */ 444 | # CF_EXPORT 445 | # CFIndex CFArrayGetLastIndexOfValue(CFArrayRef theArray, CFRange range, const void *value); 446 | 447 | # /*! 448 | # @function CFArrayBSearchValues 449 | # Searches the array for the value using a binary search algorithm. 450 | # @param theArray The array to be searched. If this parameter is not a 451 | # valid CFArray, the behavior is undefined. If the array is 452 | # not sorted from least to greatest according to the 453 | # comparator function, the behavior is undefined. 454 | # @param range The range within the array to search. If the range 455 | # location or end point (defined by the location plus length 456 | # minus 1) is outside the index space of the array (0 to 457 | # N-1 inclusive, where N is the count of the array), the 458 | # behavior is undefined. If the range length is negative, the 459 | # behavior is undefined. The range may be empty (length 0). 460 | # @param value The value for which to find a match in the array. If 461 | # value, or any of the values in the array, are not understood 462 | # by the comparator callback, the behavior is undefined. 463 | # @param comparator The function with the comparator function type 464 | # signature which is used in the binary search operation to 465 | # compare values in the array with the given value. If this 466 | # parameter is not a pointer to a function of the correct 467 | # prototype, the behavior is undefined. If there are values 468 | # in the range which the comparator function does not expect 469 | # or cannot properly compare, the behavior is undefined. 470 | # @param context A pointer-sized user-defined value, which is passed 471 | # as the third parameter to the comparator function, but is 472 | # otherwise unused by this function. If the context is not 473 | # what is expected by the comparator function, the behavior is 474 | # undefined. 475 | # @result The return value is either 1) the index of a value that 476 | # matched, if the target value matches one or more in the 477 | # range, 2) greater than or equal to the end point of the 478 | # range, if the value is greater than all the values in the 479 | # range, or 3) the index of the value greater than the target 480 | # value, if the value lies between two of (or less than all 481 | # of) the values in the range. 482 | # */ 483 | # CF_EXPORT 484 | # CFIndex CFArrayBSearchValues(CFArrayRef theArray, CFRange range, const void *value, CFComparatorFunction comparator, void *context); 485 | 486 | # /*! 487 | # @function CFArrayAppendValue 488 | # Adds the value to the array giving it a new largest index. 489 | # @param theArray The array to which the value is to be added. If this 490 | # parameter is not a valid mutable CFArray, the behavior is 491 | # undefined. 492 | # @param value The value to add to the array. The value is retained by 493 | # the array using the retain callback provided when the array 494 | # was created. If the value is not of the sort expected by the 495 | # retain callback, the behavior is undefined. The value is 496 | # assigned to the index one larger than the previous largest 497 | # index, and the count of the array is increased by one. 498 | # */ 499 | # CF_EXPORT 500 | # void CFArrayAppendValue(CFMutableArrayRef theArray, const void *value); 501 | 502 | # /*! 503 | # @function CFArrayInsertValueAtIndex 504 | # Adds the value to the array, giving it the given index. 505 | # @param theArray The array to which the value is to be added. If this 506 | # parameter is not a valid mutable CFArray, the behavior is 507 | # undefined. 508 | # @param idx The index to which to add the new value. If the index is 509 | # outside the index space of the array (0 to N inclusive, 510 | # where N is the count of the array before the operation), the 511 | # behavior is undefined. If the index is the same as N, this 512 | # function has the same effect as CFArrayAppendValue(). 513 | # @param value The value to add to the array. The value is retained by 514 | # the array using the retain callback provided when the array 515 | # was created. If the value is not of the sort expected by the 516 | # retain callback, the behavior is undefined. The value is 517 | # assigned to the given index, and all values with equal and 518 | # larger indices have their indexes increased by one. 519 | # */ 520 | # CF_EXPORT 521 | # void CFArrayInsertValueAtIndex(CFMutableArrayRef theArray, CFIndex idx, const void *value); 522 | 523 | # /*! 524 | # @function CFArraySetValueAtIndex 525 | # Changes the value with the given index in the array. 526 | # @param theArray The array in which the value is to be changed. If this 527 | # parameter is not a valid mutable CFArray, the behavior is 528 | # undefined. 529 | # @param idx The index to which to set the new value. If the index is 530 | # outside the index space of the array (0 to N inclusive, 531 | # where N is the count of the array before the operation), the 532 | # behavior is undefined. If the index is the same as N, this 533 | # function has the same effect as CFArrayAppendValue(). 534 | # @param value The value to set in the array. The value is retained by 535 | # the array using the retain callback provided when the array 536 | # was created, and the previous value with that index is 537 | # released. If the value is not of the sort expected by the 538 | # retain callback, the behavior is undefined. The indices of 539 | # other values is not affected. 540 | # */ 541 | # CF_EXPORT 542 | # void CFArraySetValueAtIndex(CFMutableArrayRef theArray, CFIndex idx, const void *value); 543 | 544 | # /*! 545 | # @function CFArrayRemoveValueAtIndex 546 | # Removes the value with the given index from the array. 547 | # @param theArray The array from which the value is to be removed. If 548 | # this parameter is not a valid mutable CFArray, the behavior 549 | # is undefined. 550 | # @param idx The index from which to remove the value. If the index is 551 | # outside the index space of the array (0 to N-1 inclusive, 552 | # where N is the count of the array before the operation), the 553 | # behavior is undefined. 554 | # */ 555 | # CF_EXPORT 556 | # void CFArrayRemoveValueAtIndex(CFMutableArrayRef theArray, CFIndex idx); 557 | 558 | # /*! 559 | # @function CFArrayRemoveAllValues 560 | # Removes all the values from the array, making it empty. 561 | # @param theArray The array from which all of the values are to be 562 | # removed. If this parameter is not a valid mutable CFArray, 563 | # the behavior is undefined. 564 | # */ 565 | # CF_EXPORT 566 | # void CFArrayRemoveAllValues(CFMutableArrayRef theArray); 567 | 568 | # /*! 569 | # @function CFArrayReplaceValues 570 | # Replaces a range of values in the array. 571 | # @param theArray The array from which all of the values are to be 572 | # removed. If this parameter is not a valid mutable CFArray, 573 | # the behavior is undefined. 574 | # @param range The range of values within the array to replace. If the 575 | # range location or end point (defined by the location plus 576 | # length minus 1) is outside the index space of the array (0 577 | # to N inclusive, where N is the count of the array), the 578 | # behavior is undefined. If the range length is negative, the 579 | # behavior is undefined. The range may be empty (length 0), 580 | # in which case the new values are merely inserted at the 581 | # range location. 582 | # @param newValues A C array of the pointer-sized values to be placed 583 | # into the array. The new values in the array are ordered in 584 | # the same order in which they appear in this C array. This 585 | # parameter may be NULL if the newCount parameter is 0. This 586 | # C array is not changed or freed by this function. If this 587 | # parameter is not a valid pointer to a C array of at least 588 | # newCount pointers, the behavior is undefined. 589 | # @param newCount The number of values to copy from the values C 590 | # array into the CFArray. If this parameter is different than 591 | # the range length, the excess newCount values will be 592 | # inserted after the range, or the excess range values will be 593 | # deleted. This parameter may be 0, in which case no new 594 | # values are replaced into the array and the values in the 595 | # range are simply removed. If this parameter is negative, or 596 | # greater than the number of values actually in the newValues 597 | # C array, the behavior is undefined. 598 | # */ 599 | # CF_EXPORT 600 | # void CFArrayReplaceValues(CFMutableArrayRef theArray, CFRange range, const void **newValues, CFIndex newCount); 601 | 602 | # /*! 603 | # @function CFArrayExchangeValuesAtIndices 604 | # Exchanges the values at two indices of the array. 605 | # @param theArray The array of which the values are to be swapped. If 606 | # this parameter is not a valid mutable CFArray, the behavior 607 | # is undefined. 608 | # @param idx1 The first index whose values should be swapped. If the 609 | # index is outside the index space of the array (0 to N-1 610 | # inclusive, where N is the count of the array before the 611 | # operation), the behavior is undefined. 612 | # @param idx2 The second index whose values should be swapped. If the 613 | # index is outside the index space of the array (0 to N-1 614 | # inclusive, where N is the count of the array before the 615 | # operation), the behavior is undefined. 616 | # */ 617 | # CF_EXPORT 618 | # void CFArrayExchangeValuesAtIndices(CFMutableArrayRef theArray, CFIndex idx1, CFIndex idx2); 619 | 620 | # /*! 621 | # @function CFArraySortValues 622 | # Sorts the values in the array using the given comparison function. 623 | # @param theArray The array whose values are to be sorted. If this 624 | # parameter is not a valid mutable CFArray, the behavior is 625 | # undefined. 626 | # @param range The range of values within the array to sort. If the 627 | # range location or end point (defined by the location plus 628 | # length minus 1) is outside the index space of the array (0 629 | # to N-1 inclusive, where N is the count of the array), the 630 | # behavior is undefined. If the range length is negative, the 631 | # behavior is undefined. The range may be empty (length 0). 632 | # @param comparator The function with the comparator function type 633 | # signature which is used in the sort operation to compare 634 | # values in the array with the given value. If this parameter 635 | # is not a pointer to a function of the correct prototype, the 636 | # the behavior is undefined. If there are values in the array 637 | # which the comparator function does not expect or cannot 638 | # properly compare, the behavior is undefined. The values in 639 | # the range are sorted from least to greatest according to 640 | # this function. 641 | # @param context A pointer-sized user-defined value, which is passed 642 | # as the third parameter to the comparator function, but is 643 | # otherwise unused by this function. If the context is not 644 | # what is expected by the comparator function, the behavior is 645 | # undefined. 646 | # */ 647 | # CF_EXPORT 648 | # void CFArraySortValues(CFMutableArrayRef theArray, CFRange range, CFComparatorFunction comparator, void *context); 649 | 650 | # /*! 651 | # @function CFArrayAppendArray 652 | # Adds the values from an array to another array. 653 | # @param theArray The array to which values from the otherArray are to 654 | # be added. If this parameter is not a valid mutable CFArray, 655 | # the behavior is undefined. 656 | # @param otherArray The array providing the values to be added to the 657 | # array. If this parameter is not a valid CFArray, the 658 | # behavior is undefined. 659 | # @param otherRange The range within the otherArray from which to add 660 | # the values to the array. If the range location or end point 661 | # (defined by the location plus length minus 1) is outside 662 | # the index space of the otherArray (0 to N-1 inclusive, where 663 | # N is the count of the otherArray), the behavior is 664 | # undefined. The new values are retained by the array using 665 | # the retain callback provided when the array was created. If 666 | # the values are not of the sort expected by the retain 667 | # callback, the behavior is undefined. The values are assigned 668 | # to the indices one larger than the previous largest index 669 | # in the array, and beyond, and the count of the array is 670 | # increased by range.length. The values are assigned new 671 | # indices in the array from smallest to largest index in the 672 | # order in which they appear in the otherArray. 673 | # */ 674 | # CF_EXPORT 675 | # void CFArrayAppendArray(CFMutableArrayRef theArray, CFArrayRef otherArray, CFRange otherRange); 676 | 677 | # CF_EXTERN_C_END 678 | # CF_IMPLICIT_BRIDGING_DISABLED 679 | 680 | # #endif /* ! __COREFOUNDATION_CFARRAY__ */ 681 | 682 | --------------------------------------------------------------------------------