[dali_1.0.48] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / buttons / radio-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
19 // CLASS HEADER
20 #include "radio-button-impl.h"
21
22 // EXTERNAL INCLUDES
23 #include <dali/public-api/object/type-registry.h>
24 #include <dali/public-api/images/resource-image.h>
25
26 namespace Dali
27 {
28
29 namespace Toolkit
30 {
31
32 namespace Internal
33 {
34
35 namespace
36 {
37
38 BaseHandle Create()
39 {
40   return Toolkit::RadioButton::New();
41 }
42
43 TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
44
45 const char* const UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-unselected.png";
46 const char* const SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-selected.png";
47 const char* const DISABLED_UNSELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-unselected-disabled.png";
48 const char* const DISABLED_SELECTED_BUTTON_IMAGE_DIR = DALI_IMAGE_DIR "radio-button-selected-disabled.png";
49
50 const float DISTANCE_BETWEEN_IMAGE_AND_LABEL( 5.0f );
51 }
52
53 Dali::Toolkit::RadioButton RadioButton::New()
54 {
55   // Create the implementation, temporarily owned on stack
56   IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
57
58   // Pass ownership to CustomActor
59   Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
60
61   // Second-phase init of the implementation
62   // This can only be done after the CustomActor connection has been made...
63   internalRadioButton->Initialize();
64
65   return radioButton;
66 }
67
68 RadioButton::RadioButton()
69 {
70   SetTogglableButton(true);
71 }
72
73 RadioButton::~RadioButton()
74 {
75 }
76
77 void RadioButton::OnButtonInitialize()
78 {
79   Actor self = Self();
80
81   // Wrap size of radio button around all its children
82   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
83
84   Image buttonImage = Dali::ResourceImage::New( UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
85   Image selectedImage = Dali::ResourceImage::New( SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
86   Image disabledImage = Dali::ResourceImage::New( DISABLED_UNSELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
87   Image disabledSelectedImage = Dali::ResourceImage::New( DISABLED_SELECTED_BUTTON_IMAGE_DIR, ResourceImage::ON_DEMAND, ResourceImage::NEVER );
88
89   SetButtonImage( ImageActor::New( buttonImage ) );
90   SetSelectedImage( ImageActor::New( selectedImage ) );
91   SetDisabledImage( ImageActor::New( disabledImage ) );
92   SetDisabledSelectedImage( ImageActor::New( disabledSelectedImage ) );
93
94   RelayoutRequest();
95 }
96
97 void RadioButton::OnButtonUp()
98 {
99   if( ButtonDown == GetState() )
100   {
101     // Don't allow selection on an already selected radio button
102     if( !IsSelected() )
103     {
104       SetSelected( !IsSelected() );
105     }
106   }
107 }
108
109 void RadioButton::OnLabelSet()
110 {
111   Actor& label = GetLabel();
112
113   if( label )
114   {
115     label.SetParentOrigin( ParentOrigin::CENTER_LEFT );
116     label.SetAnchorPoint( AnchorPoint::CENTER_LEFT );
117
118     // Radio button width is FIT_TO_CHILDREN, so the label must have a sensible policy to fill out the space
119     if( label.GetResizePolicy( Dimension::WIDTH ) == ResizePolicy::FILL_TO_PARENT )
120     {
121       label.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::WIDTH );
122     }
123
124     if( IsSelected() && GetSelectedImage() )
125     {
126       label.SetX( GetSelectedImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
127     }
128     else if( GetButtonImage() )
129     {
130       label.SetX( GetButtonImage().GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
131     }
132     else
133     {
134       label.SetX( DISTANCE_BETWEEN_IMAGE_AND_LABEL );
135     }
136   }
137 }
138
139 void RadioButton::OnSelected()
140 {
141   Actor& label = GetLabel();
142
143   PaintState paintState = GetPaintState();
144   switch( paintState )
145   {
146     case UnselectedState:
147     {
148       Actor parent = Self().GetParent();
149       if( parent )
150       {
151         for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
152         {
153           Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast( parent.GetChildAt( i ) );
154           if( radioButtonChild && radioButtonChild != Self() )
155           {
156             radioButtonChild.SetSelected( false );
157           }
158         }
159       }
160
161       Actor& selectedImage = GetSelectedImage();
162       if( label && selectedImage )
163       {
164         label.SetX( selectedImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
165       }
166       break;
167     }
168     case SelectedState:
169     {
170       Actor& buttonImage = GetButtonImage();
171       if( label && buttonImage )
172       {
173         label.SetX( buttonImage.GetNaturalSize().width + DISTANCE_BETWEEN_IMAGE_AND_LABEL );
174       }
175       break;
176     }
177     default:
178     {
179       break;
180     }
181   }
182 }
183
184 } // namespace Internal
185
186 } // namespace Toolkit
187
188 } // namespace Dali