Remove deprecated SetRemoveTime API
[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 Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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
18 #include "relayout-helper.h"
19
20 #include <dali-toolkit/public-api/controls/control.h>
21
22
23 namespace Dali
24 {
25
26 namespace Toolkit
27 {
28
29 namespace Internal
30 {
31
32 namespace RelayoutHelper
33 {
34
35 Vector3 GetNaturalSize( Actor actor )
36 {
37   Vector3 size( 0.0f, 0.0f, 0.0f );
38
39   Toolkit::Control control = Toolkit::Control::DownCast( actor );
40   if( control )
41   {
42     size = control.GetNaturalSize();
43   }
44   else
45   {
46     size = actor.GetCurrentSize();
47     const float depth = size.depth;
48
49     // Get natural size for ImageActor.
50     // TODO: currently it doesn't work as expected.
51     ImageActor imageActor = ImageActor::DownCast( actor );
52     if( ( imageActor ) && ( imageActor.GetImage() ) )
53     {
54       Image image = imageActor.GetImage();
55       size = Vector3( static_cast<float>( image.GetWidth() ), static_cast<float>( image.GetHeight() ), depth );
56     }
57     else
58     {
59       // Get natural size for TextActor.
60       TextActor textActor = TextActor::DownCast( actor );
61       if( textActor )
62       {
63         Font font = textActor.GetFont();
64         if( !font )
65         {
66           font = Font::New();
67         }
68         size = font.MeasureText( textActor.GetText() );
69         size.depth = depth;
70       }
71     }
72   }
73
74   return size;
75 }
76
77 float GetHeightForWidth( Actor actor, float width )
78 {
79   float height = 0.0f;
80
81   Toolkit::Control control = Toolkit::Control::DownCast( actor );
82   if( control )
83   {
84     height = control.GetHeightForWidth( width );
85   }
86   else
87   {
88     bool constrainSize = false;
89     Vector3 size( 0.0f, 0.0f, 0.0f );
90
91     ImageActor imageActor = ImageActor::DownCast( actor );
92     if( ( imageActor ) && ( imageActor.GetImage() ) )
93     {
94       Image image = imageActor.GetImage();
95       size = Vector3( static_cast<float>( image.GetWidth() ), static_cast<float>( image.GetHeight() ), 0.0f );
96
97       constrainSize = true;
98     }
99     else
100     {
101       TextActor textActor = TextActor::DownCast( actor );
102       if( textActor )
103       {
104         Font font = textActor.GetFont();
105         if( !font )
106         {
107           font = Font::New();
108         }
109         size = font.MeasureText( textActor.GetText() );
110
111         constrainSize = true;
112       }
113       else
114       {
115         size = actor.GetCurrentSize();
116       }
117     }
118
119     // Scale the actor
120     float scaleRatio = width / size.width;
121     if( constrainSize )
122     {
123       // Allow the scale to decrease if greater than input width but not increase if less than input width
124       if( scaleRatio > 1.0f )
125       {
126         scaleRatio = 1.0f;
127       }
128     }
129
130     height = size.height * scaleRatio;
131   }
132
133   return height;
134 }
135
136 } // namespace RelayoutHelper
137
138 } // namespace Internal
139
140 } // namespace Toolkit
141
142 } // namespace Dali