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 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
|
/*
Copyright (c) 1998, 1999, 2000, 2001, 2003, 2004 Benhur Stein
This file is part of Paj.
Paj is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
Paj 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 Lesser General Public License
for more details.
You should have received a copy of the GNU Lesser General Public License
along with Paj; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
#include "HierarchyRuler.h"
#include "../General/Macros.h"
#define SHOW_TYPES
#define CEIL(f) (1000-((int)(1000-(f))))
#define CEIL2(f) (1000-((int)(999.5-(f))))
@interface VCell : NSCell
@end
@implementation VCell
- (void) _drawAttributedText: (NSAttributedString*)aString
inFrame: (NSRect)aRect
{
NSSize titleSize;
NSRect newRect;
if (aString == nil || NSWidth(aRect) < 5 || NSHeight(aRect) < 5) {
return;
}
titleSize = [aString size];
#if defined __MINGW32__
if (YES) {
#else
if (NSWidth(aRect) > NSHeight(aRect)) {
#endif
if (titleSize.width < aRect.size.width) {
newRect.origin.x = NSMidX(aRect) - titleSize.width/2;
newRect.origin.y = NSMidY(aRect) - titleSize.height/2;
newRect.size = titleSize;
//newRect.size.width = aRect.size.height;
} else {
newRect.origin.x = NSMinX(aRect);
newRect.size.width = aRect.size.width + 1;
newRect.size.height = titleSize.height
* CEIL(titleSize.width / newRect.size.width);
if (newRect.size.height > aRect.size.height) {
newRect.size.height = aRect.size.height;
}
newRect.origin.y = NSMidY(aRect) - newRect.size.height/2;
}
[aString drawInRect: newRect];
} else {
if (titleSize.width < aRect.size.height) {
newRect.origin.x = NSMidX(aRect) - titleSize.height/2;
newRect.origin.y = NSMidY(aRect) + titleSize.width/2;
newRect.size = titleSize;
newRect.size.width += 6;
} else {
newRect.origin.y = NSMaxY(aRect);
newRect.size.width = aRect.size.height + 1;
newRect.size.height = titleSize.height
* CEIL2(titleSize.width / newRect.size.width);
if (newRect.size.height > aRect.size.width) {
newRect.size.height = aRect.size.width;
}
newRect.origin.x = NSMidX(aRect) - newRect.size.height/2;
}
NSAffineTransform *transf;
transf = [NSAffineTransform transform];
[transf translateXBy: newRect.origin.x yBy: newRect.origin.y];
[transf rotateByDegrees:-90];
[transf concat];
newRect.origin = NSMakePoint(0, 0);
[aString drawInRect: newRect];
[transf invert];
[transf concat];
}
}
- (void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
{
if ([controlView window] == nil) {
return;
}
cellFrame = [self drawingRectForBounds: cellFrame];
[self _drawAttributedText: [self attributedStringValue]
inFrame: cellFrame];
}
@end
@interface HierarchyRuler(Private)
- (id)instanceAtPoint:(NSPoint)point
ofType:(PajeContainerType *)containerType
inContainer:(PajeContainer *)container
level:(int)level;
- (id)instanceAtPoint:(NSPoint)point
inInstance:(id)instance
ofType:(PajeContainerType *)containerType
level:(int)level;
- (id)instanceAtPoint:(NSPoint)point;
- (void)drawContainer:(PajeContainer *)container
layout:(STContainerTypeLayout *)layout
inRect:(NSRect)drawRect
level:(int)level
offset:(float)offset;
- (void)drawContainerLayout:(STContainerTypeLayout *)layout
inContainer:(PajeContainer *)container
inRect:(NSRect)drawRect
level:(int)level
offset:(float)offset;
@end
@implementation HierarchyRuler
- (id)initWithScrollView:(NSScrollView *)scrollView
controller:(STController *)c;
{
int i;
self = [super initWithScrollView:scrollView orientation:NSVerticalRuler];
if (self == nil)
return self;
controller = c;
thicknesses = [[[NSUserDefaults standardUserDefaults]
arrayForKey:@"HierarchyRulerThicknesses"]
mutableCopy];
if (thicknesses == nil) {
thicknesses = [[NSMutableArray arrayWithObjects:
[NSNumber numberWithFloat:0.],
[NSNumber numberWithFloat:40.],
[NSNumber numberWithFloat:60.],
nil] retain];
}
for (i = 0; i < [thicknesses count]; i++) {
id obj;
obj = [thicknesses objectAtIndex:i];
if (![obj isKindOfClass:[NSNumber class]]) {
obj = [NSNumber numberWithFloat:[obj floatValue]];
[thicknesses replaceObjectAtIndex:i withObject:obj];
}
}
cell = [[VCell alloc] initTextCell:@""];
[cell setWraps:YES];
[cell setBordered:YES];
[cell setFont:[NSFont systemFontOfSize:10.0]];
vcell = [[VCell alloc] initTextCell:@""];
[vcell setWraps:YES];
[vcell setBordered:YES];
[vcell setFont:[NSFont systemFontOfSize:10.0]];
return self;
}
- (void)dealloc
{
[thicknesses release];
[cell release];
[super dealloc];
}
- (void)setWidth:(float)width forLevel:(int)level
{
if (width < 0.0) width = 0.0;
while (level > [thicknesses count]-1) {
[thicknesses addObject:[NSNumber numberWithFloat:50.0]];
}
[thicknesses replaceObjectAtIndex:level
withObject:[NSNumber numberWithFloat:width]];
[[NSUserDefaults standardUserDefaults]
setObject:thicknesses
forKey:@"HierarchyRulerThicknesses"];
}
- (float)widthForLevel:(int)level
{
if (level > maxLevel) maxLevel = level;
if (level >= [thicknesses count]) {
[self setWidth:50 forLevel:level];
}
return [[thicknesses objectAtIndex:level] floatValue];
}
- (float)positionForLevel:(int)level
{
int i;
float pos = 0.0;
for (i=0; i<level; i++) {
pos += [self widthForLevel:i];
}
return pos;
}
- (int)indexForPosition:(float)position
{
int i = 0;
float pos = 0.0;
int index = -1;
do {
pos += [self widthForLevel:i];
i = i + 1;
if (position > pos-2 && position < pos+2) {
index = i;
}
} while (pos < position+3);
return index;
}
- (int)levelForPosition:(float)position
{
int i = 0;
float pos = 0.0;
do {
pos += [self widthForLevel:i];
i = i + 1;
} while (pos < position);
return i-1;
}
- (hierarchyRulerPart)hitPart:(NSPoint)position
{
int i = 0;
float pos = 0.0;
do {
pos += [self widthForLevel:i];
i = i + 1;
if (position.x > pos-2 && position.x < pos+2) {
return hierarchyRulerVerticalDivider;
}
if (position.x < pos) {
return hierarchyRulerBox;
}
} while (pos < position.x+3);
return hierarchyRulerNone;
}
- (void)refreshSizes
{
if (maxLevel == 0) maxLevel = 1;
[self setRuleThickness:[self positionForLevel:maxLevel+1]];//[thicknesses count]/* - 1*/]];
[[self enclosingScrollView] tile];
}
- (void)drawHashMarksAndLabelsInRect:(NSRect)drawRect
forVariableLayout:(STVariableTypeLayout *)layout
{
float vMin;
float vMax;
float yScale;
float yOffset;
PajeEntityType *entityType;
entityType = [layout entityType];
vMin = [layout minValue];
vMax = [layout maxValue];
if (vMax <= vMin) {
vMin = [controller minValueForEntityType:entityType];
vMax = [controller maxValueForEntityType:entityType];
}
if (vMin != vMax) {
yScale = -([layout height] - 4) / (vMax - vMin);
} else {
yScale = 1;
}
yOffset = NSMinY(drawRect) + 2 - (vMax * yScale);
//y = [filter valueForEntity:entity] * yScale + yOffset;
[[NSColor controlColor] set];
//NSRectFill(drawRect);
NSBezierPath *path;
path = [NSBezierPath bezierPath];
[path moveToPoint:NSMakePoint(NSMaxX(drawRect), NSMinY(drawRect) + 2)];
[path lineToPoint:NSMakePoint(NSMaxX(drawRect), NSMaxY(drawRect) - 2)];
NSFont *font = [NSFont systemFontOfSize: [NSFont smallSystemFontSize]];
NSDictionary *attr = [[NSDictionary alloc] initWithObjectsAndKeys:
font, NSFontAttributeName,
[NSColor blackColor], NSForegroundColorAttributeName,
nil];
NSEnumerator *en = [[layout hashMarkValues] objectEnumerator];
NSNumber *n;
while ((n = [en nextObject]) != nil) {
NSSize size;
NSString *str;
NSString *hashValueFormat = [layout hashValueFormat];
float v = [n floatValue];
float y = v * yScale + yOffset;
float x;
[path moveToPoint:NSMakePoint(NSMaxX(drawRect) - 4, y)];
[path lineToPoint:NSMakePoint(NSMaxX(drawRect), y)];
str = [NSString stringWithFormat:hashValueFormat, v];
size = [str sizeWithAttributes:attr];
x = NSMaxX(drawRect) - size.width;
if (y > NSMinY(drawRect) + size.height + 1) {
[str drawAtPoint:NSMakePoint(x, y - size.height + 2)
withAttributes:attr];
} else {
[str drawAtPoint:NSMakePoint(x, y + 1)
withAttributes:attr];
}
}
[[NSColor blackColor] set];
[path stroke];
[attr release];
}
- (void)drawHashMarksAndLabelsInRect:(NSRect)drawRect
forContainerLayout:(STContainerTypeLayout *)layout
{
float vMin;
float vMax;
float yScale;
float yOffset;
PajeEntityType *entityType;
entityType = [layout entityType];
vMin = [layout minValue];
vMax = [layout maxValue];
if (vMax <= vMin) {
return;
}
yScale = -([layout heightForVariables] - 4) / (vMax - vMin);
yOffset = NSMinY(drawRect) + 2 - (vMax * yScale)
+ [layout variablesOffset] ;
//y = [filter valueForEntity:entity] * yScale + yOffset;
[[NSColor controlColor] set];
//NSRectFill(drawRect);
NSBezierPath *path;
path = [NSBezierPath bezierPath];
[path moveToPoint:NSMakePoint(NSMaxX(drawRect), NSMinY(drawRect) + 2)];
[path lineToPoint:NSMakePoint(NSMaxX(drawRect), NSMaxY(drawRect) - 2)];
NSFont *font = [NSFont systemFontOfSize: [NSFont smallSystemFontSize]];
NSDictionary *attr = [[NSDictionary alloc] initWithObjectsAndKeys:
font, NSFontAttributeName,
[NSColor blackColor], NSForegroundColorAttributeName,
nil];
NSEnumerator *en = [[layout hashMarkValues] objectEnumerator];
NSNumber *n;
while ((n = [en nextObject]) != nil) {
NSSize size;
NSString *str;
NSString *hashValueFormat = [layout hashValueFormat];
float v = [n floatValue];
float y = v * yScale + yOffset;
float x;
[path moveToPoint:NSMakePoint(NSMaxX(drawRect) - 4, y)];
[path lineToPoint:NSMakePoint(NSMaxX(drawRect), y)];
str = [NSString stringWithFormat:hashValueFormat, v];
size = [str sizeWithAttributes:attr];
x = NSMaxX(drawRect) - size.width;
if (y > NSMinY(drawRect) + size.height + 1) {
[str drawAtPoint:NSMakePoint(x, y - size.height + 2)
withAttributes:attr];
} else {
[str drawAtPoint:NSMakePoint(x, y + 1)
withAttributes:attr];
}
}
[[NSColor blackColor] set];
[path stroke];
[attr release];
}
- (void)drawHashMarksAndLabelsInRect:(NSRect)drawRect
{
int x;
x = maxLevel;//[thicknesses count];
maxLevel = 0;
[self drawContainer:[controller rootInstance]
layout:[controller rootLayout]
inRect:drawRect
level:0
offset:NSMinY([[self clientView] visibleRect])];
if (x != maxLevel/*[thicknesses count]*/) {
[self performSelector:@selector(refreshSizes) withObject:nil afterDelay:0.0];
}
}
- (void)drawContainer:(PajeContainer *)container
layout:(STContainerTypeLayout *)layout
inRect:(NSRect)drawRect
level:(int)level
offset:(float)offset
{
NSEnumerator *sublayoutEnum;
STContainerTypeLayout *sublayout;
if ([layout heightForVariables] > 10) {
NSRect r = [layout rectOfInstance:container];
r.origin.x = NSMinX(drawRect);
r.origin.y -= offset;
r.size.width = NSMaxX([self bounds]) - r.origin.x - 1;
[self drawHashMarksAndLabelsInRect:r forContainerLayout:layout];
}
sublayoutEnum = [[layout subtypes] objectEnumerator];
while ((sublayout = [sublayoutEnum nextObject]) != nil) {
[self drawContainerLayout:sublayout
inContainer:container
inRect:drawRect
level:level
offset:offset];
}
}
- (void)drawContainerLayout:(STContainerTypeLayout *)layout
inContainer:(PajeContainer *)container
inRect:(NSRect)drawRect
level:(int)level
offset:(float)offset
{
NSRect r;
#ifndef SHOW_TYPES
if (![layout isContainer]) {
return;
}
#endif
r = [layout rectInContainer:container];
r.origin.y -= offset;
//r.size.height += 1;
r.origin.x = drawRect.origin.x;
r.size.width = drawRect.size.width;
if (!NSIsEmptyRect(r /*NSIntersectionRect(r, drawRect)*/)) {
NSEnumerator *ienum;
id instance;
#ifdef SHOW_TYPES
if (r.size.width > 2 && [layout drawingType] != PajeLinkDrawingType
&& [layout drawingType] != PajeVariableDrawingType) {
[vcell setStringValue:[layout description]];
r.origin.x = [self positionForLevel:level] - 1;
if ([layout isContainer]) {
r.size.width = [self widthForLevel:level] /*+ 1*/;
} else {
[self widthForLevel:level];
r.size.width = NSMaxX([self bounds]) - r.origin.x - 1;
}
[vcell drawWithFrame:NSInsetRect(r, -.5, -.5) inView:self];
}
if (r.size.width > 2 && [layout drawingType] == PajeVariableDrawingType) {
[self widthForLevel:level];
//r.size.width = NSMaxX([self bounds]) - r.origin.x - 1;
//[self drawHashMarksAndLabelsInRect:r forVariableLayout:layout];
}
if (![layout isContainer]) return;
level++;
#endif
// check all instances on this hierarchy
ienum = [controller enumeratorOfContainersTyped:[layout entityType]
inContainer:container];
while ((instance = [ienum nextObject]) != nil) {
r = [layout rectOfInstance:instance];
r.origin.y -= offset;
//r.size.height += 1;
r.origin.x = [self positionForLevel:level] - 1;
r.size.width = [self widthForLevel:level] /*+ 1*/;
if (!NSIsEmptyRect(r /*NSIntersectionRect(r, drawRect)*/)) {
if (r.size.width > 2) {
if (containerBeingDragged != nil
&& [instance isEqual:containerBeingDragged]) {
NSDrawWhiteBezel(NSInsetRect(r, -.5, -.5),
NSInsetRect(r, -.5, -.5));
} else if ([[controller selectedContainers]
containsObject:instance]) {
[[NSColor whiteColor] set];
NSRectFill(r);
}
[cell setStringValue:[instance name/*description*/]];
[cell drawWithFrame:NSInsetRect(r, -.5, -.5) inView:self];
}
}
[self drawContainer:instance
layout:layout
inRect:drawRect
level:level+1
offset:offset];
}
}
}
- (id)instanceAtPoint:(NSPoint)point
ofType:(PajeContainerType *)containerType
inContainer:(PajeContainer *)container
level:(int)level
{
STEntityTypeLayout *layoutDescriptor;
NSRect r;
float position = [self positionForLevel:level];
float width = [self widthForLevel:level];
if (point.x < position) {
return nil;
}
layoutDescriptor = [controller descriptorForEntityType:containerType];
if (![layoutDescriptor isContainer]) {
return nil;
}
r = [layoutDescriptor rectInContainer:container];
if (point.y >= NSMinY(r) && point.y <= NSMaxY(r)) {
NSEnumerator *ienum;
id instance;
// check all instances on this hierarchy
ienum = [controller enumeratorOfContainersTyped:containerType
inContainer:container];
while ((instance = [ienum nextObject]) != nil) {
if (point.x < position+width) {
r = [(STContainerTypeLayout *)layoutDescriptor rectOfInstance:instance];
//r.size.height += 1;
r.origin.x = position - 1;
r.size.width = width /*+ 1*/;
if (NSMouseInRect(point, r, [self isFlipped])) {
return instance;
}
} else {
id subinstance;
subinstance = [self instanceAtPoint:point
inInstance:instance
ofType:containerType
level:level+1];
if (subinstance != nil)
return subinstance;
}
}
}
return nil;
}
- (id)instanceAtPoint:(NSPoint)point
inInstance:(id)instance
ofType:(PajeContainerType *)containerType
level:(int)level
{
NSArray *subtypes;
NSEnumerator *subtypeEnum;
PajeContainerType *subtype;
id foundInstance = nil;
subtypes = [controller containedTypesForContainerType:containerType];
subtypeEnum = [subtypes objectEnumerator];
while ((subtype = [subtypeEnum nextObject]) != nil) {
foundInstance = [self instanceAtPoint:point
ofType:subtype
inContainer:instance
#ifdef SHOW_TYPES
level:level+1];
#else
level:level];
#endif
if (foundInstance != nil) {
break;
}
}
return foundInstance;
}
- (id)instanceAtPoint:(NSPoint)point
{
float offset;
PajeContainer *root;
PajeContainerType *rootType;
offset = NSMinY([[self clientView] visibleRect]);
point.y += offset;
root = [controller rootInstance];
rootType = (PajeContainerType *)[controller entityTypeForEntity:root];
return [self instanceAtPoint:point
inInstance:root
ofType:rootType
level:0];
}
- (void)trackVerticalDivider:(NSEvent *)event
{
NSPoint mouseDownLocation;
NSPoint currentMouseLocation;
int mask = NSLeftMouseUpMask | NSLeftMouseDraggedMask;
int indexBeingDragged;
int deltaPosition;
int originalWidth;
int currentWidth;
mouseDownLocation = [self convertPoint:[event locationInWindow]
fromView:nil];
indexBeingDragged = [self indexForPosition:mouseDownLocation.x];
if (indexBeingDragged == -1) {
return;
}
originalWidth = [self widthForLevel:indexBeingDragged - 1];
[self lockFocus];
do {
event = [NSApp nextEventMatchingMask: mask
untilDate: [NSDate distantFuture]
inMode: NSEventTrackingRunLoopMode
dequeue: YES];
currentMouseLocation = [self convertPoint:[event locationInWindow]
fromView:nil];
deltaPosition = currentMouseLocation.x - mouseDownLocation.x;
currentWidth = originalWidth + deltaPosition;
if (currentWidth < 0) currentWidth = 0;
[self setWidth:currentWidth
forLevel:indexBeingDragged - 1];
[self refreshSizes];
} while ([event type] != NSLeftMouseUp);
[self unlockFocus];
}
- (void)trackHorizontalDivider:(NSEvent *)event
{
}
/** scroll client view if necessary, to make r visible.
* r has x coordinates in ruler, y in client view.
* return YES if scroll was made, NO if rect was already visible
*/
- (BOOL)_makeRectVisible:(NSRect) r
{
id drawView = [self clientView];
NSRect visible = [drawView visibleRect];
r.origin.x = NSMinX(visible);
r.size.width = 1;
if (!NSEqualRects(visible, NSUnionRect(visible, r))) {
[drawView scrollRectToVisible:r];
[self displayIfNeeded];
return YES;
}
return NO;
}
- (NSImage *)_getImageInRect:(NSRect)rect
{
NSCachedImageRep *imageRep;
NSImage *image;
NSRect imageRect;
NSRect windowRect;
imageRect.origin = NSZeroPoint;
imageRect.size = rect.size;
imageRep = [[NSCachedImageRep alloc] initWithWindow:nil rect:imageRect];
[[[imageRep window] contentView] lockFocus];
windowRect = rect;
windowRect.origin.y -= NSMinY([[self clientView] visibleRect]);
windowRect = [self convertRect:windowRect toView:nil];
NSCopyBits([[self window] gState], windowRect, NSZeroPoint);
[[[imageRep window] contentView] unlockFocus];
image = [[NSImage alloc] initWithSize:rect.size];
[image lockFocus];
[imageRep draw];
[image unlockFocus];
[imageRep release];
return [image autorelease];
}
- (void)trackBox:(NSEvent *)event
{
NSPoint mouseDownLocation;
float mouseDownYInView;
float currentMouseYInView;
NSRect limitRect;
NSRect instanceRect;
NSRect currentRect;
STEntityTypeLayout *layoutDescriptor;
DrawView *drawView;
//PajeFilter *filter;
PajeEntityType *entityType;
PajeContainer *container;
float minX;
float width;
float minY;
float maxY;
int indexBeingDragged;
id clickedContainer;
PajeContainer *droppedInstance;
NSImage *dragImage;
/* coordinate conversion here is a bit hard:
* y ruler coordinates start from 0 independent of view position.
* must always convert to view coordinates to know where we are.
*/
mouseDownLocation = [self convertPoint:[event locationInWindow]
fromView:nil];
clickedContainer = [self instanceAtPoint:mouseDownLocation];
if (clickedContainer == nil || ![clickedContainer isContainer]) {
return;
}
drawView = (DrawView *)[self clientView];
//filter = [drawView filter];
entityType = [controller entityTypeForEntity:clickedContainer];
layoutDescriptor = [controller descriptorForEntityType:entityType];
container = [controller containerForEntity:clickedContainer];
limitRect = [layoutDescriptor rectInContainer:container];
instanceRect = [(STContainerTypeLayout *)layoutDescriptor rectOfInstance:clickedContainer];
instanceRect.size.height += 1;
instanceRect.size.width += 1;
minY = NSMinY(limitRect);
maxY = NSMaxY(limitRect) - NSHeight(instanceRect) + 1;
indexBeingDragged = [self levelForPosition:mouseDownLocation.x];
if (indexBeingDragged == -1) {
return;
}
minX = [self positionForLevel:indexBeingDragged];
width = [self widthForLevel:indexBeingDragged];
limitRect.origin.x = instanceRect.origin.x = minX - 1;
limitRect.size.width = instanceRect.size.width = width + 1;
[self _makeRectVisible:instanceRect];
mouseDownYInView = [drawView convertPoint:[event locationInWindow]
fromView:nil].y;
currentMouseYInView = mouseDownYInView;
Assign(containerBeingDragged, clickedContainer);
dragImage = [[self _getImageInRect:instanceRect] retain];
[self lockFocus];
[[self window] disableFlushWindow];
currentRect = instanceRect;
currentRect.origin.y -= NSMinY([drawView visibleRect]);
[self setNeedsDisplayInRect:currentRect];
[self displayIfNeeded];
[[self window] cacheImageInRect:[self convertRect:currentRect toView:nil]];
[dragImage dissolveToPoint:NSMakePoint(NSMinX(currentRect),
NSMaxY(currentRect))
fraction:(float)0.7];
[[self window] enableFlushWindow];
[[self window] flushWindow];
do {
float newYInView;
float deltaY;
int mask = NSLeftMouseUpMask | NSLeftMouseDraggedMask;
event = [NSApp nextEventMatchingMask: mask
untilDate: [NSDate distantFuture]
inMode: NSEventTrackingRunLoopMode
dequeue: YES];
newYInView = [drawView convertPoint:[[self window]
mouseLocationOutsideOfEventStream]
fromView:nil].y;
if (newYInView == currentMouseYInView) continue;
currentMouseYInView = newYInView;
deltaY = currentMouseYInView - mouseDownYInView;
currentRect.origin.y = instanceRect.origin.y + deltaY;
if (currentRect.origin.y < minY) currentRect.origin.y = minY;
if (currentRect.origin.y > maxY) currentRect.origin.y = maxY;
[[self window] disableFlushWindow];
if ([self _makeRectVisible:currentRect]) {
[[self window] discardCachedImage];
} else {
[[self window] restoreCachedImage];
}
currentRect.origin.y -= NSMinY([drawView visibleRect]);
[[self window] cacheImageInRect:[self convertRect:currentRect
toView:nil]];
[dragImage dissolveToPoint:NSMakePoint(NSMinX(currentRect),
NSMaxY(currentRect))
fraction:(float)0.7];
[[self window] enableFlushWindow];
[[self window] flushWindow];
} while ([event type] != NSLeftMouseUp);
[self unlockFocus];
[[self window] discardCachedImage];
[dragImage release];
droppedInstance = [self instanceAtPoint:NSMakePoint(NSMidX(currentRect),
NSMidY(currentRect))];
if ([droppedInstance isEqual:containerBeingDragged]) {
NSMutableSet *containers;
containers = [[controller selectedContainers] mutableCopy];
if ([containers containsObject:containerBeingDragged]) {
[containers removeObject:containerBeingDragged];
} else {
[containers addObject:containerBeingDragged];
}
[controller setSelectedContainers:containers];
[containers release];
} else if (droppedInstance != nil) {
NSMutableArray *order;
NSEnumerator *contEnum;
PajeContainer *orgContainer;
BOOL before;
float droppedCenter = NSMidY([(STContainerTypeLayout *)layoutDescriptor rectOfInstance:droppedInstance]);
float currentCenter = NSMidY(currentRect) - 0.5
+ NSMinY([drawView visibleRect]) - 0.5;
before = (currentCenter < droppedCenter)
|| ((currentCenter == droppedCenter)
&& (currentMouseYInView < mouseDownYInView));
order = [NSMutableArray array];
contEnum = [controller enumeratorOfContainersTyped:entityType
inContainer:container];
while ((orgContainer = [contEnum nextObject]) != nil) {
if ([orgContainer isEqual:containerBeingDragged]) {
continue;
} else if ([orgContainer isEqual:droppedInstance]) {
if (!before) [order addObject:orgContainer];
[order addObject:containerBeingDragged];
if (before) [order addObject:orgContainer];
} else {
[order addObject:orgContainer];
}
}
[controller setOrder:order
ofContainersTyped:entityType
inContainer:container];
} else {
[self setNeedsDisplay:YES];
}
Assign(containerBeingDragged, nil);
}
- (void)mouseDown:(NSEvent *)event
{
NSPoint mouseDownLocation;
hierarchyRulerPart hitPart;
mouseDownLocation = [self convertPoint:[event locationInWindow]
fromView:nil];
hitPart = [self hitPart:mouseDownLocation];
switch (hitPart) {
case hierarchyRulerVerticalDivider:
[self trackVerticalDivider:event];
break;
case hierarchyRulerHorizontalDivider:
[self trackHorizontalDivider:event];
break;
case hierarchyRulerBox:
[self trackBox:event];
break;
case hierarchyRulerNone:
break;
default:
NSAssert(NO, @"Invalid hit part in HierarchyRuler");
}
}
@end
|