File: InsetLimit.m

package info (click to toggle)
paje.app 1.98-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,428 kB
  • sloc: objc: 24,517; ansic: 6,998; makefile: 134; sh: 42; java: 31
file content (430 lines) | stat: -rw-r--r-- 12,214 bytes parent folder | download | duplicates (4)
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
/*
    Copyright (c) 1998, 1999, 2000, 2001, 2003, 2004 Benhur Stein
    
    This file is part of Paj.

    Paj is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License as published by the
    Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    Paj 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 Lesser General Public License
    for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Paj; if not, write to the Free Software Foundation, Inc.,
    59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
/* InsetLimit.m created by benhur on 25-feb-2001 */

#include "InsetLimit.h"
#include "../General/FoundationAdditions.h"
#include "../General/UniqueString.h"
#include "../General/Macros.h"


@interface ImbricationFilteringEnumerator : NSEnumerator
{
    NSEnumerator *origEnum;
    PajeFilter *inputComponent;
    int minValue;
    int maxValue;
}
+ (ImbricationFilteringEnumerator *)enumeratorWithEnumerator:(NSEnumerator *)e
                                              inputComponent:(PajeFilter *)comp
                                                    minValue:(int)min
                                                    maxValue:(int)max;
- (id)initWithEnumerator:(NSEnumerator *)e
          inputComponent:(PajeFilter *)comp
                minValue:(int)min
                maxValue:(int)max;
- (id)nextObject;
@end
@implementation ImbricationFilteringEnumerator
+ (ImbricationFilteringEnumerator *)enumeratorWithEnumerator:(NSEnumerator *)e
                                              inputComponent:(PajeFilter *)comp
                                                    minValue:(int)min
                                                    maxValue:(int)max
{
    return [[[self alloc] initWithEnumerator:e
                              inputComponent:comp
                                    minValue:min
                                    maxValue:max] autorelease];
}
- (id)initWithEnumerator:(NSEnumerator *)e
          inputComponent:(PajeFilter *)comp
                minValue:(int)min
                maxValue:(int)max
{
    self = [super init];
    if (self) {
        origEnum = [e retain];
        inputComponent = comp;
        minValue = min;
        maxValue = max;
    }
    return self;
}

- (void)dealloc
{
    [origEnum release];
    [super dealloc];
}

- (id)nextObject
{
    id obj;
    while ((obj = [origEnum nextObject]) != nil) {
        int val = [inputComponent imbricationLevelForEntity:obj];
        if (val >= minValue && val <= maxValue)
            break;
    }
    return obj;
}
@end


@implementation InsetLimit

- (id)initWithController:(PajeTraceController *)c
{
    self = [super initWithController:c];

    if (self != nil) {
        NSWindow *win;

        [self readDefaults];

        if (![NSBundle loadNibNamed:@"InsetLimit" owner:self]) {
            NSRunAlertPanel(@"InsetLimit", @"Couldn't load interface file",
                            nil, nil, nil);
            return self;
        }

        [entityTypePopUp removeAllItems];

        win = [view window];
        
        // view is an NSBox. we need its contents.
        view = [[(NSBox *)view contentView] retain];

        [self registerFilter:self];

#ifndef GNUSTEP
        [win release];
#endif
    }

    return self;
}

- (void)dealloc
{
    [entityTypePopUp removeAllItems];
    [limits release];
    [view release];
    [super dealloc];
}

- (NSView *)filterView
{
    return view;
}

- (NSString *)filterName
{
    return @"Imbrication Limit";
}

- (id)filterDelegate
{
    return self;
}

//
// Notifications sent by other filters
//
- (void)hierarchyChanged
{
    [self calcPopUp];
    [super hierarchyChanged];
}

