-
Notifications
You must be signed in to change notification settings - Fork 6
/
SFRuler.m
155 lines (107 loc) · 5.46 KB
/
SFRuler.m
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
//
// SFRuler.m
// ShinyRuler
//
// Created by Matteo Rattotti on 1/7/10.
// Copyright 2010 www.shinyfrog.net. All rights reserved.
//
#import "SFRuler.h"
#import "SFResizeControl.h"
@implementation SFRuler
#pragma mark -
#pragma mark Constructors
- (id) initWithFrame:(NSRect)frame {
if (![super init])
return nil;
rulerView = [[SFRulerView alloc] initWithFrame:NSMakeRect(0, 0, frame.size.width, frame.size.height)];
rulerWindow = [[SFTranFadeWindow alloc]initWithContentRect:frame styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
[rulerWindow setFrame:frame display:NO];
[rulerWindow setMinSize:frame.size];
if([rulerView isOrizontal])
[rulerWindow setMaxSize:NSMakeSize(10000, frame.size.height)];
else
[rulerWindow setMaxSize:NSMakeSize(frame.size.width, 10000)];
[rulerWindow setContentView:rulerView];
[rulerView release];
[self setupCursorWindow];
[self setupObservers];
[rulerWindow makeKeyAndOrderFront:self];
windowDragging = NO;
windowMoving = NO;
return self;
}
#pragma mark -
#pragma mark Setup functions
- (void) setupCursorWindow{
NSRect cursorWindowFrame;
if([rulerView isOrizontal])
cursorWindowFrame = NSMakeRect([rulerWindow frame].origin.x-30, [rulerWindow frame].size.height + [rulerWindow frame].origin.y + 5, [rulerWindow frame].size.width+60, 20);
else
cursorWindowFrame = NSMakeRect([rulerWindow frame].origin.x-65, [rulerWindow frame].origin.y-15, 60, [rulerWindow frame].size.height+30);
cursorWindow = [[SFTranFadeWindow alloc]initWithContentRect:cursorWindowFrame styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
[cursorWindow setMovableByWindowBackground:NO];
ballonView = [[SFCursorBallonView alloc]initWithFrame:cursorWindowFrame];
[cursorWindow setContentView:ballonView];
[ballonView release];
[rulerWindow addChildWindow:cursorWindow ordered:NSWindowAbove];
[cursorWindow setAlphaValue:0];
}
- (void) setupObservers {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidResized:)
name:NSWindowDidResizeNotification
object:rulerWindow];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowWillMove:)
name:NSWindowWillMoveNotification
object:rulerWindow];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidMove:)
name:NSWindowDidMoveNotification
object:rulerWindow];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(rulerMouseUp:)
name:@"SFRulerViewMouseUp"
object:rulerView];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(rulerMouseDown:)
name:@"SFRulerViewMouseDown"
object:rulerView];
}
#pragma mark -
#pragma mark Ruler Management Functions
- (void) windowDidResized: (id) sender{
NSRect cursorWindowFrame;
if([rulerView isOrizontal])
cursorWindowFrame = NSMakeRect([rulerWindow frame].origin.x-30, [rulerWindow frame].size.height + [rulerWindow frame].origin.y + 5, [rulerWindow frame].size.width+60, 20);
else
cursorWindowFrame = NSMakeRect([rulerWindow frame].origin.x-65, [rulerWindow frame].origin.y-15, 60, [rulerWindow frame].size.height+30);
[cursorWindow setFrame:cursorWindowFrame display:YES];
}
- (void) windowWillMove: (id) sender { windowMoving = YES; }
- (void) windowDidMove: (id) sender { windowMoving = NO; }
- (void) rulerMouseDown: (id) sender { windowDragging = YES; }
- (void) rulerMouseUp: (id) sender { windowDragging = NO; [self performSelector:@selector(windowDidMove:) withObject:self afterDelay:0.1];}
- (void) updateCursorMarkWithPoint: (NSPoint) markPoint {
if(windowDragging || windowMoving)
return;
float markerOriz = -1;
float markerVer = -1;
if([rulerView isOrizontal] && markPoint.x >= [rulerWindow frame].origin.x && markPoint.x <= [rulerWindow frame].origin.x + [rulerWindow frame].size.width){
markerOriz = markPoint.x - [rulerWindow frame].origin.x;
[[rulerView cursorView] setCurrentMark:markerOriz];
[ballonView setCurrentValue: (int)markerOriz];
[cursorWindow setAlphaValue:1];
}
else if(![rulerView isOrizontal] && markPoint.y >= [rulerWindow frame].origin.y && markPoint.y <= [rulerWindow frame].origin.y + [rulerWindow frame].size.height){
markerVer = [rulerWindow frame].origin.y + [rulerWindow frame].size.height -markPoint.y;
[[rulerView cursorView] setCurrentMark:markerVer];
[ballonView setCurrentValue: (int)markerVer];
[cursorWindow setAlphaValue:1];
}
else{
[[rulerView cursorView] setCurrentMark:-1000000];
[cursorWindow setAlphaValue:0];
}
}
@end