| 1 |
// |
| 2 |
// TrackAttributeCell.m |
| 3 |
// |
| 4 |
/* |
| 5 |
Copyright (c) 2000-2011 Toshi Nagata. All rights reserved. |
| 6 |
|
| 7 |
This program is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation version 2 of the License. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
*/ |
| 16 |
|
| 17 |
#import "TrackAttributeCell.h" |
| 18 |
#include "MDTrack.h" |
| 19 |
|
| 20 |
@implementation TrackAttributeCell |
| 21 |
|
| 22 |
static NSDictionary *sBlackTextAttribute = nil; |
| 23 |
static NSDictionary *sGrayTextAttribute = nil; |
| 24 |
static int sLastPartCode = 0; |
| 25 |
|
| 26 |
+ (BOOL)prefersTrackingUntilMouseUp |
| 27 |
{ |
| 28 |
/* Continue to track mouse when the cursor moves outside the cell */ |
| 29 |
return YES; |
| 30 |
} |
| 31 |
|
| 32 |
- (int)partForPoint:(NSPoint)point inView:(NSView *)controlView |
| 33 |
{ |
| 34 |
int part; |
| 35 |
NSRect bounds = startCellFrame; |
| 36 |
if (NSMouseInRect(point, bounds, [controlView isFlipped])) { |
| 37 |
part = (int)((point.x - bounds.origin.x) * 3 / bounds.size.width) + 1; |
| 38 |
if (part > 3) |
| 39 |
part = 3; |
| 40 |
else if (part < 1) |
| 41 |
part = 1; |
| 42 |
} else part = 0; |
| 43 |
return part; |
| 44 |
} |
| 45 |
|
| 46 |
- (BOOL)trackMouse:(NSEvent *)theEvent inRect:(NSRect)cellFrame ofView:(NSView *)controlView untilMouseUp:(BOOL)untilMouseUp |
| 47 |
{ |
| 48 |
startCellFrame = cellFrame; |
| 49 |
return [super trackMouse: theEvent inRect: cellFrame ofView: controlView untilMouseUp: untilMouseUp]; |
| 50 |
} |
| 51 |
|
| 52 |
- (BOOL)startTrackingAt:(NSPoint)startPoint inView:(NSView *)controlView |
| 53 |
{ |
| 54 |
id obj = [self objectValue]; |
| 55 |
int value; |
| 56 |
if ([obj respondsToSelector: @selector(intValue)]) |
| 57 |
value = [obj intValue]; |
| 58 |
else value = 0; |
| 59 |
partCode = 0; |
| 60 |
startPartCode = [self partForPoint: startPoint inView: controlView]; |
| 61 |
if (startPartCode < 3 && (value & kMDTrackAttributeMuteBySolo)) |
| 62 |
startPartCode = 0; |
| 63 |
currentPartCode = startPartCode; |
| 64 |
[controlView display]; |
| 65 |
// NSLog(@"startTrackingAt:inView: partCode = %d", partCode); |
| 66 |
if (currentPartCode == 0) { |
| 67 |
sLastPartCode = 0; |
| 68 |
return NO; |
| 69 |
} else return YES; |
| 70 |
} |
| 71 |
|
| 72 |
- (BOOL)continueTracking:(NSPoint)lastPoint at:(NSPoint)currentPoint inView:(NSView *)controlView |
| 73 |
{ |
| 74 |
int aPart; |
| 75 |
if (startPartCode == 0) |
| 76 |
return YES; |
| 77 |
aPart = [self partForPoint: currentPoint inView: controlView]; |
| 78 |
if (aPart == startPartCode) |
| 79 |
currentPartCode = startPartCode; |
| 80 |
else |
| 81 |
currentPartCode = 0; |
| 82 |
// NSLog(@"continueTracking:at:inView: aPart = %d", aPart); |
| 83 |
[controlView display]; |
| 84 |
return YES; |
| 85 |
} |
| 86 |
|
| 87 |
- (void)stopTracking:(NSPoint)lastPoint at:(NSPoint)stopPoint inView:(NSView *)controlView mouseIsUp:(BOOL)flag |
| 88 |
{ |
| 89 |
if (flag) |
| 90 |
sLastPartCode = currentPartCode; |
| 91 |
// NSLog(@"stopTracking:at:inView:mouseIsUp: endPartCode = %d", endPartCode); |
| 92 |
currentPartCode = 0; |
| 93 |
[controlView setNeedsDisplay: YES]; |
| 94 |
} |
| 95 |
|
| 96 |
+ (int)lastPartCode |
| 97 |
{ |
| 98 |
return sLastPartCode; |
| 99 |
} |
| 100 |
|
| 101 |
static void |
| 102 |
drawString(NSString *string, NSDictionary *attr, NSRect frame) |
| 103 |
{ |
| 104 |
[string drawInRect: frame withAttributes: attr]; |
| 105 |
// NSSize size = [string sizeWithAttributes: attr]; |
| 106 |
// NSPoint pt = NSMakePoint(floor(frame.origin.x + (frame.size.width - size.width) / 2), floor(frame.origin.y + frame.size.height - 0 - size.height)); |
| 107 |
// [string drawAtPoint: pt withAttributes: attr]; |
| 108 |
} |
| 109 |
|
| 110 |
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView |
| 111 |
{ |
| 112 |
id obj = [self objectValue]; |
| 113 |
int value; |
| 114 |
NSRect frame = cellFrame; |
| 115 |
float x0, x1, x2, x3, y0, y1; |
| 116 |
NSDictionary *attr; |
| 117 |
if ([obj respondsToSelector: @selector(intValue)]) |
| 118 |
value = [obj intValue]; |
| 119 |
else value = 0; |
| 120 |
if (sBlackTextAttribute == nil) { |
| 121 |
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 122 |
NSFont *font = [NSFont systemFontOfSize: [NSFont smallSystemFontSize]]; |
| 123 |
// [style setAlignment: NSCenterTextAlignment]; |
| 124 |
sBlackTextAttribute = [[NSDictionary dictionaryWithObjectsAndKeys: [NSColor blackColor], NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, font, NSFontAttributeName, nil] retain]; |
| 125 |
sGrayTextAttribute = [[NSDictionary dictionaryWithObjectsAndKeys: [NSColor lightGrayColor], NSForegroundColorAttributeName, style, NSParagraphStyleAttributeName, font, NSFontAttributeName, nil] retain]; |
| 126 |
} |
| 127 |
// printf("value=%d\n", value); |
| 128 |
x0 = (float)floor(frame.origin.x); |
| 129 |
x1 = x0 + (float)floor(frame.size.width / 3); |
| 130 |
x2 = x0 + (float)floor(frame.size.width * 2 / 3); |
| 131 |
x3 = x0 + (float)floor(frame.size.width); |
| 132 |
frame.size.width = x1 - x0; |
| 133 |
if (value & kMDTrackAttributeRecord) { |
| 134 |
[[NSColor redColor] set]; |
| 135 |
NSRectFill(frame); |
| 136 |
} |
| 137 |
if (value & kMDTrackAttributeMuteBySolo) |
| 138 |
attr = sGrayTextAttribute; |
| 139 |
else attr = sBlackTextAttribute; |
| 140 |
drawString(@"R", attr, frame); |
| 141 |
frame.origin.x = x1; |
| 142 |
frame.size.width = x2 - x1; |
| 143 |
if (value & kMDTrackAttributeMute) { |
| 144 |
[[NSColor lightGrayColor] set]; |
| 145 |
NSRectFill(frame); |
| 146 |
} |
| 147 |
drawString(@"M", attr, frame); |
| 148 |
frame.origin.x = x2; |
| 149 |
frame.size.width = x3 - x2; |
| 150 |
if (value & kMDTrackAttributeSolo) { |
| 151 |
[[NSColor cyanColor] set]; |
| 152 |
NSRectFill(frame); |
| 153 |
} |
| 154 |
drawString(@"S", sBlackTextAttribute, frame); |
| 155 |
[[NSColor lightGrayColor] set]; |
| 156 |
// NSFrameRect(cellFrame); |
| 157 |
y0 = cellFrame.origin.y; |
| 158 |
y1 = cellFrame.origin.y + cellFrame.size.height; |
| 159 |
[NSBezierPath strokeLineFromPoint: NSMakePoint(x0, y1 - 0.5f) toPoint: NSMakePoint(x3, y1 - 0.5f)]; |
| 160 |
[NSBezierPath strokeLineFromPoint: NSMakePoint(x0, y0 + 0.5f) toPoint: NSMakePoint(x3, y0 + 0.5f)]; |
| 161 |
[NSBezierPath strokeLineFromPoint: NSMakePoint(x0 + 0.5f, y1) toPoint: NSMakePoint(x0 + 0.5f, y0)]; |
| 162 |
[NSBezierPath strokeLineFromPoint: NSMakePoint(x1 + 0.5f, y1) toPoint: NSMakePoint(x1 + 0.5f, y0)]; |
| 163 |
[NSBezierPath strokeLineFromPoint: NSMakePoint(x2 + 0.5f, y1) toPoint: NSMakePoint(x2 + 0.5f, y0)]; |
| 164 |
[NSBezierPath strokeLineFromPoint: NSMakePoint(x3 - 0.5f, y1) toPoint: NSMakePoint(x3 - 0.5f, y0)]; |
| 165 |
if (currentPartCode > 0) { |
| 166 |
switch(currentPartCode) { |
| 167 |
case 1: frame.origin.x = x0; frame.size.width = x1 - x0; break; |
| 168 |
case 2: frame.origin.x = x1; frame.size.width = x2 - x1; break; |
| 169 |
case 3: frame.origin.x = x2; frame.size.width = x3 - x2; break; |
| 170 |
} |
| 171 |
[[NSColor lightGrayColor] set]; |
| 172 |
NSRectFillUsingOperation(frame, NSCompositePlusDarker); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
@end |