Merge "Fix SWIG C# Build" into 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/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 bool RadioButton::OnToggleReleased()
82 {
83   // Radio button overrides toggle release (button up) as doesn't allow un-selection to be performed on it directly.
84   bool stateChanged = false;
85   if( !IsSelected() )
86   {
87     Button::SetSelected( true ); // Set button to selected as previously unselected
88     stateChanged = true;
89   }
90   return stateChanged;
91 }
92
93 void RadioButton::OnStateChange( State newState )
94 {
95   // Radio button can be part of a group, if a button in the group is selected then all others should be unselected
96   DALI_LOG_INFO( gLogButtonFilter, Debug::Verbose, "RadioButton::OnStateChange state(%d)\n", newState );
97
98    switch( newState )
99    {
100      case SELECTED_STATE:
101      {
102        Actor parent = Self().GetParent();
103        if( parent )
104        {
105          for( unsigned int i = 0; i < parent.GetChildCount(); ++i )
106          {
107            Dali::Toolkit::RadioButton radioButtonChild = Dali::Toolkit::RadioButton::DownCast( parent.GetChildAt( i ) );
108            if( radioButtonChild && radioButtonChild != Self() )
109            {
110              radioButtonChild.SetSelected( false );
111            }
112          }
113        }
114      }
115
116      default:
117      {
118        break;
119      }
120    }
121 }
122
123 } // namespace Internal
124
125 } // namespace Toolkit
126
127 } // namespace Dali