~ubuntu-branches/ubuntu/raring/vice/raring

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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
 * resourcetreeitem.m - item of the resource tree
 *
 * Written by
 *  Christian Vogelgsang <chris@vogelgsang.org>
 *
 * This file is part of VICE, the Versatile Commodore Emulator.
 * See README for copyright notice.
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307  USA.
 *
 */

#import "resourcetreeitem.h"
#import "resourceeditorcontroller.h"

@implementation ResourceTreeItem

-(id)initWithTitle:(NSString *)t
{
    self = [super init];
    if(self == nil)
        return nil;
    
    title = [t retain];
    resource = nil;
    children = nil;
    cacheValue = nil;
    dataCell = nil;
    extraCell = nil;
    
    return self;
}

-(void)dealloc
{
    [title release];
    [resource release];
    [children release];
    [cacheValue release];
    [dataCell release];
    [extraCell release];
    [args release];
    [super dealloc];
}

-(BOOL)isLeaf
{
    return children == nil;
}

-(ResourceTreeItem *)childAtIndex:(int)index
{
    return (ResourceTreeItem *)[children objectAtIndex:index];
}

-(int)numChildren
{
    if(children==nil)
        return 0;
    else
        return [children count];
}

-(BOOL)addFromDictionary:(NSDictionary *)dict
{
    NSMutableArray *a = [[NSMutableArray alloc] init];
    NSEnumerator *enumerator = [dict keyEnumerator];
    id key;
    while((key = [enumerator nextObject])) {
        ResourceTreeItem *item = [[ResourceTreeItem alloc] initWithTitle:key];
        [a addObject:item];
        
        id value = [dict objectForKey:key];
        
        // its a dictionary itself
        if([value isKindOfClass:[NSDictionary class]]) {
            if(![item addFromDictionary:value])
                return FALSE;
        }
        // its a string
        else if([value isKindOfClass:[NSString class]]) {
            if(![item parseResourceString:(NSString *)value])
                return FALSE;
        }
        // unknown
        else {
            NSLog(@"Invalid Class in Dictionary: %@",[value class]);
            return FALSE;
        }
    }
    
    // sort array and keep
    children = [a sortedArrayUsingSelector:@selector(compare:)];
    [children retain];
    [a release];
    return TRUE;
}

-(BOOL)parseResourceString:(NSString *)string
{
    args = [string componentsSeparatedByString:@","];
    [args retain];
    numArgs = [args count];
    if(numArgs < 2) {
        NSLog(@"ERROR: resource string invalid: %@",string);
        return FALSE;
    }
    
    resource = (NSString *)[args objectAtIndex:0];
    [resource retain];

    NSString *typeStr = (NSString *)[args objectAtIndex:1];
    unichar ch = [typeStr characterAtIndex:0];
    switch(ch) {
    case 'i': // integer
        type = ResourceTreeItemTypeInteger;
        hint = ResourceTreeItemHintNone;
        break;
    case 'b': // boolean
        type = ResourceTreeItemTypeInteger;
        hint = ResourceTreeItemHintBoolean;
        break;
    case 'e': // enum
        type = ResourceTreeItemTypeInteger;
        hint = ResourceTreeItemHintEnum;
        break;
    case 'E': // enum direct
        type = ResourceTreeItemTypeInteger;
        hint = ResourceTreeItemHintEnumDirect;
        break;
    case 'r': // range
        type = ResourceTreeItemTypeInteger;
        hint = ResourceTreeItemHintRange;
        break;
    case 's': // string
        type = ResourceTreeItemTypeString;
        hint = ResourceTreeItemHintNone;
        break;
    case 'm': // map string to integer
        type = ResourceTreeItemTypeInteger;
        hint = ResourceTreeItemHintMapInteger;
        break;
    case 'f': // file
        {
            unichar mode = [typeStr characterAtIndex:1];
            type = ResourceTreeItemTypeString;
            switch(mode) {
            case 'o':
                hint = ResourceTreeItemHintFileOpen;
                break;
            case 's':
                hint = ResourceTreeItemHintFileSave;
                break;
            case 'd':
                hint = ResourceTreeItemHintFileDir;
                break;
            default:
                NSLog(@"ERROR: Invalid file resource mode: %@",typeStr);
                return false;
            }
        }
        break;
    default:
        NSLog(@"ERROR: Invalid resource type: %@",typeStr);
        return FALSE;
    }

    return TRUE;
}

