News Software Download Buy Now About Us Developers Contact
software TOC | PREV | NEXT
/* SDMovingRowsProtocol.h created by andrew on Thu 26-Mar-1998 */


@protocol SDMovingRowsProtocol

// Which key modifiers enable reordering? OR them together
// exp: return (NSCommandKeyMask|NSAlternateKeyMask);
// if you want the default behaviour to alas drag cells
// pass this pack for

#define DRAG_ALWAYS    0
- (unsigned int)dragReorderingMask:(int)forColumn;

// Delegate called after the reordering of cells, you must reorder your data.
// Returning YES will cause the table to be reloaded.
- (BOOL)tableView:(NSTableView *)tv didDepositRow:(int)rowToMove at:(int)newPosition;

// Notifies the delegate of impending movement
- (void)willMoveRow:(int)row;

// When we move an image around in the reordering process
// we'll use this method to get the correct image:
- (NSImage *)imageForRow:(int)row;

@end

//
// SDTableView.h
//

#import <AppKit/AppKit.h>

@interface SDTableView : NSTableView
{
}
@end

//
// SDTableView.m
//

#import "SDTableView.h"
#import "SDMovingRowsProtocol.h"

// TODO: a real "drag the image" reordering of rows

@implementation SDTableView

//
// We want instant drag & drop, so we accept the first mouse:
//

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent {return YES;}

//
// Here, we ask our delegate to do the work for cut, copy and paste:
//

- (void)copy:sender
{
    [[self delegate]copy:self];
}

- (void)cut:sender
{
    [[self delegate]cut:self];
}

- (void)paste:sender
{
    [[self delegate]paste:self];
}

//
// here's how to create your own Cursor:
//

- moveCursor
{
    static id vertCursor = nil;
    if (!vertCursor) {
        NSPoint spot;
        spot.x = 7.0; spot.y = 7.0;
        vertCursor = [[NSCursor alloc] initWithImage:
        [NSImage imageNamed:@"draggingPoint.tiff"] hotSpot:spot];
    }
    return vertCursor;
}

//
// We send the information to our delegate when we are done moving:
//

- (BOOL)startRow:(int)start endedAt:(int)end
{
    if (end != -1 && start != -1 && end != start &&
        [[self delegate] tableView:self didDepositRow:start at:end]) {
        [self reloadData];
        [self selectRow:end byExtendingSelection:NO];
        return YES;
    }
    return NO;
}

- (BOOL)move:(NSEvent *)e
{
    unsigned int flags = [e modifierFlags];
    NSPoint mouseDownLocation = [e locationInWindow];
    int col;


    mouseDownLocation = [self convertPoint:mouseDownLocation fromView:nil];
    col = [self columnAtPoint:mouseDownLocation];

    if ([[self delegate] conformsToProtocol:@protocol(SDMovingRowsProtocol)] &&
((flags & [[self delegate]dragReorderingMask:col]) ||
([[self delegate]dragReorderingMask:col] == DRAG_ALWAYS))) {
        int endRow, hitRow;

        hitRow = [self rowAtPoint:mouseDownLocation];
        // notify client of impending action:
        [[self delegate] willMoveRow:hitRow];

        // our Omni reposition row cursor:
        [[self moveCursor] push];
        
        // Super does the actual work of highlighting etc:
        [super mouseDown:e];

        // never forget to restore NSCursor state:
        [[self moveCursor] pop];

        mouseDownLocation = [[[self window] currentEvent] locationInWindow];
        mouseDownLocation = [self convertPoint:mouseDownLocation fromView:nil];
        endRow = [self rowAtPoint:mouseDownLocation];

    // finally return the status from our delegate
    // response to the SDMovingRowsProtocol didDepositRow:at:
    return [self startRow:hitRow endedAt:endRow];
}
return NO;
}

- (void)mouseDown:(NSEvent *)e
{
    if (![self move:e]) [super mouseDown:e];
}

@end
TOC | PREV | NEXT
Created by Stone Design's Create on 4/30/1998
©2000 Stone Design top