1//
2//  RSVerticallyCenteredTextField.m
3//  RSCommon
4//
5//  Created by Daniel Jalkut on 6/17/06.
6//  Copyright 2006 Red Sweater Software. All rights reserved.
7//
8
9/*
10 This source code is provided to you compliments of Red Sweater
11 Software under the license as described below. NOTE: This is the MIT
12 License.
13
14 Copyright (c) 2006 Red Sweater Software
15
16 Permission is hereby granted, free of charge, to any person obtaining
17 a copy of this software and associated documentation files (the
18 "Software"), to deal in the Software without restriction, including
19 without limitation the rights to use, copy, modify, merge, publish,
20 distribute, sublicense, and/or sell copies of the Software, and to
21 permit persons to whom the Software is furnished to do so, subject to
22 the following conditions:
23
24 The above copyright notice and this permission notice shall be
25 included in all copies or substantial portions of the Software.
26
27 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
34 SOFTWARE.
35 */
36
37#import "RSVerticallyCenteredTextFieldCell.h"
38
39@implementation RSVerticallyCenteredTextFieldCell
40
41- (NSRect)drawingRectForBounds:(NSRect)theRect
42{
43	// Get the parent's idea of where we should draw
44	NSRect newRect = [super drawingRectForBounds:theRect];
45
46	// When the text field is being
47	// edited or selected, we have to turn off the magic because it screws up
48	// the configuration of the field editor.  We sneak around this by
49	// intercepting selectWithFrame and editWithFrame and sneaking a
50	// reduced, centered rect in at the last minute.
51	if (mIsEditingOrSelecting == NO)
52	{
53		// Get our ideal size for current text
54		NSSize textSize = [self cellSizeForBounds:theRect];
55
56		// Center that in the proposed rect
57		float heightDelta = newRect.size.height - textSize.height;
58		if (heightDelta > 0)
59		{
60			newRect.size.height -= heightDelta;
61			newRect.origin.y += (heightDelta / 2);
62		}
63	}
64
65	return newRect;
66}
67
68- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength
69{
70	aRect = [self drawingRectForBounds:aRect];
71	mIsEditingOrSelecting = YES;
72	[super selectWithFrame:aRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
73	mIsEditingOrSelecting = NO;
74}
75
76- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent
77{
78	aRect = [self drawingRectForBounds:aRect];
79	mIsEditingOrSelecting = YES;
80	[super editWithFrame:aRect inView:controlView editor:textObj delegate:anObject event:theEvent];
81	mIsEditingOrSelecting = NO;
82}
83
84@end
85