1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/font_smoothing_win.h"
7 #include "base/memory/singleton.h"
8 #include "ui/base/win/singleton_hwnd.h"
12 // Helper class to cache font smoothing settings and listen for notifications
13 // to re-query them from the system.
14 class CachedFontSmoothingSettings
: public ui::SingletonHwnd::Observer
{
16 static CachedFontSmoothingSettings
* GetInstance();
18 // Returns the cached Windows font smoothing settings. Queries the settings
19 // via Windows APIs and begins listening for changes when called for the
21 void GetFontSmoothingSettings(bool* smoothing_enabled
,
22 bool* cleartype_enabled
);
25 friend struct DefaultSingletonTraits
<CachedFontSmoothingSettings
>;
27 CachedFontSmoothingSettings();
28 virtual ~CachedFontSmoothingSettings();
30 // Listener for WM_SETTINGCHANGE notifications.
31 virtual void OnWndProc(HWND hwnd
,
34 LPARAM lparam
) OVERRIDE
;
36 // Queries the font settings from the system.
37 void QueryFontSettings();
39 // Indicates whether the MessagePumpObserver has been registered.
42 // Indicates whether |smoothing_enabled_| and |cleartype_enabled_| are valid
43 // or need to be re-queried from the system.
44 bool need_to_query_settings_
;
46 // Indicates that font smoothing is enabled.
47 bool smoothing_enabled_
;
49 // Indicates that the ClearType font smoothing is enabled.
50 bool cleartype_enabled_
;
52 DISALLOW_COPY_AND_ASSIGN(CachedFontSmoothingSettings
);
56 CachedFontSmoothingSettings
* CachedFontSmoothingSettings::GetInstance() {
57 return Singleton
<CachedFontSmoothingSettings
>::get();
60 void CachedFontSmoothingSettings::GetFontSmoothingSettings(
61 bool* smoothing_enabled
,
62 bool* cleartype_enabled
) {
63 // If cached settings are stale, query them from the OS.
64 if (need_to_query_settings_
) {
66 need_to_query_settings_
= false;
68 if (!observer_added_
) {
69 ui::SingletonHwnd::GetInstance()->AddObserver(this);
70 observer_added_
= true;
72 *smoothing_enabled
= smoothing_enabled_
;
73 *cleartype_enabled
= cleartype_enabled_
;
76 CachedFontSmoothingSettings::CachedFontSmoothingSettings()
77 : observer_added_(false),
78 need_to_query_settings_(true),
79 smoothing_enabled_(false),
80 cleartype_enabled_(false) {
83 CachedFontSmoothingSettings::~CachedFontSmoothingSettings() {
84 // Can't remove the SingletonHwnd observer here since SingletonHwnd may have
85 // been destroyed already (both singletons).
88 void CachedFontSmoothingSettings::OnWndProc(HWND hwnd
,
92 if (message
== WM_SETTINGCHANGE
)
93 need_to_query_settings_
= true;
96 void CachedFontSmoothingSettings::QueryFontSettings() {
97 smoothing_enabled_
= false;
98 cleartype_enabled_
= false;
100 BOOL enabled
= false;
101 if (SystemParametersInfo(SPI_GETFONTSMOOTHING
, 0, &enabled
, 0) && enabled
) {
102 smoothing_enabled_
= true;
104 UINT smooth_type
= 0;
105 if (SystemParametersInfo(SPI_GETFONTSMOOTHINGTYPE
, 0, &smooth_type
, 0))
106 cleartype_enabled_
= (smooth_type
== FE_FONTSMOOTHINGCLEARTYPE
);
114 void GetCachedFontSmoothingSettings(bool* smoothing_enabled
,
115 bool* cleartype_enabled
) {
116 CachedFontSmoothingSettings::GetInstance()->GetFontSmoothingSettings(