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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
/*
* PQCharactersView.m - Font Manager
*
* Copyright 2008 Isaiah Beerbower.
*
* Author: Isaiah Beerbower
* Created: 02/29/08
* License: 3-Clause BSD license (see file COPYING)
*/
#import "PQCharactersView.h"
#import "PQCompat.h"
#import <math.h>
#define rectForIndex(x) NSMakeRect((((x < columns) ? \
x : (x % columns)) * width), \
(floorf((float)x / (float)columns)) * height, \
width, height)
@implementation PQCharactersView
- (id) initWithFrame: (NSRect)frame
{
[super initWithFrame: frame];
font = [NSFont userFontOfSize: 24.0];
length = 0;
columns = 0;
rows = 0;
selectedIndex = 0;
width = 0.0;
height = 0.0;
RETAIN(font);
return self;
}
- (void) dealloc
{
RELEASE(font);
[super dealloc];
}
/* Data source */
- (void) setDataSource: (id)anObject
{
if ([anObject
respondsToSelector:@selector(numberOfCharactersInCharactersView:)] == NO)
{
[NSException raise: NSInternalInconsistencyException
format: @"Data source does not respond to "
@"numberOfCharactersInCharactersView:"];
}
else if ([anObject
respondsToSelector:@selector(charactersView:characterAtIndex:)] == NO)
{
[NSException raise: NSInternalInconsistencyException
format: @"Data source does not respond to "
@"charactersView:characterAtIndex:"];
}
dataSource = anObject;
[self setNeedsDisplay: YES];
}
- (id) dataSource
{
return dataSource;
}
- (void) setDelegate: (id)anObject
{
delegate = anObject;
}
- (id) delegate
{
return delegate;
}
- (void) setFont: (NSFont *)newFont
{
ASSIGN(font, newFont);
[self setNeedsDisplay: YES];
}
- (NSFont *) font
{
return font;
}
- (void) setSelectedIndex: (unsigned int)newSelectedIndex
{
if (selectedIndex != newSelectedIndex &&
newSelectedIndex < [dataSource numberOfCharactersInCharactersView: self])
{
if (length > 0)
{
[self setNeedsDisplayInRect: rectForIndex(selectedIndex)];
[self setNeedsDisplayInRect: rectForIndex(newSelectedIndex)];
}
else
{
[self setNeedsDisplay: YES];
}
selectedIndex = newSelectedIndex;
}
if ([delegate
respondsToSelector:@selector(selectionDidChangeInCharactersView:)] == YES)
{
[delegate selectionDidChangeInCharactersView: self];
}
}
- (unsigned int) selectedIndex
{
return selectedIndex;
}
/* Mouse Events */
- (void) mouseDown: (NSEvent *)theEvent
{
NSPoint point =
[self convertPoint: [theEvent locationInWindow] fromView: nil];
unsigned int newSelectedIndex =
((unsigned int)floorf(point.y / height) * columns) +
(unsigned int)floorf(point.x / width);
[self setSelectedIndex: newSelectedIndex];
}
/* Drawing */
- (BOOL) isFlipped
{
return YES;
}
- (void) drawRect: (NSRect)rect
{
length = [dataSource numberOfCharactersInCharactersView: self];
width = [font defaultLineHeightForFont];
/*[font maximumAdvancement].width*/
columns = (int)floorf([self frame].size.width / width);
width = [self frame].size.width / columns;
height = [font defaultLineHeightForFont];
rows = (int)ceilf((float)length / (float)columns);
if ((rows * height) != [self frame].size.height)
{
[self setFrameSize: NSMakeSize([self frame].size.width, rows * height)];
}
int i = 1;
[[NSColor whiteColor] set];
[NSBezierPath fillRect: rect];
[[NSColor selectedTextBackgroundColor] set];
[NSBezierPath fillRect: rectForIndex(selectedIndex)];
/*NSBezierPath *path = [[NSBezierPath alloc] init];
[[NSColor gridColor] set];
while (i < columns)
{
[path moveToPoint: NSMakePoint(i * width, 0.0)];
[path lineToPoint: NSMakePoint(i * width, [self frame].size.height)];
++i;
}
i = 1;
while (i < rows)
{
[path moveToPoint: NSMakePoint(0.0, i * height)];
[path lineToPoint: NSMakePoint([self frame].size.width, i * height)];
++i;
}
[path stroke];*/
#ifndef GNUSTEP
NSCharacterSet *fontCharacterSet = [font coveredCharacterSet];
#endif
/* Text system components */
NSTextStorage *textStorage;
NSLayoutManager *layoutManager;
/* Set up text system */
textStorage = [[NSTextStorage alloc] init];
layoutManager = [[NSLayoutManager alloc] init];
[textStorage addLayoutManager: layoutManager];
RELEASE(layoutManager); /* Retained by textStorage */
NSDictionary *attributes = [NSDictionary dictionaryWithObject: font
forKey: NSFontAttributeName];
i = 0;
while (i < length)
{
NSRect charRect =
NSInsetRect(rectForIndex(i), width - (width * 2), height - (height * 2));
if (NSContainsRect(rect, charRect) || NSIntersectsRect(charRect, rect) ||
NSContainsRect(charRect, rect))
{
unichar character = [dataSource charactersView: self characterAtIndex: i];
NSAttributedString *string =
[[NSAttributedString alloc]
initWithString: [NSString stringWithCharacters: &character length: 1]
attributes: attributes];
[textStorage setAttributedString: string];
float glyphWidth =
[font advancementForGlyph: [layoutManager glyphAtIndex: 0]].width;
#ifndef GNUSTEP
if ((!(glyphWidth > 0.0)) ||
(![fontCharacterSet characterIsMember: character]))
#else /* GNUstep's NSFont -coveredCharacterSet or NSCharacterSet is broken */
if (!(glyphWidth > 0.0))
#endif
{
[textStorage setAttributedString:
[[NSAttributedString alloc] initWithString: @"?"
attributes: attributes]];
[textStorage addAttribute: NSForegroundColorAttributeName
value: [NSColor lightGrayColor]
range: NSMakeRange(0, [textStorage length])];
glyphWidth =
[font advancementForGlyph: [layoutManager glyphAtIndex: 0]].width;
}
NSPoint point =
NSMakePoint((((i < columns) ? i : (i % columns)) * width) +
((width - glyphWidth) / 2.0),
((floorf((float)i / (float)columns)) * height) /*+
(([font capHeight] - height) / 2.0)*/);
[textStorage drawAtPoint: point];
}
++i;
}
}
@end
|