有可拖动的NSTabViewItem

时间:2013-02-05 12:45:37

标签: objective-c cocoa nstableview nsview

是否可以提供可拖动的NSTabViewItem,
基本上我想要的,如果我在NSTabViewITem和Move的标签上按下L Button,我应该允许拖动TabView项目, 我想要移动NSTabView项目并有另外一个功能,如果用户拖动NSTabView项目的标签并将其移动到一个特定区域,那么我应该允许删除该NSTabView项目,

我只能找到一种拥有PSMTab栏的方法,但我在NSTabView项目上还有其他功能,如果我采用这种方法,我将会失踪。

1 个答案:

答案 0 :(得分: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

它需要一些更精细的调整并继续......