PageRenderTime 38ms CodeModel.GetById 23ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/XeeSegmentedItem.m

https://code.google.com/p/xee/
Objective C | 171 lines | 127 code | 43 blank | 1 comment | 10 complexity | 66e285e1fcc44073932f903b977ae00c MD5 | raw file
  1#import "XeeSegmentedItem.h"
  2#import "XeeController.h"
  3
  4
  5
  6@implementation XeeSegmentedItem
  7
  8+(XeeSegmentedItem *)itemWithIdentifier:(NSString *)identifier label:(NSString *)label paletteLabel:(NSString *)pallabel segments:(int)segments
  9{
 10	return [[[XeeSegmentedItem alloc] initWithItemIdentifier:identifier label:label paletteLabel:pallabel segments:segments] autorelease];
 11}
 12
 13-(id)initWithItemIdentifier:(NSString *)identifier label:(NSString *)label paletteLabel:(NSString *)pallabel segments:(int)segments
 14{
 15	if(self=[super initWithItemIdentifier:identifier])
 16	{
 17		[self setLabel:label];
 18		[self setPaletteLabel:pallabel];
 19
 20		control=[[NSSegmentedControl alloc] init];
 21		[control setSegmentCount:segments];
 22		[[control cell] setTrackingMode:NSSegmentSwitchTrackingMomentary];
 23
 24		if(segments!=1)
 25		{
 26			menu=[[NSMenu alloc] init];
 27
 28			for(int i=0;i<segments;i++)
 29			[menu addItem:[[[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""] autorelease]];
 30
 31			NSMenuItem *item=[[[NSMenuItem alloc] initWithTitle:pallabel action:NULL keyEquivalent:@""] autorelease];
 32			[item setSubmenu:menu];
 33
 34			[self setMenuFormRepresentation:item];
 35		}
 36		else
 37		{
 38			NSMenuItem *item=[[[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""] autorelease];
 39			[self setMenuFormRepresentation:item];
 40			menu=nil;
 41		}
 42
 43		[control setTarget:self];
 44		[control setAction:@selector(clicked:)];
 45
 46		actions=malloc(sizeof(SEL)*segments);
 47	}
 48	return self;
 49}
 50
 51-(void)dealloc
 52{
 53	[control release];
 54	[menu release];
 55
 56	free(actions);
 57
 58	[super dealloc];
 59}
 60
 61-(void)validate
 62{
 63	if([[NSApplication sharedApplication] mainWindow]!=[control window]) [self setEnabled:NO];
 64	else
 65	{
 66		[self setEnabled:YES];
 67
 68		int count=[control segmentCount];
 69		for(int i=0;i<count;i++)
 70		{
 71			XeeController *controller=(XeeController *)[[control window] delegate];
 72			[control setEnabled:[controller validateAction:actions[i]] forSegment:i];
 73		}
 74	}
 75}
 76
 77-(void)setSegment:(int)segment label:(NSString *)label image:(NSImage *)image longLabel:(NSString *)longlabel width:(int)width action:(SEL)action
 78{
 79	if(segment<0 || segment>=[control segmentCount]) return;
 80
 81	[control setLabel:label forSegment:segment];
 82	[control setImage:image forSegment:segment];
 83	[control setWidth:width forSegment:segment];
 84	[[control cell] setToolTip:longlabel forSegment:segment];
 85
 86	actions[segment]=action;
 87
 88	NSMenuItem *item;
 89	if(menu) item=[menu itemAtIndex:segment];
 90	else item=[self menuFormRepresentation];
 91
 92	[item setTitle:longlabel];
 93	[item setImage:image];
 94	[item setAction:action];
 95}
 96
 97-(void)setSegment:(int)segment label:(NSString *)label longLabel:(NSString *)longlabel action:(SEL)action
 98{
 99	[self setSegment:segment label:label image:nil longLabel:longlabel width:0 action:action];
100}
101
102-(void)setSegment:(int)segment imageName:(NSString *)imagename longLabel:(NSString *)longlabel action:(SEL)action
103{
104	NSImage *image=[NSImage imageNamed:imagename];
105	[self setSegment:segment label:nil image:image longLabel:longlabel width:[image size].width action:action];
106}
107
108-(void)setupView
109{
110	[control sizeToFit];
111	[self setView:control];
112	[self setMinSize:[control frame].size];
113	[self setMaxSize:[control frame].size];
114}
115
116-(void)clicked:(id)sender
117{
118	[[NSApplication sharedApplication] sendAction:actions[[sender selectedSegment]] to:nil from:self];
119}
120
121@end
122
123
124
125@implementation XeeToolItem
126
127+(XeeSegmentedItem *)itemWithIdentifier:(NSString *)identifier label:(NSString *)label
128paletteLabel:(NSString *)pallabel imageName:(NSString *)imagename longLabel:(NSString *)longlabel
129action:(SEL)action activeSelector:(SEL)activeselector target:(id)activetarget
130{
131	return [[[XeeToolItem alloc] initWithItemIdentifier:identifier label:label
132	paletteLabel:pallabel imageName:imagename longLabel:longlabel action:action
133	activeSelector:activeselector target:activetarget] autorelease];
134}
135
136-(id)initWithItemIdentifier:(NSString *)identifier label:(NSString *)label
137paletteLabel:(NSString *)pallabel imageName:(NSString *)imagename longLabel:(NSString *)longlabel
138action:(SEL)action activeSelector:(SEL)activeselector target:(id)activetarget
139{
140	if(self=[super initWithItemIdentifier:identifier label:label paletteLabel:pallabel segments:1])
141	{
142		sel=activeselector;
143		target=activetarget;
144
145		[[control cell] setTrackingMode:NSSegmentSwitchTrackingSelectAny];
146
147		[self setSegment:0 imageName:imagename longLabel:longlabel action:action];
148		[self setupView];
149	}
150	return self;
151}
152
153-(void)validate
154{
155	[super validate];
156
157	BOOL active=target&&(BOOL)(int)[target performSelector:sel];
158
159	[control setSelected:active forSegment:0];
160//	[[self menuFormRepresentation] setState:active?NSOnState:NSOffState]; // doesn't work?
161}
162
163@end
164
165
166
167@implementation NSSegmentedCell (AlwaysTextured)
168
169-(BOOL)_isTextured  { return YES; }
170
171@end