Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / Platform / osx / scide_scapp / CocoaMenu.sc
blobf8029f59923ad21f806a4688e5de6175565df780
1 CocoaMenuItem {
2         classvar <topLevelItems;
3         classvar default;
5         var dataptr;
6         var <name;
7         var parent; // nil = top level
8         var <children;
9         var <state = false;
10         var <isBranch = false;
11         var <>action;
13         *clearCustomItems {
14                 topLevelItems.copy.do({|item| item.remove });
15                 default = nil;
16         }
18         *initDefaultMenu {
19                 default = SCMenuGroup(nil, "Library", 7);
20         }
22         *default {
23                 if(default.isNil, {this.initDefaultMenu});
24                 ^default;
25         }
27         *add { |names, action|
28                 if(default.isNil) {this.initDefaultMenu };
29                 ^this.deepNew(default, nil, names, action)
30         }
32         *deepNew { |parent, index, names = #[""], action|
33                 var menu, name, insertionIndex;
34                 name = names.removeAt(0);
35                 insertionIndex = if(names.size <= 1) { index } ? parent.lastIndex;
37                 menu = parent.findByName(name);
38                 if(menu.isNil) {
39                         menu = if(names.size > 0, {SCMenuGroup}, {SCMenuItem})
40                                 .new(parent, name, insertionIndex);
41                 };
43                 if(names.size < 1) {
44                         if(menu.isBranch) {
45                                 "failed to add menu item: item % has submenu".format(name.quote).warn;
46                                 menu = nil;
47                         } {
48                                 menu.action = action;
49                         }
50                 } {
51                         if(menu.isBranch.not) {
52                                 "failed to add menu item: item % allows no submenu".format(name.quote).warn;
53                                 menu = nil;
54                         } {
55                                 menu = this.deepNew(menu, index, names, action)
56                         }
57                 };
58                 ^menu
59         }
61         lastIndex {
62                 ^children.asArray.size
63         }
65         findByName { |name|
66                 ^children.detect { |x| x.name == name };
67         }
69         remove {
70                 children.copy.do({|child| child.remove}); // cleanup my kids
71                 children = nil;
72                 (parent.notNil && (parent != 'Help')).if({parent.removeChild(this)});
73                 this.prRemoveMenuItem;
74                 topLevelItems.remove(this);
75         }
77         addChild {|child|
78                 children = children.add(child);
79         }
81         removeChild {|child|
82                 children.remove(child);
83         }
85         enabled_ {|bool|
86                 _EnableMenuItem
87                 ^this.primitiveFailed;
88         }
90         state_ {|bool|
91                 state = bool;
92                 this.prSetState(bool);
93         }
95         // Cmd is assumed
96         setShortCut {|string, alt = false, ctrl = false|
97                 _SetMenuItemKeyboardEquivalent
98         }
100         prSetState {|bool|
101                 _SetMenuItemState
102                 ^this.primitiveFailed;
103         }
105         prAddMenuItem { |parent, index, name, hasSubmenu|
106                 _NewMenuItem
107                 ^this.primitiveFailed;
108         }
110         prRemoveMenuItem {
111                 _RemoveMenuItem
112                 ^this.primitiveFailed;
113         }
115         doAction {
116                 action.value(this);
117         }
123 SCMenuItem : CocoaMenuItem {
125         *new { |parent, name = "", index|
126                 ^super.new.init(parent, index, name);
127         }
129         init { |argparent, argindex, argname|
131                 parent = argparent;
132                 name = argname;
133                 isBranch = false;
134                 this.prAddMenuItem(argparent, argindex, argname, isBranch);
135                 if(parent.notNil && (parent != 'Help')) { parent.addChild(this) }
136                         { topLevelItems = topLevelItems.add(this) };
137         }
140 SCMenuGroup : CocoaMenuItem {
142         *new { |parent, name = "", index|
143                 ^super.new.init(parent, index, name);
144         }
146         init { |argparent, argindex, argname|
148                 parent = argparent;
149                 name = argname;
150                 isBranch = true;
151                 this.prAddMenuItem(argparent, argindex, argname, isBranch);
152                 if(parent.notNil && (parent != 'Help')) { parent.addChild(this) }
153                         { topLevelItems = topLevelItems.add(this) };
154         }
157 SCMenuRoot {
158         *new { ^nil }
161 SCMenuSeparator : CocoaMenuItem {
163         *new { arg parent, index;
164                 ^super.new.init( parent, index );
165         }
167         init { |argparent, argindex|
169                 parent = argparent;
170                 isBranch = false;
171                 this.prAddMenuSeparator(argparent, argindex);
172                 if(parent.notNil && (parent != 'Help')) { parent.addChild(this) }
173                         { topLevelItems = topLevelItems.add(this) };
174         }
176         prAddMenuSeparator { |parent, index|
177                 _NewMenuSeparator
178         }