Audacity 3.2.0
ThemedWrappers.h
Go to the documentation of this file.
1/*!********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file ThemedWrappers.h
6
7 @author Vitaly Sverchinsky
8
9 A set of classes which decorate given base classes with
10 simplified theme support. Note that classes are declared
11 final, which means they could be only used 'in-place'.
12
13**********************************************************************/
14
15#pragma once
16
17#include <optional>
18#include <array>
19#include <map>
20#include <type_traits>
21
22#include "widgets/AButton.h"
23#include "Internat.h"
24#include "Observer.h"
25#include "Theme.h"
26#include "Prefs.h"
27
29
30template<class WindowBase>
32 public WindowBase,
33 public std::conditional_t<std::is_base_of_v<PrefsListener, WindowBase>, PrefsListenerDummy, PrefsListener>
34{
36 std::optional<TranslatableString> mTranslatableLabel;
39
40 using PrefsListenerBase = std::conditional_t<std::is_base_of_v<PrefsListener, WindowBase>, WindowBase, PrefsListener>;
41
42public:
43 template <typename... Args>
44 ThemedWindowWrapper(Args&&... args) : WindowBase( std::forward<Args>(args)... )
45 {
48 }
49
51 {
53 if(index != -1)
54 WindowBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
55 }
56
58 {
60 if(index != -1)
61 WindowBase::SetForegroundColour(theTheme.Colour(mForegroundColorIndex));
62 }
63
65 {
66 mTranslatableLabel = std::move(label);
67 WindowBase::SetLabel(mTranslatableLabel->Translation());
68 }
69
70 void UpdatePrefs() override
71 {
72 PrefsListenerBase::UpdatePrefs();
74 WindowBase::SetLabel(mTranslatableLabel->Translation());
75 }
76
77protected:
79 {
80 if (message.appearance)
81 {
82 if(mBackgroundColorIndex != -1)
83 WindowBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
84 if(mForegroundColorIndex != -1)
85 WindowBase::SetForegroundColour(theTheme.Colour(mForegroundColorIndex));
86 WindowBase::Refresh();
87 }
88 }
89};
90
91template<class ButtonBase>
93 public ButtonBase,
94 public PrefsListener
95{
97 int mBitmapIndex { -1 };
103 std::optional<TranslatableString> mTranslatableLabel;
104
105public:
106 template <typename... Args>
107 ThemedButtonWrapper(Args&&... args) : ButtonBase( std::forward<Args>(args)... )
108 {
111 }
112
113 int GetBitmapIndex() const
114 {
115 return mBitmapIndex;
116 }
117
118 void SetBitmapFocusIndex(int index)
119 {
120 mFocusBitmapIndex = index;
121 ButtonBase::SetBitmapFocus(theTheme.Bitmap(mFocusBitmapIndex));
122 }
123
124 void SetBitmapIndex(int index)
125 {
126 mBitmapIndex = index;
127 ButtonBase::SetBitmap(theTheme.Bitmap(mBitmapIndex));
128 }
129
130 void SetBitmapLabelIndex(int index)
131 {
132 mLabelBitmapIndex = index;
133 ButtonBase::SetBitmapLabel(theTheme.Bitmap(mLabelBitmapIndex));
134 }
135 void SetBitmapPressedIndex(int index)
136 {
137 mPressedBitmapIndex = index;
138 ButtonBase::SetBitmapPressed(theTheme.Bitmap(mPressedBitmapIndex));
139 }
140
141 void SetBitmapCurrentIndex(int index)
142 {
143 mCurrentBitmapIndex = index;
144 ButtonBase::SetBitmapCurrent(theTheme.Bitmap(mCurrentBitmapIndex));
145 }
146
148 {
149 mBackgroundColorIndex = index;
150 if(index != -1)
151 ButtonBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
152 }
153
155 {
156 mTranslatableLabel = std::move(label);
157 ButtonBase::SetLabel(mTranslatableLabel->Translation());
158 }
159
160 void UpdatePrefs() override
161 {
164 ButtonBase::SetLabel(mTranslatableLabel->Translation());
165 }
166
167protected:
169 {
170 if (message.appearance)
171 {
172 if(mBitmapIndex != -1)
173 ButtonBase::SetBitmap(theTheme.Bitmap(mBitmapIndex));
174 if(mLabelBitmapIndex != -1)
175 ButtonBase::SetBitmapLabel(theTheme.Bitmap(mLabelBitmapIndex));
176 if(mPressedBitmapIndex != -1)
177 ButtonBase::SetBitmapPressed(theTheme.Bitmap(mPressedBitmapIndex));
178 if(mCurrentBitmapIndex != -1)
179 ButtonBase::SetBitmapCurrent(theTheme.Bitmap(mCurrentBitmapIndex));
180 if(mFocusBitmapIndex != -1)
181 ButtonBase::SetBitmapFocus(theTheme.Bitmap(mFocusBitmapIndex));
182
183 if(mBackgroundColorIndex != -1)
184 ButtonBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
185 ButtonBase::Refresh();
186 }
187 }
188};
189
190template<typename AButtonBase>
192 : public AButtonBase
193 , public PrefsListener
194{
196
197 std::map<int, std::array<int, AButton::AButtonStateCount>> mSets;
200 std::optional<TranslatableString> mTranslatableLabel;
201
202public:
203 template <typename... Args>
204 ThemedAButtonWrapper(Args&&... args) : AButtonBase( std::forward<Args>(args)... )
205 {
208 }
209
210 void SetImageIndices(int setIndex, int up, int over, int down, int overDown, int disabled)
211 {
212 mSets[setIndex] = { up, over, down, overDown, disabled };
213 AButtonBase::SetAlternateImages(
214 setIndex,
215 theTheme.Image(up),
216 theTheme.Image(over),
217 theTheme.Image(down),
218 theTheme.Image(overDown),
219 theTheme.Image(disabled)
220 );
221 }
222
224 {
225 mBackgroundColorIndex = index;
226 if(index != -1)
227 AButtonBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
228 }
229
231 {
232 mForegroundColorIndex = index;
233 if(index != -1)
234 AButtonBase::SetForegroundColour(theTheme.Colour(mForegroundColorIndex));
235 }
236
238 {
239 mTranslatableLabel = std::move(label);
240 AButtonBase::SetLabel(*mTranslatableLabel);
241 }
242
243 void UpdatePrefs() override
244 {
247 {
248 AButtonBase::SetLabel(*mTranslatableLabel);
249 AButtonBase::Refresh(false);
250 }
251 }
252
253private:
254
256 {
257 if (message.appearance)
258 {
259 for(const auto& p : mSets)
260 AButtonBase::SetAlternateImages(
261 p.first,
267 );
268
269 if(mBackgroundColorIndex != -1)
270 AButtonBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
271
272 if(mForegroundColorIndex != -1)
273 AButtonBase::SetForegroundColour(theTheme.Colour(mForegroundColorIndex));
274
275 AButtonBase::Refresh(false);
276 }
277 }
278
279};
TranslatableString label
Definition: TagsEditor.cpp:165
THEME_API Theme theTheme
Definition: Theme.cpp:82
@ AButtonUp
Definition: AButton.h:227
@ AButtonDis
Definition: AButton.h:231
@ AButtonOver
Definition: AButton.h:228
@ AButtonDown
Definition: AButton.h:229
@ AButtonOverDown
Definition: AButton.h:230
Subscription Subscribe(Callback callback)
Connect a callback to the Publisher; later-connected are called earlier.
Definition: Observer.h:199
A move-only handle representing a connection to a Publisher.
Definition: Observer.h:70
A listener notified of changes in preferences.
Definition: Prefs.h:652
virtual void UpdatePrefs()=0
Definition: Prefs.cpp:154
wxColour & Colour(int iIndex)
wxImage & Image(int iIndex)
wxBitmap & Bitmap(int iIndex)
void UpdatePrefs() override
void SetBackgroundColorIndex(int index)
Observer::Subscription mThemeChangeSubscription
void SetForegroundColorIndex(int index)
void SetTranslatableLabel(TranslatableString label)
ThemedAButtonWrapper(Args &&... args)
void OnThemeChange(ThemeChangeMessage message)
void SetImageIndices(int setIndex, int up, int over, int down, int overDown, int disabled)
std::map< int, std::array< int, AButton::AButtonStateCount > > mSets
std::optional< TranslatableString > mTranslatableLabel
void SetBitmapFocusIndex(int index)
void SetBitmapIndex(int index)
void SetBackgroundColorIndex(int index)
ThemedButtonWrapper(Args &&... args)
int GetBitmapIndex() const
void UpdatePrefs() override
void SetBitmapCurrentIndex(int index)
Observer::Subscription mThemeChangeSubscription
void SetBitmapLabelIndex(int index)
void OnThemeChange(ThemeChangeMessage message)
void SetTranslatableLabel(TranslatableString label)
std::optional< TranslatableString > mTranslatableLabel
void SetBitmapPressedIndex(int index)
void SetBackgroundColorIndex(int index)
std::optional< TranslatableString > mTranslatableLabel
void UpdatePrefs() override
std::conditional_t< std::is_base_of_v< PrefsListener, WindowBase >, WindowBase, PrefsListener > PrefsListenerBase
void OnThemeChange(ThemeChangeMessage message)
Observer::Subscription mThemeChangeSubscription
void SetTranslatableLabel(TranslatableString label)
void SetForegroundColorIndex(int index)
ThemedWindowWrapper(Args &&... args)
Holds a msgid for the translation catalog; may also bind format arguments.
STL namespace.
std::optional< PreferredSystemAppearance > appearance
Definition: Theme.h:111