gwenhywfar  4.3.1
CocoaWindowContentView.m
Go to the documentation of this file.
00001 //
00002 //  CocoaVLayout.m
00003 //  
00004 //
00005 //  Created by Samuel Strupp on 10.08.10.
00006 //  Copyright 2010 Synium Software GmbH. All rights reserved.
00007 //
00008 
00009 #ifdef HAVE_CONFIG_H
00010 # include <config.h>
00011 #endif
00012 
00013 
00014 
00015 #import "CocoaWindowContentView.h"
00016 #import "CocoaGwenGUIProtocol.h"
00017 
00018 
00019 #ifndef COCOA_WINDOW_CONTENT_VIEW_MM
00020 #define COCOA_WINDOW_CONTENT_VIEW_MM
00021 
00022 @implementation CocoaWindowContentView
00023 
00024 @synthesize fillX;
00025 @synthesize fillY;
00026 
00027 - (id)initWithFrame:(NSRect)frame {
00028     self = [super initWithFrame:frame];
00029     if (self) {
00030                 subviewsInOrder = [[NSMutableArray alloc] init];
00031                 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(layoutSubviews) name:NSViewFrameDidChangeNotification object:self];
00032     }
00033     return self;
00034 }
00035 
00036 -(void) dealloc {
00037         [[NSNotificationCenter defaultCenter] removeObserver:self];
00038         [subviewsInOrder release];
00039         [super dealloc];
00040 }
00041 
00042 //#define borderDistance 0.0
00043 //#define cellDistance 0.0
00044 
00045 -(void) layoutSubviews {
00046         NSRect bounds = [self bounds];
00047         
00048         NSUInteger numOfSubViews = [subviewsInOrder count];
00049         
00050         if (numOfSubViews > 0) {
00051                 //Prepass to compute the sizes
00052                 
00053                 CGFloat sizesHeight[numOfSubViews];
00054                 CGFloat sizesWidth[numOfSubViews];
00055                 CGFloat exclusiveHeight = 0.0;
00056                 NSUInteger exclusiveChilds = 0;
00057                 
00058                 NSUInteger i;
00059                 for (i=0; i<numOfSubViews; i++) {
00060                         NSView* subview = [subviewsInOrder objectAtIndex:i];
00061                         if ([subview conformsToProtocol:@protocol(CocoaGwenGUIProtocol)]) {
00062                                 if ([(<CocoaGwenGUIProtocol>)subview fillX]) sizesWidth[i] = -1.0;
00063                                 else {
00064                                         CGFloat neededWidth = [(<CocoaGwenGUIProtocol>)subview minSize].width;
00065                                         sizesWidth[i] = neededWidth;
00066                                 }
00067                                 if ([(<CocoaGwenGUIProtocol>)subview fillY]) sizesHeight[i] = -1.0;
00068                                 else {
00069                                         CGFloat neededHeight = [(<CocoaGwenGUIProtocol>)subview minSize].height;
00070                                         sizesHeight[i] = neededHeight;
00071                                         exclusiveHeight += neededHeight;
00072                                         exclusiveChilds++;
00073                                 }
00074                         }
00075                         else {
00076                                 sizesWidth[i] = -1.0;
00077                                 sizesHeight[i] = -1.0;
00078                         }
00079                 }
00080                 
00081                 
00082                 //Compute standard Sizes for Subviews
00083                 
00084                 CGFloat stdHeight = 0.0;
00085                 if (numOfSubViews > exclusiveChilds) {
00086                         CGFloat fillHeight = bounds.size.height-exclusiveHeight;
00087                         stdHeight = (fillHeight/*-(borderDistance+borderDistance)-((numOfSubViews-1)*cellDistance)*/)/(numOfSubViews-exclusiveChilds);
00088                 }
00089                 else {
00090                         CGFloat fillHeight = bounds.size.height;
00091                         stdHeight = (fillHeight/*-(borderDistance+borderDistance)-((numOfSubViews-1)*cellDistance)*/)/(numOfSubViews);
00092                 }
00093                 
00094                 CGFloat stdWidth = bounds.size.width/*-(borderDistance+borderDistance)*/;
00095                 
00096                 
00097                 //change Subviews Frame
00098                 NSRect actualFrame = bounds;
00099                 actualFrame.origin.x = 0.0; //borderDistance;
00100                 actualFrame.origin.y += bounds.size.height; //-borderDistance;
00101                 for (i=0; i<numOfSubViews; i++) {
00102                         
00103                         CGFloat usedHeight = sizesHeight[i];
00104                         if (usedHeight < 0.0) usedHeight = stdHeight;
00105                         actualFrame.origin.y -= usedHeight;
00106                         actualFrame.size.height = usedHeight;
00107                         
00108                         CGFloat usedWidth = sizesWidth[i];
00109                         if (usedWidth < 0.0) usedWidth = stdWidth;
00110                         NSView* subview = [subviewsInOrder objectAtIndex:i];
00111                         actualFrame.size.width = usedWidth;
00112                         
00113                         [subview setFrame:actualFrame];
00114                         //actualFrame.origin.y -= cellDistance;
00115                 }
00116         }
00117         
00118 }
00119 
00120 -(void) addLayoutSubview:(NSView*)new_subview {
00121         [subviewsInOrder addObject:new_subview];
00122         [self addSubview:new_subview];
00123         [self layoutSubviews];
00124 }
00125 
00126 #pragma mark Protocoll Methods
00127 
00128 
00129 - (NSSize) minSize {
00130         NSUInteger numOfSubViews = [subviewsInOrder count];
00131         //CGFloat borderWidth = borderDistance+borderDistance;
00132         NSSize size = NSMakeSize(0,0/*borderWidth, borderWidth*/);
00133         if (numOfSubViews > 0) {
00134                 NSUInteger i;
00135                 for (i=0; i<numOfSubViews; i++) {
00136                         NSView* subview = [subviewsInOrder objectAtIndex:i];
00137                         if ([subview conformsToProtocol:@protocol(CocoaGwenGUIProtocol)]) {
00138                                 NSSize subViewMinSize = [(<CocoaGwenGUIProtocol>)subview minSize];
00139                                 if (subViewMinSize.width/*+borderWidth*/ > size.width) {
00140                                         size.width = subViewMinSize.width/*+borderWidh*/;
00141                                 }
00142                                 size.height += subViewMinSize.height;
00143                                 //if (i>0) size.height += cellDistance;
00144                         }
00145                 }
00146         }
00147         return size;
00148 }
00149 
00150 /*- (void)setFrame:(NSRect)frameRect {
00151         NSSize minSize = [self minSize];
00152         if (frameRect.size.height < minSize.height) {
00153                 frameRect.size.height = minSize.height;
00154         }
00155         if (frameRect.size.width < minSize.width) {
00156                 frameRect.size.width = minSize.width;
00157         }
00158         [super setFrame:frameRect];
00159 }*/
00160 
00161 @end
00162 
00163 #endif