| 1 |
// |
| 2 |
// MyPopUpButton.m |
| 3 |
// Alchemusica |
| 4 |
// |
| 5 |
// Created by Toshi Nagata on Sun Jan 1 2006. |
| 6 |
/* |
| 7 |
Copyright (c) 2006-2011 Toshi Nagata. All rights reserved. |
| 8 |
|
| 9 |
This program is free software; you can redistribute it and/or modify |
| 10 |
it under the terms of the GNU General Public License as published by |
| 11 |
the Free Software Foundation version 2 of the License. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
*/ |
| 18 |
|
| 19 |
#import "MyPopUpButton.h" |
| 20 |
|
| 21 |
@implementation MyPopUpButton |
| 22 |
|
| 23 |
static NSImage *sTriangleImage, *sDoubleTriangleImage; |
| 24 |
|
| 25 |
+ (NSImage *)triangleImage |
| 26 |
{ |
| 27 |
if (sTriangleImage == nil) { |
| 28 |
sTriangleImage = [[NSImage allocWithZone: [self zone]] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"triangle.png" ofType: nil]]; |
| 29 |
} |
| 30 |
return sTriangleImage; |
| 31 |
} |
| 32 |
|
| 33 |
+ (NSImage *)doubleTriangleImage |
| 34 |
{ |
| 35 |
if (sDoubleTriangleImage == nil) { |
| 36 |
sDoubleTriangleImage = [[NSImage allocWithZone: [self zone]] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"double_triangle.png" ofType: nil]]; |
| 37 |
} |
| 38 |
return sDoubleTriangleImage; |
| 39 |
} |
| 40 |
|
| 41 |
- (void)dealloc |
| 42 |
{ |
| 43 |
if (textColor != nil) |
| 44 |
[textColor release]; |
| 45 |
if (backgroundColor != nil) |
| 46 |
[backgroundColor release]; |
| 47 |
[super dealloc]; |
| 48 |
} |
| 49 |
|
| 50 |
- (void)superDrawRect: (NSRect)aRect |
| 51 |
{ |
| 52 |
if (backgroundColor != nil) { |
| 53 |
NSRect boundsRect = [self bounds]; |
| 54 |
[[NSColor lightGrayColor] set]; |
| 55 |
NSFrameRect(boundsRect); |
| 56 |
[backgroundColor set]; |
| 57 |
NSRectFill(NSInsetRect(boundsRect, 1, 1)); |
| 58 |
} else [super drawRect:aRect]; |
| 59 |
} |
| 60 |
|
| 61 |
- (void)drawRect: (NSRect)aRect |
| 62 |
{ |
| 63 |
NSRect theRect, r; |
| 64 |
NSSize size; |
| 65 |
float fraction; |
| 66 |
NSImage *theImage; |
| 67 |
NSString *theTitle; |
| 68 |
id item = [self selectedItem]; |
| 69 |
theImage = [item image]; |
| 70 |
theTitle = [item title]; |
| 71 |
theRect = [self bounds]; |
| 72 |
if ([self isEnabled]) |
| 73 |
fraction = 1.0f; |
| 74 |
else fraction = 0.5f; |
| 75 |
if (theTitle != nil) |
| 76 |
[[theTitle retain] autorelease]; |
| 77 |
[item setTitle:@""]; |
| 78 |
if (theImage != nil) { |
| 79 |
// Draw only background |
| 80 |
NSPoint center; |
| 81 |
[[theImage retain] autorelease]; |
| 82 |
[item setImage:nil]; |
| 83 |
[self superDrawRect:aRect]; |
| 84 |
[item setImage:theImage]; |
| 85 |
// And draw the image as we like |
| 86 |
center.x = theRect.origin.x + theRect.size.width / 2; |
| 87 |
center.y = theRect.origin.y + theRect.size.height / 2; |
| 88 |
if (theImage != nil) { |
| 89 |
size = [theImage size]; |
| 90 |
r.origin.x = center.x - size.width / 2; |
| 91 |
r.origin.y = center.y - size.height / 2; |
| 92 |
r.size = size; |
| 93 |
[theImage drawInRect:r fromRect:NSZeroRect operation:NSCompositeSourceAtop fraction:fraction respectFlipped:YES hints:nil]; |
| 94 |
} |
| 95 |
} else { |
| 96 |
NSAttributedString *atitle; |
| 97 |
NSFont *font; |
| 98 |
NSMutableDictionary *attr; |
| 99 |
[self superDrawRect:aRect]; |
| 100 |
if (theTitle != nil) { |
| 101 |
// We draw the title by ourselves and restore title |
| 102 |
NSControlSize controlSize = [[self cell] controlSize]; |
| 103 |
font = [NSFont labelFontOfSize:[NSFont systemFontSizeForControlSize:controlSize]]; |
| 104 |
attr = [NSMutableDictionary dictionaryWithObjectsAndKeys: |
| 105 |
font, NSFontAttributeName, |
| 106 |
nil]; |
| 107 |
if (textColor != nil) { |
| 108 |
[attr setObject:textColor forKey:NSForegroundColorAttributeName]; |
| 109 |
} |
| 110 |
atitle = [[NSAttributedString alloc] initWithString:theTitle attributes: attr]; |
| 111 |
[atitle drawInRect:NSInsetRect(theRect, 4, 1)]; |
| 112 |
} |
| 113 |
} |
| 114 |
[item setTitle:theTitle]; |
| 115 |
r.origin.x = theRect.origin.x + theRect.size.width - 7; |
| 116 |
r.origin.y = theRect.origin.y + theRect.size.height - 7; |
| 117 |
r.size.width = 5; |
| 118 |
r.size.height = 5; |
| 119 |
[[MyPopUpButton triangleImage] drawInRect:r fromRect:NSZeroRect operation:NSCompositeSourceAtop fraction:fraction respectFlipped:YES hints:nil]; |
| 120 |
} |
| 121 |
|
| 122 |
- (void)setTextColor:(NSColor *)color |
| 123 |
{ |
| 124 |
[color retain]; |
| 125 |
[textColor release]; |
| 126 |
textColor = color; |
| 127 |
} |
| 128 |
|
| 129 |
- (NSColor *)textColor |
| 130 |
{ |
| 131 |
return textColor; |
| 132 |
} |
| 133 |
|
| 134 |
- (void)setBackgroundColor:(NSColor *)color |
| 135 |
{ |
| 136 |
[color retain]; |
| 137 |
if (backgroundColor != nil) |
| 138 |
[backgroundColor release]; |
| 139 |
backgroundColor = color; |
| 140 |
} |
| 141 |
|
| 142 |
- (NSColor *)backgroundColor |
| 143 |
{ |
| 144 |
return backgroundColor; |
| 145 |
} |
| 146 |
|
| 147 |
// Implement the menu item selection with hierarchical menus |
| 148 |
- (NSMenuItem *)selectedItem |
| 149 |
{ |
| 150 |
NSMenuItem *item = [super selectedItem]; |
| 151 |
if (item == nil && selectedItem != nil) |
| 152 |
return selectedItem; |
| 153 |
else return item; |
| 154 |
} |
| 155 |
|
| 156 |
- (void)selectItem:(NSMenuItem *)anItem |
| 157 |
{ |
| 158 |
NSMenuItem *item; |
| 159 |
if (selectedItem) |
| 160 |
[selectedItem setState:NSOffState]; |
| 161 |
[super selectItem:anItem]; |
| 162 |
item = [super selectedItem]; |
| 163 |
if (item == nil) { |
| 164 |
// Should be a submenu item |
| 165 |
selectedItem = anItem; |
| 166 |
[selectedItem setState:NSOnState]; |
| 167 |
} |
| 168 |
else selectedItem = nil; |
| 169 |
} |
| 170 |
|
| 171 |
@end |