2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "push-button-impl.h"
23 #include <dali/public-api/actors/image-actor.h>
24 #include <dali/public-api/object/type-registry.h>
27 #include "push-button-default-painter-impl.h"
29 #include <dali-toolkit/internal/controls/relayout-helper.h>
45 return Toolkit::PushButton::New();
48 TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create );
50 } // unnamed namespace
55 const float TEXT_PADDING = 12.0f;
58 * Find the first image actor in the actor hierarchy
60 ImageActor FindImageActor( Actor root )
62 ImageActor imageActor = ImageActor::DownCast( root );
63 if( !imageActor && root )
65 for( unsigned int i = 0, numChildren = root.GetChildCount(); i < numChildren; ++i )
67 ImageActor childImageActor = FindImageActor( root.GetChildAt( i ) );
70 return childImageActor;
79 } // unnamed namespace
81 Dali::Toolkit::PushButton PushButton::New()
83 // Create the implementation, temporarily owned on stack
84 IntrusivePtr< PushButton > internalPushButton = new PushButton();
86 // Pass ownership to CustomActor
87 Dali::Toolkit::PushButton pushButton( *internalPushButton );
89 // Second-phase init of the implementation
90 // This can only be done after the CustomActor connection has been made...
91 internalPushButton->Initialize();
96 void PushButton::OnButtonInitialize()
98 // Push button requires the Leave event.
100 root.SetLeaveRequired( true );
103 PushButton::PushButton()
106 // Creates specific painter.GetBu
107 ButtonPainterPtr painter = PushButtonDefaultPainterPtr( new PushButtonDefaultPainter() );
108 SetPainter( painter );
111 PushButton::~PushButton()
116 Vector3 PushButton::GetNaturalSize()
118 Vector3 size = Control::GetNaturalSize();
120 const bool widthIsZero = EqualsZero( size.width );
121 const bool heightIsZero = EqualsZero( size.height );
123 if( widthIsZero || heightIsZero )
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 )
129 Vector3 imageSize = RelayoutHelper::GetNaturalSize( imageActor );
133 size.width = imageSize.width;
138 size.height = imageSize.height;
142 ImageActor backgroundImageActor = FindImageActor( GetBackgroundImage() );
143 if( backgroundImageActor && backgroundImageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
145 Vector3 imageSize = RelayoutHelper::GetNaturalSize( backgroundImageActor );
149 size.width = std::max( size.width, imageSize.width );
154 size.height = std::max( size.height, imageSize.height );
162 } // namespace Internal
164 } // namespace Toolkit