File: MoveMatrix.m

package info (click to toggle)
cenon.app 4.0.6%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 35,392 kB
  • sloc: objc: 74,441; ansic: 1,270; sh: 124; makefile: 35
file content (456 lines) | stat: -rw-r--r-- 18,405 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/* MoveMatrix.m
 *
 * Copyright (C) 1993-2015 by Cenon GmbH
 * Author: Georg Fleischmann
 *
 * Created:  1993-05-17
 * Modified: 2015-11-30 (-mouseDown, dont shuffle cells if -matrixShouldShuffleCells returns NO)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Cenon Public License as
 * published by Cenon GmbH. Among other things, the
 * License requires that the copyright notices and this notice
 * be preserved on all copies.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the Cenon Public License for more details.
 *
 * You should have received a copy of the Cenon Public License along
 * with this program; see the file LICENSE. If not, write to Cenon.
 *
 * Cenon GmbH, Schwabstr. 45, 72108 Rottenburg a.N., Germany
 * eMail: info@Cenon.com
 * http://www.Cenon.de
 */

#include <AppKit/AppKit.h>
#include <VHFShared/vhfCompatibility.h>
#include "FlippedView.h"
#include "MoveCell.h"
#include "MoveMatrix.h"

@implementation MoveMatrix

- (id)initCellClass:aCellClass
{   NSRect	scrollRect, matrixRect;
    NSSize	interCellSpacing = {0.0, 0.0}, newCellSize;

    scrollRect = [[[self superview] superview] frame];	// get the scrollView's dimensions

    matrixRect.size = [NSScrollView contentSizeForFrameSize:(scrollRect.size)
                                      hasHorizontalScroller:NO hasVerticalScroller:YES
                                                 borderType:NSBezelBorder];

    //[self setMode:NSListModeMatrix];			// prepare a matrix to the right state
    //	Wenn dieser Status gesetzt ist, kommt das trackMouse nicht mehr in der LayerCell an
    //  Andererseits funktioniert dann das Move nicht mehr ganz so gut

    [self setCellClass:aCellClass];
    [self renewRows:0 columns:1];

    [self setIntercellSpacing:interCellSpacing];	// we don't want any space between the matrix's cells

    newCellSize = [self cellSize];			// resize the matrix's cells and size the
    newCellSize.width = NSWidth(matrixRect);		// matrix to contain them

    [self setCellSize:newCellSize];
    [self sizeToCells];
    [self setAutosizesCells:YES];
    [self setAutoscroll:YES];

    return self;
}

- (void)calcCellSize
{
    if ( [self numberOfRows] > 0 )
    {   id	cell = [self cellAtRow:0 column:0];
        NSSize	size = [cell cellSize];
        NSRect	frame = [[self superview] frame];

        if ( frame.size.width )
        {   size.width = frame.size.width;
            [self setCellSize:size];
        }
    }
}

/* Traegt automatisch in die neue Cell sich selbst als MoveMatrix ein
 */
- (NSCell *)makeCellAtRow:(int)row column:(int)col;
{   MoveCell	*newCell = (MoveCell*)[super makeCellAtRow:row column:col];

    if ( [newCell respondsToSelector:@selector(setMatrix:)] )
        [newCell setMatrix:self];
    else
        NSLog(@"MoveMatrix (makeCellAt::): Cell does not respond to setMatrix");

    return newCell;
}


/*
 * Timers used to automatically scroll when the mouse is
 * outside the drawing view and not moving.
 */
#define startTimer(inTimerLoop) if (!inTimerLoop) { [NSEvent startPeriodicEventsAfterDelay:0.1 withPeriod:0.01]; inTimerLoop=YES; }
#define stopTimer(inTimerLoop) if (inTimerLoop) { [NSEvent stopPeriodicEvents]; inTimerLoop=NO; }

#define MOVE_MASK NSLeftMouseUpMask|NSLeftMouseDraggedMask

