Make radio buttons work with size negotiation, bug fixes.
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / factory / localized-control-factory-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 // CLASS HEADER
19 #include "localized-control-factory-impl.h"
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/public-api/controls/control-impl.h>
23 #include <dali/integration-api/debug.h>
24
25 // EXTERNAL INCLUDES
26 #include <libintl.h>
27
28 using std::string;
29 using namespace Dali;
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Internal
38 {
39
40 namespace // unnamed namespace
41 {
42
43
44 }
45
46 LocalizedControlFactory::LocalizedControlFactory()
47 : mObjectEntries(),
48   mSignalsConnected( false )
49 {
50 }
51
52 LocalizedControlFactory::~LocalizedControlFactory()
53 {
54 }
55
56 Dali::Toolkit::TextView LocalizedControlFactory::CreateLocalizedTextView( const std::string& textID, const std::string& textDomain, const std::string& textViewTheme )
57 {
58   if( !mSignalsConnected )
59   {
60     Stage::GetCurrent().GetObjectRegistry().ObjectDestroyedSignal().Connect( this, &LocalizedControlFactory::OnObjectDestruction );
61     Adaptor::Get().LanguageChangedSignal().Connect( this, &LocalizedControlFactory::OnLanguageChanged );
62     mSignalsConnected = true;
63   }
64
65   const string& localizedText = dgettext(textDomain.c_str(), textID.c_str());
66   Dali::Toolkit::TextView textView = Dali::Toolkit::TextView::New();
67   textView.SetText(localizedText);
68
69   LocalisedStringInfo info(textID, textDomain, textViewTheme);
70
71   mObjectEntries[textView.GetObjectPtr()] = info;
72
73   return textView;
74 }
75
76 void LocalizedControlFactory::OnObjectDestruction( const Dali::RefObject* objectPointer )
77 {
78   if(!mObjectEntries.empty())
79   {
80     //Needs optimization. All the destructed objects are currently checked for existence in entries.
81     mObjectEntries.erase(objectPointer);
82   }
83 }
84
85 void LocalizedControlFactory::OnLanguageChanged( Dali::Adaptor& adaptor)
86 {
87   if(!mObjectEntries.empty())
88   {
89     ObjectEntriesIterator iter = mObjectEntries.begin();
90     ObjectEntriesIterator iterEnd = mObjectEntries.end();
91     while(iter != iterEnd)
92     {
93       RefObject* refObjectPtr = const_cast<RefObject*> (iter->first);
94       BaseHandle handle(static_cast<BaseObject*>(refObjectPtr));
95       LocalisedStringInfo info = iter->second;
96
97       const string& localizedText = dgettext(info.textDomain.c_str(), info.textID.c_str());
98
99       Toolkit::TextView textView =  Dali::Toolkit::TextView::DownCast( handle );
100
101       if(textView)
102       {
103         textView.SetText( localizedText );
104       }
105       else
106       {
107         DALI_ASSERT_ALWAYS(false && "Corrupt TextView internal pointer in entries!");
108       }
109
110       ++iter;
111     }
112   }
113 }
114
115
116
117 } // namespace Internal
118
119 } // namespace Toolkit
120
121 } // namespace Dali