| 1 |
// |
| 2 |
// GraphicRulerView.m |
| 3 |
// |
| 4 |
/* |
| 5 |
Copyright (c) 2000-2017 Toshi Nagata. All rights reserved. |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation version 2 of the License. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
*/ |
| 16 |
|
| 17 |
#import "GraphicRulerView.h" |
| 18 |
#import "GraphicClientView.h" |
| 19 |
#import "NSCursorAdditions.h" |
| 20 |
#import "MyDocument.h" |
| 21 |
#import "MyAppController.h" // for getOSXVersion |
| 22 |
|
| 23 |
static NSFont *sRulerLabelFont; |
| 24 |
|
| 25 |
@implementation GraphicRulerView |
| 26 |
|
| 27 |
+ (NSFont *)rulerLabelFont |
| 28 |
{ |
| 29 |
if (sRulerLabelFont == nil) |
| 30 |
sRulerLabelFont = [[NSFont userFontOfSize: 0] retain]; |
| 31 |
return sRulerLabelFont; |
| 32 |
} |
| 33 |
|
| 34 |
+ (void)setRulerLabelFont: (NSFont *)aFont |
| 35 |
{ |
| 36 |
[sRulerLabelFont release]; |
| 37 |
sRulerLabelFont = [aFont retain]; |
| 38 |
} |
| 39 |
|
| 40 |
- (int)rulerViewType |
| 41 |
{ |
| 42 |
if (clientView != nil) |
| 43 |
return [(GraphicClientView *)clientView clientViewType]; |
| 44 |
else return kGraphicGenericViewType; |
| 45 |
} |
| 46 |
|
| 47 |
- (id)initWithFrame:(NSRect)frame |
| 48 |
{ |
| 49 |
self = [super initWithFrame:frame]; |
| 50 |
if (self) { |
| 51 |
// Initialization code here. |
| 52 |
dragStartPoint = NSMakePoint(-10000, -10000); |
| 53 |
} |
| 54 |
return self; |
| 55 |
} |
| 56 |
|
| 57 |
- (void)dealloc |
| 58 |
{ |
| 59 |
[self releaseClientView]; |
| 60 |
[super dealloc]; |
| 61 |
} |
| 62 |
|
| 63 |
- (NSRect)loupeRect |
| 64 |
{ |
| 65 |
if (dragStartPoint.x > -10000) { |
| 66 |
NSRect rect = [self bounds]; |
| 67 |
if (dragStartPoint.y > dragEndPoint.y) { |
| 68 |
rect.origin.y = dragEndPoint.y; |
| 69 |
rect.size.height = dragStartPoint.y - dragEndPoint.y; |
| 70 |
} else { |
| 71 |
rect.origin.y = dragStartPoint.y; |
| 72 |
rect.size.height = dragEndPoint.y - dragStartPoint.y; |
| 73 |
} |
| 74 |
return rect; |
| 75 |
} else return NSZeroRect; |
| 76 |
} |
| 77 |
|
| 78 |
- (void)drawRect:(NSRect)rect |
| 79 |
{ |
| 80 |
float x, y; |
| 81 |
NSRect lrect = [self loupeRect]; |
| 82 |
if (lrect.size.width > 0) { |
| 83 |
[[[MyDocument colorForSelectingRange] colorWithAlphaComponent: 0.1f] set]; |
| 84 |
[NSBezierPath fillRect:lrect]; |
| 85 |
x = lrect.origin.x; |
| 86 |
y = lrect.origin.y; |
| 87 |
[[NSColor blackColor] set]; |
| 88 |
[NSBezierPath strokeLineFromPoint: NSMakePoint(x, y) toPoint:NSMakePoint(x + lrect.size.width, y)]; |
| 89 |
y += lrect.size.height; |
| 90 |
[NSBezierPath strokeLineFromPoint: NSMakePoint(x, y) toPoint:NSMakePoint(x + lrect.size.width, y)]; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
- (void)releaseClientView |
| 95 |
{ |
| 96 |
if (clientView != nil) { |
| 97 |
[[NSNotificationCenter defaultCenter] |
| 98 |
removeObserver: self name: nil object: clientView]; |
| 99 |
[[NSNotificationCenter defaultCenter] |
| 100 |
removeObserver: self name: nil object: [clientView superview]]; |
| 101 |
[clientView release]; |
| 102 |
clientView = nil; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
// Respond when the scroll position of the client view changed |
| 107 |
- (void)superBoundsDidChange: (NSNotification *)aNotification |
| 108 |
{ |
| 109 |
NSView *view = (NSView *)[aNotification object]; |
| 110 |
if (view == [clientView superview]) { |
| 111 |
NSRect rect = [view bounds]; |
| 112 |
NSRect newRect = [[self superview] bounds]; |
| 113 |
newRect.origin.y = rect.origin.y; |
| 114 |
[self scrollPoint: newRect.origin]; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
// Respond when the frame of the superview of the client view changed |
| 119 |
// i.e. the scroll view containing the client view is resized |
| 120 |
- (void)superFrameDidChange: (NSNotification *)aNotification |
| 121 |
{ |
| 122 |
NSView *view = (NSView *)[aNotification object]; |
| 123 |
if (view == [clientView superview]) { |
| 124 |
NSRect rect = [view frame]; |
| 125 |
NSRect newRect = [[self superview] frame]; |
| 126 |
rect = [[[self superview] superview] convertRect: rect fromView: [view superview]]; |
| 127 |
newRect.origin.y = rect.origin.y; |
| 128 |
newRect.size.height = rect.size.height; |
| 129 |
[[self superview] setFrame: newRect]; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
// Respond when the frame of the client view, i.e. data range and/or scale are changed |
| 134 |
- (void)frameDidChange: (NSNotification *)aNotification |
| 135 |
{ |
| 136 |
NSView *view = (NSView *)[aNotification object]; |
| 137 |
if (view == clientView) { |
| 138 |
NSRect rect = [view frame]; |
| 139 |
NSRect newRect = [self frame]; |
| 140 |
newRect.size.height = rect.size.height; |
| 141 |
[self setFrame: newRect]; |
| 142 |
[self setNeedsDisplay:YES]; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
- (void)setClientView: (NSView *)aView |
| 147 |
{ |
| 148 |
[self releaseClientView]; |
| 149 |
[[NSNotificationCenter defaultCenter] |
| 150 |
addObserver: self |
| 151 |
selector: @selector(superBoundsDidChange:) |
| 152 |
name: NSViewBoundsDidChangeNotification |
| 153 |
object: [aView superview]]; |
| 154 |
[[NSNotificationCenter defaultCenter] |
| 155 |
addObserver: self |
| 156 |
selector: @selector(superFrameDidChange:) |
| 157 |
name: NSViewFrameDidChangeNotification |
| 158 |
object: [aView superview]]; |
| 159 |
[[NSNotificationCenter defaultCenter] |
| 160 |
addObserver: self |
| 161 |
selector: @selector(frameDidChange:) |
| 162 |
name: NSViewFrameDidChangeNotification |
| 163 |
object: aView]; |
| 164 |
clientView = [aView retain]; |
| 165 |
} |
| 166 |
|
| 167 |
- (NSView *)clientView |
| 168 |
{ |
| 169 |
return clientView; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
- (void)invalidateLoupeRect |
| 174 |
{ |
| 175 |
NSRect lrect = [self loupeRect]; |
| 176 |
lrect = NSInsetRect(lrect, -1, -1); |
| 177 |
[self setNeedsDisplayInRect:lrect]; |
| 178 |
} |
| 179 |
|
| 180 |
- (void)mouseUp: (NSEvent *)theEvent |
| 181 |
{ |
| 182 |
GraphicClientView *cv = (GraphicClientView *)clientView; |
| 183 |
if ([theEvent clickCount] == 2 && ([theEvent modifierFlags] & NSAlternateKeyMask) != 0) { |
| 184 |
[cv setVisibleRangeMin:0.0f max:1.0f]; |
| 185 |
} else if (dragStartPoint.x != -10000) { |
| 186 |
float y1, y2, miny, maxy; |
| 187 |
int32_t tick; // Dummy |
| 188 |
[self invalidateLoupeRect]; |
| 189 |
[cv convertFromPoint:dragStartPoint toY:&y1 andTick:&tick]; |
| 190 |
[cv convertFromPoint:[cv convertPoint:[theEvent locationInWindow] fromView:nil] toY:&y2 andTick:&tick]; |
| 191 |
if (y1 > y2) { |
| 192 |
float yw = y1; |
| 193 |
y1 = y2; |
| 194 |
y2 = yw; |
| 195 |
} |
| 196 |
miny = [cv minValue]; |
| 197 |
maxy = [cv maxValue]; |
| 198 |
if (y2 - y1 < 2.0f) { |
| 199 |
if (y1 > maxy - 2.0f) { |
| 200 |
y2 = maxy; |
| 201 |
y1 = maxy - 2.0f; |
| 202 |
} else { |
| 203 |
y2 = y1 + 2.0f; |
| 204 |
} |
| 205 |
} |
| 206 |
y1 = (y1 - miny) / (maxy - miny); |
| 207 |
y2 = (y2 - miny) / (maxy - miny); |
| 208 |
[cv setVisibleRangeMin:y1 max:y2]; |
| 209 |
} |
| 210 |
dragStartPoint = NSMakePoint(-10000, -10000); |
| 211 |
} |
| 212 |
|
| 213 |
- (void)mouseDragged: (NSEvent *)theEvent |
| 214 |
{ |
| 215 |
NSPoint pt; |
| 216 |
// NSRect bounds; |
| 217 |
// NSValue *val; |
| 218 |
pt = [self convertPoint: [theEvent locationInWindow] fromView: nil]; |
| 219 |
if (dragStartPoint.x == -10000) { |
| 220 |
if ([theEvent modifierFlags] & NSAlternateKeyMask) { |
| 221 |
/* Start option-dragging */ |
| 222 |
dragStartPoint = pt; |
| 223 |
} |
| 224 |
return; |
| 225 |
} else { |
| 226 |
[self invalidateLoupeRect]; |
| 227 |
dragEndPoint = pt; |
| 228 |
[self invalidateLoupeRect]; |
| 229 |
} |
| 230 |
[self displayIfNeeded]; |
| 231 |
} |
| 232 |
|
| 233 |
- (void)doMouseMoved: (NSEvent *)theEvent |
| 234 |
{ |
| 235 |
if ([theEvent modifierFlags] & NSAlternateKeyMask) |
| 236 |
[[NSCursor loupeCursor] set]; |
| 237 |
else [[NSCursor arrowCursor] set]; |
| 238 |
} |
| 239 |
|
| 240 |
- (void)scrollWheel:(NSEvent *)theEvent |
| 241 |
{ |
| 242 |
float dy, newpos; |
| 243 |
static int scroll_direction = 0; |
| 244 |
|
| 245 |
if (scroll_direction == 0) { |
| 246 |
// Scroll wheel behavior was changed in 10.7 |
| 247 |
if ([(MyAppController *)[NSApp delegate] getOSXVersion] < 10700) |
| 248 |
scroll_direction = -1; |
| 249 |
else scroll_direction = 1; |
| 250 |
} |
| 251 |
dy = [theEvent deltaY]; |
| 252 |
if (dy != 0.0) { |
| 253 |
// Implement vertical scroll by ourselves |
| 254 |
GraphicClientView *cview = (GraphicClientView *)[self clientView]; |
| 255 |
newpos = [cview scrollVerticalPosition] + dy * (4 * scroll_direction); |
| 256 |
[cview scrollToVerticalPosition:newpos]; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
@end |