File: MyImageViewSelection.m

package info (click to toggle)
lynkeos.app 3.1%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 15,740 kB
  • sloc: objc: 36,412; ansic: 684; cpp: 148; sh: 68; makefile: 21
file content (692 lines) | stat: -rw-r--r-- 26,796 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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//
//  Lynkeos
//  $Id: MyImageViewSelection.m 589 2018-11-21 22:13:30Z j-etienne $
//
//  Created by Jean-Etienne LAMIAUD on Sat Nov 01 2003.
//  Copyright (c) 2003-2018. Jean-Etienne LAMIAUD
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// 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
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//

#import "MyImageView.h"

#define K_CURSRECT_SIZE 10

#define max(a,b) ((a) > (b) ? a : b )
#define min(a,b) ((a) > (b) ? b : a )

static const NSString
*crossImage = @"cross",
*leftImage = @"left",
*rightImage = @"right",
*topImage = @"top",
*bottomImage = @"bottom",
*topLeftImage = @"topLeft",
*topRightImage = @"topRight",
*bottomLeftImage = @"bottomLeft",
*bottomRightImage = @"bottomRight",
*insideImage = @"hand";

static const NSPoint
crossSpot = {8,8},
leftSpot = {1,7},
rightSpot = {14,7},
topSpot = {7,1},
bottomSpot = {7,14},
topLeftSpot = {1,1},
topRightSpot = {14,1},
bottomLeftSpot = {1,14},
bottomRightSpot = {14,14},
insideSpot = {8,8};

// Utility function to construct the new selection
static short processDrag( long mouse, u_short origin, u_short maximum, 
                          BOOL upright )
{
   long val;

   if ( upright )
      val = mouse > origin ? mouse : origin;
   else
      val = mouse > origin ? origin : mouse;

   if ( val < 0.0 )
      val = 0.0;
   if ( val > maximum )
      val = maximum;

   return( val );
}

@implementation MyImageSelection
- (id) initWithRect:(LynkeosIntegerRect)rect movable:(BOOL)move resizable:(BOOL)resize
{
   if ( (self = [self init]) != nil )
   {
      NSRect vide = { {0.0,0.0}, {0.0,0.0} };
      _rect = rect;

      _resizable = resize;
      _movable = move;

      _left = vide;
      _right = vide;
      _top = vide;
      _bottom = vide;
      _topLeft = vide;
      _topRight = vide;
      _bottomLeft = vide;
      _bottomRight = vide;
      _inside = vide;
   }

   return( self );
}

- (void) processCursorRect:(NSRect*)r cursor:(NSCursor*)cur size:(float)s
           horizontalOrder:(SelectionPosition_t)hPos
             verticalOrder:(SelectionPosition_t)vPos
                   visible:(NSRect)v view:(NSView*)view
{
   if ( hPos == PrecedingSide )
   {
      r->origin.x = (float)_rect.origin.x - s/2.0;
      r->size.width = s;
   }
   else if ( hPos == FollowingSide )
   {
      r->origin.x = (float)_rect.origin.x + (float)_rect.size.width - s/2.0;
      r->size.width = s;
   }
   else
   {
      r->origin.x = (float)_rect.origin.x + s/2.0;
      r->size.width = (float)_rect.size.width - s;
   }

   if ( vPos == PrecedingSide )
   {
      r->origin.y = (float)_rect.origin.y - s/2.0;
      r->size.height = s;
   }
   else if ( vPos == FollowingSide )
   {
      r->origin.y = (float)_rect.origin.y + (float)_rect.size.height - s/2.0;
      r->size.height = s;
   }
   else
   {
      r->origin.y = (float)_rect.origin.y + s/2.0;
      r->size.height = (float)_rect.size.height - s;
   }

   *r = NSIntersectionRect(*r,v);

   if ( r->size.width > 0.0 && r->size.height > 0.0 )
      [view addCursorRect:*r cursor:cur];
}

- (BOOL) isEqual:(id)anObj
{
   if ( ![anObj isKindOfClass:[MyImageSelection class]] )
      return( NO );

   LynkeosIntegerRect r = ((MyImageSelection*)anObj)->_rect;
   return(    _rect.origin.x    == r.origin.x
           && _rect.origin.y    == r.origin.y
           && _rect.size.width  == r.size.width
           && _rect.size.height == r.size.height );
}
@end

