1// VariationFormatter.mm
2// this file is part of Context Free
3// ---------------------
4// Copyright (C) 2005 Mark Lentczner - markl@glyphic.com
5//
6// This program is free software; you can redistribute it and/or
7// modify it under the terms of the GNU General Public License
8// as published by the Free Software Foundation; either version 2
9// of the License, or (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19//
20// Mark Lentczner can be contacted at markl@glyphic.com or at
21// Mark Lentczner, 1209 Villa St., Mountain View, CA 94041-1123, USA
22//
23//
24
25#import "VariationFormatter.h"
26#include "variation.h"
27
28@implementation VariationFormatter
29
30+ (int)minVariation     { return Variation::recommendedMin(); }
31+ (int)maxVariation     { return Variation::recommendedMax(6); }
32+ (int)randomVariation  { return Variation::random(3); }
33
34+ (NSString *)stringForVariation:(int)v lowerCase:(BOOL)lowerCase
35{
36    std::string code = Variation::toString(v, lowerCase);
37    return [NSString stringWithUTF8String: code.c_str()];
38}
39
40- (NSString *)stringForObjectValue:(id)obj
41{
42    int v = 0;
43    if ([obj respondsToSelector: @selector(intValue)]) {
44        v = [obj intValue];
45    }
46    return [VariationFormatter stringForVariation: v lowerCase: NO];
47}
48
49- (BOOL)getObjectValue:(id *)obj forString:(NSString *)string
50    errorDescription:(NSString **)error
51{
52    NSUInteger l = [string length];
53    if (l > 6) {
54        if (error)
55            *error = NSLocalizedString(@"No more than six letters", @"");
56        return NO;
57    }
58
59    for (unsigned int i = 0; i < l; ++i) {
60        unichar c = [string characterAtIndex: i];
61
62        if (('A' <= c && c <= 'Z') ||  ('a' <= c && c <= 'z'))
63            continue;
64
65        if (error)
66            *error = NSLocalizedString(@"Only accepts letters", @"");
67        return NO;
68    }
69
70    int v = Variation::fromString([string UTF8String]);
71    if (v == -1) {
72        if (error)
73            *error = NSLocalizedString(@"Error parsing variation", @"");
74        return NO;
75    }
76
77    if (obj)
78        *obj = [NSNumber numberWithInt: v];
79    return YES;
80}
81
82
83- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
84    proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
85    originalString:(NSString *)origString
86    originalSelectedRange:(NSRange)origSelRange
87    errorDescription:(NSString **)error
88{
89    return [self getObjectValue: nil forString: *partialStringPtr
90                errorDescription: error];
91}
92
93@end
94