-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathApplicationButtonMatrix.m
More file actions
146 lines (135 loc) · 3.42 KB
/
ApplicationButtonMatrix.m
File metadata and controls
146 lines (135 loc) · 3.42 KB
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
//
// ApplicationButtonMatrix.m
// MatrixSample
//
// Created by - on 09/12/08.
// Copyright 2009 Hiroshi Hashiguchi. All rights reserved.
//
#import "ApplicationButtonMatrix.h"
#import "ApplicationButtonCell.h"
#define CELL_SPACING 4.0
@implementation ApplicationButtonMatrix
@synthesize trackingArea;
#pragma mark -
#pragma mark Utility
- (void)updateTrackingArea
{
if (self.trackingArea) {
[self removeTrackingArea:self.trackingArea];
}
self.trackingArea = [[[NSTrackingArea alloc]
initWithRect:[self bounds]
options:(NSTrackingMouseEnteredAndExited |
NSTrackingMouseMoved |
NSTrackingActiveAlways )
owner:self
userInfo:nil] autorelease];
[self addTrackingArea:self.trackingArea];
}
#pragma mark -
#pragma mark Initializatin and Deallocation
- (id)init
{
ApplicationButtonCell* cell = [[[ApplicationButtonCell alloc] init] autorelease];
self = [super initWithFrame:NSZeroRect
mode:NSHighlightModeMatrix
prototype:cell
numberOfRows:0
numberOfColumns:1];
if (self) {
[self setIntercellSpacing:NSMakeSize(0, CELL_SPACING)];
[self setWantsLayer:YES];
}
return self;
}
- (void) dealloc
{
if (self.trackingArea) {
[self removeTrackingArea:self.trackingArea];
}
self.trackingArea = nil;
[super dealloc];
}
#pragma mark -
#pragma mark Overriden methods
- (void)sizeToCells
{
[super sizeToCells];
[self updateTrackingArea];
}
#pragma mark -
#pragma mark Event handling
- (ApplicationButtonCell*)cellAtPoint:(NSPoint)point
{
NSInteger max_row = [self numberOfRows];
NSInteger row;
for (row=0; row < max_row; row++) {
NSRect cellFrame = [self cellFrameAtRow:row column:0];
if (NSPointInRect(point, cellFrame)) {
return (ApplicationButtonCell*)[self cellAtRow:row column:0];
}
}
return nil;
}
- (void)mouseDown:(NSEvent*)theEvent
{
NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
ApplicationButtonCell* cell = [self cellAtPoint:point];
cell.cellState = CELL_STATE_ON;
if (pushedCell != cell) {
if (overedCell != pushedCell) {
pushedCell.cellState = CELL_STATE_OFF;
}
pushedCell = cell;
}
[self setNeedsDisplay:YES];
[super mouseDown:theEvent];
}
- (void)mouseUp:(NSEvent*)theEvent
{
if (pushedCell) {
if (pushedCell == overedCell) {
pushedCell.cellState = CELL_STATE_OVER;
} else {
pushedCell.cellState = CELL_STATE_OFF;
}
pushedCell = nil;
}
[self setNeedsDisplay:YES];
[super mouseUp:theEvent];
}
- (void)mouseEntered:(NSEvent *)theEvent
{
NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
ApplicationButtonCell* cell = [self cellAtPoint:point];
cell.cellState = CELL_STATE_OVER;
if (overedCell != cell) {
overedCell.cellState = CELL_STATE_OFF;
overedCell = cell;
}
[self setNeedsDisplay:YES];
// [super mouseEntered:theEvent];
}
- (void)mouseExited:(NSEvent *)theEvent
{
if (overedCell) {
overedCell.cellState = CELL_STATE_OFF;
overedCell = nil;
}
[self setNeedsDisplay:YES];
// [super mouseExited:theEvent];
}
- (void)mouseMoved:(NSEvent *)theEvent
{
// NSPoint point = [self convertPointFromBase:[theEvent locationInWindow]];
NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
ApplicationButtonCell* cell = [self cellAtPoint:point];
cell.cellState = CELL_STATE_OVER;
if (overedCell != cell) {
overedCell.cellState = CELL_STATE_OFF;
overedCell = cell;
}
[self setNeedsDisplay:YES];
// [super mouseMoved:theEvent];
}
@end