/*!
 * @category MyImageView(SelectionPrivate)
 * @abstract Selection management part of MyImageView
 */
@interface MyImageView(SelectionPrivate)

/*!
 * @abstract Management of the "off view" scrolling.
 * @discussion This method gets called when the mouse stands still outside the
 *    view to continue auto-scrolling.
 * @param timer The timer which calls this method.
 */
- (void) timerDrag :(NSTimer*)timer ;

@end

@implementation MyImageView(SelectionPrivate)

- (void) timerDrag :(NSTimer*)timer
{
   // Extract the drag event in userInfo, and call drag selector
   [self mouseDragged:[timer userInfo]];
}

@end

@implementation MyImageView(Selection)

- (void) getSelectionAtIndex:(SelectionIndex_t)index
                        rect:(LynkeosIntegerRect*)rect
                   resizable:(BOOL*)resize
                     movable:(BOOL*)move
{
   if ( [_selection count] > 0 )
   {
      MyImageSelection *sel = [_selection objectAtIndex:index];
      NSAssert( sel != nil, @"No selection in non nil selection list" );
      *rect = sel->_rect;
      *resize = sel->_resizable;
      *move = sel->_movable;
   }
   else
   {
      *rect = LynkeosMakeIntegerRect(0, 0, 0, 0);
      *resize = YES;
      *move = YES;
   }
}

- (void) initCursors
{
   _crossCursor = [[[NSCursor alloc] initWithImage:
                                     [NSImage imageNamed: (NSString*)crossImage]
                                           hotSpot:crossSpot] retain];
   _leftCursor = [[[NSCursor alloc] initWithImage:
                                       [NSImage imageNamed:(NSString*)leftImage]
                                          hotSpot:leftSpot] retain];
   _rightCursor = [[[NSCursor alloc] initWithImage:
                                      [NSImage imageNamed:(NSString*)rightImage]
                                           hotSpot:rightSpot] retain];
   _topCursor = [[[NSCursor alloc] initWithImage:
                                        [NSImage imageNamed:(NSString*)topImage]
                                         hotSpot:topSpot] retain];
   _bottomCursor = [[[NSCursor alloc] initWithImage:
                                     [NSImage imageNamed:(NSString*)bottomImage]
                                            hotSpot:bottomSpot] retain];
   _topLeftCursor = [[[NSCursor alloc] initWithImage:
                                    [NSImage imageNamed:(NSString*)topLeftImage]
                                             hotSpot:topLeftSpot] retain];
   _topRightCursor = [[[NSCursor alloc] initWithImage:
                                   [NSImage imageNamed:(NSString*)topRightImage]
                                              hotSpot:topRightSpot] retain];
   _bottomLeftCursor = [[[NSCursor alloc] initWithImage:
                                 [NSImage imageNamed:(NSString*)bottomLeftImage]
                                                hotSpot:bottomLeftSpot] retain];
   _bottomRightCursor = [[[NSCursor alloc] initWithImage:
                                [NSImage imageNamed:(NSString*)bottomRightImage]
                                               hotSpot:bottomRightSpot] retain];
   _insideCursor = [[[NSCursor alloc] initWithImage:
                                    [NSImage imageNamed:(NSString*)insideImage]
                                            hotSpot:insideSpot] retain];
}

