forked from seanm/GCUndoManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCUndoTestView.m
172 lines (124 loc) · 3.17 KB
/
GCUndoTestView.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// GCUndoTestView.m
// GCDrawKit
//
// Created by graham on 5/12/09.
// Copyright 2009-2011 Apptree.net. All rights reserved.
//
#import "GCUndoTestView.h"
#import "GCUndoManager.h"
@implementation GCUndoTestView
- (id) initWithFrame:(NSRect) frame
{
self = [super initWithFrame:frame];
if (self)
{
draggedBox = NSMakeRect( 100, 100, 100, 100 );
dragBoxColour = [[NSColor redColor] retain];
}
return self;
}
- (void) drawRect:(NSRect) dirtyRect
{
[[NSColor whiteColor] set];
NSRectFill(dirtyRect);
[[NSColor lightGrayColor] set];
NSFrameRectWithWidth([self bounds], (CGFloat)1.0);
[[self draggedBoxColour] set];
NSRectFill(draggedBox);
[[NSColor blackColor] set];
NSFrameRectWithWidth(draggedBox, (CGFloat)0.5);
NSRect sizeRect = NSMakeRect( NSMaxX( draggedBox ) - 10, NSMaxY( draggedBox ) -10, 10, 10 );
NSFrameRectWithWidth( sizeRect, (CGFloat)0.5);
}
- (BOOL) isFlipped
{
return YES;
}
- (void) mouseDown:(NSEvent*) event
{
[[self undoManager] beginUndoGrouping];
NSPoint local = [self convertPoint:[event locationInWindow] fromView:nil];
hit = [self hitTestBox:local];
if( hit )
{
mouseOffset.x = local.x - draggedBox.origin.x;
mouseOffset.y = local.y - draggedBox.origin.y;
}
}
- (void) mouseDragged:(NSEvent*) event
{
NSPoint local = [self convertPoint:[event locationInWindow] fromView:nil];
switch( hit )
{
case kHitForMove:
local.x -= mouseOffset.x;
local.y -= mouseOffset.y;
[self setDraggedBoxPosition:local];
break;
case kHitForSize:
{
NSSize size;
size.width = local.x - draggedBox.origin.x;
size.height = local.y - draggedBox.origin.y;
[self setDraggedBoxSize:size];
}
break;
}
}
- (void) mouseUp:(NSEvent*) event
{
(void)event;
[[self undoManager] endUndoGrouping];
}
- (void) setDraggedBoxPosition:(NSPoint) p
{
[[[self undoManager] prepareWithInvocationTarget:self] setDraggedBoxPosition:[self draggedBoxPosition]];
[self setNeedsDisplayInRect:draggedBox];
draggedBox.origin = p;
[self setNeedsDisplayInRect:draggedBox];
[[self undoManager] setActionName:@"Move"];
}
- (NSPoint) draggedBoxPosition
{
return draggedBox.origin;
}
- (void) setDraggedBoxSize:(NSSize) size
{
[[[self undoManager] prepareWithInvocationTarget:self] setDraggedBoxSize:[self draggedBoxSize]];
[self setNeedsDisplayInRect:draggedBox];
draggedBox.size = size;
[self setNeedsDisplayInRect:draggedBox];
[[self undoManager] setActionName:@"Resize"];
}
- (NSSize) draggedBoxSize
{
return draggedBox.size;
}
- (void) setDraggedBoxColour:(NSColor*) aColour
{
[[[self undoManager] prepareWithInvocationTarget:self] setDraggedBoxColour:[self draggedBoxColour]];
[aColour retain];
[dragBoxColour release];
dragBoxColour = aColour;
[self setNeedsDisplayInRect:draggedBox];
[[self undoManager] setActionName:@"Change Colour"];
}
- (NSColor*) draggedBoxColour
{
return dragBoxColour;
}
- (NSInteger) hitTestBox:(NSPoint) p
{
if( NSPointInRect( p, draggedBox ))
{
NSRect sizeRect = NSMakeRect( NSMaxX( draggedBox ) - 10, NSMaxY( draggedBox ) -10, 10, 10 );
if( NSPointInRect( p, sizeRect ))
return kHitForSize;
else
return kHitForMove;
}
else
return kNoHit;
}
@end