File: MyImageListEnumerator.m

package info (click to toggle)
lynkeos.app 3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,740 kB
  • sloc: objc: 40,528; ansic: 811; cpp: 150; sh: 68; makefile: 27
file content (232 lines) | stat: -rw-r--r-- 6,444 bytes parent folder | download
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
//
//  Lynkeos
//  $Id$
//
//  Created by Jean-Etienne LAMIAUD on Sun Nov 30 2003.
//  Copyright (c) 2003-2014. Jean-Etienne LAMIAUD
//
// 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.
//

#include "MyImageListEnumerator.h"

@implementation MyImageListEnumerator

- (id) init
{
   self = [super init];
   if ( self != nil )
   {
      _lock = [[NSRecursiveLock alloc] init];

      _itemList = nil;
      _listSize = 0;
      _step = 0;
      _skipUnselected = NO;
      _firstItem = nil;
      _itemIndex = NSNotFound;
      _currentContainer = nil;
      _containerSize = 0;
      _itemIndexInContainer = NSNotFound;
   }

   return( self );
}

- (id) initWithImageList :(NSArray*)list startAt:(MyImageListItem*)item
              directSense:(BOOL)direct skipUnselected:(BOOL)skip
{
   if ( (self = [self init]) == nil )
      return( self );

   _itemList = [list retain];
   _listSize = [list count];
   _step = (direct ? 1 : -1);
   _skipUnselected = skip;
   _firstItem = item;

   if ( item != nil )
   {
      // Check that the item is in the list hierarchy
      MyImageListItem *parent, *topItem;
      for( parent = item, topItem = item;
           parent != nil ;
           parent = [topItem getParent] )
         topItem = parent;
      NSAssert( topItem != nil && [list containsObject:topItem],
                @"Initial item of enumerator is not in the list" );

      parent = [item getParent];
      if ( parent != nil )
      {
         NSAssert( [list indexOfObject:parent] != NSNotFound,
                   @"container is not present in the list" );
         NSAssert( [parent indexOfItem:item] != NSNotFound,
                   @"item is not present in the container" );
      }
   }

   [self reset];

   return( self );
}

- (id) initWithImageList :(NSArray*)list
{
   return( [self initWithImageList:list startAt:nil
                       directSense:YES skipUnselected:NO] );
}

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

- (void) reset
{
   [_lock lock];

   if ( _firstItem != nil )
   {
      _currentContainer = [_firstItem getParent];
      if ( _currentContainer != nil )
      {
         _containerSize = [_currentContainer numberOfChildren];
         _itemIndex = [_itemList indexOfObject:_currentContainer];
         _itemIndexInContainer = [_currentContainer indexOfItem:_firstItem];

         if ( (_step > 0 && _itemIndexInContainer >= _containerSize-1) ||
             (_step < 0 && _itemIndexInContainer <= 0) )
         {
            _itemIndexInContainer = NSNotFound;
            if ( (_step > 0 && _itemIndex < _listSize-1) ||
                (_step < 0 && _itemIndex > 0) )
               _itemIndex += _step;
            else
               _itemIndex = NSNotFound;
         }
         else
            _itemIndexInContainer += _step;
      }
      else if ( (_containerSize = [_firstItem numberOfChildren]) != 0 )
      {
         _currentContainer = _firstItem;
         _itemIndex = [_itemList indexOfObject:_currentContainer];
         _itemIndexInContainer = (_step > 0 ? 0 : _containerSize-1);
      }
      else
      {
         _currentContainer = nil;
         _containerSize = 0;
         _itemIndex = [_itemList indexOfObject:_firstItem];
         if ( (_step > 0 && _itemIndex < _listSize-1) ||
             (_step < 0 && _itemIndex > 0) )
            _itemIndex += _step;
         else
            _itemIndex = NSNotFound;
         _itemIndexInContainer = NSNotFound;
      }
   }
   else
   {
      _currentContainer = nil;
      _containerSize = 0;
      _itemIndex = (_step > 0 ? 0 : (_listSize == 0 ? NSNotFound : _listSize-1));
      _itemIndexInContainer = NSNotFound;
   }

   [_lock unlock];
}

- (NSArray *) allObjects
{
   NSMutableArray *array = [NSMutableArray array];
   id item;

   [_lock lock];

   while ( (item = [self nextObject]) != nil )
      [array addObject:item];

   [_lock unlock];

   return( (NSArray*)array );
}

- (id) nextObject
{
   id item = nil;

   [_lock lock];

   while ( item == nil &&  _itemIndex != NSNotFound )
   {
      // Look for the next image item (inside a movie or self contained)
      if ( _currentContainer == nil || _itemIndexInContainer == NSNotFound )
      {
         // At first level
         item = [_itemList objectAtIndex:_itemIndex];
         if ( (_containerSize = [item numberOfChildren]) != 0 )
         {
            // First level item is a container, go down
            _currentContainer = item;
            item = nil;
            _itemIndexInContainer = (_step > 0 ? 0 : _containerSize-1);
         }

         else if ( (_step > 0 && (_itemIndex+1) < _listSize) ||
                   (_step < 0 && _itemIndex > 0) )
            _itemIndex += _step;

         else
            _itemIndex = NSNotFound;
      }

      if ( _currentContainer != nil )
      {
         // Inside a container
         if ( _itemIndexInContainer != NSNotFound )
         {
            item = [_currentContainer getChildAtIndex:_itemIndexInContainer];
            if ( (_step > 0 && _itemIndexInContainer < _containerSize-1) ||
                 (_step < 0 && _itemIndexInContainer > 0))
               _itemIndexInContainer += _step;
            else
               _itemIndexInContainer = NSNotFound;
         }
         if ( _itemIndexInContainer == NSNotFound )
         {
            if ( (_step > 0 && _itemIndex < _listSize-1) ||
                 (_step < 0 && _itemIndex > 0) )
               _itemIndex += _step;
            else
               _itemIndex = NSNotFound;
         }
      }

      // Do not iterate over unselected items if told so
      if ( _skipUnselected && item != nil &&
          [item getSelectionState] != NSOnState )
         item = nil;
   }

   [_lock unlock];

   return( item );
}

@end