-  (void) mouseDown:(NSEvent *)theEvent
{
   NSPoint o = [self convertPoint:[theEvent locationInWindow] fromView:nil];

   // Find the "clicked" selection and where
   NSEnumerator *selectionList = [_selection objectEnumerator];
   MyImageSelection *sel;
   SelectionIndex_t selIndex = 0;

   _draggingMode = SelNone;

   // Nothing to do if no selection is authorized
   if ( _selectionMode == NoSelection )
      return;

   while( _draggingMode == SelNone &&
          (sel = [selectionList nextObject]) != nil )
   {
      if ( [self mouse:o inRect:sel->_bottomLeft] )
      {
         _selectionOrigin.x = sel->_rect.origin.x + sel->_rect.size.width;
         _selectionOrigin.y = sel->_rect.origin.y + sel->_rect.size.height;
         _lastPoint = sel->_rect.origin;
         _draggingMode = SelNormal;
      }
      else if ( [self mouse:o inRect:sel->_bottomRight] )
      {
         _selectionOrigin.x = sel->_rect.origin.x;
         _selectionOrigin.y = sel->_rect.origin.y + sel->_rect.size.height;
         _lastPoint.x = sel->_rect.origin.x + sel->_rect.size.width;
         _lastPoint.y = sel->_rect.origin.y;
         _draggingMode = SelNormal;
      }
      else if ( [self mouse:o inRect:sel->_topLeft] )
      {
         _selectionOrigin.x = sel->_rect.origin.x + sel->_rect.size.width;
         _selectionOrigin.y = sel->_rect.origin.y;
         _lastPoint.x = sel->_rect.origin.x;
         _lastPoint.y = sel->_rect.origin.y + sel->_rect.size.height;
         _draggingMode = SelNormal;
      }
      else if ( [self mouse:o inRect:sel->_topRight] )
      {
         _selectionOrigin = sel->_rect.origin;
         _lastPoint.x = sel->_rect.origin.x + sel->_rect.size.width;
         _lastPoint.y = sel->_rect.origin.y + sel->_rect.size.height;
         _draggingMode = SelNormal;
      }
      else if ( [self mouse:o inRect:sel->_left] )
      {
         _selectionOrigin.x = sel->_rect.origin.x + sel->_rect.size.width;
         _selectionOrigin.y = sel->_rect.origin.y;
         _lastPoint.x = sel->_rect.origin.x;
         _lastPoint.y = sel->_rect.origin.y + sel->_rect.size.height;
         _draggingMode = SelH;
      }
      else if ( [self mouse:o inRect:sel->_right] )
      {
         _selectionOrigin = sel->_rect.origin;
         _lastPoint.x = sel->_rect.origin.x + sel->_rect.size.width;
         _lastPoint.y = sel->_rect.origin.y + sel->_rect.size.height;
         _draggingMode = SelH;
      }
      else if ( [self mouse:o inRect:sel->_bottom] )
      {
         _selectionOrigin.x = sel->_rect.origin.x;
         _selectionOrigin.y = sel->_rect.origin.y + sel->_rect.size.height;
         _lastPoint.x = sel->_rect.origin.x + sel->_rect.size.width;
         _lastPoint.y = sel->_rect.origin.y;
         _draggingMode = SelV;
      }
      else if ( [self mouse:o inRect:sel->_top] )
      {
         _selectionOrigin = sel->_rect.origin;
         _lastPoint.x = sel->_rect.origin.x + sel->_rect.size.width;
         _lastPoint.y = sel->_rect.origin.y + sel->_rect.size.height;
         _draggingMode = SelV;
      }
      else if ( [self mouse:o inRect:sel->_inside] )
      {
         _selectionOrigin = sel->_rect.origin;
         _lastPoint = LynkeosIntegerPointFromNSPoint(o);
         _draggingMode = SelMove;
      }
      else
         selIndex++;
   }

   if ( _draggingMode == SelNone )
   {
      // Replace the current selection, if any, or set the single one
      if ( _selectionMode == MultiSelection ||
           (_selectionMode == SingleSelection &&
              ([_selection count] == 0 ||
               ((MyImageSelection*)[_selection objectAtIndex:0])->_resizable)) )
      {
         _selectionOrigin = LynkeosIntegerPointFromNSPoint(o);
         LynkeosIntegerRect r = { _selectionOrigin, {0,0} };
         _lastPoint = _selectionOrigin;
         _inProgressSelection = r;
         _draggingMode = SelNormal;
         if ( _selectionMode != MultiSelection || _currentSelectionIndex == NSNotFound )
            // Modify the only selection
            _currentSelectionIndex = 0;
      }
   }
   else
   {
      NSAssert( sel != nil, @"Modification of a nonexistent selection" );
      _currentSelectionIndex = selIndex;
      _inProgressSelection = sel->_rect;
   }

   if ( _draggingMode != SelNone )
      [self setNeedsDisplay:YES];
}