extern NSEvent *periodicEventWithLocationSetToPoint(NSEvent *oldEvent, NSPoint point);

- (void)mouseDown:(NSEvent *)theEvent
{   NSPoint		mouseDownLocation, mouseUpLocation, mouseLocation;
    NSInteger	row, column, newRow;
    NSRect		visibleRect, cellCacheBounds, cellFrame;
    float		dy;
    NSEvent		*event;
    BOOL 		scrolled = NO, inTimerLoop = NO;

    if ( ! ([theEvent modifierFlags] & NSControlKeyMask) )
    {	if ( [theEvent clickCount] == 1 )   // we only handle single click here to avoid issues
            [super mouseDown:theEvent];
        //else  // double click is making trouble (see commetn in IPLayerCell.m -trackMouse)
        //    NSLog(@"Double click");
        return;
    }

    /* shuffle Cell */

    /* prepare the cell and matrix cache windows */
    [self setupCache];

    /* find the cell that got clicked on and select it */
    mouseDownLocation = [self convertPoint:[theEvent locationInWindow] fromView:nil];
    [self getRow:&row column:&column forPoint:mouseDownLocation];
    activeCell = [[self cellAtRow:row column:column] retain];
    [self selectCell:activeCell];
    cellFrame = [self cellFrameAtRow:row column:column];

    /* do whatever's required for a single-click */
    [self sendAction];

    /* draw a "well" in place of the selected cell (see -drawRect:) */
    [self display];

    /* copy what's currently visible into the matrix cache */
#if defined(GNUSTEP_BASE_VERSION)
    [matrixCache lockFocus];
    visibleRect = [self convertRect:[self visibleRect] toView:nil];
    PScomposite(NSMinX(visibleRect), NSMinY(visibleRect), NSWidth(visibleRect), NSHeight(visibleRect), [[self window] gState], 0.0, 0.0, NSCompositeCopy);
    [matrixCache unlockFocus];
#elif defined(__APPLE__)
    [matrixCache release]; matrixCache = nil;
    //matrixCache = [[NSImage alloc] initWithData:[self dataWithPDFInsideRect:[self visibleRect]]];
    visibleRect = [self convertRect:[self visibleRect] toView:nil];
    [[self window] cacheImageInRect:visibleRect];
#else
    [matrixCache lockFocus];
    visibleRect = [self convertRect:[self visibleRect] toView:nil];
    PScomposite(NSMinX(visibleRect), NSMinY(visibleRect), NSWidth(visibleRect), NSHeight(visibleRect), [[self window] gState], 0.0, NSHeight(visibleRect), NSCompositeCopy);
    [matrixCache unlockFocus];
#endif

/* debug: draw cache into document view */
/*{   NSRect	bRect = [self bounds];
    [[[NSApp currentDocument] documentView] lockFocus];
    [matrixCache compositeToPoint:NSZeroPoint
                         fromRect:NSMakeRect(0.0, 0.0, bRect.size.width, bRect.size.height)
                        operation:NSCompositeCopy];
    [[[NSApp currentDocument] documentView] unlockFocus];
    [[[NSApp currentDocument] window] flushWindow];
}*/

    PSWait();

    /* image the cell into its cache */
    [cellCache lockFocus];
    cellCacheBounds.origin = NSZeroPoint;
    cellCacheBounds.size   = [cellCache size];
    [activeCell drawWithFrame:cellCacheBounds inView:[NSView focusView]];
    [cellCache unlockFocus];

    /* save the mouse's location relative to the cell's origin */
    dy = mouseDownLocation.y - cellFrame.origin.y;

    /* from now on we'll be drawing into ourself */
    [self lockFocus];

    event = theEvent;
    while ([event type] != NSLeftMouseUp)
    {
        /* erase the active cell using the image in the matrix cache */
        visibleRect = [self visibleRect];
#ifdef GNUSTEP_BASE_VERSION
        /* origin y of cellFrame is upper/left, composite origin is lower/left on GNUstep */
        /*[matrixCache compositeToPoint:NSMakePoint(NSMinX(cellFrame), NSMinY(cellFrame))
                             fromRect:NSMakeRect(0.0, NSHeight(visibleRect) - NSMinY(cellFrame) +
                                                      NSMinY(visibleRect) - NSHeight(cellFrame),
                                                 NSWidth(cellFrame), NSHeight(cellFrame))
                            operation:NSCompositeCopy];*/
        /* there is something wrong with the composit on GNUstep, the obove doesn't work */
        [matrixCache compositeToPoint:NSMakePoint(NSMinX(cellFrame), NSMaxY(visibleRect))
                             fromRect:NSMakeRect(0.0, 0.0, NSWidth(cellFrame), NSHeight(visibleRect))
                            operation:NSCompositeCopy];
#else
        [[self window] restoreCachedImage];
        /*if ( NSAppKitVersionNumber >= NSAppKitVersionNumber10_6 )   // 10.6: composite = deprecated, drawAtPoint doesn't flip, drawRect >= 10.6
            [matrixCache drawInRect:[self visibleRect] fromRect:NSZeroRect
                          operation:NSCompositeCopy fraction:1.0 respectFlipped:YES hints:nil]; // >= 10.6
        else    // <= 10.5
            [matrixCache compositeToPoint:NSMakePoint(NSMinX(cellFrame), NSMaxY(cellFrame))
                                 fromRect:NSMakeRect(0.0, NSHeight(visibleRect) - NSMinY(cellFrame) +
                                                     NSMinY(visibleRect) - NSHeight(cellFrame),
                                                     NSWidth(cellFrame), NSHeight(cellFrame))
                                operation:NSCompositeCopy];*/
#endif

        /* move the active cell */
        mouseLocation = [self convertPoint:[event locationInWindow] fromView:nil];
        cellFrame.origin.y = mouseLocation.y - dy;

        /* constrain the cell's location to our bounds */
        if (NSMinY(cellFrame) < NSMinX([self bounds]) )
            cellFrame.origin.y = NSMinX([self bounds]);
        else if (NSMaxY(cellFrame) > NSMaxY([self bounds]))
            cellFrame.origin.y = NSHeight([self bounds]) - NSHeight(cellFrame);

        /* make sure the cell will be entirely visible in its new location (if
         * we're in a scrollView, it may not be)
         */
        if (!NSContainsRect(visibleRect, cellFrame) && [self isAutoscroll])
        {
            /* the cell won't be entirely visible, so scroll, dood, scroll, but
	         * don't display on-screen yet
	         */
            [[self window] disableFlushWindow];
            [self scrollRectToVisible:cellFrame];
            [[self window] enableFlushWindow];

            /* copy the new image to the matrix cache */
            [matrixCache lockFocus];
            //visibleRect = [self convertRect:[self visibleRect] fromView:[self superview]];
            //visibleRect = [self convertRect:visibleRect toView:nil];
            visibleRect = [self convertRect:[self visibleRect] toView:nil];
#ifdef GNUSTEP_BASE_VERSION
            PScomposite(NSMinX(visibleRect), NSMinY(visibleRect), NSWidth(visibleRect), NSHeight(visibleRect),
                        [[self window] gState], 0.0, 0.0, NSCompositeCopy);
#else
            [[self window] cacheImageInRect:visibleRect];
            // FIXME: this is a NOP on Apple
            //PScomposite(NSMinX(visibleRect), NSMinY(visibleRect), NSWidth(visibleRect), NSHeight(visibleRect),
            //            [[self window] gState], 0.0, NSHeight(visibleRect), NSCompositeCopy);
#endif
            [matrixCache unlockFocus];

            /* note that we scrolled and start generating timer events for autoscrolling */
            scrolled = YES;
            startTimer(inTimerLoop);
        }
        else
            stopTimer(inTimerLoop);

        /* composite the active cell's image on top of ourself */
        if ( NSAppKitVersionNumber >= NSAppKitVersionNumber10_6 )   // 10.6: composite = deprecated, drawAtPoint doesn't flip, drawRect >= 10.6
        {    NSRect  srcRect = NSMakeRect(0.0, 0.0, NSWidth(cellFrame), NSHeight(cellFrame));
             NSRect  dstRect = NSMakeRect(NSMinX(cellFrame), NSMinY(cellFrame), NSWidth(cellFrame), NSHeight(cellFrame));

            [cellCache drawInRect:dstRect fromRect:srcRect operation:NSCompositeCopy fraction:1.0 respectFlipped:YES hints:nil]; // >= 10.6
            /*[cellCache drawAtPoint:NSMakePoint(cellFrame.origin.x, NSMaxY(cellFrame))
                          fromRect:NSMakeRect(0.0, 0.0, NSWidth(cellFrame), NSHeight(cellFrame))
                         operation:NSCompositeCopy fraction:1.0];*/
        }
        else    // <= 10.5
            [cellCache compositeToPoint:NSMakePoint(cellFrame.origin.x, NSMaxY(cellFrame))
                               fromRect:NSMakeRect(0.0, 0.0, NSWidth(cellFrame), NSHeight(cellFrame))
                              operation:NSCompositeCopy];

        /* now show what we've done */
        [[self window] flushWindow];

        if (scrolled)		// if we autoscrolled, flush any lingering
        {   PSWait();		// window server events to make the scrolling
            scrolled = NO;	// smooth
        }

        /* save the current mouse location, just in case we need it again */
        mouseLocation = [event locationInWindow];

        /* no mouse moved available, then take mouseMoved, mouseUp, or timer */
        if (![[self window] nextEventMatchingMask:MOVE_MASK untilDate:[NSDate date]
                                           inMode:NSEventTrackingRunLoopMode dequeue:NO])
            event = [[self window] nextEventMatchingMask:MOVE_MASK | NSPeriodicMask];
        else
            event = [[self window] nextEventMatchingMask:MOVE_MASK];

        if ([event type] == NSPeriodic)
            event = periodicEventWithLocationSetToPoint(event, mouseLocation);
    }

    stopTimer(inTimerLoop);
    [self unlockFocus];

    /* find the cell under the mouse's location */
    mouseUpLocation = [event locationInWindow];
    mouseUpLocation = [self convertPoint:mouseUpLocation fromView:nil];
    if (![self getRow:&newRow column:&column forPoint:mouseUpLocation])
        [self getRow:&newRow column:&column forPoint:(cellFrame.origin)];

    /* we need to shuffle cells if the active cell's going to a new location */
    if ( ![[self cellAtRow:row column:0] dependant] && ![[self cellAtRow:newRow column:0] dependant] )
    {
        if ( !((delegate && [delegate respondsToSelector:@selector(matrixShouldShuffleCells)])
               && ![delegate matrixShouldShuffleCells]) ) // no shuffle
        {
            [self shuffleCell:row to:newRow];
            if ( [[self cellAtRow:(row<newRow)?row:row+1 column:0] dependant] )
                [self shuffleCell:(row<newRow)?row:row+1 to:(row<newRow)?newRow:newRow+1];
        }
    }

    /* make sure the active cell's visible if we're autoscrolling */
    if ([self isAutoscroll])
        [self scrollCellToVisibleAtRow:newRow column:0];

    /* no longer dragging the cell */
    [activeCell release];
    activeCell = 0;

    /* size to cells after all this shuffling and turn autodisplay back on */
    [self sizeToCells];
    [self display];
}


