Revert "Revert "[SRUK] (StyleManager) Create a style manager""
[platform/core/uifw/dali-toolkit.git] / base / dali-toolkit / internal / controls / style-change-processor.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 #include "style-change-processor.h"
18
19 #include <boost/thread/tss.hpp>
20
21 #include "dali-toolkit/public-api/controls/control.h"
22 #include "dali-toolkit/public-api/controls/control-impl.h"
23
24 namespace Dali
25 {
26
27 namespace Toolkit
28 {
29
30 namespace Internal
31 {
32
33 namespace
34 {
35 boost::thread_specific_ptr<StyleChangeProcessor> gThreadLocalStyleChangeProcessor;
36 } // unnamed namespace
37
38 StyleChangeProcessor::~StyleChangeProcessor()
39 {
40 }
41
42 void StyleChangeProcessor::Register( Control* control )
43 {
44   // Only create a style change processor if we have not created one in the local thread storage.
45   if (!gThreadLocalStyleChangeProcessor.get())
46   {
47     gThreadLocalStyleChangeProcessor.reset(new StyleChangeProcessor);
48   }
49
50   gThreadLocalStyleChangeProcessor->Reference();
51
52   std::vector<Control*>& controls( gThreadLocalStyleChangeProcessor->mControls );
53
54   // Store the Control raw pointer to allow traverse all off stage controls.
55   DALI_ASSERT_ALWAYS( ( std::find( controls.begin(), controls.end(), control ) == controls.end() ) && "StyleChangeProcessor::Register. The control has been registered twice." );
56
57   controls.push_back( control );
58 }
59
60 void StyleChangeProcessor::Unregister( Control* control )
61 {
62   if (gThreadLocalStyleChangeProcessor.get())
63   {
64     std::vector<Control*>& controls( gThreadLocalStyleChangeProcessor->mControls );
65
66     // Removes the control from the vector as is not needed to notify it about style changes.
67     std::vector<Control*>::iterator it = std::find( controls.begin(), controls.end(), control );
68     std::vector<Control*>::iterator endIt = controls.end();
69     DALI_ASSERT_ALWAYS( ( it != endIt ) && "StyleChangeProcessor::UnRegister. The control has not been registered in the StyleChangeProcessor." );
70
71     *it = *(endIt - 1);
72     controls.erase( endIt - 1 );
73
74     gThreadLocalStyleChangeProcessor->Unreference();
75   }
76 }
77
78 void StyleChangeProcessor::Reference()
79 {
80   ++mCount;
81 }
82
83 void StyleChangeProcessor::Unreference()
84 {
85   if (--mCount == 0)
86   {
87     // If our count is 0, then we should reset the local storage which will call our destructor as well.
88     gThreadLocalStyleChangeProcessor.reset();
89   }
90 }
91
92 StyleChangeProcessor::StyleChangeProcessor()
93 : mCount(0)
94 {
95   if ( Adaptor::IsAvailable() )
96   {
97     StyleMonitor::Get().StyleChangeSignal().Connect(this, &StyleChangeProcessor::StyleChanged);
98   }
99 }
100
101 void StyleChangeProcessor::StyleChanged(Dali::StyleMonitor styleMonitor, StyleChange styleChange)
102 {
103   // Traverse all registered controls.
104   std::vector<Control*>& controls( gThreadLocalStyleChangeProcessor->mControls );
105
106   for( std::vector<Control*>::iterator it = controls.begin(), endIt = controls.end(); it != endIt; ++it )
107   {
108     // Create a valid handle.
109     IntrusivePtr<Control> implementation( *it );
110
111     if (implementation)
112     {
113       implementation->OnStyleChange( styleChange );
114     }
115   }
116 }
117
118 } // namespace Internal
119
120 } // namespace Toolkit
121
122 } // namespace Dali