gwenhywfar 5.12.0
CocoaWindowContentView.m
Go to the documentation of this file.
1//
2// CocoaVLayout.m
3//
4//
5// Created by Samuel Strupp on 10.08.10.
6// Copyright 2010 Synium Software GmbH. All rights reserved.
7//
8
9#ifdef HAVE_CONFIG_H
10# include <config.h>
11#endif
12
13
14
17
18
19#ifndef COCOA_WINDOW_CONTENT_VIEW_MM
20#define COCOA_WINDOW_CONTENT_VIEW_MM
21
22@implementation CocoaWindowContentView
23
24@synthesize fillX;
25@synthesize fillY;
26
27- (id)initWithFrame:(NSRect)frame {
28 self = [super initWithFrame:frame];
29 if (self) {
30 subviewsInOrder = [[NSMutableArray alloc] init];
31 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(layoutSubviews) name:NSViewFrameDidChangeNotification object:self];
32 }
33 return self;
34}
35
36-(void) dealloc {
37 [[NSNotificationCenter defaultCenter] removeObserver:self];
38 [subviewsInOrder release];
39 [super dealloc];
40}
41
42//#define borderDistance 0.0
43//#define cellDistance 0.0
44
45-(void) layoutSubviews {
46 NSRect bounds = [self bounds];
47
48 NSUInteger numOfSubViews = [subviewsInOrder count];
49
50 if (numOfSubViews > 0) {
51 //Prepass to compute the sizes
52
53 CGFloat sizesHeight[numOfSubViews];
54 CGFloat sizesWidth[numOfSubViews];
55 CGFloat exclusiveHeight = 0.0;
56 NSUInteger exclusiveChilds = 0;
57
58 NSUInteger i;
59 for (i=0; i<numOfSubViews; i++) {
60 NSView* subview = [subviewsInOrder objectAtIndex:i];
61 if ([subview conformsToProtocol:@protocol(CocoaGwenGUIProtocol)]) {
62 if ([(<CocoaGwenGUIProtocol>)subview fillX]) sizesWidth[i] = -1.0;
63 else {
64 CGFloat neededWidth = [(<CocoaGwenGUIProtocol>)subview minSize].width;
65 sizesWidth[i] = neededWidth;
66 }
67 if ([(<CocoaGwenGUIProtocol>)subview fillY]) sizesHeight[i] = -1.0;
68 else {
69 CGFloat neededHeight = [(<CocoaGwenGUIProtocol>)subview minSize].height;
70 sizesHeight[i] = neededHeight;
71 exclusiveHeight += neededHeight;
72 exclusiveChilds++;
73 }
74 }
75 else {
76 sizesWidth[i] = -1.0;
77 sizesHeight[i] = -1.0;
78 }
79 }
80
81
82 //Compute standard Sizes for Subviews
83
84 CGFloat stdHeight = 0.0;
85 if (numOfSubViews > exclusiveChilds) {
86 CGFloat fillHeight = bounds.size.height-exclusiveHeight;
87 stdHeight = (fillHeight/*-(borderDistance+borderDistance)-((numOfSubViews-1)*cellDistance)*/)/(numOfSubViews-exclusiveChilds);
88 }
89 else {
90 CGFloat fillHeight = bounds.size.height;
91 stdHeight = (fillHeight/*-(borderDistance+borderDistance)-((numOfSubViews-1)*cellDistance)*/)/(numOfSubViews);
92 }
93
94 CGFloat stdWidth = bounds.size.width/*-(borderDistance+borderDistance)*/;
95
96
97 //change Subviews Frame
98 NSRect actualFrame = bounds;
99 actualFrame.origin.x = 0.0; //borderDistance;
100 actualFrame.origin.y += bounds.size.height; //-borderDistance;
101 for (i=0; i<numOfSubViews; i++) {
102
103 CGFloat usedHeight = sizesHeight[i];
104 if (usedHeight < 0.0) usedHeight = stdHeight;
105 actualFrame.origin.y -= usedHeight;
106 actualFrame.size.height = usedHeight;
107
108 CGFloat usedWidth = sizesWidth[i];
109 if (usedWidth < 0.0) usedWidth = stdWidth;
110 NSView* subview = [subviewsInOrder objectAtIndex:i];
111 actualFrame.size.width = usedWidth;
112
113 [subview setFrame:actualFrame];
114 //actualFrame.origin.y -= cellDistance;
115 }
116 }
117
118}
119
120-(void) addLayoutSubview:(NSView*)new_subview {
121 [subviewsInOrder addObject:new_subview];
122 [self addSubview:new_subview];
123 [self layoutSubviews];
124}
125
126#pragma mark Protocoll Methods
127
128
129- (NSSize) minSize {
130 NSUInteger numOfSubViews = [subviewsInOrder count];
131 //CGFloat borderWidth = borderDistance+borderDistance;
132 NSSize size = NSMakeSize(0,0/*borderWidth, borderWidth*/);
133 if (numOfSubViews > 0) {
134 NSUInteger i;
135 for (i=0; i<numOfSubViews; i++) {
136 NSView* subview = [subviewsInOrder objectAtIndex:i];
137 if ([subview conformsToProtocol:@protocol(CocoaGwenGUIProtocol)]) {
138 NSSize subViewMinSize = [(<CocoaGwenGUIProtocol>)subview minSize];
139 if (subViewMinSize.width/*+borderWidth*/ > size.width) {
140 size.width = subViewMinSize.width/*+borderWidh*/;
141 }
142 size.height += subViewMinSize.height;
143 //if (i>0) size.height += cellDistance;
144 }
145 }
146 }
147 return size;
148}
149
150/*- (void)setFrame:(NSRect)frameRect {
151 NSSize minSize = [self minSize];
152 if (frameRect.size.height < minSize.height) {
153 frameRect.size.height = minSize.height;
154 }
155 if (frameRect.size.width < minSize.width) {
156 frameRect.size.width = minSize.width;
157 }
158 [super setFrame:frameRect];
159}*/
160
161@end
162
163#endif