Changeset 7
- Timestamp:
- 06/11/06 15:20:38 (6 years ago)
- Files:
-
- trunk/Rakefile (modified) (2 diffs)
- trunk/docoa/appkit/nsbuttoncell.d (added)
- trunk/docoa/appkit/nspanel.d (added)
- trunk/docoa/objc/convenience.d (modified) (12 diffs)
- trunk/main.d (modified) (1 diff)
- trunk/winmain.d (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Rakefile
r6 r7 1 1 require 'rake/clean' 2 2 3 aresdir = '/Users/john/build/cvs/d/ares/ares' 3 4 PROG = './testcoco.app/Contents/MacOS/testcoco' 4 5 … … 8 9 9 10 10 SRC = FileList['winmain.d', 'objc_classinfos.d', 'docoa/objc/convenience.d' ]11 SRC = FileList['winmain.d', 'objc_classinfos.d', 'docoa/objc/convenience.d', 'docoa/objc/objc_classinfos.d'] 11 12 12 13 OBJ = SRC.ext('o') 13 14 14 DFLAGS = "-Iares -g -fdebug" 15 raise "specify ares directory here and remove this line" 15 DFLAGS = "-I#{aresdir} -g -fdebug" 16 16 LDFLAGS = "-framework Cocoa" 17 17 trunk/docoa/objc/convenience.d
r5 r7 7 7 8 8 private import docoa.objc.objc; 9 private import docoa.objc.objc_classinfos; 9 10 10 11 private import docoa.corefoundation.cfstring; … … 14 15 private import docoa.foundation.nsobjcruntime; 15 16 16 BOOL objc_create_class_definition(char[] name, char[] superclassName ) { 17 Class meta_class; 18 Class super_class; 19 Class new_class; 20 Class root_class; 21 22 // Ensure that the superclass exists and that someone 23 // hasn't already implemented a class with the same name 24 // 25 super_class = cast(Class)objc_lookUpClass(superclassName); 26 if (super_class is Nil) { 27 return NO; 28 } 29 30 if (cast(Class)objc_lookUpClass(name) !is Nil) { 31 return NO; 32 } 33 34 // Find the root class 35 // 36 root_class = super_class; 37 while(root_class.super_class !is Nil) { 38 root_class = root_class.super_class; 39 } 40 41 // Allocate space for the class and its metaclass 42 // 43 new_class = cast(Class)calloc(2, objc_class.sizeof); 44 if (!new_class) 45 throw new OutOfMemoryException(); 46 meta_class = &new_class[1]; 47 48 49 // setup class 50 new_class.isa = meta_class; 51 new_class.info = ObjCClassType.CLS_CLASS; 52 meta_class.info = ObjCClassType.CLS_META; 53 54 // Create a copy of the class name. 55 // For efficiency, we have the metaclass and the class itself 56 // to share this copy of the name, but this is not a requirement 57 // imposed by the runtime. 58 // 59 new_class.name = cast(char*)malloc(name.length + 1); 60 if (!new_class.name) 61 throw new OutOfMemoryException(); 62 memcpy(new_class.name, name.ptr, name.length); 63 meta_class.name = new_class.name; 64 65 // Allocate empty method lists. 66 // We can add methods later. 67 // 68 new_class.methodLists = cast(objc_method_list**)calloc(2, (objc_method_list*).sizeof); 69 if (!new_class.methodLists) 70 throw new OutOfMemoryException(); 71 *new_class.methodLists = cast(objc_method_list*)-1; 72 meta_class.methodLists = &new_class.methodLists[1]; 73 *meta_class.methodLists = cast(objc_method_list*)-1; 74 75 // Connect the class definition to the class hierarchy: 76 // Connect the class to the superclass. 77 // Connect the metaclass to the metaclass of the superclass. 78 // Connect the metaclass of the metaclass to the metaclass of the root class. 79 // 80 new_class.super_class = super_class; 81 meta_class.super_class = super_class.isa; 82 meta_class.isa = root_class.isa; 83 84 // Set the sizes of the class and the metaclass. 85 // 86 new_class.instance_size = super_class.instance_size; 87 meta_class.instance_size = meta_class.super_class.instance_size; 88 89 // Finally, register the class with the runtime. 90 // 91 objc_addClass(new_class); 92 return YES; 17 class DocoaException : Exception 18 { 19 this(char[] msg) { 20 super("Docoa Error: " ~ msg); 21 } 22 23 this(char[] msg, id obj) { 24 char[] str = msg; 25 str = str ~ "Info: " ~ get_info(obj); 26 super(str); 27 } 28 29 char[] get_info(id obj) { 30 Class klass = obj.isa; 31 char[] str = klass.name[0..(.strlen(klass.name))]; 32 return "Class name: " ~ str; 33 } 93 34 } 94 35 … … 105 46 meta_class.info = ObjCClassType.CLS_META; 106 47 107 new_class.name = cast(char*) malloc(name.length + 1);48 new_class.name = cast(char*)calloc(1, name.length + 1); 108 49 if (!new_class.name) 109 50 throw new OutOfMemoryException(); … … 111 52 meta_class.name = new_class.name; 112 53 113 debug { writef("setting up class: %s\n", new_class.name ); }54 debug { writef("setting up class: %s\n", new_class.name[0..(.strlen(new_class.name))]); } 114 55 115 56 // Allocate empty method lists. … … 143 84 objc_method_list* mlist; 144 85 145 mlist = cast(objc_method_list*)calloc(1, objc_method_list.sizeof + objc_method.sizeof * (size+1));86 mlist = cast(objc_method_list*)calloc(1, objc_method_list.sizeof + objc_method.sizeof * size); 146 87 if (!mlist) 147 88 throw new OutOfMemoryException(); … … 187 128 base_class = cast(Class)objc_lookUpClass(superclass_name); 188 129 if (base_class is Nil) { 189 return null;130 throw new DocoaException("No Such superclass: " ~ superclass_name); 190 131 } 191 132 … … 203 144 // the same name 204 145 // 205 debug { writef("class already implemented: %s\n", ((cast(Class)objc_lookUpClass(name) != Nil) ? "Yes" : "No")); }206 146 if ((intermediate_class = cast(Class)objc_lookUpClass(name)) != Nil) { 147 writef("Docoa Warning: Class already implemented, returning class."); 207 148 return intermediate_class; 208 149 } … … 214 155 root_class = root_class.super_class; 215 156 } 216 debug { writef("root class name: %s\n", root_class.name ); }157 debug { writef("root class name: %s\n", root_class.name[0..(.strlen(root_class.name))]); } 217 158 218 159 // Allocate space for the class and its metaclass … … 227 168 meta_class, 228 169 name, 229 base_class, 230 root_class, 231 base_class.instance_size, null, 232 null 170 base_class, root_class, 171 base_class.instance_size, 172 null, null 233 173 ); 234 174 … … 241 181 // 242 182 objc_addClass(intermediate_class); 243 debug { writef("added class %s to runtime.\n", intermediate_class.name ); }183 debug { writef("added class %s to runtime.\n", intermediate_class.name[0..(.strlen(intermediate_class.name))]); } 244 184 245 185 return intermediate_class; … … 250 190 } 251 191 192 // FIXME: doesn't work 252 193 char[] fromCFString(CFStringRef cfstr) { 253 194 char* cstr = null; … … 317 258 printf("iv offset: %i\n", var.ivar_offset); 318 259 } 260 261 class ObjCClass { 262 Class klass; 263 264 this(char[] name, char[] base_class = "NSObject") { 265 klass = objc_build_class_with_base(name, base_class); 266 assert(klass != Nil); 267 } 268 269 void info() { 270 char[] name = klass.name[0..(.strlen(klass.name))]; 271 showClassInfos(name); 272 } 273 274 Class objc_class() { 275 return klass; 276 } 277 278 ObjCMethodList new_methodlist(size_t initial_size = 0) { 279 return new ObjCMethodList(initial_size); 280 } 281 282 void add_methods(ObjCMethodList mlst) { 283 objc_method_list* lst = mlst.lst; 284 285 debug { writefln("adding ", lst.method_count, " methods"); } 286 287 class_addMethods(klass, lst); 288 } 289 290 class ObjCMethodList { 291 objc_method_list* lst; 292 size_t max_methods; 293 294 this(size_t initial_size = 0) { 295 lst = objc_alloc_method_list(initial_size); 296 max_methods = initial_size; 297 } 298 299 void add(char[] name, char[] types, IMP imp) { 300 lst.method_count += 1; 301 302 if (lst.method_count > max_methods) { 303 objc_method_list* newlst = cast(objc_method_list*)realloc(lst, objc_method_list.sizeof + (objc_method.sizeof * lst.method_count)); 304 305 if (newlst is null) { 306 throw new OutOfMemoryException(); 307 } 308 lst = newlst; 309 max_methods += 1; 310 } 311 312 objc_method* m = &((cast(objc_method*)&lst.method_list)[lst.method_count-1]); 313 objc_init_method(m, sel_registerName(name), types, imp); 314 } 315 } // end class ObjCMethodList 316 } // end class ObjCClass trunk/main.d
r5 r7 3 3 private import std.stdio; 4 4 5 importobjc_classinfos;5 private import docoa.objc.objc_classinfos; 6 6 7 7 int main(char[][] args) { trunk/winmain.d
r5 r7 9 9 private import std.c.string; 10 10 11 importobjc_classinfos;11 private import docoa.objc.objc_classinfos; 12 12 13 13 alias objc_object NSTableView; … … 19 19 */ 20 20 class TestController { 21 static SEL[char[]] selectors; 22 static Class klass; 23 21 // data source for the table 24 22 static char[][char[]][] table; 25 23 26 24 static id nitroProjectTable; // IBOutlet 27 25 28 static this() { 26 // Contains an Objective-C class 27 static ObjCClass kl; 28 29 static this() 30 { 31 // this builds a objc class with a given base (empty second parameter 32 // would actually mean the same) 33 // 34 kl = new ObjCClass(this.classinfo.name, "NSObject"); 29 35 30 selectors["testAction"] = sel_registerName("testAction:"); 31 selectors["testAction2"] = sel_registerName("testAction2:"); 32 selectors["numberOfRowsInTableView"] = sel_registerName("numberOfRowsInTableView:"); 33 selectors["tableView_objectValueForTableColumn_row"] = sel_registerName("tableView:objectValueForTableColumn:row:"); 34 selectors["setNitroProjTable"] = sel_registerName("setNitroProjTable:"); 36 // create a new method list, the parameter is only an advice, the 37 // function will aquire more space, if you go beyond it 38 // 39 auto mlist = kl.new_methodlist(5); 35 40 36 klass = objc_build_class_with_base(this.classinfo.name, "NSObject"); 41 mlist.add("testAction:", "v8@0:4@", &testAction); 42 mlist.add("testAction2:", "v8@0:4@", &testAction2); 43 mlist.add("numberOfRowsInTableView:", "i8@0:4@", &numberOfRowsInTableView); 44 mlist.add("tableView:objectValueForTableColumn:row:", "@8@0:4@@i", &tableView_objectValueForTableColumn_row); 45 mlist.add("setNitroProjTable:", "v12@0:4@", &setNitroProjTable); 37 46 38 int method_count = 5; 39 objc_method_list* lst = objc_alloc_method_list(method_count); 40 objc_add_method(lst, method_count, selectors["testAction"], "v8@0:4@", &testAction); 41 objc_add_method(lst, method_count, selectors["testAction2"], "v8@0:4@", &testAction2); 42 objc_add_method(lst, method_count, selectors["numberOfRowsInTableView"], "i8@0:4@", &numberOfRowsInTableView); 43 objc_add_method(lst, method_count, selectors["tableView_objectValueForTableColumn_row"], "@8@0:4@@i", &tableView_objectValueForTableColumn_row); 44 objc_add_method(lst, method_count, selectors["setNitroProjTable"], "v12@0:4@", &setNitroProjTable); 45 46 class_addMethods(klass, lst); 47 kl.add_methods(mlist); 47 48 48 49 table.length = 3; … … 71 72 Data-sources for table view 72 73 ALWAYS add (id self, SEL _cmd) to the beginning of functions, ObjC issue 73 74 idea:75 76 NSPopUpButtonCell * popup = [[[NSPopUpButtonCell alloc] init] autorelease];77 [popup setBordered:NO];78 [popup addItemWithTitle:@"1"];79 //...80 [col setDataCell:popup];81 74 */ 82 75 static int numberOfRowsInTableView(id self, SEL _cmd, NSTableView* aTableView) { … … 117 110 id inst = class_createInstance(kl, kl.instance_size); 118 111 119 objc_msgSend(inst, TestController.selectors["testAction"], kl);112 objc_msgSend(inst, sel_getUid("testAction:"), kl); 120 113 121 114 printf("after obj test\n");
