source: trunk/PrefsPane/src/NamedEnum.m @ 1046

Revision 1046, 4.1 KB checked in by speck, 3 days ago (diff)

printf %d formatting fixes.
1.5b23

Line 
1/* Copyright (C) 2009 Peter Speck
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15 */
16
17//
18//  NamedEnum.m
19//  GlimmerBlockerPrefsPane
20//
21//  Created by Peter Speck on 21/05/09.
22//  Copyright 2009 Vitality. All rights reserved.
23//
24
25#import "NamedEnum.h"
26#import "Xml.h"
27#import "Prefs.h"
28
29@implementation NamedEnum
30
31- (id)initWithSemicolonList:(NSString*)encoded
32{
33    if (!(self = [super init]))
34        return nil;
35    NSArray* a = [encoded componentsSeparatedByString:@";"];
36    NSInteger num = [a count];
37    _valueArray = [[NSMutableArray arrayWithCapacity:num] retain];
38    _defaultValueIndex = NSNotFound;
39    for (int i = 0; i < num; i++) {
40        NSString* s = [a objectAtIndex:i];
41        if ([s hasPrefix:@"+"]) {
42            _defaultValueIndex = i;
43            s = [s substringFromIndex:1];
44        }
45        [_valueArray addObject:s];
46    }
47    if (_defaultValueIndex == NSNotFound) {
48        NSLog(@"NamedEnum: Default value missing in semicolon list '%@'", encoded);
49        _defaultValueIndex = 0;
50    }
51    return self;
52}
53
54- (void)dealloc
55{
56    [_valueArray release];
57    [super dealloc];
58}
59
60- (NSString*)nameFromIndex:(NSInteger)idx
61{
62    if (idx >= 0 && idx <= (NSInteger)[_valueArray count])
63        return [_valueArray objectAtIndex:idx];
64    else
65        return [_valueArray objectAtIndex:_defaultValueIndex];
66}
67
68- (NSInteger)indexFromXPath:(NSString*)xpath
69                  withPrefs:(Prefs*)prefs
70{
71    return [self indexFromXPath:xpath withPrefs:prefs withDefault:NULL];
72}
73
74- (NSInteger)indexFromXPath:(NSString*)xpath
75                  withPrefs:(Prefs*)prefs
76                withDefault:(NSString*)defaultValue
77{
78    NSString* s = [prefs stringForXPath:xpath];
79    NSString* value = [s length] ? s : defaultValue;
80    NSUInteger idx = value ? [_valueArray indexOfObject:value] : NSNotFound;
81    //DebugNSLog(@"NamedEnum.update: %@ = '%@'/'%@' = %d  values %@", xpath, s, value, idx, _valueArray);
82    if (idx == NSNotFound)
83        idx = _defaultValueIndex;
84    return idx;
85}
86
87- (NSInteger)indexFromXPath:(NSString*)xpath
88                   withRoot:(NSXMLElement*)rootE
89                withDefault:(NSString*)defaultValue
90{
91    NSString* s = [Xml stringForXPath:xpath withRoot:rootE];
92    NSString* value = [s length] ? s : defaultValue;
93    NSUInteger idx = value ? [_valueArray indexOfObject:value] : NSNotFound;
94    //DebugNSLog(@"NamedEnum.update: %@ = '%@'/'%@' = %d  values %@", xpath, s, value, idx, _valueArray);
95    if (idx == NSNotFound)
96        idx = _defaultValueIndex;
97    return idx;
98}
99
100
101- (void)selectDefaultItemInPop:(NSPopUpButton*)pop
102{
103    [pop selectItemAtIndex:_defaultValueIndex];
104}
105
106- (void)updatePopButton:(NSPopUpButton*)pop
107                toValue:(NSString*)value
108{
109    NSUInteger idx = value ? [_valueArray indexOfObject:value] : NSNotFound;
110    //DebugNSLog(@"NamedEnum.update: '%@' = %d  values %@", value, idx, _valueArray);
111    if (idx == NSNotFound)
112        idx = _defaultValueIndex;
113    [pop selectItemAtIndex:idx];
114}
115
116- (void)updatePopButton:(NSPopUpButton*)pop
117              fromXPath:(NSString*)xpath
118               withRoot:(NSXMLElement*)rootE
119{
120    [self updatePopButton:pop fromXPath:xpath withRoot:rootE withDefault:NULL];
121}
122
123- (void)updatePopButton:(NSPopUpButton*)pop
124              fromXPath:(NSString*)xpath
125               withRoot:(NSXMLElement*)rootE
126            withDefault:(NSString*)defaultValue
127{
128    NSInteger idx = [self indexFromXPath:xpath withRoot:rootE withDefault:defaultValue];
129    [pop selectItemAtIndex:idx];
130}
131
132- (NSString*)selectedPopValue:(NSPopUpButton*)pop
133{
134    NSInteger idx = [pop indexOfSelectedItem];
135    if (idx < 0)
136        idx = _defaultValueIndex;
137    if (idx >= (int)[_valueArray count]) {
138        NSLog(@"NamedEnum: Selected-index = %ld => too few items in value array: %@", (long)idx, _valueArray);
139        return NULL;
140    }
141    NSString* s = [_valueArray objectAtIndex:idx];
142    return [s isEqual:@"ignored"] ? NULL : s;
143}
144
145@end
Note: See TracBrowser for help on using the repository browser.