280north / cappuccino

Web Application Framework in JavaScript and Objective-J

cappuccino / Tools / nib2cib / NSButton.j
100644 212 lines (171 sloc) 6.14 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* NSButton.j
* nib2cib
*
* Portions based on NSButtonCell.m (09/09/2008) in Cocotron (http://www.cocotron.org/)
* Copyright (c) 2006-2007 Christopher J. W. Lloyd
*
* Created by Francisco Tolmasky.
* Copyright 2008, 280 North, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
 
@import <AppKit/CPButton.j>
@import <AppKit/CPCheckBox.j>
@import <AppKit/CPRadio.j>
 
@import "NSCell.j"
@import "NSControl.j"
 
 
var _CPButtonBezelStyleHeights = {};
_CPButtonBezelStyleHeights[CPRoundedBezelStyle] = 18;
_CPButtonBezelStyleHeights[CPTexturedRoundedBezelStyle] = 20;
_CPButtonBezelStyleHeights[CPHUDBezelStyle] = 20;
 
@implementation CPButton (NSCoding)
 
- (id)NS_initWithCoder:(CPCoder)aCoder
{
    self = [super NS_initWithCoder:aCoder];
 
    if (self)
    {
        var cell = [aCoder decodeObjectForKey:@"NSCell"];
 
        _controlSize = CPRegularControlSize;
 
        _title = [cell title];
 
        if (![self NS_isCheckBox] && ![self NS_isRadio])
        {
            [self setBordered:[cell isBordered]];
 
            _bezelStyle = [cell bezelStyle];
 
            // clean up:
            switch (_bezelStyle)
            {
                // implemented:
                case CPRoundedBezelStyle:
                case CPTexturedRoundedBezelStyle:
                case CPHUDBezelStyle:
                    break;
                // approximations:
                case CPRoundRectBezelStyle:
                    _bezelStyle = CPRoundedBezelStyle;
                    break;
                case CPSmallSquareBezelStyle:
                case CPThickSquareBezelStyle:
                case CPThickerSquareBezelStyle:
                case CPRegularSquareBezelStyle:
                case CPTexturedSquareBezelStyle:
                case CPShadowlessSquareBezelStyle:
                    _bezelStyle = CPTexturedRoundedBezelStyle;
                    break;
                case CPRecessedBezelStyle:
                    _bezelStyle = CPHUDBezelStyle;
                    break;
                // unsupported
                case CPRoundedDisclosureBezelStyle:
                case CPHelpButtonBezelStyle:
                case CPCircularBezelStyle:
                case CPDisclosureBezelStyle:
                    CPLog.warn("Unsupported bezel style: " + _bezelStyle);
                    _bezelStyle = CPHUDBezelStyle;
                    break;
                // error:
                default:
                    CPLog.error("Unknown bezel style: " + _bezelStyle);
                    _bezelStyle = CPHUDBezelStyle;
            }
 
            //if (_CPButtonBezelStyleHeights[_bezelStyle] != undefined)
            {
                //CPLog.warn("Adjusting CPButton height from " +_frame.size.height+ " / " + _bounds.size.height+" to " + _CPButtonBezelStyleHeights[_bezelStyle]);
                _frame.size.height = 24.0;//_CPButtonBezelStyleHeights[_bezelStyle];
                _bounds.size.height = 24.0;//_CPButtonBezelStyleHeights[_bezelStyle];
            }
        }
        else
            [self setBordered:YES];
    }
 
    return self;
}
 
- (BOOL)NS_isCheckBox
{
    return NO;
}
 
- (BOOL)NS_isRadio
{
    return NO;
}
 
@end
 
@implementation NSButton : CPButton
{
    BOOL _isCheckBox @accessors(readonly, getter=NS_isCheckBox);
    BOOL _isRadio @accessors(readonly, getter=NS_isRadio);
}
 
- (id)initWithCoder:(CPCoder)aCoder
{
    // We need to do a bit of magic to determine if this is a checkbox or radio button.
    var cell = [aCoder decodeObjectForKey:@"NSCell"],
        alternateImage = [cell alternateImage];
 
    if ([alternateImage isKindOfClass:[NSButtonImageSource class]])
    {
        if ([alternateImage imageName] === @"NSSwitch")
            _isCheckBox = YES;
 
        else if ([alternateImage imageName] === @"NSRadioButton")
        {
            _isRadio = YES;
            self._radioGroup = [CPRadioGroup new];
        }
    }
 
    return [self NS_initWithCoder:aCoder];
}
 
- (Class)classForKeyedArchiver
{
    if ([self NS_isCheckBox])
        return [CPCheckBox class];
 
    if ([self NS_isRadio])
        return [CPRadio class];
 
    return [CPButton class];
}
 
@end
 
@implementation NSButtonCell : NSActionCell
{
    BOOL _isBordered @accessors(readonly, getter=isBordered);
    int _bezelStyle @accessors(readonly, getter=bezelStyle);
 
    CPString _title @accessors(readonly, getter=title);
    CPImage _alternateImage @accessors(readonly, getter=alternateImage);
}
 
- (id)initWithCoder:(CPCoder)aCoder
{
    self = [super initWithCoder:aCoder];
 
    if (self)
    {
        var buttonFlags = [aCoder decodeIntForKey:@"NSButtonFlags"],
            buttonFlags2 = [aCoder decodeIntForKey:@"NSButtonFlags2"];
 
        _isBordered = (buttonFlags & 0x00800000) ? YES : NO;
        _bezelStyle = (buttonFlags2 & 0x7) | ((buttonFlags2 & 0x20) >> 2);
 
        // NSContents for NSButton is actually the title
        _title = [aCoder decodeObjectForKey:@"NSContents"];
        // ... and _objectValue is _state
        _objectValue = [self state];
 
        _alternateImage = [aCoder decodeObjectForKey:@"NSAlternateImage"];
    }
 
    return self;
}
 
@end
 
@implementation NSButtonImageSource : CPObject
{
    CPString _imageName @accessors(readonly, getter=imageName);
}
 
- (id)initWithCoder:(CPCoder)aCoder
{
    self = [super init];
 
    if (self)
        _imageName = [aCoder decodeObjectForKey:@"NSImageName"];
 
    return self;
}
 
@end