Permalink
Browse files

Changing the frame updates the relative frame. Origin now BOTTOM left!

  • Loading branch information...
1 parent 27b0b9e commit 15f2edc1c674f0177fc4bd54569a5b99acf63f34 @p2 committed Dec 21, 2012
@@ -398,6 +398,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "growth-charts-helper/growth-charts-helper-Prefix.pch";
INFOPLIST_FILE = "growth-charts-helper/growth-charts-helper-Info.plist";
+ MACOSX_DEPLOYMENT_TARGET = 10.8;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
@@ -410,6 +411,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "growth-charts-helper/growth-charts-helper-Prefix.pch";
INFOPLIST_FILE = "growth-charts-helper/growth-charts-helper-Info.plist";
+ MACOSX_DEPLOYMENT_TARGET = 10.8;
PRODUCT_NAME = "$(TARGET_NAME)";
WRAPPER_EXTENSION = app;
};
@@ -43,8 +43,7 @@
@property (nonatomic, weak) CHChartArea *area; ///< The area model that describes the receiver
-@property (nonatomic, assign) CGPoint origin; ///< Origin between 0 and 1 relative to its parent's grid
-@property (nonatomic, assign) CGSize size; ///< Size between 0 and 1 relative to its parent's grid
+@property (nonatomic, assign) CGRect relFrame; ///< The frame relative to its parent's grid (from 0 to 1)
@property (nonatomic, assign) CGPathRef outline; ///< The outline of the area. We do *not* clip to this area, but you can use it to do so.
@property (nonatomic, assign) CGSize pageSize; ///< The size of the page we're currently displayed on, in screen pixels
@@ -27,6 +27,8 @@
@interface CHChartAreaView () {
+ CGRect inParentRect;
+
BOOL clickStartedInside;
BOOL clickDidMove;
}
@@ -71,8 +73,7 @@ - (void)setArea:(CHChartArea *)area
_area = area;
// set some properties
- self.origin = area.frame.origin;
- self.size = area.frame.size;
+ self.relFrame = area.frame;
}
}
@@ -196,12 +197,12 @@ - (void)positionInFrame:(CGRect)targetRect onView:(NSView *)aView pageSize:(CGSi
{
if (!CGRectIsEmpty(targetRect)) {
CGRect appliedRect = targetRect;
+ inParentRect = targetRect;
- appliedRect.origin.x += _origin.x * appliedRect.size.width;
- //appliedRect.origin.y += _origin.y * appliedRect.size.height;
- appliedRect.origin.y += (1.f - _origin.y) * appliedRect.size.height - (_size.height * appliedRect.size.height);
- appliedRect.size.width *= _size.width;
- appliedRect.size.height *= _size.height;
+ appliedRect.origin.x += _relFrame.origin.x * appliedRect.size.width;
+ appliedRect.origin.y += _relFrame.origin.y * appliedRect.size.height;
+ appliedRect.size.width *= _relFrame.size.width;
+ appliedRect.size.height *= _relFrame.size.height;
self.frame = appliedRect; // will cause setNeedsDisplay to be set if the size changed
self.pageSize = pageSize;
@@ -216,6 +217,25 @@ - (void)positionInFrame:(CGRect)targetRect onView:(NSView *)aView pageSize:(CGSi
}
}
+
+/**
+ * We override setFrame to update the relative frame when moving the box.
+ */
+- (void)setFrame:(NSRect)frameRect
+{
+ if (!NSEqualRects(self.frame, frameRect)) {
+ [super setFrame:frameRect];
+
+ // update relative frame
+ _relFrame.origin.x = frameRect.origin.x / inParentRect.size.width;
+ _relFrame.origin.y = frameRect.origin.y / inParentRect.size.height;
+ _relFrame.size.width = frameRect.size.width / inParentRect.size.width;
+ _relFrame.size.height = frameRect.size.height / inParentRect.size.height;
+
+ self.area.frame = _relFrame;
+ }
+}
+
/**
* The rect in our own coordinate system describing the area needed to cover all our subviews.
*
@@ -376,6 +396,9 @@ - (BOOL)pointInside:(CGPoint)point withEvent:(NSEvent *)event
return NSPointInRect(location, [self bounds]);
}
+
+
+#pragma mark - Mouse Handling
- (void)mouseDown:(NSEvent *)theEvent
{
clickStartedInside = YES;
@@ -435,7 +458,7 @@ + (Class)registeredClassForType:(NSString *)aType
#pragma mark - Utilities
- (NSString *)description
{
- return [NSString stringWithFormat:@"%@ <%p> {%@,%@}, %d sub-areas", NSStringFromClass([self class]), self, NSStringFromCGPoint(_origin), NSStringFromCGSize(_size), (int)[_areas count]];
+ return [NSString stringWithFormat:@"%@ <%p> %@, %d sub-areas", NSStringFromClass([self class]), self, NSStringFromCGRect(_relFrame), (int)[_areas count]];
}
@@ -33,8 +33,6 @@ @interface CHChartPDFView (){
@property (nonatomic, strong) NSButton *zoomIn;
@property (nonatomic, strong) NSButton *zoomOut;
-@property (nonatomic, strong) NSMutableSet *addedAreas;
-
@end
@@ -57,26 +55,14 @@ - (void)drawPagePost:(PDFPage *)page
NSRect pageFrame = NSMakeRect(0.f, 0.f, pageSize.width, pageSize.height);
NSSize origSize = [page boundsForBox:[self displayBox]].size; // kPDFDisplayBoxCropBox is our default display mode, not kPDFDisplayBoxMediaBox
- // remove old areas
- if ([_addedAreas count] > 0) {
- // TODO: Do NOT remove areas on other pages
- [_addedAreas makeObjectsPerformSelector:@selector(removeFromSuperviewWithoutNeedingDisplay)];
- [_addedAreas removeAllObjects];
- }
- if (!_addedAreas) {
- self.addedAreas = [NSMutableSet set];
- }
-
- // add our areas
+ // add/reposition our areas
NSSet *areas = _chart.chartAreas;
if ([areas count] > 0) {
for (CHChartArea *area in areas) {
if (pageNum == area.page) {
- CHChartAreaView *areaView = [area view];
+ CHChartAreaView *areaView = [area viewForParent:self];
areaView.pageView = self;
[areaView positionInFrame:pageFrame onView:docView pageSize:origSize];
-
- [_addedAreas addObject:areaView];
}
else {
DLog(@"Skipping area %@, not on page %lu", area, pageNum);
@@ -62,7 +62,7 @@
@property (nonatomic, assign) BOOL topmost; ///< YES if this area lies directly on the PDF, i.e. not nested in another area
@property (nonatomic, copy) NSArray *areas; ///< An area can have any number of subareas
-- (CHChartAreaView *)view;
+- (CHChartAreaView *)viewForParent:(id)parentView;
+ (NSCharacterSet *)outlinePathSplitSet;
@@ -26,6 +26,8 @@
@interface CHChartArea ()
+@property (nonatomic, strong) NSMapTable *knownViews;
+
@end
@@ -285,12 +287,19 @@ - (void)setChart:(CHChart *)chart
#pragma mark - Returning the View
/**
- * Returns a new CHChartAreaView instance created from the properties of the receiver (including sub-areas)
+ * Returns a CHChartAreaView instance, newly created if not previously done for the parentView, from the properties of the receiver (including sub-areas).
*/
-- (CHChartAreaView *)view
+- (CHChartAreaView *)viewForParent:(id)parentView
{
+ // do we already have one?
+ CHChartAreaView *view = [_knownViews objectForKey:parentView];
+ if (view) {
+ return view;
+ }
+
+ // nope, don't have one!
Class viewClass = [CHChartAreaView registeredClassForType:_type];
- CHChartAreaView *view = [viewClass new];
+ view = [viewClass new];
if (!view) {
DLog(@"Failed to create a view for area %@", self);
return nil;
@@ -303,35 +312,41 @@ - (CHChartAreaView *)view
if ([_areas count] > 0) {
NSMutableArray *subviews = [NSMutableArray arrayWithCapacity:[_areas count]];
for (CHChartArea *subarea in _areas) {
- CHChartAreaView *subview = [subarea view];
+ CHChartAreaView *subview = [subarea viewForParent:self];
[subviews addObject:subview];
}
view.areas = subviews;
}
+ // store and return
+ if (!_knownViews) {
+ self.knownViews = [NSMapTable mapTableWithKeyOptions:NSMapTableWeakMemory valueOptions:NSMapTableWeakMemory];
+ }
+ [_knownViews setObject:view forKey:parentView];
+
return view;
}
#pragma mark - Class Static Methods
- /**
- * Where to split the points in the "outline" property.
- *
- * This usually is just the semi-colon, but we also add whitespace in case the spec is not 100% accurate.
- * @return A character set at which to split the points in the "outline" property.
- */
- + (NSCharacterSet *)outlinePathSplitSet
- {
- static NSCharacterSet *outlinePathSplitSet = nil;
- if (!outlinePathSplitSet) {
- NSMutableCharacterSet *set = [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy];
- [set addCharactersInString:@";"];
- outlinePathSplitSet = [set copy];
- }
-
- return outlinePathSplitSet;
- }
+/**
+ * Where to split the points in the "outline" property.
+ *
+ * This usually is just the semi-colon, but we also add whitespace in case the spec is not 100% accurate.
+ * @return A character set at which to split the points in the "outline" property.
+ */
++ (NSCharacterSet *)outlinePathSplitSet
+{
+ static NSCharacterSet *outlinePathSplitSet = nil;
+ if (!outlinePathSplitSet) {
+ NSMutableCharacterSet *set = [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy];
+ [set addCharactersInString:@";"];
+ outlinePathSplitSet = [set copy];
+ }
+
+ return outlinePathSplitSet;
+}

0 comments on commit 15f2edc

Please sign in to comment.