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