Bug 1683849 [wpt PR 26986] - Fix hyphenation for words with leading/trailing punctuat...
[gecko.git] / widget / nsXPLookAndFeel.h
blob66b22294f7320755e31b719831017b7a69998e2f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef __nsXPLookAndFeel
7 #define __nsXPLookAndFeel
9 #include "mozilla/LookAndFeel.h"
10 #include "mozilla/ServoStyleConsts.h"
11 #include "mozilla/widget/LookAndFeelTypes.h"
12 #include "nsTArray.h"
14 class nsLookAndFeel;
16 struct nsLookAndFeelIntPref {
17 const char* name;
18 mozilla::LookAndFeel::IntID id;
19 bool isSet;
20 int32_t intVar;
23 struct nsLookAndFeelFloatPref {
24 const char* name;
25 mozilla::LookAndFeel::FloatID id;
26 bool isSet;
27 float floatVar;
30 #define CACHE_BLOCK(x) (uint32_t(x) >> 5)
31 #define CACHE_BIT(x) (1 << (uint32_t(x) & 31))
33 #define COLOR_CACHE_SIZE (CACHE_BLOCK(uint32_t(LookAndFeel::ColorID::End)) + 1)
34 #define IS_COLOR_CACHED(x) \
35 (CACHE_BIT(x) & nsXPLookAndFeel::sCachedColorBits[CACHE_BLOCK(x)])
36 #define CLEAR_COLOR_CACHE(x) \
37 nsXPLookAndFeel::sCachedColors[uint32_t(x)] = 0; \
38 nsXPLookAndFeel::sCachedColorBits[CACHE_BLOCK(x)] &= ~(CACHE_BIT(x));
39 #define CACHE_COLOR(x, y) \
40 nsXPLookAndFeel::sCachedColors[uint32_t(x)] = y; \
41 nsXPLookAndFeel::sCachedColorBits[CACHE_BLOCK(x)] |= CACHE_BIT(x);
43 class nsXPLookAndFeel : public mozilla::LookAndFeel {
44 public:
45 virtual ~nsXPLookAndFeel();
47 static nsXPLookAndFeel* GetInstance();
48 static void Shutdown();
50 void Init();
52 // These functions will return a value specified by an override pref, if it
53 // exists, and otherwise will call into the NativeGetXxx function to get the
54 // platform-specific value.
56 // NS_ERROR_NOT_AVAILABLE is returned if there is neither an override pref or
57 // a platform-specific value.
58 nsresult GetColorValue(ColorID aID, bool aUseStandinsForNativeColors,
59 nscolor& aResult);
60 nsresult GetIntValue(IntID aID, int32_t& aResult);
61 nsresult GetFloatValue(FloatID aID, float& aResult);
62 // Same, but returns false if there is no platform-specific value.
63 // (There are no override prefs for font values.)
64 bool GetFontValue(FontID aID, nsString& aName, gfxFontStyle& aStyle) {
65 return NativeGetFont(aID, aName, aStyle);
68 virtual nsresult NativeGetInt(IntID aID, int32_t& aResult) = 0;
69 virtual nsresult NativeGetFloat(FloatID aID, float& aResult) = 0;
70 virtual nsresult NativeGetColor(ColorID aID, nscolor& aResult) = 0;
71 virtual bool NativeGetFont(FontID aID, nsString& aName,
72 gfxFontStyle& aStyle) = 0;
74 virtual void RefreshImpl();
76 virtual char16_t GetPasswordCharacterImpl() { return char16_t('*'); }
78 virtual bool GetEchoPasswordImpl() { return false; }
80 virtual uint32_t GetPasswordMaskDelayImpl() { return 600; }
82 using FullLookAndFeel = mozilla::widget::FullLookAndFeel;
83 using LookAndFeelCache = mozilla::widget::LookAndFeelCache;
84 using LookAndFeelInt = mozilla::widget::LookAndFeelInt;
85 using LookAndFeelFont = mozilla::widget::LookAndFeelFont;
86 using LookAndFeelColor = mozilla::widget::LookAndFeelColor;
87 using LookAndFeelTheme = mozilla::widget::LookAndFeelTheme;
89 virtual LookAndFeelCache GetCacheImpl();
90 virtual void SetCacheImpl(const LookAndFeelCache& aCache) {}
91 virtual void SetDataImpl(FullLookAndFeel&& aTables) {}
93 virtual void NativeInit() = 0;
95 virtual void WithThemeConfiguredForContent(
96 const std::function<void(const LookAndFeelTheme& aTheme)>& aFn) {
97 aFn(LookAndFeelTheme{});
100 protected:
101 nsXPLookAndFeel() = default;
103 static void IntPrefChanged(nsLookAndFeelIntPref* data);
104 static void FloatPrefChanged(nsLookAndFeelFloatPref* data);
105 static void ColorPrefChanged(unsigned int index, const char* prefName);
106 void InitFromPref(nsLookAndFeelIntPref* aPref);
107 void InitFromPref(nsLookAndFeelFloatPref* aPref);
108 void InitColorFromPref(int32_t aIndex);
109 bool IsSpecialColor(ColorID aID, nscolor& aColor);
110 bool ColorIsNotCSSAccessible(ColorID aID);
111 nscolor GetStandinForNativeColor(ColorID aID);
112 void RecordTelemetry();
113 virtual void RecordLookAndFeelSpecificTelemetry() {}
115 static void OnPrefChanged(const char* aPref, void* aClosure);
117 static bool sInitialized;
118 static nsLookAndFeelIntPref sIntPrefs[];
119 static nsLookAndFeelFloatPref sFloatPrefs[];
120 /* this length must not be shorter than the length of the longest string in
121 * the array see nsXPLookAndFeel.cpp
123 static const char sColorPrefs[][41];
124 static int32_t sCachedColors[size_t(LookAndFeel::ColorID::End)];
125 static int32_t sCachedColorBits[COLOR_CACHE_SIZE];
127 static nsXPLookAndFeel* sInstance;
128 static bool sShutdown;
131 #endif