-  (void) mouseDragged:(NSEvent *)theEvent
{
   NSPoint where = [self convertPoint:[theEvent locationInWindow] fromView:nil];
   NSRect inval;
   LynkeosIntegerRect prevSelection;
   float delta;

   // Cancel the autoscroll timer if a regular drag happens
   if ( _autoscrollTimer != nil && [_autoscrollTimer isValid] && 
        [_autoscrollTimer userInfo] != theEvent )
      [_autoscrollTimer invalidate];
   // One way or the other, _autoscrollTimer validity ends at this selector
   _autoscrollTimer = nil;

   if ( _draggingMode != SelNone  &&
        ((u_short)where.x != _lastPoint.x || (u_short)where.y != _lastPoint.y) )
   {
      //Adjust scroll to follow mouse if needed
      [self autoscroll:theEvent];

      prevSelection = _inProgressSelection;

      switch ( _draggingMode )
      {
         case SelNormal :
            _inProgressSelection.origin.x =
                            processDrag( where.x, _selectionOrigin.x, 
                                          _canvasRect.size.width, NO );
            _inProgressSelection.size.width =
                            processDrag( where.x, _selectionOrigin.x, 
                                         _canvasRect.size.width, YES )
                            - _inProgressSelection.origin.x;
            _inProgressSelection.origin.y =
                            processDrag( where.y, _selectionOrigin.y, 
                                         _canvasRect.size.height, NO );
            _inProgressSelection.size.height =
                            processDrag( where.y, _selectionOrigin.y, 
                                         _canvasRect.size.height, YES )
                            - _inProgressSelection.origin.y;
            break;
         case SelH :
            _inProgressSelection.origin.x =
                                       processDrag( where.x, _selectionOrigin.x,
                                                    _canvasRect.size.width, NO );
            _inProgressSelection.size.width =
                                       processDrag( where.x, _selectionOrigin.x,
                                                    _canvasRect.size.width, YES )
                                       - _inProgressSelection.origin.x;
            break;
         case SelV :
            _inProgressSelection.origin.y =
                                       processDrag( where.y, _selectionOrigin.y,
                                                    _canvasRect.size.height, NO );
            _inProgressSelection.size.height =
                                       processDrag( where.y, _selectionOrigin.y,
                                                    _canvasRect.size.height, YES )
                                       - _inProgressSelection.origin.y;
            break;
         case SelMove :
            delta = where.x - _lastPoint.x;
            if ( delta < - _inProgressSelection.origin.x )
               delta = -_inProgressSelection.origin.x;
            else if ( delta > _canvasRect.size.width - _inProgressSelection.origin.x
                              - _inProgressSelection.size.width )
               delta = _canvasRect.size.width - _inProgressSelection.origin.x
                       - _inProgressSelection.size.width;
            _inProgressSelection.origin.x += delta;
            delta = where.y - _lastPoint.y;
            if ( delta < -_inProgressSelection.origin.y )
               delta = -_inProgressSelection.origin.y;
            else if ( delta > _canvasRect.size.height - _inProgressSelection.origin.y
                              - _inProgressSelection.size.height )
               delta = _canvasRect.size.height - _inProgressSelection.origin.y
                       - _inProgressSelection.size.height;
            _inProgressSelection.origin.y += delta;
            break;
         default:
            break;
      }

      // Invalidate what needs to be redrawn
      if ( _inProgressSelection.origin.x != prevSelection.origin.x )
      {
         inval.origin.x = min( _inProgressSelection.origin.x,
                               prevSelection.origin.x )
                          - 1.0/_zoom;
         inval.size.width = max( _inProgressSelection.origin.x,
                                 prevSelection.origin.x )
                            - inval.origin.x + 1/_zoom;
         inval.origin.y = min( _inProgressSelection.origin.y,
                               prevSelection.origin.y )
                          - 1.0/_zoom;
         inval.size.height = max( _inProgressSelection.origin.y
                                  + _inProgressSelection.size.height,
                                  prevSelection.origin.y
                                  + prevSelection.size.height )
                             - inval.origin.y + 1/_zoom;
         [self setNeedsDisplayInRect:inval];
      }
      if ( _inProgressSelection.origin.x + _inProgressSelection.size.width !=
                             prevSelection.origin.x + prevSelection.size.width )
      {
         inval.origin.x = min( _inProgressSelection.origin.x
                               + _inProgressSelection.size.width,
                               prevSelection.origin.x
                               + prevSelection.size.width )
                          - 1.0/_zoom;
         inval.size.width = max( _inProgressSelection.origin.x
                                 + _inProgressSelection.size.width,
                                 prevSelection.origin.x
                                 + prevSelection.size.width )
                            - inval.origin.x + 1/_zoom;
         inval.origin.y = min( _inProgressSelection.origin.y,
                               prevSelection.origin.y )
                          - 1.0/_zoom;
         inval.size.height = max( _inProgressSelection.origin.y
                                  + _inProgressSelection.size.height,
                                  prevSelection.origin.y
                                  + prevSelection.size.height )
                           - inval.origin.y + 1/_zoom;
         [self setNeedsDisplayInRect:inval];
      }
      if ( _inProgressSelection.origin.y != prevSelection.origin.y )
      {
         inval.origin.x = min( _inProgressSelection.origin.x,
                               prevSelection.origin.x )
                          - 1.0/_zoom;
         inval.size.width = max( _inProgressSelection.origin.x
                                 + _inProgressSelection.size.width,
                                 prevSelection.origin.x
                                 + prevSelection.size.width )
                            - inval.origin.x + 1/_zoom;
         inval.origin.y = min( _inProgressSelection.origin.y,
                               prevSelection.origin.y )
                          - 1.0/_zoom;
         inval.size.height = max( _inProgressSelection.origin.y,
                                  prevSelection.origin.y )
                             - inval.origin.y + 1/_zoom;
         [self setNeedsDisplayInRect:inval];
      }
      if ( _inProgressSelection.origin.y + _inProgressSelection.size.height !=
                            prevSelection.origin.y + prevSelection.size.height )
      {
         inval.origin.x = min( _inProgressSelection.origin.x,
                               prevSelection.origin.x )
                          - 1.0/_zoom;
         inval.size.width = max( _inProgressSelection.origin.x
                                 + _inProgressSelection.size.width,
                                 prevSelection.origin.x
                                 + prevSelection.size.width )
                            - inval.origin.x + 1/_zoom;
         inval.origin.y = min( _inProgressSelection.origin.y
                               + _inProgressSelection.size.height,
                               prevSelection.origin.y
                               + prevSelection.size.height )
                          - 1.0/_zoom;
         inval.size.height = max( _inProgressSelection.origin.y
                                  + _inProgressSelection.size.height,
                                  prevSelection.origin.y
                                  + prevSelection.size.height )
                             - inval.origin.y + 1/_zoom;
         [self setNeedsDisplayInRect:inval];
      }
      _lastPoint = LynkeosIntegerPointFromNSPoint(where);
   }

   // Arm a timer for autoscrolling while mouse stands still
   if ( _draggingMode != SelNone )
   {
      _autoscrollTimer = [NSTimer timerWithTimeInterval:0.1 target:self 
                                               selector:@selector(timerDrag:)
                                               userInfo: theEvent
                                                repeats:NO];
      [[NSRunLoop currentRunLoop] addTimer:_autoscrollTimer 
                                   forMode:NSDefaultRunLoopMode];
   }
}

