NSView

From GNUstepWiki

NSView is an AppKit class. It's role as a superclass is to provide most of the core functionality and services that specific GUI classes (such as NSButton or NSMenuView) use to display/render and handle events.

Code chunks

Dragging objects

Note: following code can be somehow optimised by adding small 0.01s delay or something like that. If someone knows how to, please write it here.

Implement the mouseDown: method:

- (void)mouseDown:(NSEvent *)event
{
   NSPoint        oldPoint;
   NSEvent       *presentEvent;
   NSEventType    eventType = [event type];
   unsigned int   eventMask = NSLeftMouseDownMask | NSLeftMouseUpMask
                                                | NSLeftMouseDraggedMask;
   NSPoint        point;
   float          dx,dy;
   NSPoint        location;

   point = [self convertPoint:[event locationInWindow] fromView:nil];

   oldPoint = point;

   do
   {
       while (event && eventType != NSLeftMouseUp)
       {
           presentEvent = event;
           event = [NSApp nextEventMatchingMask: eventMask
                        untilDate: [NSDate distantPast]
                           inMode: NSEventTrackingRunLoopMode
                          dequeue: YES];
           eventType = [event type];
       }
                          
       point = [self convertPoint: [presentEvent locationInWindow]
                         fromView: nil];
   
       dx = point.x - oldPoint.x;
       dy = point.y - oldPoint.y;

       [self scrollRectToVisible:frame];

Add code here:

       //
       // here move the dragged thing by: dx, dy
       // ....
       

Then finish the loop:

       [self setNeedsDisplay:YES];

       oldPoint = point;

       if (eventType == NSLeftMouseUp)
       {
           break;
       }

       event = [NSApp nextEventMatchingMask: eventMask
                     untilDate: nil
                        inMode: NSEventTrackingRunLoopMode
                       dequeue: YES];
      eventType = [event type];

   } while (eventType != NSLeftMouseUp);

   [self setNeedsDisplay:YES];
}