Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 245 lines (192 sloc) 6.254 kB
a516cc8 Initial check-in.
uli authored
1 //
2 // UKLineSizeView.m
3 // UKPaintView
4 //
5 // Created by Uli Kusterer on Mon Nov 03 2003.
6 // Copyright (c) 2003 M. Uli Kusterer. All rights reserved.
7 //
6a767a7 @uliwitness Big clean-up. Adopted new prefix. Fixed support for line and fill col…
authored
8 // This software is provided 'as-is', without any express or implied
9 // warranty. In no event will the authors be held liable for any damages
10 // arising from the use of this software.
11 //
12 // Permission is granted to anyone to use this software for any purpose,
13 // including commercial applications, and to alter it and redistribute it
14 // freely, subject to the following restrictions:
15 //
16 // 1. The origin of this software must not be misrepresented; you must not
17 // claim that you wrote the original software. If you use this software
18 // in a product, an acknowledgment in the product documentation would be
19 // appreciated but is not required.
20 //
21 // 2. Altered source versions must be plainly marked as such, and must not be
22 // misrepresented as being the original software.
23 //
24 // 3. This notice may not be removed or altered from any source
25 // distribution.
26 //
27
28 #import "ULILineSizeView.h"
a516cc8 Initial check-in.
uli authored
29
30
6a767a7 @uliwitness Big clean-up. Adopted new prefix. Fixed support for line and fill col…
authored
31 @implementation ULILineSizeView
a516cc8 Initial check-in.
uli authored
32
6a767a7 @uliwitness Big clean-up. Adopted new prefix. Fixed support for line and fill col…
authored
33 @synthesize delegate;
a516cc8 Initial check-in.
uli authored
34
35 -(id) initWithFrame: (NSRect)frame
36 {
65bfc40 @uliwitness Shut up some warnings.
authored
37 if(( self = [super initWithFrame:frame] ))
a516cc8 Initial check-in.
uli authored
38 {
39 floatValue = 1.0;
40 maxValue = 20.0;
41 target = nil;
42 action = @selector(takeLineSizeFromObject:);
43 [[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(colorChanged:)
44 name: NSColorPanelColorDidChangeNotification object: [NSColorPanel sharedColorPanel]];
45 }
46
47 return self;
48 }
49
50
51 -(void) dealloc
52 {
53 [[NSNotificationCenter defaultCenter] removeObserver:self
54 name: NSColorPanelColorDidChangeNotification
55 object: [NSColorPanel sharedColorPanel]];
56 [super dealloc];
57 }
58
59 -(void) drawRect: (NSRect)rect
60 {
61 NSPoint topPos = rect.origin,
62 botPos = rect.origin;
63 float maxX = rect.origin.x +rect.size.width -4,
64 currSize = 1.0, boxBot = rect.origin.y;
65 short numSamples,
66 x;
67 NSColor* currColor = [[NSColorPanel sharedColorPanel] color];
6a767a7 @uliwitness Big clean-up. Adopted new prefix. Fixed support for line and fill col…
authored
68 if( delegate && [delegate respondsToSelector: @selector(lineColor)] )
69 currColor = [delegate lineColor];
a516cc8 Initial check-in.
uli authored
70
71 topPos.x += 4 +(maxValue /2) +0.5;
72 botPos.x += topPos.x;
73 botPos.y += 4;
74 topPos.y = botPos.y +rect.size.height -8;
75
76 numSamples = maxX -rect.origin.x -4;
77 numSamples /= maxValue;
78 if( (numSamples * maxValue) < (maxX -rect.origin.x -4) )
79 numSamples += 1;
80 currSize = 1;
81
82 // Draw box that will hold line samples:
83 NSColor* checkColor = [currColor colorUsingColorSpaceName: NSCalibratedRGBColorSpace];
84 if( (([checkColor redComponent] +[checkColor greenComponent] +[checkColor blueComponent]) / 3) >= 0.85 )
85 NSDrawGrayBezel( rect, rect ); // Very bright color? Probably invisible on white surface, so draw dark one.
86 else
87 NSDrawWhiteBezel( rect, rect ); // Darker color? Draw on white surface, which is prettier.
88
89 // Get a font to use for displaying line size numbers:
90 NSFont* descFont = [NSFont systemFontOfSize: [NSFont smallSystemFontSize]];
91 NSDictionary* sizeStrAttributes = [NSDictionary dictionaryWithObjectsAndKeys: descFont, NSFontAttributeName, currColor, NSForegroundColorAttributeName, nil];
92 NSDictionary* selSizeStrAttributes = [NSDictionary dictionaryWithObjectsAndKeys: descFont, NSFontAttributeName, [NSColor selectedTextColor], NSForegroundColorAttributeName, nil];
93
94 boxBot = botPos.y;
c331828 @uliwitness Make this compile on 10.6 and without a prefix header.
authored
95 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_2
96 botPos.y += 18.0 +2;
97 #else
a516cc8 Initial check-in.
uli authored
98 botPos.y += [descFont defaultLineHeightForFont] +2;
c331828 @uliwitness Make this compile on 10.6 and without a prefix header.
authored
99 #endif
a516cc8 Initial check-in.
uli authored
100 [descFont set];
101
102 // Make sure line samples look like taken out of a line:
103 [NSBezierPath setDefaultLineCapStyle: NSButtLineCapStyle];
104 [NSBezierPath setDefaultLineJoinStyle: NSMiterLineJoinStyle];
105
106 for( x = 1; x <= numSamples; x++ )
107 {
108 NSColor* lineColor = currColor;
109 NSSize sizeStrSize;
110 NSString* sizeStr;
111 NSDictionary* sizeStrAttrs = sizeStrAttributes;
112
113 // Draw highlighted box if this is the current choice:
114 if( currSize == floatValue )
115 {
116 NSRect box;
117
118 box.origin = botPos;
119 box.origin.x -= (maxValue /2);
120 box.origin.y = boxBot -2;
121 box.size.width = maxValue;
122 box.size.height = topPos.y -boxBot +4;
123
124 [NSBezierPath setDefaultLineWidth: 1.0];
125 [[NSColor selectedTextBackgroundColor] set];
126 [NSBezierPath fillRect: box];
127
128 // Make sure drawings are done in "selected text" color:
129 lineColor = [NSColor selectedTextColor];
130 sizeStrAttrs = selSizeStrAttributes;
131 }
132
133 // Draw line sample:
134 [NSBezierPath setDefaultLineWidth: currSize];
135 [lineColor set];
136 [NSBezierPath strokeLineFromPoint: botPos toPoint: topPos];
137
138 // Draw line size below sample:
139 sizeStr = [NSString stringWithFormat: @"%d", (int) currSize];
140 sizeStrSize = [sizeStr sizeWithAttributes: sizeStrAttrs];
141 [sizeStr drawAtPoint: NSMakePoint(botPos.x -(sizeStrSize.width /2), boxBot)
142 withAttributes: sizeStrAttrs];
143
144 // Move drawing to next swatch:
145 currSize += 1.0;
146 botPos.x += maxValue;
147 topPos.x += maxValue;
148 }
149 }
150
151
152 -(void) mouseDown: (NSEvent*)event
153 {
154 NSPoint clickPos = [event locationInWindow];
155 NSPoint topPos = [self bounds].origin,
156 botPos = [self bounds].origin;
157 float maxX = [self bounds].origin.x +[self bounds].size.width -4,
158 currSize = 1.0;
159 short numSamples;
160
161 clickPos = [self convertPoint: clickPos fromView: nil];
162
163 topPos.x += 4 +(maxValue /2) +0.5;
164 botPos.x += topPos.x;
165 botPos.y += 4;
166 topPos.y = botPos.y +[self bounds].size.height -8;
167
168 numSamples = maxX -[self bounds].origin.x -4;
169 numSamples /= maxValue;
170 currSize = 1;
171
172 floatValue = currSize +truncf((clickPos.x -4) / maxValue);
173 if( target )
174 [target performSelector: action withObject: self];
175
176 [self setNeedsDisplay: YES];
177 }
178
179
180 -(void) mouseDragged: (NSEvent*)event
181 {
182 [self mouseDown: event];
183 }
184
185
186 -(BOOL) acceptsFirstMouse: (NSEvent*)theEvent
187 {
188 return YES;
189 }
190
191
192 -(void) colorChanged: (id)sender
193 {
194 [self setNeedsDisplay: YES];
195 }
196
197
198 -(float) floatValue
199 {
200 return floatValue;
201 }
202
203
204 -(void) setFloatValue: (float)v
205 {
206 floatValue = v;
207 }
208
209
210 -(float) maxValue
211 {
212 return maxValue;
213 }
214
215
216 -(void) setMaxValue: (float)v
217 {
218 maxValue = v;
219 }
220
221
222 -(id) target
223 {
224 return target;
225 }
226
227 -(void) setTarget: (id)anObject
228 {
229 target = anObject;
230 }
231
232 -(SEL) action
233 {
234 return action;
235 }
236
237
238 -(void) setAction: (SEL)aSelector
239 {
240 action = aSelector;
241 }
242
243
244 @end
Something went wrong with that request. Please try again.