-(NSInteger)cellAtPoint:(NSPoint)point{
NaviButtonCell* cell;
for(NSUInteger i=0;i<[buttonArray count];i++){
cell=[buttonArray objectAtIndex:i];
NSRect rect=[cell drawedRect];
if(NSPointInRect(point, rect)){
return i;
}
}
return -1;
}
-(void)mouseDown:(NSEvent *)theEvent{
NSPoint point = [self convertPointFromBase:[theEvent locationInWindow]];
NSInteger index = [self cellAtPoint:point];
if(index!=-1){
NaviButtonCell* cell=[buttonArray objectAtIndex:index];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir, valid;
valid = [fileManager fileExistsAtPath:[cell fullPath] isDirectory:&isDir];
if (valid && isDir) {
NSArray *array = [fileManager contentsOfDirectoryAtPath:[cell fullPath] error:NULL];
if(array){
NSMenu* menu=[[NSMenu alloc]initWithTitle:@"Directory"];
for (NSInteger i=0; i<[array count]; i++) {
NSString* testpath=[[cell fullPath] stringByAppendingPathComponent:[array objectAtIndex:i]];
valid = [fileManager fileExistsAtPath:testpath isDirectory:&isDir];
if(valid && isDir){
NSMenuItem* item=[[NSMenuItem alloc]initWithTitle:[array objectAtIndex:i] action:@selector(menuSelected:) keyEquivalent:@""];
[menu addItem:item];
[item release];
}
}
[NSMenu popUpContextMenu:menu withEvent:theEvent forView:self];
[menu release];
}
}
}
}
この程度で可能。ただ、popUpContextMenuだとmouseDownを拾ったロケーションでメニューがpopupしてしまうわけで、こういう事態も起こる。
うーん、Xcode4とはちょっと違う動作だなあ。もしかしてXcode4、popupButton使っているのかなあ。もう少し調査してみよう。


0 件のコメント:
コメントを投稿