- (void)calcPopUp
{
    NSEnumerator *typeEnum;
    PajeEntityType *selectedEntityType;
    PajeEntityType *entityType;

    selectedEntityType = [self selectedEntityType];
    [entityTypePopUp removeAllItems];
    typeEnum = [[self allEntityTypes] objectEnumerator];
    while ((entityType = [typeEnum nextObject]) != nil) {
        // only states have indentationLevel
        if ([self drawingTypeForEntityType:entityType]
            == PajeStateDrawingType) {
            [entityTypePopUp addItemWithTitle:[self descriptionForEntityType:entityType]];
            [[entityTypePopUp lastItem] setRepresentedObject:entityType];
        }
    }
    if ([entityTypePopUp numberOfItems] > 0) {
        if (selectedEntityType != nil) {
            [entityTypePopUp selectItemWithTitle:[self descriptionForEntityType:selectedEntityType]];
        }
        if ([entityTypePopUp selectedItem] == nil) {
            [entityTypePopUp selectItemAtIndex:0];
        }
    }
    [self synchronizeValues];
}

- (void)synchronizeValues
{
    PajeEntityType *selectedEntityType;

    selectedEntityType = [self selectedEntityType];
    if (selectedEntityType == nil) {
        [minField setIntValue:0];
        [maxField setIntValue:0];
        [minField setEnabled:NO];
        [maxField setEnabled:NO];
        [minStepper setEnabled:NO];
        [maxStepper setEnabled:NO];
    } else {
        NSString *value;
        NSRange range;
        value = [limits objectForKey:[self descriptionForEntityType:selectedEntityType]];
        if (value) {
            range = [value rangeValue];
        } else {
            range = NSMakeRange(0,0);
        }
        [minField setIntValue:range.location];
        [maxField setIntValue:range.length];
        [minStepper setIntValue:range.location];
        [maxStepper setIntValue:range.length];
        [minField setEnabled:YES];
        [maxField setEnabled:YES];
        [minStepper setEnabled:YES];
        [maxStepper setEnabled:YES];
	[minStepper setValueWraps:NO];
	[maxStepper setValueWraps:NO];
    }
}

- (PajeEntityType *)selectedEntityType
{
    return [[entityTypePopUp selectedItem] representedObject];
}



//
//
//

- (void)readDefaults
{
    NSString *defaultName = [NSStringFromClass([self class]) stringByAppendingString:@" Limits"];
    NSDictionary *limitsDict = [[NSUserDefaults standardUserDefaults] dictionaryForKey:defaultName];
    [limits release];
    if (limitsDict != nil)
        limits = [limitsDict mutableCopy];
    else
        limits = [[NSMutableDictionary alloc] init];
}

- (void)registerDefaults
{
    NSString *defaultName = [NSStringFromClass([self class]) stringByAppendingString:@" Limits"];
    [[NSUserDefaults standardUserDefaults] setObject:limits forKey:defaultName];
}


#define SETCACHEABSENT(et) \
    do { \
        _cachedEntityType = et; \
        _cachedPresence = NO; \
        _cachedRange = NSMakeRange(0,0); \
    } while(0)
#define SETCACHERANGE(et, ran) \
    do { \
        _cachedEntityType = et; \
        _cachedPresence = YES; \
        _cachedRange = ran; \
    } while(0)
#define FILLCACHE(et) \
    do { \
        if (et != _cachedEntityType) { \
            NSString * val; \
            val = [limits objectForKey:[self descriptionForEntityType:et]]; \
            if (val == nil) { \
                SETCACHEABSENT(et); \
            } else { \
                SETCACHERANGE(et, [val rangeValue]); \
            } \
        } \
    } while(0)

- (BOOL)isFilteredEntityType:(PajeEntityType *)entityType
{
    FILLCACHE(entityType);
    return _cachedPresence;    
}

- (NSRange)rangeForEntityType:(PajeEntityType *)entityType
{
    FILLCACHE(entityType);
    return _cachedRange;    
}

