Added a new method, moveTab:toIndex:
[ITKit.git] / ITTabView.m
1 #import "ITTabView.h"
2
3 @implementation ITTabView
4
5 - (id)initWithFrame:(NSRect)frame
6 {
7     if ( (self = [super initWithFrame:frame]) ) {
8         _draggedTab = nil;
9         _allowsDragging = NO;
10     }
11     return self;
12 }
13
14 - (void)setAllowsDragging:(bool)flag
15 {
16     _allowsDragging = flag;
17 }
18
19 - (bool)allowsDragging
20 {
21     return _allowsDragging;
22 }
23
24 - (void)moveTab:(NSTabViewItem *)tab toIndex:(int)index
25 {
26     if ([self indexOfTabViewItem:tab] != index)
27     {
28         [tab retain];
29         [self removeTabViewItem:tab];
30         [self insertTabViewItem:tab atIndex:index];
31         [self selectTabViewItem:tab];
32     }
33 }
34
35 - (void)mouseDown:(NSEvent *)event
36 {
37     if ([self allowsDragging]) {
38         NSPoint clickedPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]];
39         NSTabViewItem *clickedTab = [self tabViewItemAtPoint:clickedPoint];
40         _draggedTab = clickedTab;
41     }
42     [super mouseDown:event];
43 }
44
45 - (void)mouseUp:(NSEvent *)event
46 {
47     if ([self allowsDragging]) {
48         NSPoint releasedPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]];
49         NSTabViewItem *releasedTab = [self tabViewItemAtPoint:releasedPoint];
50         if (releasedTab && ![releasedTab isEqualTo:_draggedTab]) {
51             [self moveTab:_draggedTab toIndex:[self indexOfTabViewItem:releasedTab]];
52         }
53         _draggedTab = nil;
54     }
55     [super mouseUp:event];
56 }
57
58 - (void)mouseDragged:(NSEvent *)event
59 {
60     if ([self allowsDragging]) {
61         NSPoint currentPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]];
62         NSTabViewItem *curTab = [self tabViewItemAtPoint:currentPoint];
63         if (curTab && ![curTab isEqualTo:_draggedTab]) {
64             [self moveTab:_draggedTab toIndex:[self indexOfTabViewItem:curTab]];
65             [self selectTabViewItem:_draggedTab];
66         }
67     }
68     [super mouseDragged:event];
69 }
70
71 @end