Merge "Button Refactoring phase 1" into tizen
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / push-button-impl.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 "push-button-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23 #include <dali/public-api/actors/image-actor.h>
24 #include <dali/public-api/object/type-registry.h>
25
26 // INTERNAL INCLUDES
27 #include "push-button-default-painter-impl.h"
28
29 #include <dali-toolkit/public-api/controls/text-view/text-view.h>
30 #include <dali-toolkit/internal/controls/relayout-helper.h>
31
32 namespace Dali
33 {
34
35 namespace Toolkit
36 {
37
38 namespace Internal
39 {
40
41 namespace
42 {
43
44 BaseHandle Create()
45 {
46   return Toolkit::PushButton::New();
47 }
48
49 TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create );
50
51 } // unnamed namespace
52
53 namespace
54 {
55
56 const float TEXT_PADDING = 12.0f;
57
58 /**
59  * Find the first image actor in the actor hierarchy
60  */
61 ImageActor FindImageActor( Actor root )
62 {
63   ImageActor imageActor = ImageActor::DownCast( root );
64   if( !imageActor && root )
65   {
66     for( unsigned int i = 0, numChildren = root.GetChildCount(); i < numChildren; ++i )
67     {
68       ImageActor childImageActor = FindImageActor( root.GetChildAt( i ) );
69       if( childImageActor )
70       {
71         return childImageActor;
72       }
73     }
74   }
75
76   return imageActor;
77 }
78
79
80 } // unnamed namespace
81
82 Dali::Toolkit::PushButton PushButton::New()
83 {
84   // Create the implementation, temporarily owned on stack
85   IntrusivePtr< PushButton > internalPushButton = new PushButton();
86
87   // Pass ownership to CustomActor
88   Dali::Toolkit::PushButton pushButton( *internalPushButton );
89
90   // Second-phase init of the implementation
91   // This can only be done after the CustomActor connection has been made...
92   internalPushButton->Initialize();
93
94   return pushButton;
95 }
96
97 void PushButton::OnButtonInitialize()
98 {
99   // Push button requires the Leave event.
100   Actor root = Self();
101   root.SetLeaveRequired( true );
102 }
103
104 PushButton::PushButton()
105 : Button()
106 {
107   // Creates specific painter.GetBu
108   ButtonPainterPtr painter = PushButtonDefaultPainterPtr( new PushButtonDefaultPainter() );
109   SetPainter( painter );
110 }
111
112 PushButton::~PushButton()
113 {
114   SetPainter( NULL );
115 }
116
117 Vector3 PushButton::GetNaturalSize()
118 {
119   Vector3 size = Control::GetNaturalSize();
120
121   const bool widthIsZero = EqualsZero( size.width );
122   const bool heightIsZero = EqualsZero( size.height );
123
124   if( widthIsZero || heightIsZero )
125   {
126     // If background and background not scale9 try get size from that
127     ImageActor imageActor = FindImageActor( GetButtonImage() );
128     if( imageActor && imageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
129     {
130       Vector3 imageSize = RelayoutHelper::GetNaturalSize( imageActor );
131
132       if( widthIsZero )
133       {
134         size.width = imageSize.width;
135       }
136
137       if( heightIsZero )
138       {
139         size.height = imageSize.height;
140       }
141     }
142
143     ImageActor backgroundImageActor = FindImageActor( GetBackgroundImage() );
144     if( backgroundImageActor && backgroundImageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
145     {
146       Vector3 imageSize = RelayoutHelper::GetNaturalSize( backgroundImageActor );
147
148       if( widthIsZero )
149       {
150         size.width = std::max( size.width, imageSize.width );
151       }
152
153       if( heightIsZero )
154       {
155         size.height = std::max( size.height, imageSize.height );
156       }
157     }
158
159     // If label, test against it's size
160     Toolkit::TextView textView = Toolkit::TextView::DownCast( GetLabel() );
161     if( textView )
162     {
163       Vector3 textViewSize = textView.GetNaturalSize();
164
165       if( widthIsZero )
166       {
167         size.width = std::max( size.width, textViewSize.width + TEXT_PADDING * 2.0f );
168       }
169
170       if( heightIsZero )
171       {
172         size.height = std::max( size.height, textViewSize.height + TEXT_PADDING * 2.0f );
173       }
174     }
175   }
176
177   return size;
178 }
179
180 } // namespace Internal
181
182 } // namespace Toolkit
183
184 } // namespace Dali