2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "style-change-processor.h"
20 #include <boost/thread/tss.hpp>
22 #include "dali-toolkit/public-api/controls/control.h"
23 #include "dali-toolkit/public-api/controls/control-impl.h"
36 boost::thread_specific_ptr<StyleChangeProcessor> gThreadLocalStyleChangeProcessor;
37 } // unnamed namespace
39 StyleChangeProcessor::~StyleChangeProcessor()
43 void StyleChangeProcessor::Register( ControlImpl* control )
45 // Only create a style change processor if we have not created one in the local thread storage.
46 if (!gThreadLocalStyleChangeProcessor.get())
48 gThreadLocalStyleChangeProcessor.reset(new StyleChangeProcessor);
51 gThreadLocalStyleChangeProcessor->Reference();
53 std::vector<ControlImpl*>& controls( gThreadLocalStyleChangeProcessor->mControls );
55 // Store the Control raw pointer to allow traverse all off stage controls.
56 DALI_ASSERT_ALWAYS( ( std::find( controls.begin(), controls.end(), control ) == controls.end() ) && "StyleChangeProcessor::Register. The control has been registered twice." );
58 controls.push_back( control );
61 void StyleChangeProcessor::Unregister( ControlImpl* control )
63 if (gThreadLocalStyleChangeProcessor.get())
65 std::vector<ControlImpl*>& controls( gThreadLocalStyleChangeProcessor->mControls );
67 // Removes the control from the vector as is not needed to notify it about style changes.
68 std::vector<ControlImpl*>::iterator it = std::find( controls.begin(), controls.end(), control );
69 std::vector<ControlImpl*>::iterator endIt = controls.end();
70 DALI_ASSERT_ALWAYS( ( it != endIt ) && "StyleChangeProcessor::UnRegister. The control has not been registered in the StyleChangeProcessor." );
73 controls.erase( endIt - 1 );
75 gThreadLocalStyleChangeProcessor->Unreference();
79 void StyleChangeProcessor::Reference()
84 void StyleChangeProcessor::Unreference()
88 // If our count is 0, then we should reset the local storage which will call our destructor as well.
89 gThreadLocalStyleChangeProcessor.reset();
93 StyleChangeProcessor::StyleChangeProcessor()
96 if ( Adaptor::IsAvailable() )
98 StyleMonitor::Get().StyleChangeSignal().Connect(this, &StyleChangeProcessor::StyleChanged);
102 void StyleChangeProcessor::StyleChanged(Dali::StyleMonitor styleMonitor, StyleChange styleChange)
104 // Traverse all registered controls.
105 std::vector<ControlImpl*>& controls( gThreadLocalStyleChangeProcessor->mControls );
107 for( std::vector<ControlImpl*>::iterator it = controls.begin(), endIt = controls.end(); it != endIt; ++it )
109 // Create a valid handle.
110 IntrusivePtr<ControlImpl> implementation( *it );
114 implementation->OnStyleChange( styleChange );
119 } // namespace Internal
121 } // namespace Toolkit