1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 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:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 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. */
9 #import <AppKit/NSFormCell.h>
10 #import <AppKit/NSAttributedString.h>
11 #import <AppKit/NSStringDrawing.h>
12 #import <AppKit/NSGraphics.h>
13 #import <AppKit/NSColor.h>
14 #import <AppKit/NSParagraphStyle.h>
15 #import <AppKit/NSStringDrawer.h>
16 #import <Foundation/NSKeyedArchiver.h>
17 #import <AppKit/NSTextFieldCell.h>
18 #import <AppKit/NSRaise.h>
20 @implementation NSFormCell
22 -(void)encodeWithCoder:(NSCoder *)coder {
23 NSUnimplementedMethod();
26 -initWithCoder:(NSCoder *)coder {
27 [super initWithCoder:coder];
29 if([coder allowsKeyedCoding]){
30 NSKeyedUnarchiver *keyed=(NSKeyedUnarchiver *)coder;
32 _titleWidth=[keyed decodeFloatForKey:@"NSTitleWidth"];
33 _titleCell=[[keyed decodeObjectForKey:@"NSTitleCell"] retain];
36 [NSException raise:NSInvalidArgumentException format:@"-[%@ %s] is not implemented for coder %@",isa,sel_getName(_cmd),coder];
41 -copyWithZone:(NSZone *)zone {
42 NSFormCell *copy = [super copyWithZone:zone];
44 copy->_titleCell=[_titleCell copy];
49 -initTextCell:(NSString *)value {
50 [super initTextCell:value];
51 NSUnimplementedMethod();
61 return NSTextCellType;
65 if(_titleWidth==0 && [self isBezeled])
75 -(float)titleWidth:(NSSize)size {
81 return [_titleCell stringValue];
84 -(NSAttributedString *)attributedTitle {
85 return [_titleCell attributedStringValue];
88 -(NSFont *)titleFont {
89 return [_titleCell font];
92 -(NSTextAlignment)titleAlignment {
93 return [_titleCell alignment];
96 -(NSWritingDirection)titleBaseWritingDirection {
97 return [_titleCell baseWritingDirection];
100 -(NSString *)placeholderString {
101 if([_placeholder isKindOfClass:[NSString class]])
107 -(NSAttributedString *)placeholderAttributedString {
108 if([_placeholder isKindOfClass:[NSAttributedString class]])
114 -(void)setTitleWidth:(float)value {
118 -(void)setTitle:(NSString *)value {
119 [_titleCell setStringValue:value];
122 -(void)setAttributedTitle:(NSAttributedString *)value {
123 [_titleCell setAttributedStringValue:value];
126 -(void)setTitleFont:(NSFont *)value {
127 [_titleCell setFont:value];
130 -(void)setTitleAlignment:(NSTextAlignment)value {
131 [_titleCell setAlignment:value];
134 -(void)setTitleBaseWritingDirection:(NSWritingDirection)value {
135 [_titleCell setBaseWritingDirection:value];
138 -(void)setPlaceholderString:(NSString *)value {
140 [_placeholder release];
144 -(void)setPlaceholderAttributedString:(NSAttributedString *)value {
146 [_placeholder release];
150 -(void)setTitleWithMnemonic:(NSString *)value {
152 [_titleCell setStringValue:value];
155 -(void)setEnabled:(BOOL)enabled {
156 [super setEnabled:enabled];
157 [_titleCell setEnabled:enabled];
160 -(NSAttributedString *)attributedStringValue {
161 NSMutableDictionary *attributes=[NSMutableDictionary dictionary];
162 NSMutableParagraphStyle *paraStyle=[[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
163 NSFont *font=[self font];
166 [attributes setObject:font forKey:NSFontAttributeName];
168 [attributes setObject:[self isEnabled]?[NSColor controlTextColor]:[NSColor disabledControlTextColor]
169 forKey:NSForegroundColorAttributeName];
171 [attributes setObject:[NSColor whiteColor]
172 forKey:NSBackgroundColorAttributeName];
174 [paraStyle setLineBreakMode:_lineBreakMode];
175 [paraStyle setAlignment:_textAlignment];
176 [attributes setObject:paraStyle forKey:NSParagraphStyleAttributeName];
178 return [[[NSAttributedString alloc] initWithString:[self stringValue] attributes:attributes] autorelease];
181 -(void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)control {
182 NSAttributedString *title=[self attributedTitle];
183 NSAttributedString *value=[self attributedStringValue];
184 NSSize titleSize=[title size];
188 titleRect.origin.x=frame.origin.x;
189 titleRect.origin.y=frame.origin.y+floor((frame.size.height-titleSize.height)/2);
190 titleRect.size.width=_titleWidth;
191 titleRect.size.height=titleSize.height;
193 [title _clipAndDrawInRect:titleRect];
196 valueRect.origin.x+=(_titleWidth+1);
197 valueRect.size.width-=(_titleWidth+1);
199 if([self isBezeled]){
200 NSDrawWhiteBezel(valueRect,valueRect);
203 valueRect=[self titleRectForBounds:frame];
205 [value _clipAndDrawInRect:valueRect];
208 -(void)drawWithFrame:(NSRect)frame inView:(NSView *)control {
209 _controlView=control;
211 [self drawInteriorWithFrame:frame inView:control];
214 -(NSRect)titleRectForBounds:(NSRect)rect {
215 rect.origin.x+=(_titleWidth+1);
216 rect.size.width-=(_titleWidth+1);
219 rect=NSInsetRect(rect,3,3);