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