• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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 #ifndef UI_VIEWS_LAYOUT_BOX_LAYOUT_H_
6 #define UI_VIEWS_LAYOUT_BOX_LAYOUT_H_
7 
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "ui/gfx/insets.h"
11 #include "ui/views/layout/layout_manager.h"
12 
13 namespace gfx {
14 class Rect;
15 class Size;
16 }
17 
18 namespace views {
19 
20 class View;
21 
22 // A Layout manager that arranges child views vertically or horizontally in a
23 // side-by-side fashion with spacing around and between the child views. The
24 // child views are always sized according to their preferred size. If the
25 // host's bounds provide insufficient space, child views will be clamped.
26 // Excess space will not be distributed.
27 class VIEWS_EXPORT BoxLayout : public LayoutManager {
28  public:
29   enum Orientation {
30     kHorizontal,
31     kVertical,
32   };
33 
34   // This specifies where along the main axis the children should be laid out.
35   // e.g. a horizontal layout of MAIN_AXIS_ALIGNMENT_END will result in the
36   // child views being right-aligned.
37   enum MainAxisAlignment {
38     MAIN_AXIS_ALIGNMENT_START,
39     MAIN_AXIS_ALIGNMENT_CENTER,
40     MAIN_AXIS_ALIGNMENT_END,
41 
42     // This distributes extra space among the child views. This increases the
43     // size of child views along the main axis rather than the space between
44     // them.
45     MAIN_AXIS_ALIGNMENT_FILL,
46     // TODO(calamity): Add MAIN_AXIS_ALIGNMENT_JUSTIFY which spreads blank space
47     // in-between the child views.
48   };
49 
50   // TODO(calamity): Add CrossAxisAlignment property to allow cross axis
51   // alignment.
52 
53   // Use |inside_border_horizontal_spacing| and
54   // |inside_border_vertical_spacing| to add additional space between the child
55   // view area and the host view border. |between_child_spacing| controls the
56   // space in between child views.
57   BoxLayout(Orientation orientation,
58             int inside_border_horizontal_spacing,
59             int inside_border_vertical_spacing,
60             int between_child_spacing);
61   virtual ~BoxLayout();
62 
set_main_axis_alignment(MainAxisAlignment main_axis_alignment)63   void set_main_axis_alignment(MainAxisAlignment main_axis_alignment) {
64     main_axis_alignment_ = main_axis_alignment;
65   }
66 
set_inside_border_insets(const gfx::Insets & insets)67   void set_inside_border_insets(const gfx::Insets& insets) {
68     inside_border_insets_ = insets;
69   }
70 
71   // Overridden from views::LayoutManager:
72   virtual void Layout(View* host) OVERRIDE;
73   virtual gfx::Size GetPreferredSize(const View* host) const OVERRIDE;
74   virtual int GetPreferredHeightForWidth(const View* host,
75                                          int width) const OVERRIDE;
76 
77  private:
78   // Returns the size and position along the main axis of |child_area|.
79   int MainAxisSize(const gfx::Rect& child_area) const;
80   int MainAxisPosition(const gfx::Rect& child_area) const;
81 
82   // Sets the size and position along the main axis of |child_area|.
83   void SetMainAxisSize(int size, gfx::Rect* child_area) const;
84   void SetMainAxisPosition(int position, gfx::Rect* child_area) const;
85 
86   // The preferred size for the dialog given the width of the child area.
87   gfx::Size GetPreferredSizeForChildWidth(const View* host,
88                                           int child_area_width) const;
89 
90   // The amount of space the layout requires in addition to any space for the
91   // child views.
92   gfx::Size NonChildSize(const View* host) const;
93 
94   const Orientation orientation_;
95 
96   // Spacing between child views and host view border.
97   gfx::Insets inside_border_insets_;
98 
99   // Spacing to put in between child views.
100   const int between_child_spacing_;
101 
102   // The alignment of children in the main axis. This is
103   // MAIN_AXIS_ALIGNMENT_START by default.
104   MainAxisAlignment main_axis_alignment_;
105 
106   DISALLOW_IMPLICIT_CONSTRUCTORS(BoxLayout);
107 };
108 
109 } // namespace views
110 
111 #endif  // UI_VIEWS_LAYOUT_BOX_LAYOUT_H_
112