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