| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
#import "AICustomTabDragging.h" |
|---|
| 10 |
#import "AICustomTabDragWindow.h" |
|---|
| 11 |
#import "AICustomTabsView.h" |
|---|
| 12 |
#import "AICustomTabCell.h" |
|---|
| 13 |
|
|---|
| 14 |
#define CUSTOM_TABS_INDENT 3 //Indent on left and right of tabbar |
|---|
| 15 |
|
|---|
| 16 |
@interface AICustomTabDragging (PRIVATE) |
|---|
| 17 |
+ (NSImage *)dragTabImageForTabCell:(AICustomTabCell *)tabCell inCustomTabsView:(AICustomTabsView *)customTabsView; |
|---|
| 18 |
+ (NSImage *)dragWindowImageForWindow:(NSWindow *)window customTabsView:(AICustomTabsView *)customTabsView tabCell:(AICustomTabCell *)tabCell; |
|---|
| 19 |
- (void)cleanupDrag; |
|---|
| 20 |
@end |
|---|
| 21 |
|
|---|
| 22 |
@implementation AICustomTabDragging |
|---|
| 23 |
|
|---|
| 24 |
static AICustomTabDragging *sharedTabDragInstance = nil; |
|---|
| 25 |
+ (AICustomTabDragging *)sharedInstance |
|---|
| 26 |
{ |
|---|
| 27 |
if(!sharedTabDragInstance) sharedTabDragInstance = [[self alloc] init]; |
|---|
| 28 |
return(sharedTabDragInstance); |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
- (id)init |
|---|
| 33 |
{ |
|---|
| 34 |
[super init]; |
|---|
| 35 |
_destinationOfLastDrag = nil; |
|---|
| 36 |
dragTabCell = nil; |
|---|
| 37 |
sourceTabBar = nil; |
|---|
| 38 |
destTabBar = nil; |
|---|
| 39 |
tabDragWindow = nil; |
|---|
| 40 |
|
|---|
| 41 |
return(self); |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
- (void)setDestinationTabView:(AICustomTabsView *)inDest |
|---|
| 46 |
{ |
|---|
| 47 |
if(inDest != destTabBar){ |
|---|
| 48 |
[destTabBar release]; |
|---|
| 49 |
destTabBar = [inDest retain]; |
|---|
| 50 |
} |
|---|
| 51 |
[tabDragWindow setDisplayingFullWindow:(!destTabBar) animate:YES]; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
- (AICustomTabsView *)destinationTabView |
|---|
| 56 |
{ |
|---|
| 57 |
return(destTabBar); |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
- (AICustomTabsView *)sourceTabView |
|---|
| 62 |
{ |
|---|
| 63 |
return(sourceTabBar); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
- (void)setDestinationHoverPoint:(NSPoint)inPoint |
|---|
| 68 |
{ |
|---|
| 69 |
[tabDragWindow moveToPoint:inPoint]; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
- (NSSize)sizeOfDraggedCell |
|---|
| 74 |
{ |
|---|
| 75 |
return([dragTabCell frame].size); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
#pragma mark Drag Start/Stop |
|---|
| 81 |
|
|---|
| 82 |
- (void)dragTabCell:(AICustomTabCell *)inTabCell fromCustomTabsView:(AICustomTabsView *)sourceView withEvent:(NSEvent *)inEvent selectTab:(BOOL)shouldSelect |
|---|
| 83 |
{ |
|---|
| 84 |
NSPasteboard *pboard; |
|---|
| 85 |
NSPoint clickLocation = [inEvent locationInWindow]; |
|---|
| 86 |
NSPoint startPoint; |
|---|
| 87 |
BOOL sourceWindowWillHide; |
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
[[NSNotificationCenter defaultCenter] postNotificationName:AICustomTabDragWillBegin object:self]; |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
[destTabBar release]; destTabBar = nil; |
|---|
| 94 |
sourceTabBar = [sourceView retain]; |
|---|
| 95 |
dragTabCell = [inTabCell retain]; |
|---|
| 96 |
selectTabAfterDrag = shouldSelect; |
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
sourceWindowWillHide = ([sourceTabBar removingLastTabHidesWindow] && [sourceTabBar numberOfTabViewItems] == 1); |
|---|
| 100 |
if(!sourceWindowWillHide){ |
|---|
| 101 |
destTabBar = [sourceView retain]; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 |
int width = [inTabCell frame].size.width; |
|---|
| 106 |
int height = [inTabCell frame].size.height; |
|---|
| 107 |
|
|---|
| 108 |
dragOffset = NSMakeSize([inTabCell frame].origin.x - clickLocation.x, [inTabCell frame].origin.y - clickLocation.y); |
|---|
| 109 |
if(dragOffset.width > width) dragOffset.width = width; |
|---|
| 110 |
if(dragOffset.width < -width) dragOffset.width = -width; |
|---|
| 111 |
if(dragOffset.height > height) dragOffset.height = height; |
|---|
| 112 |
if(dragOffset.height < -height) dragOffset.height = -height; |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
tabDragWindow = [AICustomTabDragWindow dragWindowForCustomTabView:sourceView |
|---|
| 116 |
cell:inTabCell |
|---|
| 117 |
transparent:!([sourceTabBar removingLastTabHidesWindow])]; |
|---|
| 118 |
[tabDragWindow setDisplayingFullWindow:sourceWindowWillHide animate:NO]; |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
startPoint = [[inEvent window] convertBaseToScreen:[inEvent locationInWindow]]; |
|---|
| 122 |
startPoint = NSMakePoint(startPoint.x + dragOffset.width, startPoint.y + dragOffset.height); |
|---|
| 123 |
[tabDragWindow moveToPoint:startPoint]; |
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
if(sourceWindowWillHide){ |
|---|
| 127 |
[[sourceTabBar window] setAlphaValue:0.0]; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
pboard = [NSPasteboard pasteboardWithName:NSDragPboard]; |
|---|
| 132 |
[pboard declareTypes:[NSArray arrayWithObjects:TAB_CELL_IDENTIFIER, nil] owner:self]; |
|---|
| 133 |
[pboard setString:TAB_CELL_IDENTIFIER forType:TAB_CELL_IDENTIFIER]; |
|---|
| 134 |
[[inEvent window] dragImage:[tabDragWindow dragImage] |
|---|
| 135 |
at:NSMakePoint(clickLocation.x + dragOffset.width, clickLocation.y + dragOffset.height) |
|---|
| 136 |
offset:NSMakeSize(0,0) |
|---|
| 137 |
event:inEvent |
|---|
| 138 |
pasteboard:pboard |
|---|
| 139 |
source:self |
|---|
| 140 |
slideBack:NO]; |
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
[tabDragWindow closeWindow]; |
|---|
| 148 |
if(_destinationOfLastDrag){ |
|---|
| 149 |
[_destinationOfLastDrag resetCursorTracking]; |
|---|
| 150 |
[_destinationOfLastDrag release]; _destinationOfLastDrag= nil; |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
- (void)acceptDragIntoTabView:(AICustomTabsView *)destTabView atIndex:(int)destIndex |
|---|
| 157 |
{ |
|---|
| 158 |
if(destTabView == sourceTabBar){ |
|---|
| 159 |
|
|---|
| 160 |
[sourceTabBar moveTab:[dragTabCell tabViewItem] toIndex:destIndex selectTab:selectTabAfterDrag animate:NO]; |
|---|
| 161 |
|
|---|
| 162 |
}else{ |
|---|
| 163 |
|
|---|
| 164 |
if([[sourceTabBar delegate] respondsToSelector:@selector(customTabView:didMoveTabViewItem:toCustomTabView:index:screenPoint:)]){ |
|---|
| 165 |
[[sourceTabBar delegate] customTabView:sourceTabBar |
|---|
| 166 |
didMoveTabViewItem:[dragTabCell tabViewItem] |
|---|
| 167 |
toCustomTabView:destTabView |
|---|
| 168 |
index:destIndex |
|---|
| 169 |
screenPoint:NSMakePoint(-1,-1)]; |
|---|
| 170 |
} |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
_destinationOfLastDrag = [destTabBar retain]; |
|---|
| 175 |
[self cleanupDrag]; |
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 |
[[NSNotificationCenter defaultCenter] postNotificationName:AICustomTabDragDidComplete object:self]; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
|
|---|
| 183 |
#pragma mark Drag Tracking (Source) |
|---|
| 184 |
|
|---|
| 185 |
- (void)draggedImage:(NSImage *)image beganAt:(NSPoint)screenPoint |
|---|
| 186 |
{ |
|---|
| 187 |
[self draggedImage:image movedTo:screenPoint]; |
|---|
| 188 |
} |
|---|
| 189 |
|
|---|
| 190 |
|
|---|
| 191 |
- (void)draggedImage:(NSImage *)image movedTo:(NSPoint)screenPoint |
|---|
| 192 |
{ |
|---|
| 193 |
[tabDragWindow setDisplayingFullWindow:(!destTabBar) animate:YES]; |
|---|
| 194 |
|
|---|
| 195 |
if(!destTabBar){ |
|---|
| 196 |
[tabDragWindow moveToPoint:screenPoint]; |
|---|
| 197 |
} |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
- (void)draggedImage:(NSImage *)image endedAt:(NSPoint)screenPoint operation:(NSDragOperation)operation |
|---|
| 202 |
{ |
|---|
| 203 |
if(operation == NSDragOperationNone){ |
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 |
|
|---|
| 209 |
if(destTabBar) [destTabBar draggingExited:nil]; |
|---|
| 210 |
|
|---|
| 211 |
screenPoint.x -= CUSTOM_TABS_INDENT; |
|---|
| 212 |
if([[sourceTabBar delegate] respondsToSelector:@selector(customTabView:didMoveTabViewItem:toCustomTabView:index:screenPoint:)]){ |
|---|
| 213 |
[[sourceTabBar delegate] customTabView:sourceTabBar didMoveTabViewItem:[dragTabCell tabViewItem] toCustomTabView:nil index:-1 screenPoint:screenPoint]; |
|---|
| 214 |
} |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 |
[self cleanupDrag]; |
|---|
| 219 |
} |
|---|
| 220 |
|
|---|
| 221 |
|
|---|
| 222 |
- (unsigned int)draggingSourceOperationMaskForLocal:(BOOL)isLocal |
|---|
| 223 |
{ |
|---|
| 224 |
return(isLocal ? NSDragOperationEvery : NSDragOperationNone); |
|---|
| 225 |
} |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
- (void)cleanupDrag |
|---|
| 229 |
{ |
|---|
| 230 |
[dragTabCell release]; dragTabCell = nil; |
|---|
| 231 |
[destTabBar release]; destTabBar = nil; |
|---|
| 232 |
[sourceTabBar release]; sourceTabBar = nil; |
|---|
| 233 |
} |
|---|
| 234 |
|
|---|
| 235 |
- (NSTabViewItem *)draggedTabViewItem |
|---|
| 236 |
{ |
|---|
| 237 |
return([dragTabCell tabViewItem]); |
|---|
| 238 |
} |
|---|
| 239 |
|
|---|
| 240 |
@end |
|---|