2013-02-05 24 views
1

是能夠提供可拖動NSTabViewItem,
基本上我想要什麼,如果NSTabViewITem的標籤上我倒L按鈕單擊和移動,我應該允許拖動TabView的項目, 我想這樣做的移動NSTabView項目,並有一個功能,如果用戶拖動NSTabView項目的標籤,並將其移動到一個特定的區域,那麼我應該允許刪除該NSTabView項目,具有可拖動NSTabViewItem

我只能找到一種方式PSMTab酒吧,但我也有其他功能,如果我去那種方法,我會失蹤的NSTabView項目。

回答

1

感謝尋找到它, 不知怎的,我可能能夠做到這一點....發佈的代碼一些重要的一塊...

1 - 必須有自定義TabView的類處理鼠標事件。

//接口貼在下面,

#import <Cocoa/Cocoa.h> 


typedef enum __itemDragState{ 
    itemNotDragging = 0, 
    itemDragStatNormal = 0, 
    itemDragging = 1, 
    itemDropped  = 2 
} ItemDragStat; 

@protocol CustomTabViewDelegate <NSObject> 

@required 
-(bool)allowDrag; 
-(bool)allowDrop; 
-(void)dragEnter; 
-(void)acceptDrop; 
-(void)draggingCancelled; 
-(void)itemDropped:(id)draggedTabViewItem; 
-(void)itemDroppedCompleted:(id)droppedTabViewItem; 
@end 

@interface CustomTab : NSTabView{ 
    ItemDragStat eItemDragStat; 
    id draggedItem; 
} 

@property(assign)id draggedItem; 
@end 

現在的一些重要的執行

#import "CustomTab.h" 
#include "Log.h" 

@implementation CustomTab 

@synthesize draggedItem; 

- (id)initWithFrame:(NSRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code here. 
    } 

    return self; 
} 

# if 0 
// don't delete it, might need later on 
- (void)drawRect:(NSRect)dirtyRect 
{ 
    // Drawing code here. 
} 
# endif 
- (void)mouseUp:(NSEvent *)theEvent{ 
    log(" Mouse up "); 


    NSPoint location = [self convertPoint: [theEvent locationInWindow] 
           fromView: nil]; 



    NSTabViewItem *anItem = [self tabViewItemAtPoint: location]; 

    if (anItem == nil) { 
     // if its mouse up else where, reject dragging regardless 
     eItemDragStat = itemDragStatNormal; 
     log("Item will not be dropped"); 
     return; 
    } 

    if (![anItem isEqual:[self selectedTabViewItem]]){ 
     log("Mouse up is in nonselected item"); 

     if (eItemDragStat == itemDragging){ 
      log("Item will be dropped into this "); 


      id droppedTabViewItem = anItem; 

      if (droppedTabViewItem && [droppedTabViewItem respondsToSelector:@selector(itemDropped:)]){ 

       id selectedTabViewItem = [self selectedTabViewItem]; 

       [droppedTabViewItem performSelector:@selector(itemDropped:) withObject:selectedTabViewItem]; 

      } 
     } 
    } 
    eItemDragStat = itemDragStatNormal; 
    // return; 

    // [super mouseUp:theEvent]; 
} 
- (void)mouseDown:(NSEvent *)theEvent{ 

    NSPoint location = [self convertPoint: [theEvent locationInWindow] 
           fromView: nil]; 

    draggedItem = [self tabViewItemAtPoint:location]; 

    NSTabViewItem *anItem = [self tabViewItemAtPoint: location]; 

    if (anItem != nil && ![anItem isEqual: [self selectedTabViewItem]]) 
    { 
     [self selectTabViewItem: anItem]; 
    } 


} 

- (void)mouseDragged:(NSEvent *)theEvent{ 

    NSPoint location = [self convertPoint: [theEvent locationInWindow] 
           fromView: nil]; 


    id tabViewItemId = [self tabViewItemAtPoint:location]; 

    NSTabViewItem *anItem = [self tabViewItemAtPoint: location]; 

    if (anItem){ 
     if (![anItem isEqual:draggedItem]){ 

      if (tabViewItemId && [tabViewItemId respondsToSelector:@selector(allowDrag)]){ 
       eItemDragStat = itemDragging; 

      }else{ 
       // drag will be cancelled now. 
       // tell client item to stop dragging 
       if (eItemDragStat == itemDragging){ 
        if (draggedItem && [ draggedItem respondsToSelector:@selector(draggingCancelled)]){ 

         [draggedItem performSelector:@selector(draggingCancelled)]; 
         draggedItem = nil; 
        } 
        } 
       eItemDragStat = itemNotDragging; 
       // if we have +cursor then it should be reset 
      } 

     }else{ 
      log(" Mouse dragged"); 
     } 
    }else{ 
     // dragging went elsewhere, lets close this dragging operation 
     if (draggedItem && [ draggedItem respondsToSelector:@selector(draggingCancelled)]){ 

      [draggedItem performSelector:@selector(draggingCancelled)]; 
      draggedItem = nil; 
     } 
     // here reset the mouse pointer 
     eItemDragStat = itemNotDragging; 

    } 
} 

@end 

的它需要更多的微調及其回事....