/*
Pencil V2.0, Copyright 1994, 95 by Florian Marquardt.
This program may be distributed under the terms of the GNU general
public license (Start Pencil and select "Copyright" in the Help panel for a copy of the
license).
Pencil has a built-in TCL interpreter (tcl 7.3 by John Ousterhout). This is under a separate
license (see "Copyright").
*/
#import "Group.h"

extern BOOL globalOutlines;

extern unsigned long long int layersVisible1;
extern unsigned long long int layersVisible2;
extern unsigned long long int layersSelectable1;
extern unsigned long long int layersSelectable2;

extern void initM(float *m);

@implementation Group
- (void)draw:(NXRect *)re
{
	int i, c=[slist count]; 
	
	if(layer<=63 ? (layersVisible1&(1ULL<<layer)): (layersVisible2&(1ULL<<(layer-64)))) { 	unsigned long long int lv1;
	unsigned long long int lv2;
	lv1=layersVisible1;
	lv2=layersVisible2; 
	layersVisible1=18446744073709551615ULL;
	layersVisible2=18446744073709551615ULL; // make everything in group visible...
	switch(type)
	{
	case PENCIL_STANDARD_GROUP:
	for(i=0;i<c;i++) [[slist objectAt:i] draw:re]; break;
	case PENCIL_CLIP_GROUP:
	PSgsave();
	[[slist objectAt:0] drawPath];
	if(globalOutlines)
		DPSPrintf(DPSGetCurrentContext(), " clip 0 setgray 0 setlinewidth stroke ");
	else
		DPSPrintf(DPSGetCurrentContext(), " clip newpath ");
	for(i=1;i<c;i++) [[slist objectAt:i] draw:re];
	PSgrestore();
	break;
	}
	layersVisible1=lv1;
	layersVisible2=lv2; 
	} 
}
- setGroupType:(char)typ { initM(m); type=typ; if(type==PENCIL_CLIP_GROUP && [slist count]) [[slist objectAt:0] setDrawingMethod:"dF"]; return self; }
- select:(BOOL)yesno { selected=yesno;  return self; }
- free
{
	int i;
	if(slist)
	{
	i=[slist count];
	while(i--) [[slist objectAt:i] free];
	[slist free];
	}
	return [super freeOrig];
}
- (void)drawControl:(NXRect *)re:(int)cp:(float)bsize
{
	[self draw:re];
	[self calculateBoundingBox:nil];
	PSsetgray(.3); PSsetlinewidth(0);
	PSrectstroke(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
 }
- freeLeafObjects
{
	if(slist)
		[slist free];
	return [super freeOrig];
}
- write:(NXTypedStream *)stream
{
	[super write:stream];
	NXWriteType(stream,"c",&type);
	return self;
}

- read:(NXTypedStream *)stream
{
	[super read:stream];
	NXReadType(stream,"c",&type);
	return self;
}

- (BOOL)selected:(NXEvent *)te:(int *)cp:(id)view
{
	BOOL res;
	
	if(layer<=63 ? (layersSelectable1&(1ULL<<layer)): (layersSelectable2&(1ULL<<(layer-64)))) {
	unsigned long long int lv1, lv2;
	lv1=layersSelectable1;
	lv2=layersSelectable2; 
	layersSelectable1=18446744073709551615ULL;
	layersSelectable2=18446744073709551615ULL; // make everything in group selectable...
	
	if(type==PENCIL_CLIP_GROUP)
	{
		res= [[slist objectAt:0] selected:te:cp:view];
	}
	else
		res= [super selected:te:cp:view];
		
	layersSelectable1=lv1;
	layersSelectable2=lv2;
	return res;
	}
	else
		return NO; 
}

- (int)type
{
	return 2;
}

- (void)giveDescription:(Tcl_Interp *)interp
{
	int i;
	static char str[10];
	
	[super giveDescription:interp];
	sprintf(str, "%d", [slist count]);
	Tcl_AppendElement( interp, str);
	for(i=0;i<[slist count];i++) [[slist objectAt:i] giveDescription: interp];
}

@end
