OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import "chrome/browser/cocoa/vertical_layout_view.h" | |
6 | |
7 @interface VerticalLayoutView(PrivateMethods) | |
8 - (void)layoutChildren; | |
9 @end | |
10 | |
11 @implementation VerticalLayoutView | |
12 | |
13 - (id)initWithFrame:(NSRect)frame { | |
14 self = [super initWithFrame:frame]; | |
15 if (self) { | |
16 // Turn auto resizing off, we'll be laying out our children programatically. | |
17 [self setAutoresizesSubviews:NO]; | |
18 [self setAutoresizingMask:NSViewNotSizable]; | |
19 } | |
20 | |
21 return self; | |
22 } | |
23 | |
24 // Flip the coordinate system to arrange child views from top to bottom | |
25 // with top at 0, increasing down. This simplifies the logic and plays | |
26 // well with containing scroll views. | |
27 - (BOOL)isFlipped { | |
28 return YES; | |
29 } | |
30 | |
31 // Override the default |viewWillDraw| to indicate to drawing machinery proper | |
32 // arrangement of subviews. | |
33 - (void)viewWillDraw { | |
34 // Reposition child views prior to super's descent into its |viewWillDraw| | |
35 // pass. | |
36 [self layoutChildren]; | |
37 | |
38 // Default descent into subviews. | |
39 [super viewWillDraw]; | |
40 | |
41 // Adjust children again to account for any modifications made during the | |
42 // prior descent. Most importantly we resize our own frame to properly | |
43 // adjust any containing scroll view. | |
44 [self layoutChildren]; | |
45 } | |
46 | |
47 @end | |
48 | |
49 @implementation VerticalLayoutView(PrivateMethods) | |
50 | |
51 // This method traverses the immediate subviews measuring their height and | |
52 // adjusting their frames so they are arranged vertically ordered relative | |
53 // to their sibling views. Note the dependency here on the |isFlipped| | |
54 // state. This code assumes |isFlipped| is YES. | |
55 - (void)layoutChildren { | |
56 NSArray* children = [self subviews]; | |
57 int childCount = [children count]; | |
58 | |
59 CGFloat yPosition = 0.0; | |
60 for (int i = childCount-1; i >= 0; --i) { | |
61 NSView* child = [children objectAtIndex:i]; | |
62 [child setFrameOrigin:NSMakePoint([child frame].origin.x, yPosition)]; | |
63 yPosition += [child frame].size.height; | |
64 } | |
65 | |
66 // Resize self to reflect vertical extent of children. | |
67 [self setFrame:NSMakeRect([self frame].origin.x, | |
68 [self frame].origin.y, | |
69 [self frame].size.width, | |
70 yPosition)]; | |
71 } | |
72 | |
73 @end | |
OLD | NEW |