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