Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / AppKit / NSFormCell.m
blob3c1456e170f973897097a218807a9d8fbda279c3
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;
31     
32     _titleWidth=[keyed decodeFloatForKey:@"NSTitleWidth"];
33     _titleCell=[[keyed decodeObjectForKey:@"NSTitleCell"] retain];
34    }
35    else {
36     [NSException raise:NSInvalidArgumentException format:@"-[%@ %s] is not implemented for coder %@",isa,sel_getName(_cmd),coder];
37    }
38    return self;
41 -copyWithZone:(NSZone *)zone {
42     NSFormCell *copy = [super copyWithZone:zone];
44     copy->_titleCell=[_titleCell copy];
46     return copy;
49 -initTextCell:(NSString *)value {
50    [super initTextCell:value];
51    NSUnimplementedMethod();
52    return self;
55 -(void)dealloc {
56    [_titleCell release];
57    [super dealloc];
60 -(NSCellType)type {
61    return NSTextCellType;
64 -(BOOL)isOpaque {
65    if(_titleWidth==0 && [self isBezeled])
66     return YES;
67    
68    return NO;
71 -(float)titleWidth {
72    return _titleWidth;
75 -(float)titleWidth:(NSSize)size {
76 // FIX,wrong
77    return _titleWidth;
80 -(NSString *)title {
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]])
102     return _placeholder;
104    return nil;
107 -(NSAttributedString *)placeholderAttributedString {
108    if([_placeholder isKindOfClass:[NSAttributedString class]])
109     return _placeholder;
111    return nil;
114 -(void)setTitleWidth:(float)value {
115    _titleWidth=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 {
139    value=[value copy];
140    [_placeholder release];
141    _placeholder=value;
144 -(void)setPlaceholderAttributedString:(NSAttributedString *)value {
145    value=[value copy];
146    [_placeholder release];
147    _placeholder=value;
150 -(void)setTitleWithMnemonic:(NSString *)value {
151 // FIX, wrong
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];
165    if(font!=nil)
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];
185    NSRect              titleRect;
186    NSRect              valueRect;
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];
195    valueRect=frame;
196    valueRect.origin.x+=(_titleWidth+1);
197    valueRect.size.width-=(_titleWidth+1);
199    if([self isBezeled]){
200     NSDrawWhiteBezel(valueRect,valueRect);
201    }
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);
218    if([self isBezeled])
219     rect=NSInsetRect(rect,3,3);
221    return rect;
224 @end