Merge "[4.0] new linebreak patch" into tizen_4.0
[platform/core/uifw/dali-adaptor.git] / adaptors / common / style-monitor-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 "style-monitor-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <fstream>
24 #include <sstream>
25 #include <dali/integration-api/debug.h>
26
27 // INTERNAL INCLUDES
28 #include <adaptor-impl.h>
29 #include <singleton-service-impl.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace Adaptor
38 {
39
40 namespace
41 {
42
43 #if defined(DEBUG_ENABLED)
44 Dali::Integration::Log::Filter* gLogFilter = Dali::Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_STYLE_MONITOR");
45 #endif
46
47 /**
48  * Use font client to get the system default font family
49  * @param[in] fontClient handle to font client
50  * @param[out] fontFamily string representing font family
51  */
52 void GetSystemDefaultFontFamily( TextAbstraction::FontClient& fontClient, std::string& fontFamily )
53 {
54   TextAbstraction::FontDescription defaultFontDescription;
55   if ( fontClient )
56   {
57     fontClient.GetDefaultPlatformFontDescription( defaultFontDescription );
58     fontFamily = defaultFontDescription.family;
59   }
60 }
61
62 } // unnamed namespace
63
64 Dali::StyleMonitor StyleMonitor::Get()
65 {
66   Dali::StyleMonitor styleMonitor;
67
68   Dali::SingletonService service( SingletonService::Get() );
69   if( service )
70   {
71     // Check whether the singleton is already created
72     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::StyleMonitor ) );
73     if( handle )
74     {
75       // If so, downcast the handle
76       styleMonitor = Dali::StyleMonitor( dynamic_cast< StyleMonitor* >( handle.GetObjectPtr() ) );
77     }
78     else
79     {
80       styleMonitor = Dali::StyleMonitor( new StyleMonitor() );
81       service.Register( typeid( styleMonitor ), styleMonitor );
82     }
83   }
84
85   return styleMonitor;
86 }
87
88 StyleMonitor::StyleMonitor()
89 : mDefaultFontSize(-1),
90   mIgnoreGlobalFontSizeChange(-1)
91 {
92   mFontClient = TextAbstraction::FontClient::Get();
93   GetSystemDefaultFontFamily( mFontClient, mDefaultFontFamily );
94   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "StyleMonitor::StyleMonitor::DefaultFontFamily(%s)\n", mDefaultFontFamily.c_str() );
95   mDefaultFontSize = mFontClient.GetDefaultFontSize();
96 }
97
98 StyleMonitor::~StyleMonitor()
99 {
100 }
101
102 void StyleMonitor::StyleChanged( StyleChange::Type styleChange )
103 {
104   switch ( styleChange )
105   {
106     case StyleChange::DEFAULT_FONT_CHANGE:
107     {
108       if ( mFontClient )
109       {
110         mFontClient.ResetSystemDefaults();
111         GetSystemDefaultFontFamily( mFontClient, mDefaultFontFamily );
112       }
113       DALI_LOG_INFO( gLogFilter, Debug::Verbose, "StyleMonitor::StyleChanged::DefaultFontFamily(%s)\n", mDefaultFontFamily.c_str() );
114       break;
115     }
116
117     case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
118     {
119       mDefaultFontSize = mFontClient.GetDefaultFontSize();
120       break;
121     }
122
123     case StyleChange::THEME_CHANGE:
124     {
125       break;
126     }
127   }
128
129   EmitStyleChangeSignal(styleChange);
130 }
131
132 std::string StyleMonitor::GetDefaultFontFamily() const
133 {
134   return mDefaultFontFamily;
135 }
136
137 std::string StyleMonitor::GetDefaultFontStyle() const
138 {
139   return mDefaultFontStyle;
140 }
141
142 int StyleMonitor::GetDefaultFontSize() const
143 {
144   return mDefaultFontSize;
145 }
146
147 const std::string& StyleMonitor::GetTheme() const
148 {
149   return mUserDefinedThemeFilePath;
150 }
151
152 void StyleMonitor::SetTheme(const std::string& path)
153 {
154   mUserDefinedThemeFilePath = path;
155   EmitStyleChangeSignal( StyleChange::THEME_CHANGE );
156 }
157
158 bool StyleMonitor::LoadThemeFile( const std::string& filename, std::string& output )
159 {
160   bool retval( false );
161   std::ifstream in( filename.c_str(), std::ios::in );
162   if( in )
163   {
164     std::stringstream buffer;
165     buffer << in.rdbuf();
166
167     output = buffer.str();
168
169     in.close();
170     retval = true;
171   }
172   return retval;
173 }
174
175 void StyleMonitor::EmitStyleChangeSignal( StyleChange::Type styleChange )
176 {
177   if( !mStyleChangeSignal.Empty() )
178   {
179     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "StyleMonitor::EmitStyleChangeSignal\n" );
180     Dali::StyleMonitor handle( this );
181     mStyleChangeSignal.Emit( handle, styleChange );
182   }
183 }
184
185 int StyleMonitor::GetIgnoreGlobalFontSizeChange()
186 {
187   return mIgnoreGlobalFontSizeChange;
188 }
189
190 void StyleMonitor::SetIgnoreGlobalFontSizeChange(int value)
191 {
192   mIgnoreGlobalFontSizeChange = value;
193 }
194
195 Dali::StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
196 {
197   return mStyleChangeSignal;
198 }
199
200 } // namespace Adaptor
201
202 } // namespace Internal
203
204 } // namespace Dali