PageRenderTime 23ms CodeModel.GetById 18ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/Frameworks/AppKit/rhino.platform/CPThemedAttribute.j

http://github.com/jfahrenkrug/MapKit-HelloWorld
Unknown | 256 lines | 204 code | 52 blank | 0 comment | 0 complexity | fac4c3415be8b93d7fcbef7d5b8cdc62 MD5 | raw file
  1I;21;Foundation/CPObject.jc;7686;
  2
  3
  4CPControlStateNormal = 0,
  5CPControlStateHighlighted = 1 << 0,
  6CPControlStateDisabled = 1 << 1,
  7CPControlStateSelected = 1 << 2,
  8CPControlStateDefault = 1 << 3,
  9CPControlStateBordered = 1 << 4,
 10CPControlStateBezeled = 1 << 5,
 11CPControlStateVertical = 1 << 6,
 12CPControlStateEditable = 1 << 7,
 13CPControlStateEditing = 1 << 8;
 14
 15var BIT_COUNT = [ 0 , 1 , 1 , 2 , 1 , 2 , 2 , 3 ,
 16                    1 , 2 , 2 , 3 , 2 , 3 , 3 , 4 ,
 17                    1 , 2 , 2 , 3 , 2 , 3 , 3 , 4 ,
 18                    2 , 3 , 3 , 4 , 3 , 4 , 4 , 5 ],
 19    bit_count = function(bits)
 20    {
 21        var count = 0;
 22
 23        while (bits)
 24        {
 25            ++count;
 26            bits &= (bits - 1);
 27        }
 28
 29        return count ;
 30    }
 31
 32{var the_class = objj_allocateClassPair(CPObject, "CPThemedAttribute"),
 33meta_class = the_class.isa;class_addIvars(the_class, [new objj_ivar("_cache"), new objj_ivar("_name"), new objj_ivar("_defaultValue"), new objj_ivar("_theme"), new objj_ivar("_themedClass"), new objj_ivar("_values"), new objj_ivar("_attributeFromTheme")]);
 34objj_registerClassPair(the_class);
 35objj_addClassForBundle(the_class, objj_getBundleWithPath(OBJJ_CURRENT_BUNDLE.path));
 36class_addMethods(the_class, [new objj_method(sel_getUid("initWithName:defaultValue:theme:class:"), function(self, _cmd, aName, aDefaultValue, aTheme, aClass)
 37{ with(self)
 38{
 39    self = objj_msgSendSuper({ receiver:self, super_class:objj_getClass("CPObject") }, "init");
 40
 41    if (self)
 42    {
 43        _cache = {};
 44
 45        _name = aName;
 46
 47        _defaultValue = aDefaultValue;
 48
 49        _theme = aTheme;
 50        _themedClass = aClass;
 51
 52        _values = objj_msgSend(CPDictionary, "dictionary");
 53        _attributeFromTheme = objj_msgSend(_theme, "valueForAttributeName:inClass:", _name, _themedClass);
 54    }
 55
 56    return self;
 57}
 58}), new objj_method(sel_getUid("setName:"), function(self, _cmd, aName)
 59{ with(self)
 60{
 61    _name = aName;
 62}
 63}), new objj_method(sel_getUid("name"), function(self, _cmd)
 64{ with(self)
 65{
 66    return _name;
 67}
 68}), new objj_method(sel_getUid("setDefaultValue:"), function(self, _cmd, aValue)
 69{ with(self)
 70{
 71    _cache = {};
 72    _defaultValue = aValue;
 73}
 74}), new objj_method(sel_getUid("setTheme:"), function(self, _cmd, aTheme)
 75{ with(self)
 76{
 77    if (_theme === aTheme)
 78        return;
 79
 80    _cache = {};
 81    _theme = aTheme;
 82    _attributeFromTheme = objj_msgSend(_theme, "valueForAttributeName:inClass:", _name, _themedClass);
 83}
 84}), new objj_method(sel_getUid("setThemedClass:"), function(self, _cmd, aClass)
 85{ with(self)
 86{
 87    if (_themedClass === aClass)
 88        return;
 89
 90    _cache = {};
 91    _themedClass = aClass;
 92    _attributeFromTheme = objj_msgSend(_theme, "valueForAttributeName:inClass:", _name, _themedClass);
 93}
 94}), new objj_method(sel_getUid("setValue:"), function(self, _cmd, aValue)
 95{ with(self)
 96{
 97    _cache = {};
 98
 99    if (aValue === undefined || aValue === nil)
100        _values = objj_msgSend(CPDictionary, "dictionary");
101    else
102        _values = objj_msgSend(CPDictionary, "dictionaryWithObject:forKey:", aValue, String(CPControlStateNormal));
103}
104}), new objj_method(sel_getUid("value"), function(self, _cmd)
105{ with(self)
106{
107    return objj_msgSend(self, "valueForControlState:", CPControlStateNormal);
108}
109}), new objj_method(sel_getUid("setValue:forControlState:"), function(self, _cmd, aValue, aState)
110{ with(self)
111{
112
113    _cache = {};
114
115    if ((aValue === undefined) || (aValue === nil))
116        objj_msgSend(_values, "removeObjectForKey:", String(aState));
117    else
118        objj_msgSend(_values, "setObject:forKey:", aValue, String(aState));
119}
120}), new objj_method(sel_getUid("valueForControlState:"), function(self, _cmd, aState)
121{ with(self)
122{
123    var value = _cache[aState];
124
125
126    if (value !== undefined)
127        return value;
128
129    value = objj_msgSend(_values, "objectForKey:", String(aState));
130
131
132    if ((value === undefined || value === nil) && aState > 0)
133    {
134
135        if (aState & (aState - 1))
136        {
137            var highestBitCount = 0,
138                states = objj_msgSend(_values, "allKeys"),
139                count = states.length;
140
141            while (count--)
142            {
143
144                state = Number(states[count]);
145
146
147                if ((state & aState) === state)
148                {
149                    var bitCount = (state < BIT_COUNT.length) ? BIT_COUNT[state] : bit_count(state);
150
151                    if (bitCount > highestBitCount)
152                    {
153                        highestBitCount = bitCount;
154                        value = objj_msgSend(_values, "objectForKey:", String(state));
155                    }
156                }
157            }
158        }
159
160
161        if (value === undefined || value === nil)
162            value = objj_msgSend(_values, "objectForKey:", String(CPControlStateNormal));
163    }
164
165    if (value === undefined || value === nil)
166        value = objj_msgSend(_attributeFromTheme, "valueForControlState:", aState);
167
168    if (value === undefined || value === nil)
169        value = _defaultValue;
170
171    _cache[aState] = value;
172
173    return value;
174}
175}), new objj_method(sel_getUid("themedAttributeMergedWithThemedAttribute:"), function(self, _cmd, aThemedAttribute)
176{ with(self)
177{
178    var mergedAttribute = CPThemedAttributeMake(_name, _defaultValue, _theme, _themedClass);
179
180    mergedAttribute._values = objj_msgSend(_values, "copy");
181    objj_msgSend(mergedAttribute._values, "addEntriesFromDictionary:", aThemedAttribute._values);
182
183    return mergedAttribute;
184}
185}), new objj_method(sel_getUid("initWithCoder:"), function(self, _cmd, aCoder)
186{ with(self)
187{
188    self = objj_msgSendSuper({ receiver:self, super_class:objj_getClass("CPObject") }, "init");
189
190    if (self)
191    {
192        _cache = {};
193        _values = objj_msgSend(aCoder, "decodeObjectForKey:", "values");
194    }
195
196    return self;
197}
198}), new objj_method(sel_getUid("encodeWithCoder:"), function(self, _cmd, aCoder)
199{ with(self)
200{
201    objj_msgSend(aCoder, "encodeObject:forKey:", _values, "values");
202}
203})]);
204}
205
206CPThemedAttributeMake= function(aName, aDefaultValue, aTheme, aClass)
207{
208    return objj_msgSend(objj_msgSend(CPThemedAttribute, "alloc"), "initWithName:defaultValue:theme:class:", aName, aDefaultValue, aTheme, aClass);
209}
210
211CPThemedAttributeEncode= function(aCoder, aThemedAttribute)
212{
213    var values = aThemedAttribute._values,
214        count = objj_msgSend(values, "count");
215
216    if (count === 1)
217    {
218        var key = objj_msgSend(values, "allKeys")[0];
219
220        if (Number(key) === 0)
221            return objj_msgSend(aCoder, "encodeObject:forKey:", objj_msgSend(values, "objectForKey:", key), "$a" + objj_msgSend(aThemedAttribute, "name"));
222    }
223
224    if (count >= 1)
225        objj_msgSend(aCoder, "encodeObject:forKey:", aThemedAttribute, "$a" + objj_msgSend(aThemedAttribute, "name"));
226}
227
228CPThemedAttributeDecode= function(aCoder, anAttributeName, aDefaultValue, aTheme, aClass)
229{
230    var key = "$a" + anAttributeName;
231
232    if (!objj_msgSend(aCoder, "containsValueForKey:", key))
233        return CPThemedAttributeMake(anAttributeName, aDefaultValue, aTheme, aClass);
234
235    var attribute = objj_msgSend(aCoder, "decodeObjectForKey:", key);
236
237    if (!attribute.isa || !objj_msgSend(attribute, "isKindOfClass:", objj_msgSend(CPThemedAttribute, "class")))
238    {
239        var themedAttribute = CPThemedAttributeMake(anAttributeName, aDefaultValue, aTheme, aClass);
240
241        objj_msgSend(themedAttribute, "setValue:", attribute);
242
243        attribute = themedAttribute;
244    }
245
246    else
247    {
248        objj_msgSend(attribute, "setName:", anAttributeName);
249        objj_msgSend(attribute, "setDefaultValue:", aDefaultValue);
250        objj_msgSend(attribute, "setTheme:", aTheme);
251        objj_msgSend(attribute, "setThemedClass:", aClass);
252    }
253
254    return attribute;
255}
256