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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
/*
* PQSamplerController.m - Font Manager
*
* Controller for font sampler.
*
* Copyright 2007 Isaiah Beerbower.
*
* Author: Isaiah Beerbower
* Created: 05/24/07
* License: Modified BSD license (see file COPYING)
*/
#import "PQSampleController.h"
#import "PQCompat.h"
@interface PQSampleController (FontManagerPrivate)
- (void) PQAddSampleTextToHistory: (NSString *)someText;
@end
@implementation PQSampleController
- (id) init
{
[super init];
fonts = [[NSArray alloc] init];
defaultSampleText = [[NSArray alloc] init];
sampleTextHistory = [[NSMutableArray alloc] init];
sizes = [NSArray arrayWithObjects: [NSNumber numberWithInt:9],
[NSNumber numberWithInt:10], [NSNumber numberWithInt:11],
[NSNumber numberWithInt:12], [NSNumber numberWithInt:13],
[NSNumber numberWithInt:14], [NSNumber numberWithInt:18],
[NSNumber numberWithInt:24], [NSNumber numberWithInt:36],
[NSNumber numberWithInt:48], [NSNumber numberWithInt:64],
[NSNumber numberWithInt:72], [NSNumber numberWithInt:96],
[NSNumber numberWithInt:144], [NSNumber numberWithInt:288], nil];
fontsNeedUpdate = YES;
isCustom = NO;
RETAIN(fonts);
RETAIN(defaultSampleText);
RETAIN(sampleTextHistory);
RETAIN(sizes);
return self;
}
- (void) awakeFromNib
{
if ([sampleView isKindOfClass: [PQFontSampleView class]])
{
RELEASE(defaultSampleText);
defaultSampleText =
[NSArray arrayWithObjects: NSLocalizedString(@"PQPangram", nil), nil];
RETAIN(defaultSampleText);
}
else if ([sampleView isKindOfClass: [NSTextView class]])
{
RELEASE(defaultSampleText);
defaultSampleText =
[NSArray arrayWithObjects:NSLocalizedString(@"PQParagraph", nil),
NSLocalizedString(@"PQAlphabet", nil), nil];
RETAIN(defaultSampleText);
[sampleTextHistory addObject: @""];
}
if ([sampleView isKindOfClass: [PQFontSampleView class]])
{
[sampleView setAutoSize: YES];
[sampleView setFontSize: 24];
[sampleView setSampleText: NSLocalizedString(@"PQPangram", nil)];
/* Couldn't set "Uses data source" in gorm */
#ifdef GNUSTEP
[sampleField setUsesDataSource: YES];
[sampleField setDataSource: self];
#endif
}
else if ([sampleView isKindOfClass: [NSTextView class]])
{
[sampleView setString: NSLocalizedString(@"PQParagraph", nil)];
}
/* Couldn't set "Uses data source" in gorm */
#ifdef GNUSTEP
[sizeField setUsesDataSource: YES];
[sizeField setDataSource: self];
#endif
[self updateControls];
}
- (void) dealloc
{
RELEASE(fonts);
RELEASE(defaultSampleText);
RELEASE(sampleTextHistory);
RELEASE(sizes);
[super dealloc];
}
- (void) setFonts: (NSArray *)someFonts
{
ASSIGN(fonts, someFonts);
if ([sampleView isKindOfClass: [PQFontSampleView class]])
{
fontsNeedUpdate = YES;
[sampleView setNeedsDisplay: YES];
}
else if ([sampleView isKindOfClass: [NSTextView class]])
{
if ([fonts count] > 0)
{
[sampleView setFont: [NSFont fontWithName: [fonts objectAtIndex: 0]
size: [sizeSlider intValue]]];
[self updateControls];
}
}
}
- (NSArray *) fonts
{
return fonts;
}
- (void) setSampleTextHistory: (NSArray *)aHistory
{
ASSIGN(sampleTextHistory, [aHistory mutableCopy]);
}
- (NSArray *) sampleTextHistory
{
return sampleTextHistory;
}
/* Combo box data source */
- (id) comboBox: (NSComboBox *)aComboBox objectValueForItemAtIndex: (int)index
{
if (aComboBox == sizeField)
{
return [sizes objectAtIndex:index];
}
else if (aComboBox == sampleField)
{
return [[defaultSampleText
arrayByAddingObjectsFromArray: sampleTextHistory] objectAtIndex: index];
}
/* Else: something is wrong */
return nil;
}
- (int) numberOfItemsInComboBox: (NSComboBox *)aComboBox
{
if (aComboBox == sizeField)
{
return [sizes count];
}
else if (aComboBox == sampleField)
{
return [[defaultSampleText
arrayByAddingObjectsFromArray: sampleTextHistory] count];
}
/* Else: something is wrong */
return 0;
}
/* Font sample view data source */
- (int) numberOfFontsInFontSampleView: (PQFontSampleView *)aFontSampleView
{
return [fonts count];
}
- (NSString *) fontSampleView: (PQFontSampleView *)aFontSampleView
fontAtIndex: (int)rowIndex
{
return [fonts objectAtIndex: rowIndex];
}
- (BOOL) fontsShouldChangeInFontSampleView: (PQFontSampleView *)aFontSampleView
{
if (fontsNeedUpdate == YES)
{
fontsNeedUpdate = NO;
return YES;
}
return NO;
}
/* Text view delegate */
- (BOOL) textView: (NSTextView *)aTextView
shouldChangeTextInRange: (NSRange)affectedCharRange
replacementString: (NSString *)replacementString
{
isCustom = YES;
[samplePopUpButton selectItemAtIndex: 2];
return YES;
}
/* Keep controls updated */
- (void) updateControls
{
if ([sampleView isKindOfClass: [PQFontSampleView class]])
{
[sampleField setStringValue: [sampleView sampleText]];
[sizeField setIntValue: [sampleView fontSize]];
[sizeSlider setIntValue: [sampleView fontSize]];
[sampleField reloadData];
}
else if ([sampleView isKindOfClass: [NSTextView class]])
{
if ([fonts count] > 0)
{
[sizeField setIntValue: [[sampleView font] pointSize]];
[sizeSlider setIntValue: [[sampleView font] pointSize]];
}
}
}
- (void) changeSize: (id)sender
{
if ([sampleView isKindOfClass: [PQFontSampleView class]])
{
[sampleView setFontSize: [sender intValue]];
}
else if ([sampleView isKindOfClass: [NSTextView class]])
{
if ([fonts count] > 0)
{
[sampleView setFont: [NSFont fontWithName: [fonts objectAtIndex: 0]
size: [sender intValue]]];
}
}
[self updateControls];
}
- (void) changeColor: (id)sender
{
if ([sampleView isKindOfClass: [PQFontSampleView class]])
{
[sampleView setForegroundColor: [foregroundColorWell color]];
[sampleView setBackgroundColor: [backgroundColorWell color]];
}
else if ([sampleView isKindOfClass: [NSTextView class]])
{
[sampleView setTextColor: [foregroundColorWell color]];
[sampleView setBackgroundColor: [backgroundColorWell color]];
}
}
- (void) changeSampleText: (id)sender
{
NSString *newSampleText = nil;
if ([sender isKindOfClass: [NSComboBox class]])
{
newSampleText = [sampleField stringValue];
}
else if ([sender isKindOfClass: [NSPopUpButton class]])
{
int count = [defaultSampleText count];
if (isCustom == YES && [sampleView isKindOfClass: [NSTextView class]])
{
[sampleTextHistory replaceObjectAtIndex: 0
withObject: [NSString stringWithString: [sampleView string]]];
}
if ([sender indexOfSelectedItem] < count)
{
newSampleText =
[defaultSampleText objectAtIndex: [sender indexOfSelectedItem]];
isCustom = NO;
}
else
{
newSampleText =
[sampleTextHistory objectAtIndex: [sender indexOfSelectedItem] - count];
isCustom = YES;
}
}
if ([sampleView isKindOfClass: [PQFontSampleView class]])
{
[sampleView setSampleText: newSampleText];
[self PQAddSampleTextToHistory: newSampleText];
}
else if ([sampleView isKindOfClass: [NSTextView class]])
{
[sampleView setString: newSampleText];
}
[self updateControls];
}
@end
@implementation PQSampleController (FontManagerPrivate)
- (void) PQAddSampleTextToHistory: (NSString *)someText
{
if ([defaultSampleText containsObject: someText] == NO)
{
NSInteger index = [sampleTextHistory indexOfObject: someText];
if (index != NSNotFound)
{
[sampleTextHistory removeObjectAtIndex: index];
}
[sampleTextHistory insertObject: someText atIndex: 0];
}
if ([sampleTextHistory count] > 10)
{
NSRange trimRange = NSMakeRange(10, ([sampleTextHistory count] - 10));
[sampleTextHistory removeObjectsInRange:trimRange];
}
}
@end
|