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