[Drawkit] Problems with removing the active layer

Brad Larson larson at sonoplot.com
Fri Oct 24 14:52:38 PDT 2008


In testing an inspector for handling layers within our document, I was  
getting some out of range exceptions when users were deleting layers.   
It turned out that the method call [drawing indexOfLayer:[drawing  
activeLayer]] was returning a bad value.  I tracked it down to the  
DKDrawing removeLayer:andActivateLayer: method.  It looks like the  
[super removeLayer:aLayer] call is taking place too early, and needs  
to happen after the active layer is set to a new value.  Restructuring  
the method to look like the following

{
	NSAssert( aLayer != nil, @"can't remove a nil layer from the drawing  
");
	NSAssert( aLayer != anotherLayer, @"cannot activate the layer being  
removed - layers must be different");
	
	// retain the layer until we are completely done with it
	
	[aLayer retain];
	
	BOOL removingActive = ( aLayer == [self activeLayer]);
	
	// remove it from the drawing
	
	if( removingActive && ( anotherLayer == nil ))
	{
		// for convenience activate the topmost layer of the same class as  
the one being removed. If that
		// returns nil, activate the top layer.
		
		DKLayer* newActive = [self firstLayerOfClass:[aLayer class]];
	
		if( newActive == nil )
			newActive = [self topLayer];
			
		anotherLayer = newActive;
	}
	
	[self setActiveLayer:anotherLayer withUndo:YES];
	[super removeLayer:aLayer];

	[aLayer release];
}

seemed to take care of the exceptions I was seeing.  This may cause  
other problems that I'm not aware of, but it cleared up this one.

______________________
Brad Larson
SonoPlot, Inc.
3030 Laura Lane, Suite 120
Middleton, WI 53562





More information about the Drawkit mailing list