diff --git a/examples/app/main.go b/examples/app/main.go index 336466f..689b095 100644 --- a/examples/app/main.go +++ b/examples/app/main.go @@ -10,6 +10,15 @@ import ( //Shortcut for literal NSStrings var nst = ns.NSStringWithGoString +func pb1() { + fmt.Println("Pushed button 1") +} + +func pb2() { + fmt.Println("Pushed button 2") + a.Terminate(a) +} + func didFinishLaunching(n ns.NSNotification) { fmt.Println("Go: did finish launching") fmt.Printf("Notification: %s\n",n.Name().UTF8String()) @@ -50,13 +59,25 @@ func didFinishLaunching(n ns.NSNotification) { a.SetMainMenu(m1) - //add some buttons and do some layout + //add some custom buttons - //don't do this: - //b := ns.NSButtonAlloc().InitWithFrame(ns.NSMakeRect(100,100,100,50)) + b1 := ns.GButtonAlloc() + b2 := ns.GButtonAlloc() + + b1.Init() + b1.PressedCallback(pb1) + b1.SetAction(ns.Selector("pressed")) + b1.SetTarget(b1) + b1.SetTitle(nst("PUSH")) + + b2.Init() + b2.PressedCallback(pb2) + b2.SetTarget(b2) + b2.SetAction(ns.Selector("pressed")) + b2.SetTitle(nst("QUIT")) + + //add some layout constraints - b1 := ns.NSButtonWithTitle(nst("PUSH"),ns.Id{},ns.Selector("")) - b2 := ns.NSButtonWithTitle(nst("QUIT"),ns.Id{},ns.Selector("terminate:")) b1.SetTranslatesAutoresizingMaskIntoConstraints(0) b2.SetTranslatesAutoresizingMaskIntoConstraints(0) diff --git a/examples/app/nswrap.yaml b/examples/app/nswrap.yaml index 01274bf..5b29b56 100644 --- a/examples/app/nswrap.yaml +++ b/examples/app/nswrap.yaml @@ -5,7 +5,6 @@ inputfiles: classes: - NSAutoreleasePool - - NSEnumerator - NSString - NSApplication - NSWindow @@ -13,14 +12,14 @@ classes: - NSMenu - NSMenuItem - NSButton + - NSControl - NSNotification - - NSStackView - NSLayoutConstraint - NSDictionary - NSArray - NSObject -functions: [NSMake.*] +functions: [ NSMakeRect ] enums: - NSApplication.* @@ -38,6 +37,11 @@ delegates: - applicationShouldTerminateAfterLastWindowClosed - applicationDidBecomeActive +subclasses: + GButton: + NSButton: + - -(void)pressed + frameworks: [ Foundation, AppKit ] pragma: [ clang diagnostic ignored "-Wformat-security" ] diff --git a/examples/foundation/main.go b/examples/foundation/main.go index 8760e32..81c51d5 100644 --- a/examples/foundation/main.go +++ b/examples/foundation/main.go @@ -15,6 +15,7 @@ func main() { n2 := ns.NSStringAlloc() n2 = n2.InitWithGoString("hi world") n3 := ns.NSStringWithGoString("ok bye") + fmt.Printf("%s\n",n3) a := ns.NSMutableArrayWithObjects(n1,n2,n3) fmt.Println("Length(a) = ",a.Count()) fmt.Println("is n2 in a?",a.ContainsObject(n2)) @@ -30,7 +31,7 @@ func main() { fmt.Printf("i1 = %@\n",i1) fmt.Printf("i1.Ptr() = %p\n",i1.Ptr()) a.ObjectEnumerator().ForIn(func(o ns.Id) bool { - fmt.Println(o.NSString().UTF8String()) + fmt.Println(o.NSString()) return true }) s1 := ns.NSSetWithObjects(n1,n2) diff --git a/examples/simple/nswrap.yaml b/examples/simple/nswrap.yaml index df1e96d..9481111 100644 --- a/examples/simple/nswrap.yaml +++ b/examples/simple/nswrap.yaml @@ -4,7 +4,7 @@ classes: - ClassOne - ClassTwo subclasses: - ClassThree: + classThree: ClassTwo: - geti1 - -(void)myMethod1:(int)a diff --git a/wrap/main.go b/wrap/main.go index 12feaad..9cc4562 100644 --- a/wrap/main.go +++ b/wrap/main.go @@ -664,8 +664,8 @@ func (w *Wrapper) _processType(bt *types.Type, gt string) { if gt == "Char" { w.CharHelpers() } - if gt == "NSEnumerator" { - w.EnumeratorHelpers() + if gt == "NSString" { + w.StringHelpers() } if gt == "SEL" { w.SelectorHelpers() @@ -701,6 +701,14 @@ func (c *Char) String() string { `) } +func (w *Wrapper) StringHelpers() { + w.goHelpers.WriteString(` +func (o NSString) String() string { + return o.UTF8String().String() +} +`) +} + func (w *Wrapper) EnumeratorHelpers() { w.goHelpers.WriteString(` func (e NSEnumerator) ForIn(f func(Id) bool) { @@ -791,7 +799,7 @@ func (w *Wrapper) _processMethod(m *Method,fun bool) { gname = strings.ReplaceAll(gname," ","") receiver := "" cname := m.Name - fmt.Printf("Method %s (GoClass %s)\n",cname,m.GoClass) + //fmt.Printf("Method %s (GoClass %s)\n",cname,m.GoClass) switch { case !m.ClassMethod: if types.IsGoInterface(m.GoClass) { @@ -1016,8 +1024,9 @@ func (w *Wrapper) MethodFromSig(sig,class string) *Method { func (w *Wrapper) ProcessSubclass(sname string, sc *Subclass) { fmt.Printf("Wrapping %s\n",sname) - types.Wrap(sname) - types.SetSuper(sname,sc.Super) + gname := strings.Title(sname) + types.Wrap(gname) + types.SetSuper(gname,sc.Super) ps := map[string][]string{} ps[sc.Super] = sc.Overrides nms := make([]*Method,len(sc.NewMethods)) @@ -1062,16 +1071,16 @@ func (w *Wrapper) _ProcessDelSub(dname string, ps map[string][]string, nms []*Me i++ var ms map[string]*Method if sub { + interf := w.Interfaces[pname] + supr = interf.GoName if i > 1 { fmt.Printf("Multiple inheritance is not permitted:\n subclass %s already inherits from %s\n",dname,supr) os.Exit(-1) } - interf := w.Interfaces[pname] if interf == nil { fmt.Printf("Failed to find interface %s for subclass %s\n",pname,dname) os.Exit(-1) } - supr = interf.GoName //fmt.Printf(" subclass for %s\n",pname) ms = map[string]*Method{} var addmeths func(s string) @@ -1094,6 +1103,7 @@ func (w *Wrapper) _ProcessDelSub(dname string, ps map[string][]string, nms []*Me } //fmt.Printf(" proto %s\n",pname) ms = proto.InstanceMethods + types.SetSuper(dname,"Id") supr = "Id" } for gname,m := range ms { @@ -1272,7 +1282,7 @@ func (w *Wrapper) _ProcessDelSub(dname string, ps map[string][]string, nms []*Me { %s[(%s*)self super_%s]; } -`,sfp,ret,gname,strings.Join(vpnames[i]," ")) +`,sfp,ret,dname,strings.Join(vpnames[i]," ")) } } ccode.WriteString(fmt.Sprintf(` @@ -1315,7 +1325,7 @@ func %sAlloc() %s { dispitems := make([]string,len(gnames)) sdispitems := make([]string,sms) for i,n := range gnames { - if !sub { + if !sub || sms == 0 { gtypes[i] = gtypes[i][1:] } dispitems[i] = fmt.Sprintf( @@ -1332,7 +1342,7 @@ type %sDispatch struct { var %sLookup map[unsafe.Pointer]%sDispatch = map[unsafe.Pointer]%sDispatch{} `,gname,strings.Join(dispitems,"\n"),gname,gname,gname)) - if sub { + if sub && sms > 0 { gocode.WriteString(fmt.Sprintf(` type %sSupermethods struct { %s @@ -1359,7 +1369,7 @@ func (d %s) %sCallback(f func(%s)%s) { earglist := []string{"o unsafe.Pointer"} garglist := []string{} gargconv := []string{} - if sub { + if sub && sms > 0 { garglist = []string{"super"} } for j := 1; j < len(vnames[i]); j++ { @@ -1409,7 +1419,7 @@ func (d %s) %sCallback(f func(%s)%s) { ` self.Super%s`,gnames[i]) } sper := "" - if sub && len(gnames) > 0 { + if sub && sms > 0 { sper = fmt.Sprintf( ` self := (*%s)(o) super := %sSupermethods{ @@ -1500,6 +1510,9 @@ func (w *Wrapper) Wrap(toproc []string) { continue } w.processType(types.NewTypeFromString(i.GoName,"")) + if i.Name == "NSEnumerator" { + w.EnumeratorHelpers() + } gname := i.GoName if types.IsGoInterface(i.GoName) { gname = "Id"