My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd <cjwl@objc.net>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#import <AppKit/NSBox.h>
#import <AppKit/NSApplication.h>
#import <AppKit/NSBezierPath.h>
#import <AppKit/NSGraphics.h>
#import <AppKit/NSFont.h>
#import <AppKit/NSAttributedString.h>
#import <AppKit/NSColor.h>
#import <AppKit/NSStringDrawing.h>
#import <AppKit/NSParagraphStyle.h>
#import <AppKit/NSStringDrawer.h>
#import <AppKit/NSCell.h>
#import <AppKit/NSTextFieldCell.h>
#import <Foundation/NSKeyedArchiver.h>
#import <AppKit/NSGraphicsStyle.h>
#import <AppKit/NSRaise.h>


@implementation NSBox

-(void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder:coder];
[coder encodeInt:_borderType forKey:@"NSBox borderType"];
[coder encodeInt:_titlePosition forKey:@"NSBox titlePosition"];
[coder encodeSize:_contentViewMargins forKey:@"NSBox contentViewMargins"];
[coder encodeObject:_titleCell forKey:@"NSBox titleCell"];
}

-initWithCoder:(NSCoder *)coder {
[super initWithCoder:coder];

if([coder allowsKeyedCoding]){
NSKeyedUnarchiver *keyed=(NSKeyedUnarchiver *)coder;

_boxType=[keyed decodeIntForKey:@"NSBoxType"];
_borderType=[keyed decodeIntForKey:@"NSBorderType"];
_titlePosition=[keyed decodeIntForKey:@"NSTitlePosition"];
_contentViewMargins=[keyed decodeSizeForKey:@"NSOffsets"];
_titleCell=[[keyed decodeObjectForKey:@"NSTitleCell"] retain];
// There is a key NSTransparent, but as far as I can tell it is always NO
_isTransparent=[keyed decodeBoolForKey:@"NSFullyTransparent"];

[[_subviews lastObject] setAutoresizingMask: NSViewWidthSizable| NSViewHeightSizable];
[[_subviews lastObject] setAutoresizesSubviews:YES];

if (_boxType == NSBoxCustom)
{
id obj;
_customData = [[NSMutableDictionary alloc] init];

obj = [keyed decodeObjectForKey:@"NSBorderWidth2"];
if (obj == nil) obj = [NSNumber numberWithDouble:1];
[_customData setObject:obj forKey:@"NSBorderWidth2"];

obj = [keyed decodeObjectForKey:@"NSCornerRadius2"];
if (obj == nil) obj = [NSNumber numberWithDouble:0];

[_customData setObject:obj forKey:@"NSCornerRadius2"];

obj = [keyed decodeObjectForKey:@"NSBorderColor2"];
if (obj == nil) obj = [NSColor colorWithCalibratedWhite:0.000 alpha:0.420];
[_customData setObject:obj forKey:@"NSBorderColor2"];

obj = [keyed decodeObjectForKey:@"NSFillColor2"];
if (obj == nil) obj = [NSColor colorWithCalibratedWhite:0.000 alpha:0.000];
[_customData setObject:obj forKey:@"NSFillColor2"];
}
}
else {
[NSException raise:NSInvalidArgumentException format:@"%@ can not initWithCoder:%@",isa,[coder class]];
}
return self;
}

-initWithFrame:(NSRect) frame {
NSView *contentview;

[super initWithFrame: frame];

_isTransparent = YES;
_titleCell = [[NSTextFieldCell alloc] initTextCell:@""];
_borderType = NSLineBorder;
_titlePosition = NSAboveTop;

contentview = [[NSView alloc] initWithFrame:NSMakeRect(0,0,NSWidth(frame),NSHeight(frame))];
[contentview setAutoresizingMask: NSViewWidthSizable| NSViewHeightSizable];
[self addSubview: contentview];
[contentview release];
[self setAutoresizesSubviews:YES];
return self;
}

-(void)dealloc {
[_customData release];
[_titleCell release];
[super dealloc];
}

-(NSBoxType)boxType {
return _boxType;
}

-(NSBorderType)borderType {
return _borderType;
}

-(NSString *)title {
return [_titleCell stringValue];
}

