-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathWLMovingAreaHotspotHandler.m
223 lines (193 loc) · 6.83 KB
/
WLMovingAreaHotspotHandler.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
//
// WLExitAreaHotspotHandler.m
// Welly
//
// Created by K.O.ed on 09-1-26.
// Copyright 2009 Welly Group. All rights reserved.
//
#import "WLMovingAreaHotspotHandler.h"
#import "WLMouseBehaviorManager.h"
#import "WLTerminalView.h"
#import "WLTerminal.h"
#import "WLConnection.h"
NSString *const WLCommandSequencePageUp = termKeyPageUp;
NSString *const WLCommandSequencePageDown = termKeyPageDown;
NSString *const WLCommandSequenceLeftArrow = termKeyLeft;
NSString *const WLCommandSequenceHome = termKeyHome;
NSString *const WLCommandSequenceEnd = termKeyEnd;
NSString *const WLCommandSequencePressQ = @"q";
NSString *const WLMenuTitlePressHome = @"Press Home";
NSString *const WLMenuTitlePressEnd = @"Press End";
NSString *const WLMenuTitleQuitMode = @"Quit Mode";
@implementation WLMovingAreaHotspotHandler
- (id)init {
self = [super init];
_leftArrowCursor = [NSCursor resizeLeftCursor];
_pageUpCursor = [NSCursor resizeUpCursor];
_pageDownCursor = [NSCursor resizeDownCursor];
return self;
}
- (BOOL)shouldEnablePageUpDownForState:(BBSState)bbsState {
return (bbsState.state == BBSBoardList
|| bbsState.state == BBSBrowseBoard
|| bbsState.state == BBSFriendList
|| bbsState.state == BBSMailList
|| bbsState.state == BBSViewPost);
}
- (BOOL)shouldEnablePageUpDown {
return [self shouldEnablePageUpDownForState:[[_view frontMostTerminal] bbsState]];
}
- (BOOL)shouldEnableExitAreaForState:(BBSState)bbsState {
if (bbsState.state == BBSComposePost || bbsState.state == BBSWaitingEnter)
return NO;
return YES;
}
- (BOOL)shouldEnableExitArea {
return [self shouldEnableExitAreaForState:[[_view frontMostTerminal] bbsState]];
}
#pragma mark -
#pragma mark Mouse Event Handler
- (void)mouseUp:(NSEvent *)theEvent {
NSString *commandSequence = [_manager.backgroundTrackingAreaUserInfo objectForKey:WLMouseCommandSequenceUserInfoName];
[_view sendText:commandSequence];
}
- (void)mouseEntered:(NSEvent *)theEvent {
if([[_view frontMostConnection] isConnected]) {
_manager.backgroundTrackingAreaUserInfo = [[theEvent trackingArea] userInfo];
}
}
- (void)mouseExited:(NSEvent *)theEvent {
if ([NSCursor currentCursor] == [_manager.backgroundTrackingAreaUserInfo objectForKey:WLMouseCursorUserInfoName])
[_manager restoreNormalCursor];
_manager.backgroundTrackingAreaUserInfo = nil;
}
- (void)mouseMoved:(NSEvent *)theEvent {
if ([NSCursor currentCursor] == _manager.normalCursor)
[(NSCursor *)[_manager.backgroundTrackingAreaUserInfo objectForKey:WLMouseCursorUserInfoName] set];
}
#pragma mark -
#pragma mark Contextual Menu
- (IBAction)pressHome:(id)sender {
[_view sendText:WLCommandSequenceHome];
}
- (IBAction)pressEnd:(id)sender {
[_view sendText:WLCommandSequenceEnd];
}
- (IBAction)pressQ:(id)sender {
[_view sendText:WLCommandSequencePressQ];
}
- (NSMenu *)menuForEvent:(NSEvent *)theEvent {
NSMenu *menu = [[[NSMenu alloc] init] autorelease];
if ([self shouldEnablePageUpDown]) {
[menu addItemWithTitle:NSLocalizedString(WLMenuTitlePressHome, @"Contextual Menu")
action:@selector(pressHome:)
keyEquivalent:@""];
[menu addItemWithTitle:NSLocalizedString(WLMenuTitlePressEnd, @"Contextual Menu")
action:@selector(pressEnd:)
keyEquivalent:@""];
}
if ([[_view frontMostTerminal] bbsState].state == BBSBrowseBoard) {
[menu addItemWithTitle:NSLocalizedString(WLMenuTitleQuitMode, @"Contextual Menu")
action:@selector(pressQ:)
keyEquivalent:@""];
}
for (NSMenuItem *item in [menu itemArray]) {
if ([item isSeparatorItem])
continue;
[item setTarget:self];
[item setRepresentedObject:_manager.backgroundTrackingAreaUserInfo];
}
return menu;
}
#pragma mark -
#pragma mark Update State
#pragma mark Exit Area
- (void)addExitAreaAtRow:(int)r
column:(int)c
height:(int)h
width:(int)w {
NSRect rect = [_view rectAtRow:r column:c height:h width:w];
// Generate User Info
NSArray *keys = [NSArray arrayWithObjects:WLMouseHandlerUserInfoName, WLMouseCommandSequenceUserInfoName, WLMouseCursorUserInfoName, nil];
NSArray *objects = [NSArray arrayWithObjects:self, WLCommandSequenceLeftArrow, _leftArrowCursor, nil];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[_trackingAreas addObject:[_manager addTrackingAreaWithRect:rect userInfo:userInfo cursor: _leftArrowCursor]];
}
- (void)updateExitArea {
if (![self shouldEnableExitArea]) {
return;
} else {
[self addExitAreaAtRow:3
column:0
height:20
width:20];
}
}
#pragma mark pgUp/Down Area
- (void)addPageUpAreaAtRow:(int)r
column:(int)c
height:(int)h
width:(int)w {
NSRect rect = [_view rectAtRow:r column:c height:h width:w];
NSArray *keys = [NSArray arrayWithObjects:WLMouseHandlerUserInfoName, WLMouseCommandSequenceUserInfoName, WLMouseCursorUserInfoName, nil];
NSArray *objects = [NSArray arrayWithObjects:self, WLCommandSequencePageUp, _pageUpCursor, nil];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[_trackingAreas addObject:[_manager addTrackingAreaWithRect:rect userInfo:userInfo cursor:_pageUpCursor]];
}
- (void)updatePageUpArea {
if ([self shouldEnablePageUpDown]) {
[self addPageUpAreaAtRow:0
column:20
height:_maxRow / 2
width:_maxColumn - 20];
}
}
- (void)addPageDownAreaAtRow:(int)r
column:(int)c
height:(int)h
width:(int)w {
NSRect rect = [_view rectAtRow:r column:c height:h width:w];
// Generate User Info
NSArray *keys = [NSArray arrayWithObjects:WLMouseHandlerUserInfoName, WLMouseCommandSequenceUserInfoName, WLMouseCursorUserInfoName, nil];
NSArray *objects = [NSArray arrayWithObjects:self, WLCommandSequencePageDown, _pageDownCursor, nil];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[_trackingAreas addObject:[_manager addTrackingAreaWithRect:rect userInfo:userInfo cursor:_pageDownCursor]];
}
- (void)updatePageDownArea {
if ([self shouldEnablePageUpDown]) {
[self addPageDownAreaAtRow:_maxRow / 2
column:20
height:_maxRow / 2
width:_maxColumn - 20];
}
}
- (void)clear {
// Only Moving areas use cursor rects, so just discard them all.
[_view discardCursorRects];
[self removeAllTrackingAreas];
}
- (BOOL)shouldUpdate {
if (![_view shouldEnableMouse] || ![_view isConnected]) {
return YES;
}
BBSState bbsState = [[_view frontMostTerminal] bbsState];
BBSState lastBBSState = [_manager lastBBSState];
if (bbsState.state == lastBBSState.state) {
return NO;
}
if (([self shouldEnablePageUpDownForState:bbsState] == [self shouldEnablePageUpDownForState:lastBBSState]) &&
([self shouldEnableExitAreaForState:bbsState] == [self shouldEnableExitAreaForState:lastBBSState])) {
return NO;
}
return YES;
}
- (void)update {
[self clear];
if (![_view shouldEnableMouse] || ![_view isConnected]) {
return;
}
[self updateExitArea];
[self updatePageUpArea];
[self updatePageDownArea];
}
@end