- (void)shuffleCell:(int)row to:(int)newRow
{   NSCell	*tmpCell;
    int		r = row, nr = newRow;
    int		selectedRow = [self selectedRow];

    if (newRow != row)
    {
        activeCell = [[self cellAtRow:row column:0] retain];
        if (newRow > row)
        {
            if (selectedRow <= newRow)					// adjust selected row if before
                selectedRow--;                          // new active cell location

            while (row++ < newRow)                      // push all cells above the active
            {	tmpCell = [self cellAtRow:row column:0];		// cell's new location up one row
                [self putCell:tmpCell atRow:row-1 column:0];	// so that we fill the vacant spot
                [tmpCell setTag:row-1];
            }

            [self putCell:activeCell atRow:newRow column:0];    // now place the active cell
            [(NSCell*)activeCell setTag:newRow];				// in its new home
        }
        else if (newRow < row)	
        {
            if (selectedRow >= newRow)                          // adjust selected row if after
                selectedRow++;                                  // new active cell location

            while (row-- > newRow)                              // push all cells below the active
            {	tmpCell = [self cellAtRow:row column:0];		// cell's new location down one
                [self putCell:tmpCell atRow:row+1 column:0];    // row so that we fill
                [tmpCell setTag:row+1];                         // the vacant spot
            }

            [self putCell:activeCell atRow:newRow column:0];    // now place the active cell
            [(NSCell*)activeCell setTag:newRow];                // in its new home
        }

        if ([activeCell state])                                 // if the active cell is selected,
            selectedRow = newRow;                               // note its new row
        [self selectCellAtRow:selectedRow column:0];

        activeCell = nil;						// no longer dragging the cell

        if (delegate && [delegate respondsToSelector:@selector(matrixDidShuffleCellFrom:to:)])
            [delegate matrixDidShuffleCellFrom:r to:nr];
    }
    else
        activeCell = nil;
}


