| 1 |
/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */ |
| 2 |
|
| 3 |
/* AbiSource Application Framework |
| 4 |
* Copyright (C) 1998 AbiSource, Inc. |
| 5 |
* Copyright (C) 2001, 2003, 2009 Hubert Figuiere |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU General Public License |
| 9 |
* as published by the Free Software Foundation; either version 2 |
| 10 |
* of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 |
* 02110-1301 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "ut_color.h" |
| 27 |
#include "ut_assert.h" |
| 28 |
#include "ut_string.h" |
| 29 |
|
| 30 |
#include "xap_CocoaApp.h" |
| 31 |
#include "xap_CocoaToolbar_Icons.h" |
| 32 |
|
| 33 |
static NSPoint s_ButtonOnPoint[12] = { |
| 34 |
{ 0.0f, 7.0f }, |
| 35 |
{ 0.0f, 0.0f }, |
| 36 |
{ 7.0f, 0.0f }, |
| 37 |
{ 19.0f, 0.0f }, |
| 38 |
{ 26.0f, 0.0f }, |
| 39 |
{ 26.0f, 7.0f }, |
| 40 |
{ 26.0f, 19.0f }, |
| 41 |
{ 26.0f, 26.0f }, |
| 42 |
{ 19.0f, 26.0f }, |
| 43 |
{ 7.0f, 26.0f }, |
| 44 |
{ 0.0f, 26.0f }, |
| 45 |
{ 0.0f, 19.0f } |
| 46 |
}; |
| 47 |
|
| 48 |
static NSPoint s_ButtonMenuPoint[3] = { |
| 49 |
{ 26.0f, 21.0f }, |
| 50 |
{ 20.0f, 21.0f }, |
| 51 |
{ 23.0f, 26.0f } |
| 52 |
}; |
| 53 |
|
| 54 |
@implementation XAP_CocoaToolbarButton |
| 55 |
|
| 56 |
- (id)initWithFrame:(NSRect)frameRect |
| 57 |
{ |
| 58 |
if (![super initWithFrame:frameRect]) { |
| 59 |
return nil; |
| 60 |
} |
| 61 |
m_menu = nil; |
| 62 |
m_controller = nil; |
| 63 |
return self; |
| 64 |
} |
| 65 |
|
| 66 |
- (void)setMenu:(NSMenu *)menu withController:(id <XAP_CocoaButtonController>)controller |
| 67 |
{ |
| 68 |
m_menu = menu; |
| 69 |
m_controller = controller; |
| 70 |
} |
| 71 |
|
| 72 |
- (void)mouseDown:(NSEvent *)theEvent |
| 73 |
{ |
| 74 |
if (m_menu && [self isEnabled]) |
| 75 |
{ |
| 76 |
if (m_controller) |
| 77 |
{ |
| 78 |
[m_controller menuWillActivate:m_menu forButton:self]; |
| 79 |
} |
| 80 |
[NSMenu popUpContextMenu:m_menu withEvent:theEvent forView:self]; |
| 81 |
} |
| 82 |
else |
| 83 |
{ |
| 84 |
[super mouseDown:theEvent]; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
- (void)drawRect:(NSRect)aRect |
| 89 |
{ |
| 90 |
if ([self state] == NSOnState) |
| 91 |
{ |
| 92 |
[[NSColor colorWithCalibratedWhite:0.0f alpha:0.25] set]; |
| 93 |
|
| 94 |
NSBezierPath * path = [NSBezierPath bezierPath]; |
| 95 |
|
| 96 |
[path moveToPoint:s_ButtonOnPoint[ 0]]; |
| 97 |
[path curveToPoint:s_ButtonOnPoint[ 2] controlPoint1:s_ButtonOnPoint[ 1] controlPoint2:s_ButtonOnPoint[ 1]]; |
| 98 |
[path lineToPoint:s_ButtonOnPoint[ 3]]; |
| 99 |
[path curveToPoint:s_ButtonOnPoint[ 5] controlPoint1:s_ButtonOnPoint[ 4] controlPoint2:s_ButtonOnPoint[ 4]]; |
| 100 |
[path lineToPoint:s_ButtonOnPoint[ 6]]; |
| 101 |
[path curveToPoint:s_ButtonOnPoint[ 8] controlPoint1:s_ButtonOnPoint[ 7] controlPoint2:s_ButtonOnPoint[ 7]]; |
| 102 |
[path lineToPoint:s_ButtonOnPoint[ 9]]; |
| 103 |
[path curveToPoint:s_ButtonOnPoint[11] controlPoint1:s_ButtonOnPoint[10] controlPoint2:s_ButtonOnPoint[10]]; |
| 104 |
[path closePath]; |
| 105 |
[path fill]; |
| 106 |
} |
| 107 |
[super drawRect:aRect]; |
| 108 |
|
| 109 |
if (m_menu) |
| 110 |
{ |
| 111 |
[[NSColor blackColor] set]; |
| 112 |
|
| 113 |
NSBezierPath * path = [NSBezierPath bezierPath]; |
| 114 |
|
| 115 |
[path moveToPoint:s_ButtonMenuPoint[0]]; |
| 116 |
[path lineToPoint:s_ButtonMenuPoint[1]]; |
| 117 |
[path lineToPoint:s_ButtonMenuPoint[2]]; |
| 118 |
[path closePath]; |
| 119 |
[path fill]; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
@end |
| 124 |
|
| 125 |
XAP_CocoaToolbar_Icons::XAP_CocoaToolbar_Icons(void) |
| 126 |
{ |
| 127 |
} |
| 128 |
|
| 129 |
XAP_CocoaToolbar_Icons::~XAP_CocoaToolbar_Icons(void) |
| 130 |
{ |
| 131 |
// TODO do we need to keep some kind of list |
| 132 |
// TODO of the things we have created and |
| 133 |
// TODO handed out, so that we can delete them ?? |
| 134 |
} |
| 135 |
|
| 136 |
NSString * XAP_CocoaToolbar_Icons::getPNGNameForIcon(const char * szIconID) |
| 137 |
{ |
| 138 |
const char * szIconName = NULL; |
| 139 |
|
| 140 |
if (XAP_Toolbar_Icons::_findIconNameForID(szIconID, &szIconName)) |
| 141 |
{ |
| 142 |
UT_UTF8String name(szIconName); |
| 143 |
|
| 144 |
if (char * suffix = strstr(szIconName, "_xpm")) |
| 145 |
{ |
| 146 |
name.assign(szIconName, suffix - szIconName); |
| 147 |
} |
| 148 |
name += ".png"; |
| 149 |
|
| 150 |
return [NSString stringWithUTF8String:(name.utf8_str())]; |
| 151 |
} |
| 152 |
|
| 153 |
UT_ASSERT_NOT_REACHED(); |
| 154 |
return nil; |
| 155 |
} |
| 156 |
|
| 157 |
NSString * XAP_CocoaToolbar_Icons::getFilenameForIcon(NSString * iconName) |
| 158 |
{ |
| 159 |
NSString * filename = iconName; |
| 160 |
|
| 161 |
if (iconName) |
| 162 |
{ |
| 163 |
XAP_CocoaApp * pApp = static_cast<XAP_CocoaApp *>(XAP_App::getApp()); |
| 164 |
|
| 165 |
std::string path; |
| 166 |
|
| 167 |
if (pApp->findAbiSuiteLibFile(path, [iconName UTF8String], "ToolbarIcons")) // I'd love to do this inside the bundle but Cocoa gets confused if I try |
| 168 |
{ |
| 169 |
filename = [NSString stringWithUTF8String:(path.c_str())]; |
| 170 |
} |
| 171 |
else if (pApp->findAbiSuiteBundleFile(path, [iconName UTF8String])) |
| 172 |
{ |
| 173 |
filename = [NSString stringWithUTF8String:(path.c_str())]; |
| 174 |
} |
| 175 |
} |
| 176 |
return filename; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
/*! |
| 181 |
returns the pixmap for the named icon |
| 182 |
|
| 183 |
\param szIconName the name of the icon |
| 184 |
\return the newly allocated NSImage [autoreleased] |
| 185 |
*/ |
| 186 |
NSImage * XAP_CocoaToolbar_Icons::getPixmapForIcon(const char * szIconID) |
| 187 |
{ |
| 188 |
UT_ASSERT(szIconID && *szIconID); |
| 189 |
|
| 190 |
NSImage * pixmap = nil; |
| 191 |
|
| 192 |
NSString * path = XAP_CocoaToolbar_Icons::getFilenameForIcon(XAP_CocoaToolbar_Icons::getPNGNameForIcon(szIconID)); |
| 193 |
|
| 194 |
if (path) |
| 195 |
{ |
| 196 |
pixmap = [[NSImage alloc] initWithContentsOfFile:path]; |
| 197 |
|
| 198 |
if (pixmap) |
| 199 |
{ |
| 200 |
[pixmap autorelease]; |
| 201 |
} |
| 202 |
else |
| 203 |
{ |
| 204 |
UT_ASSERT(pixmap); |
| 205 |
pixmap = [NSImage imageNamed:@"NSApplicationIcon"]; |
| 206 |
} |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
UT_ASSERT_NOT_REACHED(); |
| 211 |
} |
| 212 |
return pixmap; |
| 213 |
} |
| 214 |
|