Fix boost includes
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / relayout-helper.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://floralicense.org/license/
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "relayout-helper.h"
18
19 #include <dali-toolkit/public-api/controls/control.h>
20
21
22 namespace Dali
23 {
24
25 namespace Toolkit
26 {
27
28 namespace Internal
29 {
30
31 namespace RelayoutHelper
32 {
33
34 Vector3 GetNaturalSize( Actor actor )
35 {
36   Vector3 size( 0.0f, 0.0f, 0.0f );
37
38   Toolkit::Control control = Toolkit::Control::DownCast( actor );
39   if( control )
40   {
41     size = control.GetNaturalSize();
42   }
43   else
44   {
45     size = actor.GetCurrentSize();
46     const float depth = size.depth;
47
48     // Get natural size for ImageActor.
49     // TODO: currently it doesn't work as expected.
50     ImageActor imageActor = ImageActor::DownCast( actor );
51     if( ( imageActor ) && ( imageActor.GetImage() ) )
52     {
53       Image image = imageActor.GetImage();
54       size = Vector3( static_cast<float>( image.GetWidth() ), static_cast<float>( image.GetHeight() ), depth );
55     }
56     else
57     {
58       // Get natural size for TextActor.
59       TextActor textActor = TextActor::DownCast( actor );
60       if( textActor )
61       {
62         Font font = textActor.GetFont();
63         if( !font )
64         {
65           font = Font::New();
66         }
67         size = font.MeasureText( textActor.GetText() );
68         size.depth = depth;
69       }
70     }
71   }
72
73   return size;
74 }
75
76 float GetHeightForWidth( Actor actor, float width )
77 {
78   float height = 0.0f;
79
80   Toolkit::Control control = Toolkit::Control::DownCast( actor );
81   if( control )
82   {
83     height = control.GetHeightForWidth( width );
84   }
85   else
86   {
87     bool constrainSize = false;
88     Vector3 size( 0.0f, 0.0f, 0.0f );
89
90     ImageActor imageActor = ImageActor::DownCast( actor );
91     if( ( imageActor ) && ( imageActor.GetImage() ) )
92     {
93       Image image = imageActor.GetImage();
94       size = Vector3( static_cast<float>( image.GetWidth() ), static_cast<float>( image.GetHeight() ), 0.0f );
95
96       constrainSize = true;
97     }
98     else
99     {
100       TextActor textActor = TextActor::DownCast( actor );
101       if( textActor )
102       {
103         Font font = textActor.GetFont();
104         if( !font )
105         {
106           font = Font::New();
107         }
108         size = font.MeasureText( textActor.GetText() );
109
110         constrainSize = true;
111       }
112       else
113       {
114         size = actor.GetCurrentSize();
115       }
116     }
117
118     // Scale the actor
119     float scaleRatio = width / size.width;
120     if( constrainSize )
121     {
122       // Allow the scale to decrease if greater than input width but not increase if less than input width
123       if( scaleRatio > 1.0f )
124       {
125         scaleRatio = 1.0f;
126       }
127     }
128
129     height = size.height * scaleRatio;
130   }
131
132   return height;
133 }
134
135 } // namespace RelayoutHelper
136
137 } // namespace Internal
138
139 } // namespace Toolkit
140
141 } // namespace Dali