Buttons to use Visuals
[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/integration-api/debug.h>
24 #include <dali/public-api/object/type-registry.h>
25 #include <dali/public-api/images/resource-image.h>
26
27 #if defined(DEBUG_ENABLED)
28   extern Debug::Filter* gLogButtonFilter;
29 #endif
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::RadioButton::New();
46 }
47
48 TypeRegistration typeRegistration( typeid( Toolkit::RadioButton ), typeid( Toolkit::Button ), Create);
49
50 }
51
52 Dali::Toolkit::RadioButton RadioButton::New()
53 {
54   // Create the implementation, temporarily owned on stack
55   IntrusivePtr< RadioButton > internalRadioButton = new RadioButton();
56
57   // Pass ownership to CustomActor
58   Dali::Toolkit::RadioButton radioButton(*internalRadioButton);
59
60   // Second-phase init of the implementation
61   // This can only be done after the CustomActor connection has been made...
62   internalRadioButton->Initialize();
63
64   return radioButton;
65 }
66
67 RadioButton::RadioButton()
68 {
69   SetTogglableButton(true);
70 }
71
72 RadioButton::~RadioButton()
73 {
74 }
75
76 void RadioButton::OnInitialize()
77 {
78   Button::OnInitialize();
79 }
80
81 void RadioButton::OnButtonUp()
82 {
83   DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "RadioButton::OnStateChange selecting:%s\n", ( (!IsSelected())?"true":"false" ) );
84
85   // Don't allow un-selection on an already selected radio button, can only un-select by selecting a sibling radio button
86   if( !IsSelected() )
87   {
88     SetSelected( !IsSelected() );
89   }
90 }
91
92 void RadioButton::OnStateChange( State newState )
93 {
94   // Radio button can be part of a group, if a button in the group is selected then all others should be unselected
95   DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "RadioButton::OnStateChange state(%d)\n", newState );
96
97    switch( newState )
98    {
99      case SELECTED_STATE:
100      {
101        Actor parent = Self().GetParent();
102        if( parent )
103        {
104          for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
105          {
106            Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast( parent.GetChildAt( i ) );
107            if( radioButtonChild && radioButtonChild != Self() )
108            {
109              radioButtonChild.SetSelected( false );
110            }
111          }
112        }
113      }
114
115      default:
116      {
117        break;
118      }
119    }
120 }
121
122 } // namespace Internal
123
124 } // namespace Toolkit
125
126 } // namespace Dali