-(NSString *)title
{
    return title;
}

-(int)parseIntFromString:(NSString *)string
{
    int len = [string length];
    if(len==0)
        return 0;

    int value = 0;
    const char *str = [string cStringUsingEncoding:NSUTF8StringEncoding];
    if(*str=='$') {
        sscanf(str+1,"%x",&value);
    } else {
        sscanf(str,"%d",&value);
    }
    return value;
}

-(id)getValue:(id)ctl
{
    ResourceEditorController *controller = (ResourceEditorController *)ctl;
    if([self isLeaf]) {
        if(cacheValue == nil) {
            if(type == ResourceTreeItemTypeInteger) {
                int value = [controller getIntResource:resource];
//                NSLog(@"read integer: %@ %d",resource,value);
                
                // enum
                if(hint == ResourceTreeItemHintEnum) {
                    cacheValue = [[args objectAtIndex:value+2] retain];
                } 
                // direct enum
                else if(hint == ResourceTreeItemHintEnumDirect) {
                    int i;
                    for(i=2;i<numArgs;i++) {
                        NSString *argString = (NSString *)[args objectAtIndex:i];
                        int argValue = [self parseIntFromString:argString];
                        if(argValue == value) {
                            cacheValue = [argString retain];
                            break;
                        }
                    }
                }
                // map string to ineger:   txt=value
                else if(hint == ResourceTreeItemHintMapInteger) {
                    int i;
                    for(i=2;i<numArgs;i++) {
                        NSString *argString = (NSString *)[args objectAtIndex:i];
                        NSArray *pair = [argString componentsSeparatedByString:@"="];
                        if([pair count]>=2) {
                            NSString *keyStr = (NSString *)[pair objectAtIndex:0];
                            NSString *valueStr = (NSString *)[pair objectAtIndex:1]; 
                            if(keyStr && valueStr) {
                                if([self parseIntFromString:valueStr]==value) {
                                    cacheValue = [keyStr retain];
                                    break;
                                }
                            }
                        }
                    }
                }
                // integer
                else {
                    cacheValue = [[NSNumber alloc] initWithInt:value];
                }
            }
            else {
                NSString *value = [controller getStringResource:resource];
//                NSLog(@"read string: %@ %@",resource,value);
                cacheValue = [value retain];
            }
        }
        return cacheValue; 
    }
    else
        return @"";
}

-(void)setIntResourceToValue:(int)value withController:(id)ctl
{
    ResourceEditorController *controller = (ResourceEditorController *)ctl;
    [controller setIntResource:resource toValue:value];
    [self invalidateCache];
    [controller reloadItem:self];
//    NSLog(@"set int resource %@ %d",resource,value);
}

-(void)setStringResourceToValue:(NSString *)value withController:(id)ctl
{
    ResourceEditorController *controller = (ResourceEditorController *)ctl;
    [controller setStringResource:resource toValue:value];
    [self invalidateCache];
    [controller reloadItem:self];
//    NSLog(@"set string resource %@ %d",resource,value);    
}

-(void)setValue:(id)ctl toObject:(id)object
{
    NSString *string = (NSString *)object;
    if(type == ResourceTreeItemTypeInteger) {
        int value = 0;
        // enum resource
        if(hint==ResourceTreeItemHintEnum ||
           hint==ResourceTreeItemHintEnumDirect) {
            int i;
            for(i=2;i<numArgs;i++) {
                NSString *argVal = (NSString *)[args objectAtIndex:i];
                if([argVal isEqualToString:string]) {
                    if(hint==ResourceTreeItemHintEnum)
                        value = i-2;
                    else 
                        value = [self parseIntFromString:argVal];
                    break;
                }
            }
        }
        // map
        else if(hint==ResourceTreeItemHintMapInteger) {
            int i;
             for(i=2;i<numArgs;i++) {
                 NSString *argString = (NSString *)[args objectAtIndex:i];
                 NSArray *pair = [argString componentsSeparatedByString:@"="];
                 if([pair count]>=2) {
                     NSString *keyStr = (NSString *)[pair objectAtIndex:0];
                     NSString *valueStr = (NSString *)[pair objectAtIndex:1]; 
                     if(keyStr && valueStr) {
                         if([keyStr isEqualToString:string]) {
                             value = [self parseIntFromString:valueStr];
                             break;
                         }
                     }
                 }
             }
        } 
        // range
        else if(hint==ResourceTreeItemHintRange) {
            if(numArgs==4) {
                int min = [[args objectAtIndex:2] intValue];
                int max = [[args objectAtIndex:3] intValue];
                if(value<min)
                    value = min;
                else if(value>max)
                    value = max;
            } else {
                NSLog(@"ERROR: range invalid: %@",args);
            }
        }
        // integer resource
        else {
            value = [string intValue];
        }
        [self setIntResourceToValue:value withController:ctl];
    } else {
        [self setStringResourceToValue:string withController:ctl];
    }
}