- (void)setRange:(NSRange)ran forEntityType:(PajeEntityType *)entityType
{
    if ((ran.location == 0) && (ran.length == 0)) {
        [limits removeObjectForKey:[self descriptionForEntityType:entityType]];
        SETCACHEABSENT(entityType);
    } else {
        [limits setObject:[NSString stringWithRange:ran]
                   forKey:[self descriptionForEntityType:entityType]];
        SETCACHERANGE(entityType, ran);
    }
    [self registerDefaults];
    [self dataChangedForEntityType:entityType];
}


//
// Handle interface messages
//
- (IBAction)entityTypeSelected:(id)sender
{
    [self synchronizeValues];
}

- (void)_valueChanged
{
    [self setRange:NSMakeRange([minField intValue], [maxField intValue])
     forEntityType:[self selectedEntityType]];
}

- (IBAction)minValueChanged:(id)sender
{
    [minField setIntValue:[sender intValue]];
    [minStepper setIntValue:[sender intValue]];
    [self _valueChanged];
}

- (IBAction)maxValueChanged:(id)sender
{
    [maxField setIntValue:[sender intValue]];
    [maxStepper setIntValue:[sender intValue]];
    [self _valueChanged];
}


//
// interact with interface
//

- (void)viewWillBeSelected:(NSView *)selectedView
// message sent by PajeController when a view will be selected for display
{
    [self synchronizeValues];        
}


//
// Trace messages that are filtered
//

- (NSEnumerator *)enumeratorOfEntitiesTyped:(PajeEntityType *)entityType
                                inContainer:(PajeContainer *)container
                                   fromTime:(NSDate *)start
                                     toTime:(NSDate *)end
                                minDuration:(double)minDuration
{
    int min, max;
    NSRange ran;
    NSEnumerator *origEnum;
    origEnum = [inputComponent enumeratorOfEntitiesTyped:entityType
                                             inContainer:container
                                                fromTime:start
                                                  toTime:end
                                             minDuration:minDuration];
    if (![self isFilteredEntityType:entityType]) {
        return origEnum;
    }
    ran = [self rangeForEntityType:entityType];
    min = ran.location;
    max = ran.length;
    return [ImbricationFilteringEnumerator
                                enumeratorWithEnumerator:origEnum
                                          inputComponent:inputComponent
                                                minValue:min
                                                maxValue:max];
}

- (NSEnumerator *)enumeratorOfCompleteEntitiesTyped:(PajeEntityType *)entityType
                                        inContainer:(PajeContainer *)container
                                           fromTime:(NSDate *)start
                                             toTime:(NSDate *)end
                                        minDuration:(double)minDuration
{
    int min, max;
    NSRange ran;
    NSEnumerator *origEnum;
    origEnum = [inputComponent enumeratorOfCompleteEntitiesTyped:entityType
                                                     inContainer:container
                                                        fromTime:start
                                                          toTime:end
                                                     minDuration:minDuration];
    if (![self isFilteredEntityType:entityType]) {
        return origEnum;
    }
    ran = [self rangeForEntityType:entityType];
    min = ran.location;
    max = ran.length;
    return [ImbricationFilteringEnumerator
                                enumeratorWithEnumerator:origEnum
                                          inputComponent:inputComponent
                                                minValue:min
                                                maxValue:max];
}


- (int)imbricationLevelForEntity:(PajeEntity *)entity;
{
    int origValue;
    NSRange ran;
    PajeEntityType *entityType;

    entityType = [self entityTypeForEntity:entity];
    origValue = [inputComponent imbricationLevelForEntity:entity];
    if (![self isFilteredEntityType:entityType]) {
        return origValue;
    }
    
    ran = [self rangeForEntityType:entityType];
    if (origValue > ran.length) origValue = ran.length;
    origValue -= ran.location;

    return origValue;
}

- (id)configuration
{
    return limits;
}

- (void)setConfiguration:(id)config
{
    if (![config isKindOfClass:[NSDictionary class]]) {
        return;
    }
    
    Assign(limits, [config unifyStrings]); 
}
@end