Removing the use of private CoreGraphics APIs to draw shadows, and replacing with...
[ITKit.git] / ITTabView.m
1 #import "ITTabView.h"
2
3 @interface NSTabViewItem (ITTabViewHacks)
4 - (NSRect)_tabRect;
5 @end
6
7 @implementation ITTabView
8
9 - (id)initWithFrame:(NSRect)frame {
10         if ((self = [super initWithFrame:frame])) {
11                 _draggedTab = nil;
12                 _allowsDragging = NO;
13                 _requiredModifiers = NSCommandKeyMask;
14         }
15         return self;
16 }
17
18 - (void)setAllowsDragging:(BOOL)flag {
19         _allowsDragging = flag;
20 }
21
22 - (BOOL)allowsDragging {
23         return _allowsDragging;
24 }
25
26 - (void)setRequiredModifiers:(unsigned int)modifiers {
27         _requiredModifiers = modifiers;
28 }
29
30 - (unsigned int)requiredModifiers {
31         return _requiredModifiers;
32 }
33
34 - (void)moveTab:(NSTabViewItem *)tab toIndex:(int)index {
35         int oldIndex = [self indexOfTabViewItem:tab];
36         if ((oldIndex != NSNotFound) && (oldIndex != index)) {
37                 [tab retain];
38                 [self removeTabViewItem:tab];
39                 [self insertTabViewItem:tab atIndex:index];
40                 [self selectTabViewItem:tab];
41                 [tab release];
42         }
43 }
44
45 - (void)mouseDown:(NSEvent *)event {
46         if ((_requiredModifiers == 0 || ([[NSApp currentEvent] modifierFlags] & _requiredModifiers)) && [self allowsDragging]) {
47                 NSPoint clickedPoint;
48                 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         if (_draggedTab && [self allowsDragging]) {
57                 NSPoint releasedPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]];
58                 NSTabViewItem *releasedTab = [self tabViewItemAtPoint:releasedPoint];
59                 if (releasedTab && ![releasedTab isEqualTo:_draggedTab]) {
60                         [self moveTab:_draggedTab toIndex:[self indexOfTabViewItem:releasedTab]];
61                 }
62                 _draggedTab = nil;
63         }
64         [super mouseUp:event];
65 }
66
67 - (void)mouseDragged:(NSEvent *)event {
68         if (_draggedTab && [self allowsDragging]) {
69                 NSPoint currentPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]];
70                 NSTabViewItem *curTab = [self tabViewItemAtPoint:currentPoint];
71                 if (curTab && ![curTab isEqualTo:_draggedTab]) {
72                         [self moveTab:_draggedTab toIndex:[self indexOfTabViewItem:curTab]];
73                         [self selectTabViewItem:_draggedTab];
74                 }
75         }
76         [super mouseDragged:event];
77 }
78
79 @end