-  (void) mouseUp:(NSEvent *)theEvent
{
   // Act on current selection, except if shift was pressed to add a new selection
   _modifiers = [theEvent modifierFlags];
   if (_selectionMode == MultiSelection && (_modifiers & NSShiftKeyMask) != 0)
   {
      _previousSelectionIndex = _currentSelectionIndex;
      _currentSelectionIndex = [_selection count];
   }

   if ( _draggingMode != SelNone )
   {
      // Process "clicks"
      NSPoint o = [self convertPoint:[theEvent locationInWindow] fromView:nil];
      const SelectionIndex_t nSel = [_selection count];

      if ( _draggingMode == SelNormal && _currentSelectionIndex == nSel &&
           (u_short)o.x == _selectionOrigin.x &&
           (u_short)o.y == _selectionOrigin.y )
      {
         if ( nSel == 0 )
         {
            // A click without any selection yields the entire image
            _inProgressSelection.origin.x = 0;
            _inProgressSelection.origin.y = 0;
            _inProgressSelection.size =
               LynkeosIntegerSizeFromNSSize(_canvasRect.size);
         }

         else
         {
            // A click outside all selections removes the current selection
            _inProgressSelection.origin.x = 0;
            _inProgressSelection.origin.y = 0;
            _inProgressSelection.size.width = 0;
            _inProgressSelection.size.height = 0;
            [self removeSelectionAtIndex:_previousSelectionIndex];
         }
      }

      // Correct it if needed
      if ( _selectionDelegate != nil &&
           (_inProgressSelection.size.width != 0
            || _inProgressSelection.size.height != 0) )
      {
         if ( ![_selectionDelegate validateSelection:&_inProgressSelection
                                             atIndex:_currentSelectionIndex] )
         {
            _inProgressSelection.origin.x = 0;
            _inProgressSelection.origin.y = 0;
            _inProgressSelection.size.width = 0;
            _inProgressSelection.size.height = 0;
         }
      }

      // Set the new selection
      if (    _inProgressSelection.size.width != 0
           || _inProgressSelection.size.height != 0 )
      {
         MyImageSelection *sel;

         if ( _currentSelectionIndex < [_selection count] )
         {
            sel = [_selection objectAtIndex:_currentSelectionIndex];
            sel->_rect = _inProgressSelection;
         }
         else
         {
            sel =
               [[[MyImageSelection alloc] initWithRect:_inProgressSelection
                                        movable:YES resizable:YES] autorelease];
            [_selection addObject:sel];
         }

         _inProgressSelection.size.width = 0;
         _inProgressSelection.size.height = 0;

         // Notify
         _isNotifying = YES;
         [[NSNotificationCenter defaultCenter] postNotificationName:
                              LynkeosImageViewSelectionRectDidChangeNotification
                                                             object: self
                                                           userInfo:
                              [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithUnsignedInteger: _currentSelectionIndex],
                                 LynkeosImageViewSelectionRectIndex,
                                 nil]];
         _isNotifying = NO;

      }

      _draggingMode = SelNone;
      _previousSelectionIndex = NSNotFound;

      // Delete any running autoscroll timer
      if ( _autoscrollTimer != nil && [_autoscrollTimer isValid] )
      {
         [_autoscrollTimer invalidate];
         _autoscrollTimer = nil;
      }

      // Set up all the cursor rectangles
      [[self window] invalidateCursorRectsForView:self];

      [self setNeedsDisplay:YES];
   }
}

