-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathIPSCategories.m
executable file
·205 lines (166 loc) · 6.98 KB
/
IPSCategories.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
/*
Copyright (c) 2007, Marketcircle Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#import "IPSCategories.h"
@implementation NSImage (MCAdditions)
+ (NSImage *)imageNamed:(NSString *)imageName inBundle:(NSBundle *)aBundle;
{
NSImage *image;
NSString *path;
image = [NSImage imageNamed:imageName];
if (image && [image size].width != 0)
return image;
path = [aBundle pathForImageResource:imageName];
if (!path)
return nil;
image = [[NSImage alloc] initWithContentsOfFile:path];
[image setName:imageName];
return image;
}
@end
@implementation NSFont (MCAdditions)
// Tries to make a font with this name and size, and if it fails makes a system font with this name and size
+ (NSFont *)safeFontWithName:(NSString *)name size:(float)size;
{
NSFont *fnt = [NSFont fontWithName:name size:size];
if (!fnt)
fnt = [NSFont systemFontOfSize:size];
return fnt;
}
@end
@implementation NSAttributedString (MCAdditions)
- (NSRect)drawInRectangle:(NSRect)rectangle alignment:(NSTextAlignment)alignment verticallyCentered:(BOOL)verticallyCenter;
{
// ASSUMPTION: This is for one line
// ASSUMPTION: We're drawing into a flipped view!
static NSTextStorage *showStringTextStorage = nil;
static NSLayoutManager *showStringLayoutManager = nil;
static NSTextContainer *showStringTextContainer = nil;
NSRange drawGlyphRange;
NSRange lineCharacterRange;
NSRect lineFragmentRect;
NSSize lineSize;
NSString *ellipsisString;
NSSize ellipsisSize;
NSDictionary *ellipsisAttributes;
BOOL requiresEllipsis;
BOOL lineTooLong;
if ([self length] == 0)
return NSZeroRect;
if (showStringTextStorage == nil) {
showStringTextStorage = [[NSTextStorage alloc] init];
showStringLayoutManager = [[NSLayoutManager alloc] init];
[showStringTextStorage addLayoutManager:showStringLayoutManager];
showStringTextContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(1.0e7, 1.0e7)];
[showStringTextContainer setLineFragmentPadding:0.0];
[showStringLayoutManager addTextContainer:showStringTextContainer];
}
[showStringTextStorage setAttributedString:self];
lineFragmentRect = [showStringLayoutManager lineFragmentUsedRectForGlyphAtIndex:0 effectiveRange:&drawGlyphRange];
lineSize = lineFragmentRect.size;
lineTooLong = lineSize.width > NSWidth(rectangle);
lineCharacterRange = [showStringLayoutManager characterRangeForGlyphRange:drawGlyphRange actualGlyphRange:NULL];
requiresEllipsis = lineTooLong || NSMaxRange(lineCharacterRange) < [self length];
if (requiresEllipsis) {
unsigned int ellipsisAttributeCharacterIndex;
if (lineCharacterRange.length != 0)
ellipsisAttributeCharacterIndex = NSMaxRange(lineCharacterRange) - 1;
else
ellipsisAttributeCharacterIndex = 0;
ellipsisAttributes = [self attributesAtIndex:ellipsisAttributeCharacterIndex longestEffectiveRange:NULL inRange:NSMakeRange(0, 1)];
ellipsisString = [NSString horizontalEllipsisString];
ellipsisSize = [ellipsisString sizeWithAttributes:ellipsisAttributes];
if (lineTooLong || lineSize.width + ellipsisSize.width > NSWidth(rectangle)) {
drawGlyphRange.length = [showStringLayoutManager glyphIndexForPoint:NSMakePoint(NSWidth(rectangle) - ellipsisSize.width, 0.5 * lineSize.height) inTextContainer:showStringTextContainer];
if (drawGlyphRange.length == 0) {
// We couldn't fit any characters with the ellipsis, so try drawing some without it (rather than drawing nothing)
requiresEllipsis = NO;
drawGlyphRange.length = [showStringLayoutManager glyphIndexForPoint:NSMakePoint(NSWidth(rectangle), 0.5 * lineSize.height) inTextContainer:showStringTextContainer];
}
lineSize.width = [showStringLayoutManager locationForGlyphAtIndex:NSMaxRange(drawGlyphRange)].x;
}
if (requiresEllipsis) // NOTE: Could have been turned off if the ellipsis didn't fit
lineSize.width += ellipsisSize.width;
} else {
// Make the compiler happy, since it doesn't know we're not going to take the requiresEllipsis branch later
ellipsisString = nil;
ellipsisSize = NSMakeSize(0, 0);
ellipsisAttributes = nil;
}
if (drawGlyphRange.length) {
NSPoint drawPoint;
// determine drawPoint based on alignment
drawPoint.y = NSMinY(rectangle);
switch (alignment) {
default:
case NSLeftTextAlignment:
drawPoint.x = NSMinX(rectangle);
break;
case NSCenterTextAlignment:
drawPoint.x = NSMidX(rectangle) - lineSize.width / 2.0;
break;
case NSRightTextAlignment:
drawPoint.x = NSMaxX(rectangle) - lineSize.width;
break;
}
if (verticallyCenter)
drawPoint.y = NSMidY(rectangle) - lineSize.height / 2.0;
[showStringLayoutManager drawGlyphsForGlyphRange:drawGlyphRange atPoint:drawPoint];
if (requiresEllipsis) {
drawPoint.x += lineSize.width - ellipsisSize.width;
[ellipsisString drawAtPoint:drawPoint withAttributes:ellipsisAttributes];
}
return NSMakeRect(drawPoint.x,drawPoint.y,lineSize.width,lineSize.height);
}
return NSZeroRect;
}
@end
@implementation NSString (MCAdditions)
+ (NSString *)horizontalEllipsisString;
{
static NSString *string = nil;
if (!string)
string = [[NSString stringWithFormat:@"%C",0x2026] retain];
return string;
}
@end
@implementation NSCalendarDate (MCAdditions)
+ (NSString *)shortTimeString;
{
NSString *timeStr = [[NSCalendarDate calendarDate] descriptionWithCalendarFormat:@"%I:%M %p"];
if ([timeStr hasPrefix:@"0"])
timeStr = [timeStr substringFromIndex:1];
return timeStr;
}
@end
@implementation NSView(WVDPrivate)
- (NSClipView *)firstDescendentClipView {
id view = nil;
NSEnumerator *viewEnum = nil;
viewEnum = [[self subviews] objectEnumerator];
while ((view = [viewEnum nextObject])) {
if ([view isKindOfClass:[NSClipView class]]) {
return view;
}
}
viewEnum = [[self subviews] objectEnumerator];
while ((view = [viewEnum nextObject])) {
id subview = [view firstDescendentClipView];
if (subview != nil) {
return subview;
}
}
return nil;
}
@end