deprecate SCViewHolder-layRight
[supercollider.git] / SCClassLibrary / Common / GUI / Model.sc
blob26d8adc4b4deaf78947f2f13aac108c80d43059a
1 SimpleController {
2         var model, actions;
3         // responds to updates of a model
5         *new { arg model;
6                 ^super.newCopyArgs(model).init
7         }
8         init {
9                 model.addDependant(this);
10         }
11         put { arg what, action;
12                 if (actions.isNil, {
13                         actions = IdentityDictionary.new(4);
14                 });
15                 actions.put(what, action);
16         }
17         update { arg theChanger, what ... moreArgs;
18                 var action;
19                 if(actions.notNil) {
20                         action = actions.at(what);
21                         if (action.notNil, {
22                                 action.valueArray(theChanger, what, moreArgs);
23                         });
24                 };
25         }
26         remove {
27                 model.removeDependant(this);
28         }
31 TestDependant {
32         update { arg thing;
33                 (thing.asString ++ " was changed.\n").post;
34         }
40 NotificationCenter {
42         classvar registrations;
44         //                      who             \didSomething
45         *notify { arg object, message, args;
46                 registrations.at(object,message).copy.do({ arg function;
47                         function.valueArray(args)
48                 })
49         }
51         //                      who             \didSomething
52         *register { arg object,message,listener,action;
53                 var nr;
54                 nr = NotificationRegistration(object,message,listener);
55                 registrations.put(object,message,listener,action);
56                 ^nr;
57         }
59         *unregister { arg object,message,listener;
60                 var lastKey,lastDict;
61                 lastDict = registrations.at(object,message);
62                 if(lastDict.notNil,{
63                         lastDict.removeAt(listener);
64                         (lastDict.size == 0).if({
65                                 registrations.removeAt(object,message);
66                                 (registrations.at(object).size == 0).if({
67                                         registrations.removeAt(object);
68                                 });
69                         });
70                 });
71         }
72         *registerOneShot {  arg object,message,listener,action;
73                 var nr;
74                 nr = NotificationRegistration(object,message,listener);
75                 registrations.put(object,message,listener,
76                         { |args|
77                                 action.value(args);
78                                 this.unregister(object,message,listener)
79                         });
80                 ^nr
81         }
82         *clear {
83                 registrations = MultiLevelIdentityDictionary.new;
84         }
85         *registrationExists { |object,message,listener|
86                 ^registrations.at(object,message,listener).notNil
87         }
88         *initClass {
89                 this.clear
90         }
91         //*remove { |listener|
92         //
93         //}
96 NotificationRegistration {
98         var <>object,<>message,<>listener;
100         *new { arg o,m,l;
101                 ^super.new.object_(o).message_(m).listener_(l)
102         }
103         remove {
104                 NotificationCenter.unregister(object,message,listener)
105         }