Skip to content

Instantly share code, notes, and snippets.

@qtxie
Last active March 1, 2016 08:12
Show Gist options
  • Save qtxie/bd0f03c49070944f447c to your computer and use it in GitHub Desktop.
Save qtxie/bd0f03c49070944f447c to your computer and use it in GitHub Desktop.
Red/System Mac OSX Window
Red/System [
Title: "Red/System Mac OSX Window"
Author: "Qingtian Xie"
File: %rs-mac-window.reds
Type: 'library
Tabs: 4
Rights: "Copyright (C) 2014 Qingtian Xie. All rights reserved."
Compiling: "rebview.exe -qs red.r -t Darwin rs-mac-window.reds"
Reference: {
https://github.com/CodaFi/C-Macs
http://www.smipple.net/snippet/moriyoshi/Using%20Objective-C%20ABI%20from%20within%20a%20pure%20C%20code.
}
Note: {
Just showing an empty window. The window cannot close properly.
please using Control + C to close it due to missing some delegate functions.
}
]
#define RTLD_LAZY 1
#define NSUtilityWindowMask 16
#define NSDocModalWindowMask 32
#define NSBorderlessWindowMask 0
#define NSTitledWindowMask 1
#define NSClosableWindowMask 2
#define NSMiniaturizableWindowMask 4
#define NSResizableWindowMask 8
#define NSIconWindowMask 64
#define NSMiniWindowMask 128
#import [
LIBC-file cdecl [
dlopen: "dlopen" [
dllpath [c-string!]
flags [integer!]
return: [integer!]
]
objc_getClass: "objc_getClass" [
class [c-string!]
return: [integer!]
]
objc_allocateClassPair: "objc_allocateClassPair" [
superclass [integer!]
name [c-string!]
extraBytes [integer!]
return: [integer!]
]
objc_registerClassPair: "objc_registerClassPair" [
class [integer!]
return: [integer!]
]
sel_getUid: "sel_getUid" [
name [c-string!]
return: [integer!]
]
class_addMethod: "class_addMethod" [
class [integer!]
name [integer!]
implement [integer!]
types [c-string!]
return: [integer!]
]
objc_msgSend: "objc_msgSend" [[variadic] return: [integer!]]
]
"/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation" cdecl [
CFStringCreateWithCString: "CFStringCreateWithCString" [
allocator [integer!]
cStr [c-string!]
encoding [integer!]
return: [integer!]
]
]
]
#define kCFStringEncodingUTF8 08000100h
#define CFString(cStr) [CFStringCreateWithCString 0 cStr kCFStringEncodingUTF8]
NSRect: alias struct! [
x [float32!]
y [float32!]
w [float32!]
h [float32!]
]
AppDelegate!: alias struct! [
isa [integer!]
window [integer!]
]
ns-app: 0
view-class: 0
delegate-class: 0
init-view: does [
view-class: objc_allocateClassPair objc_getClass "NSView" "RedView" 0
objc_registerClassPair view-class
]
destroy-app: func [
[cdecl]
self [AppDelegate!]
cmd [integer!]
app [integer!]
return: [integer!]
][
1
]
will-finish: func [
[cdecl]
self [AppDelegate!]
cmd [integer!]
notify [integer!]
/local
window [integer!]
view [integer!]
r [NSRect]
][
r: declare NSRect
r/x: as float32! 80.0
r/y: as float32! 80.0
r/w: as float32! 500.0
r/h: as float32! 500.0
window: objc_msgSend [objc_getClass "NSWindow" sel_getUid "alloc"]
window: objc_msgSend [
window sel_getUid
"initWithContentRect:styleMask:backing:defer:"
r/x r/y r/w r/h
NSTitledWindowMask or NSClosableWindowMask or NSResizableWindowMask
2 0
]
view: objc_msgSend [objc_getClass "RedView" sel_getUid "alloc"]
view: objc_msgSend [view sel_getUid "initWithFrame:" r/x r/y r/w r/h]
self/window: window
objc_msgSend [window sel_getUid "setContentView:" view]
objc_msgSend [window sel_getUid "setTitle:" CFString("Red Windows")]
objc_msgSend [window sel_getUid "becomeFirstResponder"]
objc_msgSend [window sel_getUid "makeKeyAndOrderFront:" self]
objc_msgSend [window sel_getUid "makeMainWindow" self]
1
]
create-delegate: does [
delegate-class: objc_allocateClassPair objc_getClass "NSObject" "RedAppDelegate" 0
class_addMethod delegate-class sel_getUid "applicationWillFinishLaunching:" as-integer :will-finish "v12@0:4@8"
class_addMethod delegate-class sel_getUid "applicationShouldTerminateAfterLastWindowClosed:" as-integer :destroy-app "B12@0:4@8"
objc_registerClassPair delegate-class
]
run-app: func [/local app [integer!] delegate [integer!]][
app: objc_msgSend [objc_getClass "NSApplication" sel_getUid "sharedApplication"]
delegate: objc_msgSend [objc_getClass "RedAppDelegate" sel_getUid "alloc"]
delegate: objc_msgSend [delegate sel_getUid "init"]
objc_msgSend [app sel_getUid "setDelegate:" delegate]
objc_msgSend [app sel_getUid "run"]
]
main-window: func [
/local
dll [integer!]
cls-pool [integer!]
pool [integer!]
hello [integer!]
cstr [c-string!]
][
dlopen "/System/Library/Frameworks/Foundation.framework/Versions/Current/Foundation" RTLD_LAZY
dlopen "/System/Library/Frameworks/AppKit.framework/Versions/Current/AppKit" RTLD_LAZY
cls-pool: objc_getClass "NSAutoreleasePool"
pool: objc_msgSend [cls-pool sel_getUid "alloc"]
objc_msgSend [pool sel_getUid "init"]
init-view
create-delegate
run-app
]
main-window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment