Tabs drag only with command-drag.
[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         _requiredModifiers = NSCommandKeyMask;
21     }
22     return self;
23 }
24
25 - (void)setAllowsDragging:(bool)flag
26 {
27     _allowsDragging = flag;
28     _requiredModifiers = NSCommandKeyMask;
29 }
30
31 - (bool)allowsDragging
32 {
33     return _allowsDragging;
34 }
35
36 - (void)setRequiredModifiers:(unsigned int)modifiers
37 {
38     _requiredModifiers = modifiers;
39 }
40
41 - (unsigned int)requiredModifiers
42 {
43     return _requiredModifiers;
44 }
45
46 - (void)moveTab:(NSTabViewItem *)tab toIndex:(int)index
47 {
48     if ([self indexOfTabViewItem:tab] != index)
49     {
50         [tab retain];
51         [self removeTabViewItem:tab];
52         [self insertTabViewItem:tab atIndex:index];
53         [self selectTabViewItem:tab];
54     }
55 }
56
57 - (void)mouseDown:(NSEvent *)event
58 {
59     if ((_requiredModifiers == 0 || ([[NSApp currentEvent] modifierFlags] & _requiredModifiers)) && [self allowsDragging]) {
60         NSPoint clickedPoint;
61         clickedPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]];
62         NSTabViewItem *clickedTab = [self tabViewItemAtPoint:clickedPoint];
63         _draggedTab = clickedTab;
64     }
65     [super mouseDown:event];
66 }
67
68 - (void)mouseUp:(NSEvent *)event
69 {
70     if (_draggedTab && [self allowsDragging]) {
71         NSPoint releasedPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]];
72         NSTabViewItem *releasedTab = [self tabViewItemAtPoint:releasedPoint];
73         if (releasedTab && ![releasedTab isEqualTo:_draggedTab]) {
74             [self moveTab:_draggedTab toIndex:[self indexOfTabViewItem:releasedTab]];
75         }
76         _draggedTab = nil;
77     }
78     [super mouseUp:event];
79 }
80
81 - (void)mouseDragged:(NSEvent *)event
82 {
83     if (_draggedTab && [self allowsDragging]) {
84         NSPoint currentPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]];
85         NSTabViewItem *curTab = [self tabViewItemAtPoint:currentPoint];
86         if (curTab && ![curTab isEqualTo:_draggedTab]) {
87             [self moveTab:_draggedTab toIndex:[self indexOfTabViewItem:curTab]];
88             [self selectTabViewItem:_draggedTab];
89         }
90     }
91     [super mouseDragged:event];
92 }
93
94
95 @end