-(void)setValueExtra:(id)ctl
{
    ResourceEditorController *controller = (ResourceEditorController *)ctl;
    switch(hint) {
    case ResourceTreeItemHintFileOpen:
        {
            NSString *file = [controller pickOpenFileWithTitle:title types:nil];
            if(file!=nil)
                [self setStringResourceToValue:file withController:ctl];
            break;
        }
    case ResourceTreeItemHintFileSave:
        {
            NSString *file = [controller pickSaveFileWithTitle:title types:nil];
            if(file!=nil)
                [self setStringResourceToValue:file withController:ctl];
            break;
        }
    case ResourceTreeItemHintFileDir:
        {
            NSString *file = [controller pickDirectoryWithTitle:title];
            if(file!=nil)
                [self setStringResourceToValue:file withController:ctl];
            break;
        }
    }
}

-(NSCell *)dataCell:(NSCell *)colCell
{
    if([self isLeaf]) {
        if(dataCell!=nil)
            return dataCell;
            
        switch(hint) {
        // boolean cell
        case ResourceTreeItemHintBoolean:
            {
                NSButtonCell *bcell = [[NSButtonCell alloc] initTextCell:@""];
                [bcell setButtonType:NSSwitchButton];
                dataCell = bcell;
                break;
            }
        case ResourceTreeItemHintEnum:
        case ResourceTreeItemHintEnumDirect:
            {
                NSComboBoxCell *ccell = [[NSComboBoxCell alloc] initTextCell:@""];
                int i;
                [ccell setButtonBordered:FALSE];
                for(i=2;i<numArgs;i++) {
                    [ccell addItemWithObjectValue:[args objectAtIndex:i]];
                }
                dataCell = ccell;
                break;
            }
        case ResourceTreeItemHintMapInteger:
            {
                NSComboBoxCell *ccell = [[NSComboBoxCell alloc] initTextCell:@""];
                int i;
                [ccell setButtonBordered:FALSE];
                for(i=2;i<numArgs;i++) {
                    NSString *argString = (NSString *)[args objectAtIndex:i];
                    NSArray *pair = [argString componentsSeparatedByString:@"="];
                    if([pair count]>=2) {
                         NSString *keyStr = (NSString *)[pair objectAtIndex:0];
                         NSString *valueStr = (NSString *)[pair objectAtIndex:1]; 
                         [ccell addItemWithObjectValue:keyStr];
                    }
                }
                dataCell = ccell;
                break;
            }
        default:
            return colCell;
        }
        // reuse default font
        [dataCell setFont:[colCell font]];
        return dataCell;
    }
    return colCell;
}

-(NSCell *)extraCell:(NSCell *)colCell
{
    if([self isLeaf]) {
        if(extraCell!=nil) {
            return extraCell;
        }
        
        switch(hint) {
        // file name cell
        case ResourceTreeItemHintFileOpen:
        case ResourceTreeItemHintFileSave:
        case ResourceTreeItemHintFileDir:
            {
                NSButtonCell *bcell = [[NSButtonCell alloc] initTextCell:@""];
                [bcell setButtonType:NSMomentaryLightButton];
                [bcell setTitle:@"..."];
                extraCell = bcell;                
                break;
            }
        default:
            return colCell;
        }
        [extraCell setFont:[colCell font]];
        return extraCell;
    }
    return colCell;
}

-(NSComparisonResult)compare:(ResourceTreeItem *)item
{
    return [title caseInsensitiveCompare:[item title]];
}

-(void)invalidateCache
{
    if([self isLeaf]) {
        [cacheValue release];
        cacheValue = nil;
    } else {
        int num = [children count];
        int i;
        for(i=0;i<num;i++) {
            [(ResourceTreeItem *)[children objectAtIndex:i] invalidateCache];
        }
    }
}

@end