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
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd

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. */

// Original - David Young <daver@geeks.org>
#import <AppKit/NSStepperCell.h>
#import <AppKit/NSStepper.h>
#import <AppKit/NSGraphics.h>
#import <AppKit/NSButtonCell.h>
#import <AppKit/NSWindow.h>
#import <AppKit/NSInterfacePart.h>
#import <AppKit/NSMatrix.h>

@implementation NSStepperCell

-(double)minValue {
return _minValue;
}

-(double)increment {
return _increment;
}

-(double)maxValue {
return _maxValue;
}

-(BOOL)autorepeat {
return _autorepeat;
}

-(BOOL)valueWraps {
return _valueWraps;
}

-(void)setMinValue:(double)value {
_minValue = value;
}

-(void)setIncrement:(double)value {
_increment = value;
}

-(void)setMaxValue:(double)value {
_maxValue = value;
}

-(void)setAutorepeat:(BOOL)flag {
_autorepeat = flag;
}

-(void)setValueWraps:(BOOL)flag {
_valueWraps = flag;
}

-(NSRect)_upArrowFrameForFrame:(NSRect)frame {
int half = frame.size.height/2;
if (![_controlView isFlipped])
frame.origin.y += half;
frame.size.height = half;
return frame;
}

-(NSRect)_downArrowFrameForFrame:(NSRect)frame {
int half = frame.size.height/2;
frame.size.height = half;
if ([_controlView isFlipped])
frame.origin.y += half;
return frame;
}

- (void)_incrementAndConstrainBy:(double)delta {
double value = [self doubleValue];

value += delta;
if (value < _minValue) {
if (_valueWraps)
value = _maxValue;
else
value = _minValue;
}
if (value > _maxValue) {
if (_valueWraps)
value = _minValue;
else
value = _maxValue;
}

[self setDoubleValue:value];
}

// not so pretty
-(BOOL)trackMouse:(NSEvent *)event inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp {
NSPoint point;
double delta=0.0;
NSRect trackFrame;
BOOL sendAction=NO;

if (_autorepeat && untilMouseUp)
[NSEvent startPeriodicEventsAfterDelay:0.0 withPeriod:0.5];

do {
if ([event type] != NSPeriodic)
point=[controlView convertPoint:[event locationInWindow] fromView:nil];

if ([event type] == NSLeftMouseUp)
untilMouseUp=NO;

if (NSMouseInRect(point, [self _upArrowFrameForFrame:cellFrame],[controlView isFlipped])) {
delta=[self increment];
trackFrame=[self _upArrowFrameForFrame:cellFrame];
_isUpHighlighted=YES;
sendAction=YES;
}
else if (NSMouseInRect(point, [self _downArrowFrameForFrame:cellFrame],[controlView isFlipped])) {
delta=0.0-[self increment];
trackFrame=[self _downArrowFrameForFrame:cellFrame];
_isDownHighlighted=YES;
sendAction=YES;
}
else
sendAction=NO;

[self drawWithFrame:cellFrame inView:controlView];
if (sendAction)
[self _incrementAndConstrainBy:delta];

if (untilMouseUp) {
if (_autorepeat && sendAction)
[(NSControl *)controlView sendAction:_action to:_target];

event=[[controlView window] nextEventMatchingMask:NSPeriodicMask|NSLeftMouseUpMask|NSLeftMouseDraggedMask];
}
if ([event type] == NSLeftMouseUp)
untilMouseUp = NO;

_isUpHighlighted = _isDownHighlighted = NO;
} while (untilMouseUp == YES);

if (_autorepeat)
[NSEvent stopPeriodicEvents];

return sendAction;
}

// I don't believe there are < > horizontal steppers?
-(void)drawWithFrame:(NSRect)frame inView:(NSView *)controlView {
BOOL drawDottedRect = NO;
NSInterfacePart *upPart, *downPart;
NSRect rect;

upPart = [NSInterfacePart interfacePartScrollerArrowUpEnabled:[self isEnabled]];
downPart = [NSInterfacePart interfacePartScrollerArrowDownEnabled:[self isEnabled]];

_controlView=controlView;
rect = [self _upArrowFrameForFrame:frame];
if (_isUpHighlighted)
NSDrawWhiteBezel(rect, frame);
else
NSDrawButton(rect, frame);
rect.origin.x += rect.size.width/2;
rect.origin.x -= [upPart size].width/2;
rect.origin.y += rect.size.height/2;
rect.origin.y -= [upPart size].height/2;
[upPart drawAtPoint:rect.origin];

rect = [self _downArrowFrameForFrame:frame];
if (_isDownHighlighted)
NSDrawWhiteBezel(rect, frame);
else
NSDrawButton(rect, frame);
rect.origin.x += rect.size.width/2;
rect.origin.x -= [downPart size].width/2;
rect.origin.y += rect.size.height/2;
rect.origin.y -= [downPart size].height/2;
[downPart drawAtPoint:rect.origin];

if ([[controlView window] firstResponder] == controlView){
if([controlView isKindOfClass:[NSMatrix class]]){
NSMatrix *matrix=(NSMatrix *)controlView;

drawDottedRect=([matrix keyCell]==self)?YES:NO;
}
else if([controlView isKindOfClass:[NSControl class]]){
NSControl *control=(NSControl *)controlView;

drawDottedRect=([control selectedCell]==self)?YES:NO;
}
}

if(drawDottedRect)
NSDottedFrameRect(NSInsetRect(frame,2,2)); // fugly
}

// *!* hmm. this is too fast! it's not visually discernible.
- (void)moveUp:(id)sender {
[self _incrementAndConstrainBy:[self increment]];

_isUpHighlighted=YES;
[self drawWithFrame:[_controlView bounds] inView:_controlView];
[[_controlView window] flushWindow];

[(NSControl *)_controlView sendAction:[self action] to:[self target]];

_isUpHighlighted=NO;
[self drawWithFrame:[_controlView bounds] inView:_controlView];
[[_controlView window] flushWindow];
}

- (void)moveDown:(id)sender {
[self _incrementAndConstrainBy:0.0-[self increment]];

_isDownHighlighted=YES;
[self drawWithFrame:[_controlView bounds] inView:_controlView];
[[_controlView window] flushWindow];

[(NSControl *)_controlView sendAction:[self action] to:[self target]];

_isDownHighlighted=NO;
[self drawWithFrame:[_controlView bounds] inView:_controlView];
[[_controlView window] flushWindow];
}

@end

Change log

2470bd80abf4 by cocotron on Jan 1, 2007   Diff
Added templates for many missing classes,
updated copyright for 2007, +[NSValue
valueWithRange:],-[NSObject className]
Go to: 
Project members, sign in to write a code review

Older revisions

e5277a40a7e8 by cocotron on Dec 21, 2006   Diff
Original
All revisions of this file

File info

Size: 7206 bytes, 228 lines
Powered by Google Project Hosting