X-Git-Url: http://git.ithinksw.org/ITKit.git/blobdiff_plain/51184d8f0f8359c82c46c079d0052cd60c3cfee1..22ddc2ccf5a32acf25445656e9c14e22a62835c9:/ITTabView.m?ds=inline diff --git a/ITTabView.m b/ITTabView.m index 9fbfe56..66f0bcc 100755 --- a/ITTabView.m +++ b/ITTabView.m @@ -1,50 +1,95 @@ #import "ITTabView.h" +/*************************************************************************/ +#pragma mark - +#pragma mark EVIL HACKERY +/*************************************************************************/ + +@interface NSTabViewItem (HACKHACKHACKHACK) +- (NSRect)_tabRect; +@end + + @implementation ITTabView - (id)initWithFrame:(NSRect)frame { if ( (self = [super initWithFrame:frame]) ) { _draggedTab = nil; + _allowsDragging = NO; + _requiredModifiers = NSCommandKeyMask; } return self; } +- (void)setAllowsDragging:(bool)flag +{ + _allowsDragging = flag; + _requiredModifiers = NSCommandKeyMask; +} + +- (bool)allowsDragging +{ + return _allowsDragging; +} + +- (void)setRequiredModifiers:(unsigned int)modifiers +{ + _requiredModifiers = modifiers; +} + +- (unsigned int)requiredModifiers +{ + return _requiredModifiers; +} + +- (void)moveTab:(NSTabViewItem *)tab toIndex:(int)index +{ + if ([self indexOfTabViewItem:tab] != index) + { + [tab retain]; + [self removeTabViewItem:tab]; + [self insertTabViewItem:tab atIndex:index]; + [self selectTabViewItem:tab]; + } +} + - (void)mouseDown:(NSEvent *)event { - NSPoint clickedPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]]; - NSTabViewItem *clickedTab = [self tabViewItemAtPoint:clickedPoint]; - _draggedTab = clickedTab; + if ((_requiredModifiers == 0 || ([[NSApp currentEvent] modifierFlags] & _requiredModifiers)) && [self allowsDragging]) { + NSPoint clickedPoint; + clickedPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]]; + NSTabViewItem *clickedTab = [self tabViewItemAtPoint:clickedPoint]; + _draggedTab = clickedTab; + } [super mouseDown:event]; } - (void)mouseUp:(NSEvent *)event { - NSPoint releasedPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]]; - NSTabViewItem *releasedTab = [self tabViewItemAtPoint:releasedPoint]; - if (releasedTab && ![releasedTab isEqualTo:_draggedTab]) { - int releasedTabIndex = [self indexOfTabViewItem:releasedTab]; - [_draggedTab retain]; - [self removeTabViewItem:_draggedTab]; - [self insertTabViewItem:_draggedTab atIndex:releasedTabIndex]; - [self selectTabViewItem:_draggedTab]; + if (_draggedTab && [self allowsDragging]) { + NSPoint releasedPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]]; + NSTabViewItem *releasedTab = [self tabViewItemAtPoint:releasedPoint]; + if (releasedTab && ![releasedTab isEqualTo:_draggedTab]) { + [self moveTab:_draggedTab toIndex:[self indexOfTabViewItem:releasedTab]]; + } + _draggedTab = nil; } - _draggedTab = nil; [super mouseUp:event]; } - (void)mouseDragged:(NSEvent *)event { - NSPoint currentPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]]; - NSTabViewItem *curTab = [self tabViewItemAtPoint:currentPoint]; - if (curTab && ![curTab isEqualTo:_draggedTab]) { - int curTabIndex = [self indexOfTabViewItem:curTab]; - [_draggedTab retain]; - [self removeTabViewItem:_draggedTab]; - [self insertTabViewItem:_draggedTab atIndex:curTabIndex]; - [self selectTabViewItem:_draggedTab]; + if (_draggedTab && [self allowsDragging]) { + NSPoint currentPoint = [self convertPoint:[event locationInWindow] fromView:[[self window] contentView]]; + NSTabViewItem *curTab = [self tabViewItemAtPoint:currentPoint]; + if (curTab && ![curTab isEqualTo:_draggedTab]) { + [self moveTab:_draggedTab toIndex:[self indexOfTabViewItem:curTab]]; + [self selectTabViewItem:_draggedTab]; + } } [super mouseDragged:event]; } + @end