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/public-api/controls/text-view/text-view.h>
30 #include <dali-toolkit/internal/controls/relayout-helper.h>
46 return Toolkit::PushButton::New();
49 TypeRegistration typeRegistration( typeid(Toolkit::PushButton), typeid(Toolkit::Button), Create );
51 } // unnamed namespace
56 const float TEXT_PADDING = 12.0f;
59 * Find the first image actor in the actor hierarchy
61 ImageActor FindImageActor( Actor root )
63 ImageActor imageActor = ImageActor::DownCast( root );
64 if( !imageActor && root )
66 for( unsigned int i = 0, numChildren = root.GetChildCount(); i < numChildren; ++i )
68 ImageActor childImageActor = FindImageActor( root.GetChildAt( i ) );
71 return childImageActor;
80 } // unnamed namespace
82 Dali::Toolkit::PushButton PushButton::New()
84 // Create the implementation, temporarily owned on stack
85 IntrusivePtr< PushButton > internalPushButton = new PushButton();
87 // Pass ownership to CustomActor
88 Dali::Toolkit::PushButton pushButton( *internalPushButton );
90 // Second-phase init of the implementation
91 // This can only be done after the CustomActor connection has been made...
92 internalPushButton->Initialize();
97 void PushButton::OnButtonInitialize()
99 // Push button requires the Leave event.
101 root.SetLeaveRequired( true );
104 PushButton::PushButton()
107 // Creates specific painter.GetBu
108 ButtonPainterPtr painter = PushButtonDefaultPainterPtr( new PushButtonDefaultPainter() );
109 SetPainter( painter );
112 PushButton::~PushButton()
117 Vector3 PushButton::GetNaturalSize()
119 Vector3 size = Control::GetNaturalSize();
121 const bool widthIsZero = EqualsZero( size.width );
122 const bool heightIsZero = EqualsZero( size.height );
124 if( widthIsZero || heightIsZero )
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 )
130 Vector3 imageSize = RelayoutHelper::GetNaturalSize( imageActor );
134 size.width = imageSize.width;
139 size.height = imageSize.height;
143 ImageActor backgroundImageActor = FindImageActor( GetBackgroundImage() );
144 if( backgroundImageActor && backgroundImageActor.GetStyle() != ImageActor::STYLE_NINE_PATCH )
146 Vector3 imageSize = RelayoutHelper::GetNaturalSize( backgroundImageActor );
150 size.width = std::max( size.width, imageSize.width );
155 size.height = std::max( size.height, imageSize.height );
159 // If label, test against it's size
160 Toolkit::TextView textView = Toolkit::TextView::DownCast( GetLabel() );
163 Vector3 textViewSize = textView.GetNaturalSize();
167 size.width = std::max( size.width, textViewSize.width + TEXT_PADDING * 2.0f );
172 size.height = std::max( size.height, textViewSize.height + TEXT_PADDING * 2.0f );
180 } // namespace Internal
182 } // namespace Toolkit