- (void)drawRect:(NSRect)rect;
{   NSInteger   row, col;
    NSRect      cellBorder;
    NSRectEdge  sides[] = {NSMinXEdge, NSMinYEdge, NSMaxXEdge, NSMaxYEdge, NSMinXEdge, NSMinYEdge};
    //CGFloat     grays[] = {NSDarkGray, NSDarkGray, NSWhite,    NSWhite,    NSBlack,    NSBlack};      // OpenStep
    CGFloat     grays[] = {NSLightGray, NSLightGray, NSWhite,  NSWhite,    NSLightGray, NSLightGray};   // Apple

    /* do the regular drawing */
    [super drawRect:rect];

    /* draw a "well" if the user's dragging a cell */
    if (activeCell)
    {
        /* get the cell's frame */
        [self getRow:&row column:&col ofCell:activeCell];
        cellBorder = [self cellFrameAtRow:row column:col];
        /* draw the well */
        //NSDrawGroove(cellBorder, cellBorder);
        cellBorder = NSDrawTiledRects(cellBorder, cellBorder, sides, grays, 4);
        //[[NSColor colorWithDeviceWhite:0.17 alpha:1.0] set];
        [[NSColor controlBackgroundColor] set];
        NSRectFill(cellBorder);
    }
}


- (void)setupCache
{   NSRect  visibleRect;

    /* create the matrix cache window */
    visibleRect = [self visibleRect];
    matrixCache = [self sizeCache:matrixCache to:(visibleRect.size)];

    /* create the cell cache window */
    cellCache = [self sizeCache:cellCache to:[self cellSize]];
}
- (NSImage*)sizeCache:(NSImage*)cache to:(NSSize)newSize
{
    /* This is a workaround for [NSImage setSize:] on GNUstep. */
#ifdef GNUSTEP_BASE_VERSION
    if (cache)
    {   [cache release];
        cache = nil;
    }
#endif
    if (!cache)
        cache = [[FlippedImage alloc] initWithSize:newSize];
    /* make sure the cache window's the right size */
    else
    {   NSSize	size = [cache size];

        if (size.width != newSize.width || size.height != newSize.height)
            [cache setSize:newSize];
    }
    return cache;
}


- (void)setDelegate:(id)anObject
{
    delegate = anObject;
}


- delegate
{
    return delegate;
}

- (void)dealloc
{
    [matrixCache release];
    [cellCache release];

    [super dealloc];
}

@end