-(NSFont *)titleFont {
return [_titleCell font];
}

-contentView {
return [[self subviews] lastObject];
}

-(NSSize)contentViewMargins {
return _contentViewMargins;
}

-(NSTitlePosition)titlePosition {
return _titlePosition;
}

-(BOOL)isTransparent {
return _isTransparent;
}

-(void)setBoxType:(NSBoxType)value {
_boxType=value;
[self setNeedsDisplay:YES];
}

-(void)setBorderType:(NSBorderType)value {
_borderType=value;
[self setNeedsDisplay:YES];
}

-(void)setTitle:(NSString *)title {
[_titleCell setStringValue:title];
[self setNeedsDisplay:YES];
}

-(void)setTitleFont:(NSFont *)font {
[_titleCell setFont:font];
}

-(void)setContentView:(NSView *)view {
if(![[self subviews] containsObject:view]){
[[self subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
// FIX, adjust size
[self addSubview:view];
}
}

-(void)setContentViewMargins:(NSSize)value {
_contentViewMargins=value;
[self setNeedsDisplay:YES];
}

-(void)setTitlePosition:(NSTitlePosition)value {
_titlePosition=value;
[self setNeedsDisplay:YES];
}

-(void)setTransparent:(BOOL)value {
_isTransparent=value;
[self setNeedsDisplay:YES];
}

-(void)setTitleWithMnemonic:(NSString *)value {
NSUnimplementedMethod();
}

-(NSAttributedString *)_attributedTitle {
NSMutableDictionary *attributes=[NSMutableDictionary dictionary];
NSMutableParagraphStyle *paraStyle=[[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
NSFont *font=[_titleCell font];

if(font!=nil)
[attributes setObject:font forKey:NSFontAttributeName];

[paraStyle setLineBreakMode:NSLineBreakByClipping];
[paraStyle setAlignment:NSLeftTextAlignment];
[attributes setObject:paraStyle forKey:NSParagraphStyleAttributeName];

[attributes setObject:[NSColor controlTextColor]
forKey: NSForegroundColorAttributeName];

[attributes setObject:[NSColor windowBackgroundColor]
forKey:NSBackgroundColorAttributeName];

return [[[NSAttributedString alloc] initWithString:[_titleCell stringValue] attributes:attributes] autorelease];
}

#define TEXTGAP 4

-(NSRect)titleRect {
// Obtain the size the title cell prefers
NSSize size = [_titleCell cellSize];
NSRect bounds=[self bounds];
NSRect result=NSZeroRect;

result.origin.x=10+TEXTGAP;
result.size.height=ceil(size.height);
result.size.width=ceil(size.width);

switch(_titlePosition){

case NSNoTitle:
break;

case NSAboveTop:
result.origin.y=bounds.size.height-result.size.height;
break;

case NSAtTop:
result.origin.y=bounds.size.height-result.size.height;
break;

case NSBelowTop:
result.origin.y=bounds.size.height-(result.size.height+TEXTGAP);
break;

case NSAboveBottom:
result.origin.y=TEXTGAP;
break;

case NSAtBottom:
break;

case NSBelowBottom:
result.origin.y=0;
break;
}

return result;
}

-(NSRect)borderRect {
NSUnimplementedMethod();
return NSMakeRect(0,0,0,0);
}

-titleCell {
return _titleCell;
}

-(void)setFrameFromContentFrame:(NSRect)content {
// FIX, adjust content frame size to accomodate border/title
[self setFrame:content];
}

-(void)sizeToFit {
NSUnimplementedMethod();
}

-(void)drawRect:(NSRect)rect {
NSRect grooveRect=_bounds;
NSRect titleRect=[self titleRect];
BOOL drawTitle=YES;

if([self isTransparent])
return;

switch(_titlePosition){

case NSNoTitle:
drawTitle=NO;
break;

case NSAboveTop:
grooveRect.size.height-=titleRect.size.height+TEXTGAP;
break;

case NSAtTop:
grooveRect.size.height-=floor(titleRect.size.height/2);
break;

case NSBelowTop:
break;

case NSAboveBottom:
break;

case NSAtBottom:
grooveRect.origin.y+=floor(titleRect.size.height/2);
grooveRect.size.height-=floor(titleRect.size.height/2);
break;

case NSBelowBottom:
grooveRect.origin.y+=titleRect.size.height+TEXTGAP;
grooveRect.size.height-=titleRect.size.height+TEXTGAP;
break;
}

if (_boxType != NSBoxSeparator) {
// Separator are transparent except for drawing a line
[[NSColor controlColor] setFill];
NSRectFill(rect);
}

if (_boxType == NSBoxCustom){

// Ignoring corner radius for now.
[[self fillColor] set];
NSRectFill(rect);

if (_borderType != NSNoBorder)
{
[[self borderColor] set];
NSFrameRectWithWidth(_bounds,[self borderWidth]);
}
} else if (_boxType == NSBoxSeparator) {
// These are just the simple divider lines
[[NSColor grayColor] set];
NSBezierPath* line = [NSBezierPath bezierPath];
if (NSWidth(grooveRect) > NSHeight(grooveRect)) {
[line moveToPoint: NSMakePoint(NSMinX(grooveRect), NSMidY(grooveRect))];
[line lineToPoint: NSMakePoint(NSMaxX(grooveRect), NSMidY(grooveRect))];
} else {
[line moveToPoint: NSMakePoint(NSMidX(grooveRect), NSMinY(grooveRect))];
[line lineToPoint: NSMakePoint(NSMidX(grooveRect), NSMaxY(grooveRect))];
}
[line stroke];
}
else{
switch(_borderType){
case NSNoBorder:
break;

case NSLineBorder:
[[self graphicsStyle] drawBoxWithLineInRect:grooveRect];
break;

case NSBezelBorder:
[[self graphicsStyle] drawBoxWithBezelInRect:grooveRect clipRect:rect];
break;

case NSGrooveBorder:
[[self graphicsStyle] drawBoxWithGrooveInRect:grooveRect clipRect:rect];
break;
}
}
if(drawTitle){
#if 0
[[NSColor windowBackgroundColor] setFill];
titleRect.origin.x-=TEXTGAP;
titleRect.size.width+=TEXTGAP*2;
NSRectFill(titleRect);
#endif
titleRect.origin.x+=TEXTGAP;

// Ask the cell to draw itself now
// TODO: Should we be doing some sort of clipping setup here?
[_titleCell setControlView:self];
[_titleCell drawWithFrame: titleRect inView: self];
}
}

- (CGFloat)borderWidth
{
return [[_customData objectForKey:@"NSBorderWidth2"] doubleValue];
}

- (CGFloat)cornerRadius;
{
return [[_customData objectForKey:@"NSCornerRadius2"] doubleValue];
}

- (NSColor *)borderColor;
{
return [_customData objectForKey:@"NSBorderColor2"];
}

- (NSColor *)fillColor;
{
return [_customData objectForKey:@"NSFillColor2"];
}


- (void)setBorderWidth:(CGFloat)value
{
[_customData setObject:[NSNumber numberWithDouble:value] forKey:@"NSBorderWidth2"];
}

- (void)setCornerRadius:(CGFloat)value
{
[_customData setObject:[NSNumber numberWithDouble:value] forKey:@"NSCornerRadius2"];
}

- (void)setBorderColor:(NSColor *)value
{
[_customData setObject:value forKey:@"NSBorderColor2"];
}

- (void)setFillColor:(NSColor *)value
{
[_customData setObject:value forKey:@"NSFillColor2"];
}

@end

Change log

6ceaceba01da by Robert Grant <r...@plasq.com> on Jan 19, 2012   Diff
box title gets cut off
Go to: 
Project members, sign in to write a code review

Older revisions

48695690d022 by Pauli Ojala <pa...@lacquer.fi> on Sep 6, 2011   Diff
improved compatibility with Cocoa for
boxes created manually in code
38c3532404aa by Robert Grant <r...@plasq.com> on Jun 24, 2011   Diff
NSBox separators are too chunky
791e03f9d334 by Robert Grant <r...@plasq.com> on May 26, 2011   Diff
Support special "separator" box style
All revisions of this file

File info

Size: 11215 bytes, 410 lines
Powered by Google Project Hosting