-
Notifications
You must be signed in to change notification settings - Fork 20
/
FPToolPaletteController.m
158 lines (141 loc) · 5.21 KB
/
FPToolPaletteController.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
#import "FPToolPaletteController.h"
#import "FPDocumentWindow.h"
#import "FPRectangle.h"
#import "FPEllipse.h"
#import "FPSquiggle.h"
#import "FPTextAreaB.h"
#import "FPCheckmark.h"
NSString *FPToolChosen = @"FPToolChosen";
@implementation FPToolPaletteController
static FPToolPaletteController *_sharedController;
- (void)windowDidLoad {
[super windowDidLoad];
[_buttonArray
makeObjectsPerformSelector:@selector(setRefusesFirstResponder:)
withObject:(id)YES];
// [arrowToolButton setRefusesFirstResponder:YES];
// [ellipseToolButton setRefusesFirstResponder:YES];
// [rectangleToolButton setRefusesFirstResponder:YES];
// [squiggleToolButton setRefusesFirstResponder:YES];
// [textAreaToolButton setRefusesFirstResponder:YES];
// [(NSPanel *)[self window] setFloatingPanel:YES];
[(NSPanel *)[self window] setBecomesKeyOnlyIfNeeded:YES];
}
- (void)awakeFromNib
{
[(NSPanel *)[self window] setFloatingPanel:YES];
[(NSPanel *)[self window] setBecomesKeyOnlyIfNeeded:YES];
_buttonArray = [NSArray arrayWithObjects:arrowToolButton,
ellipseToolButton,
rectangleToolButton,
squiggleToolButton,
textAreaToolButton,
textFieldToolButton,
checkmarkToolButton,
stampToolButton,
nil];
[_buttonArray retain];
assert([_buttonArray count] > 0);
_sharedController = self;
_inQuickMove = NO;
_toolBeforeQuickMove = 0;
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(beginQuickMove:)
name:FPBeginQuickMove object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(abortQuickMove:)
name:FPAbortQuickMove object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(endQuickMove:)
name:FPEndQuickMove object:nil];
}
+ (FPToolPaletteController *)sharedToolPaletteController
{
return _sharedController;
}
- (IBAction)chooseTool:(id)sender
{
[_buttonArray makeObjectsPerformSelector:@selector(setState:)
withObject:(id)NSOffState];
[sender setState:NSOnState];
[[NSNotificationCenter defaultCenter] postNotification:
[NSNotification notificationWithName:FPToolChosen
object:self]];
}
- (unsigned int)currentTool
{
for (unsigned int i = 0; i < [_buttonArray count]; i++) {
NSButton *b = [_buttonArray objectAtIndex:i];
DLog(@"button = 0x%08x\n", (unsigned)b);
if ([[_buttonArray objectAtIndex:i] state] == NSOnState)
return i;
}
assert(0);
return FPToolRectangle;
}
- (Class)classForCurrentTool
{
switch ([self currentTool]) {
case FPToolEllipse: DLog(@"ellispe\n"); return [FPEllipse class];
case FPToolRectangle: DLog(@"rect\n"); return [FPRectangle class];
case FPToolSquiggle: DLog(@"squiggle\n"); return [FPSquiggle class];
case FPToolTextArea: DLog(@"text area\n"); return [FPTextAreaB class];
case FPToolCheckmark: DLog(@"checkmark\n"); return [FPCheckmark class];
//case FPToolTextField: DLog(@"text field\n"); return [FPTextField class];
}
return [FPRectangle class];
}
- (void)keyDown:(NSEvent *)theEvent
{
if (1 != [[theEvent charactersIgnoringModifiers] length])
return;
// we don't want any modifiers, except numeric pad is okay
if ((NSDeviceIndependentModifierFlagsMask ^ NSNumericPadKeyMask) & [theEvent modifierFlags]) {
return;
}
unichar c = [[theEvent charactersIgnoringModifiers] characterAtIndex:0];
switch (c) {
case 't':
[self chooseTool:textAreaToolButton];
break;
case 'm':
[self chooseTool:arrowToolButton];
break;
case 'e':
[self chooseTool:ellipseToolButton];
break;
case 'u':
[self chooseTool:rectangleToolButton];
break;
case 'p':
[self chooseTool:squiggleToolButton];
break;
case 'x':
[self chooseTool:checkmarkToolButton];
break;
}
}
- (void)beginQuickMove:(id)unused
{
_toolBeforeQuickMove = [self currentTool];
[_buttonArray makeObjectsPerformSelector:@selector(setEnabled:)
withObject:(id)NO];
[arrowToolButton setEnabled:YES];
[self chooseTool:arrowToolButton];
_inQuickMove = YES;
}
- (void)abortQuickMove:(id)unused
{
[_buttonArray makeObjectsPerformSelector:@selector(setEnabled:)
withObject:(id)YES];
[self chooseTool:[_buttonArray objectAtIndex:FPToolArrow]];
_inQuickMove = NO;
}
- (void)endQuickMove:(id)unused
{
if (NO == _inQuickMove) return;
[_buttonArray makeObjectsPerformSelector:@selector(setEnabled:)
withObject:(id)YES];
[self chooseTool:[_buttonArray objectAtIndex:_toolBeforeQuickMove]];
}
@end