- (void)resetCursorRects
{
   NSRect v = [self visibleRect];
   float s = K_CURSRECT_SIZE/_zoom;

   NSEnumerator *selectionList = [_selection objectEnumerator];
   MyImageSelection *sel;

   while ( (sel = [selectionList nextObject]) != nil )
   {
      if ( sel->_rect.size.width != 0 && sel->_rect.size.height != 0 )
      {
         if ( sel->_resizable )
         {
            [sel processCursorRect:&sel->_bottomLeft cursor:_bottomLeftCursor
                              size:s
                   horizontalOrder:PrecedingSide verticalOrder:PrecedingSide
                           visible:v view:self];
            [sel processCursorRect:&sel->_left cursor:_leftCursor size:s
                   horizontalOrder:PrecedingSide verticalOrder:MiddlePosition
                           visible:v view:self];
            [sel processCursorRect:&sel->_topLeft cursor:_topLeftCursor size:s
                   horizontalOrder:PrecedingSide verticalOrder:FollowingSide
                           visible:v view:self];
            [sel processCursorRect:&sel->_top cursor:_topCursor size:s
                   horizontalOrder:MiddlePosition verticalOrder:FollowingSide
                           visible:v view:self];
            [sel processCursorRect:&sel->_topRight cursor:_topRightCursor size:s
                   horizontalOrder:FollowingSide verticalOrder:FollowingSide
                           visible:v view:self];
            [sel processCursorRect:&sel->_right cursor:_rightCursor size:s
                   horizontalOrder:FollowingSide verticalOrder:MiddlePosition
                           visible:v view:self];
            [sel processCursorRect:&sel->_bottomRight cursor:_bottomRightCursor
                              size:s
                   horizontalOrder:FollowingSide verticalOrder:PrecedingSide
                           visible:v view:self];
            [sel processCursorRect:&sel->_bottom cursor:_bottomCursor size:s
                   horizontalOrder:MiddlePosition verticalOrder:PrecedingSide
                           visible:v view:self];
         }
         if ( sel->_movable )
            [sel processCursorRect:&sel->_inside cursor:_insideCursor size:s
                   horizontalOrder:MiddlePosition verticalOrder:MiddlePosition
                           visible:v view:self];
      }
   }

   if ( v.size.width != 0 && v.size.height != 0 )
      [self addCursorRect:v cursor:_crossCursor];
}

@end