Merge remote-tracking branch 'origin/tizen' into new_text
[platform/core/uifw/dali-toolkit.git] / 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 // CLASS HEADER
19 #include "relayout-helper.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/image-actor.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/controls/control.h>
26
27
28 namespace Dali
29 {
30
31 namespace Toolkit
32 {
33
34 namespace Internal
35 {
36
37 namespace RelayoutHelper
38 {
39
40 Vector3 GetNaturalSize( Actor actor )
41 {
42   Vector3 size( 0.0f, 0.0f, 0.0f );
43
44   Toolkit::Control control = Toolkit::Control::DownCast( actor );
45   if( control )
46   {
47     size = control.GetNaturalSize();
48   }
49   else
50   {
51     size = actor.GetCurrentSize();
52     const float depth = size.depth;
53
54     // Get natural size for ImageActor.
55     // TODO: currently it doesn't work as expected.
56     ImageActor imageActor = ImageActor::DownCast( actor );
57     if( ( imageActor ) && ( imageActor.GetImage() ) )
58     {
59       Image image = imageActor.GetImage();
60       size = Vector3( static_cast<float>( image.GetWidth() ), static_cast<float>( image.GetHeight() ), depth );
61     }
62   }
63
64   return size;
65 }
66
67 float GetHeightForWidth( Actor actor, float width )
68 {
69   float height = 0.0f;
70
71   Toolkit::Control control = Toolkit::Control::DownCast( actor );
72   if( control )
73   {
74     height = control.GetHeightForWidth( width );
75   }
76   else
77   {
78     bool constrainSize = false;
79     Vector3 size( 0.0f, 0.0f, 0.0f );
80
81     ImageActor imageActor = ImageActor::DownCast( actor );
82     if( ( imageActor ) && ( imageActor.GetImage() ) )
83     {
84       Image image = imageActor.GetImage();
85       size = Vector3( static_cast<float>( image.GetWidth() ), static_cast<float>( image.GetHeight() ), 0.0f );
86
87       constrainSize = true;
88     }
89     else
90     {
91       size = actor.GetCurrentSize();
92     }
93
94     // Scale the actor
95     float scaleRatio = width / size.width;
96     if( constrainSize )
97     {
98       // Allow the scale to decrease if greater than input width but not increase if less than input width
99       if( scaleRatio > 1.0f )
100       {
101         scaleRatio = 1.0f;
102       }
103     }
104
105     height = size.height * scaleRatio;
106   }
107
108   return height;
109 }
110
111 } // namespace RelayoutHelper
112
113 } // namespace Internal
114
115 } // namespace Toolkit
116
117 } // namespace Dali