Skip to content
This repository was archived by the owner on Nov 19, 2019. It is now read-only.

Commit c8d0ce5

Browse files
author
Michael Ehrmann
committed
[FIX] made signedness aware, more checks on NSNotFound where necessary. fixing crash.
1 parent dc28ec2 commit c8d0ce5

File tree

4 files changed

+133
-121
lines changed

4 files changed

+133
-121
lines changed

Source/PSMTabBarControl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@
4242
@class PSMTabBarController;
4343
@protocol PSMTabStyle;
4444

45-
typedef enum PSMTabBarOrientation : NSUInteger {
45+
typedef enum PSMTabBarOrientation : NSInteger {
4646
PSMTabBarHorizontalOrientation,
4747
PSMTabBarVerticalOrientation
4848
} PSMTabBarOrientation;
4949

50-
typedef enum PSMTabBarTearOffStyle : NSUInteger {
50+
typedef enum PSMTabBarTearOffStyle : NSInteger {
5151
PSMTabBarTearOffAlphaWindow,
5252
PSMTabBarTearOffMiniwindow
5353
} PSMTabBarTearOffStyle;
5454

55-
typedef enum PSMTabStateMask : NSUInteger {
55+
typedef enum PSMTabStateMask : NSInteger {
5656
PSMTab_SelectedMask = 1 << 1,
5757
PSMTab_LeftIsSelectedMask = 1 << 2,
5858
PSMTab_RightIsSelectedMask = 1 << 3,
@@ -222,7 +222,7 @@ typedef enum PSMTabStateMask : NSUInteger {
222222
#pragma mark Tab Information
223223

224224
- (NSMutableArray *)representedTabViewItems;
225-
- (NSInteger)numberOfVisibleTabs;
225+
- (NSUInteger)numberOfVisibleTabs;
226226
- (PSMTabBarCell *)lastVisibleTab;
227227

228228
#pragma mark Special Effects

Source/PSMTabBarControl.m

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,9 +1301,9 @@ - (void)update:(BOOL)animate {
13011301
[[NSRunLoop currentRunLoop] addTimer:_animationTimer forMode:NSEventTrackingRunLoopMode];
13021302
[self _animateCells:_animationTimer];
13031303
} else {
1304-
for(NSInteger i = 0; i < [_cells count]; i++) {
1304+
for(NSUInteger i = 0; i < [_cells count]; i++) {
13051305
currentCell = [_cells objectAtIndex:i];
1306-
[currentCell setFrame:[_controller cellFrameAtIndex:i]];
1306+
[currentCell setFrame:[_controller cellFrameAtIndex:(NSInteger)i]];
13071307
}
13081308

13091309
[_addTabButton setFrame:[self addTabButtonRect]];
@@ -1317,11 +1317,11 @@ - (void)_animateCells:(NSTimer *)timer {
13171317
NSAnimation *animation = [[timer userInfo] objectAtIndex:1];
13181318
NSArray *targetFrames = [[timer userInfo] objectAtIndex:0];
13191319
PSMTabBarCell *currentCell;
1320-
NSInteger cellCount = [_cells count];
1320+
NSUInteger cellCount = [_cells count];
13211321

13221322
if((cellCount > 0) && [animation isAnimating]) {
13231323
//compare our target position with the current position and move towards the target
1324-
for(NSInteger i = 0; i < [targetFrames count] && i < cellCount; i++) {
1324+
for(NSUInteger i = 0; i < [targetFrames count] && i < cellCount; i++) {
13251325
currentCell = [_cells objectAtIndex:i];
13261326
NSRect cellFrame = [currentCell frame], targetFrame = [[targetFrames objectAtIndex:i] rectValue];
13271327
CGFloat sizeChange;
@@ -1358,7 +1358,7 @@ - (void)_animateCells:(NSTimer *)timer {
13581358
} else {
13591359
//put all the cells where they should be in their final position
13601360
if(cellCount > 0) {
1361-
for(NSInteger i = 0; i < [targetFrames count] && i < cellCount; i++) {
1361+
for(NSUInteger i = 0; i < [targetFrames count] && i < cellCount; i++) {
13621362
PSMTabBarCell *currentCell = [_cells objectAtIndex:i];
13631363
NSRect cellFrame = [currentCell frame], targetFrame = [[targetFrames objectAtIndex:i] rectValue];
13641364

@@ -1441,7 +1441,7 @@ - (void)updateTrackingAreas {
14411441
// recreate tracking areas and tool tip rects
14421442
NSPoint mouseLocation = [self convertPoint:[[self window] convertScreenToBase:[NSEvent mouseLocation]] fromView:nil];
14431443

1444-
NSUInteger cellIndex = 0;
1444+
NSInteger cellIndex = 0;
14451445
for (PSMTabBarCell *aCell in _cells) {
14461446

14471447
if ([aCell isInOverflowMenu])
@@ -1978,7 +1978,7 @@ - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)tabV
19781978
NSInteger tabIndex = [aTabView indexOfTabViewItem:tabViewItem];
19791979

19801980
if([_cells count] > 0 && tabIndex < [_cells count]) {
1981-
PSMTabBarCell *thisCell = [_cells objectAtIndex:tabIndex];
1981+
PSMTabBarCell *thisCell = [_cells objectAtIndex:(NSUInteger)tabIndex];
19821982
if(_alwaysShowActiveTab && [thisCell isInOverflowMenu]) {
19831983
//temporarily disable the delegate in order to move the tab to a different index
19841984
id tempDelegate = [aTabView delegate];
@@ -1989,7 +1989,7 @@ - (void)tabView:(NSTabView *)aTabView didSelectTabViewItem:(NSTabViewItem *)tabV
19891989
[thisCell retain];
19901990
[aTabView removeTabViewItem:tabViewItem];
19911991
[aTabView insertTabViewItem:tabViewItem atIndex:0];
1992-
[self removeCellAtIndex:tabIndex];
1992+
[self removeCellAtIndex:(NSUInteger)tabIndex];
19931993
[self insertCell:thisCell atIndex:0];
19941994
[thisCell setIsInOverflowMenu:NO]; //very important else we get a fun recursive loop going
19951995
[[_cells objectAtIndex:[_cells count] - 1] setIsInOverflowMenu:YES]; //these 2 lines are pretty uncool and this logic needs to be updated
@@ -2245,7 +2245,7 @@ - (id)cellForPoint:(NSPoint)point cellFrame:(NSRectPointer)outFrame {
22452245
return nil;
22462246
}
22472247

2248-
NSInteger i, cnt = [_cells count];
2248+
NSUInteger i, cnt = [_cells count];
22492249
for(i = 0; i < cnt; i++) {
22502250
PSMTabBarCell *cell = [_cells objectAtIndex:i];
22512251

@@ -2260,7 +2260,7 @@ - (id)cellForPoint:(NSPoint)point cellFrame:(NSRectPointer)outFrame {
22602260
}
22612261

22622262
- (PSMTabBarCell *)lastVisibleTab {
2263-
NSInteger i, cellCount = [_cells count];
2263+
NSUInteger i, cellCount = [_cells count];
22642264
for(i = 0; i < cellCount; i++) {
22652265
if([[_cells objectAtIndex:i] isInOverflowMenu]) {
22662266
if (i == 0)
@@ -2275,8 +2275,8 @@ - (PSMTabBarCell *)lastVisibleTab {
22752275
return nil;
22762276
}
22772277

2278-
- (NSInteger)numberOfVisibleTabs {
2279-
NSInteger i, cellCount = 0;
2278+
- (NSUInteger)numberOfVisibleTabs {
2279+
NSUInteger i, cellCount = 0;
22802280
PSMTabBarCell *nextCell;
22812281

22822282
for(i = 0; i < [_cells count]; i++) {

Source/PSMTabBarController.m

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ - (NSRect)cellFrameAtIndex:(NSInteger)index {
6666
NSRect rect;
6767

6868
if(index > -1 && index < [_cellFrames count]) {
69-
rect = [[_cellFrames objectAtIndex:index] rectValue];
69+
rect = [[_cellFrames objectAtIndex:(NSUInteger)index] rectValue];
7070
} else {
7171
NSLog(@"cellFrameAtIndex: Invalid index (%ld)", (long)index);
7272
rect = NSZeroRect;
@@ -106,16 +106,17 @@ - (void)setSelectedCell:(PSMTabBarCell *)cell {
106106
[cell setTabState:PSMTab_SelectedMask];
107107

108108
if(![cell isInOverflowMenu]) {
109-
NSInteger cellIndex = [cells indexOfObject:cell];
110-
111-
if(cellIndex > 0) {
112-
nextCell = [cells objectAtIndex:cellIndex - 1];
113-
[nextCell setTabState:[nextCell tabState] | PSMTab_RightIsSelectedMask];
114-
}
109+
NSUInteger cellIndex = [cells indexOfObject:cell];
110+
if (cellIndex != NSNotFound) {
111+
if(cellIndex > 0) {
112+
nextCell = [cells objectAtIndex:cellIndex - 1];
113+
[nextCell setTabState:[nextCell tabState] | PSMTab_RightIsSelectedMask];
114+
}
115115

116-
if(cellIndex < [cells count] - 1) {
117-
nextCell = [cells objectAtIndex:cellIndex + 1];
118-
[nextCell setTabState:[nextCell tabState] | PSMTab_LeftIsSelectedMask];
116+
if(cellIndex < [cells count] - 1) {
117+
nextCell = [cells objectAtIndex:cellIndex + 1];
118+
[nextCell setTabState:[nextCell tabState] | PSMTab_LeftIsSelectedMask];
119+
}
119120
}
120121
}
121122
}
@@ -129,7 +130,7 @@ - (void)setSelectedCell:(PSMTabBarCell *)cell {
129130

130131
- (void)layoutCells {
131132
NSArray *cells = [_control cells];
132-
NSInteger cellCount = [cells count];
133+
NSUInteger cellCount = [cells count];
133134

134135
// make sure all of our tabs are accounted for before updating
135136
if([[_control tabView] numberOfTabViewItems] != cellCount) {
@@ -152,20 +153,21 @@ - (void)layoutCells {
152153
*/
153154
- (NSInteger)_shrinkWidths:(NSMutableArray *)newWidths towardMinimum:(NSInteger)minimum withAvailableWidth:(CGFloat)availableWidth {
154155
BOOL changed = NO;
155-
NSInteger count = [newWidths count];
156+
NSUInteger count = [newWidths count];
156157
NSInteger totalWidths = [[newWidths valueForKeyPath:@"@sum.intValue"] integerValue];
157158
NSInteger originalTotalWidths = totalWidths;
158159

159160
do {
160161
changed = NO;
161162

162-
for(NSInteger q = (count - 1); q >= 0; q--) {
163-
CGFloat cellWidth = [[newWidths objectAtIndex:q] doubleValue];
163+
for(NSUInteger q = count; q > 0; q--) {
164+
NSUInteger index = q - 1;
165+
CGFloat cellWidth = [[newWidths objectAtIndex:index] doubleValue];
164166
if(cellWidth - 1 >= minimum) {
165167
cellWidth--;
166168
totalWidths--;
167169

168-
[newWidths replaceObjectAtIndex:q
170+
[newWidths replaceObjectAtIndex:index
169171
withObject:[NSNumber numberWithDouble:cellWidth]];
170172

171173
changed = YES;
@@ -188,9 +190,9 @@ - (NSInteger)_shrinkWidths:(NSMutableArray *)newWidths towardMinimum:(NSInteger)
188190
*/
189191
static NSInteger potentialMinimumForArray(NSArray *array, NSInteger minimum){
190192
NSInteger runningTotal = 0;
191-
NSInteger count = [array count];
193+
NSUInteger count = [array count];
192194

193-
for(NSInteger i = 0; i < count; i++) {
195+
for(NSUInteger i = 0; i < count; i++) {
194196
NSInteger currentValue = [[array objectAtIndex:i] integerValue];
195197
runningTotal += MIN(currentValue, minimum);
196198
}
@@ -208,7 +210,7 @@ static NSInteger potentialMinimumForArray(NSArray *array, NSInteger minimum){
208210
*/
209211

210212
- (NSArray *)_generateWidthsFromCells:(NSArray *)cells {
211-
NSInteger cellCount = [cells count], i, numberOfVisibleCells = ([_control orientation] == PSMTabBarHorizontalOrientation) ? 1 : 0;
213+
NSUInteger cellCount = [cells count], i, numberOfVisibleCells = ([_control orientation] == PSMTabBarHorizontalOrientation) ? 1 : 0;
212214
NSMutableArray *newWidths = [NSMutableArray arrayWithCapacity:cellCount];
213215
id <PSMTabStyle> style = [_control style];
214216
CGFloat availableWidth = [_control availableCellWidth], currentOrigin = 0, totalOccupiedWidth = 0.0, width;
@@ -248,12 +250,12 @@ - (NSArray *)_generateWidthsFromCells:(NSArray *)cells {
248250

249251
//If we're not going to use the overflow menu, cram all the tab cells into the bar regardless of minimum width
250252
if(![_control useOverflowMenu]) {
251-
NSInteger j, averageWidth = (availableWidth / cellCount);
253+
NSInteger averageWidth = (availableWidth / cellCount);
252254

253255
numberOfVisibleCells = cellCount;
254256
[newWidths removeAllObjects];
255257

256-
for(j = 0; j < cellCount; j++) {
258+
for(NSUInteger j = 0; j < cellCount; j++) {
257259
CGFloat desiredWidth = [[cells objectAtIndex:j] desiredWidthOfCell];
258260
[newWidths addObject:[NSNumber numberWithDouble:(desiredWidth < averageWidth && [_control sizeCellsToFit]) ? desiredWidth : averageWidth]];
259261
}
@@ -323,11 +325,10 @@ - (NSArray *)_generateWidthsFromCells:(NSArray *)cells {
323325
*/
324326
NSInteger leftoverWidth = availableWidth - totalOccupiedWidth;
325327
if(leftoverWidth > 0) {
326-
NSInteger q;
327-
for(q = numberOfVisibleCells - 1; q >= 0; q--) {
328-
NSInteger desiredAddition = (NSInteger)leftoverWidth / (q + 1);
329-
NSInteger newCellWidth = (NSInteger)[[newWidths objectAtIndex:q] doubleValue] + desiredAddition;
330-
[newWidths replaceObjectAtIndex:q withObject:[NSNumber numberWithDouble:newCellWidth]];
328+
for(NSUInteger q = numberOfVisibleCells; q > 0; q--) {
329+
NSInteger desiredAddition = leftoverWidth / (NSInteger)q;
330+
NSInteger newCellWidth = (NSInteger)[[newWidths objectAtIndex:q-1] doubleValue] + desiredAddition;
331+
[newWidths replaceObjectAtIndex:q-1 withObject:[NSNumber numberWithDouble:newCellWidth]];
331332
leftoverWidth -= desiredAddition;
332333
totalOccupiedWidth += desiredAddition;
333334
}
@@ -342,11 +343,10 @@ - (NSArray *)_generateWidthsFromCells:(NSArray *)cells {
342343

343344
// stretch - distribute leftover room among cells, since we can't add this cell
344345
NSInteger leftoverWidth = availableWidth - totalOccupiedWidth;
345-
NSInteger q;
346-
for(q = i - 1; q >= 0; q--) {
347-
NSInteger desiredAddition = (NSInteger)leftoverWidth / (q + 1);
348-
NSInteger newCellWidth = (NSInteger)[[newWidths objectAtIndex:q] doubleValue] + desiredAddition;
349-
[newWidths replaceObjectAtIndex:q withObject:[NSNumber numberWithDouble:newCellWidth]];
346+
for(NSUInteger q = i; q > 0; q--) {
347+
NSInteger desiredAddition = leftoverWidth / (NSInteger)q;
348+
NSInteger newCellWidth = (NSInteger)[[newWidths objectAtIndex:q-1] doubleValue] + desiredAddition;
349+
[newWidths replaceObjectAtIndex:q-1 withObject:[NSNumber numberWithDouble:newCellWidth]];
350350
leftoverWidth -= desiredAddition;
351351
}
352352

@@ -468,15 +468,16 @@ - (NSArray *)_generateWidthsFromCells:(NSArray *)cells {
468468
*/
469469

470470
- (void)_setupCells:(NSArray *)cells withWidths:(NSArray *)widths {
471-
NSInteger i, tabState, cellCount = [cells count];
471+
NSInteger tabState;
472+
NSUInteger cellCount = [cells count];
472473
NSRect cellRect = [_control genericCellRect];
473474
PSMTabBarCell *cell;
474475
NSTabViewItem *selectedTabViewItem = [[_control tabView] selectedTabViewItem];
475476
NSMenuItem *menuItem;
476477

477478
[_overflowMenu release], _overflowMenu = nil;
478479

479-
for(i = 0; i < cellCount; i++) {
480+
for(NSUInteger i = 0; i < cellCount; i++) {
480481
cell = [cells objectAtIndex:i];
481482

482483
if(i < [widths count]) {

0 commit comments

Comments
 (0)