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
21#include "widgets/AButton.h"
22#include "Internat.h"
23#include "Observer.h"
24#include "Theme.h"
25#include "Prefs.h"
26
27template<class WindowBase>
29 public WindowBase,
30 public PrefsListener
31{
33 std::optional<TranslatableString> mTranslatableLabel;
36public:
37 template <typename... Args>
38 ThemedWindowWrapper(Args&&... args) : WindowBase( std::forward<Args>(args)... )
39 {
42 }
43
45 {
47 if(index != -1)
48 WindowBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
49 }
50
52 {
54 if(index != -1)
55 WindowBase::SetForegroundColour(theTheme.Colour(mForegroundColorIndex));
56 }
57
59 {
60 mTranslatableLabel = std::move(label);
61 WindowBase::SetLabel(mTranslatableLabel->Translation());
62 }
63
64 void UpdatePrefs() override
65 {
68 WindowBase::SetLabel(mTranslatableLabel->Translation());
69 }
70
71protected:
73 {
74 if (message.appearance)
75 {
76 if(mBackgroundColorIndex != -1)
77 WindowBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
78 if(mForegroundColorIndex != -1)
79 WindowBase::SetForegroundColour(theTheme.Colour(mForegroundColorIndex));
80 WindowBase::Refresh();
81 }
82 }
83};
84
85template<class ButtonBase>
87 public ButtonBase,
88 public PrefsListener
89{
91 int mBitmapIndex { -1 };
97 std::optional<TranslatableString> mTranslatableLabel;
98
99public:
100 template <typename... Args>
101 ThemedButtonWrapper(Args&&... args) : ButtonBase( std::forward<Args>(args)... )
102 {
105 }
106
107 int GetBitmapIndex() const
108 {
109 return mBitmapIndex;
110 }
111
112 void SetBitmapFocusIndex(int index)
113 {
114 mFocusBitmapIndex = index;
115 ButtonBase::SetBitmapFocus(theTheme.Bitmap(mFocusBitmapIndex));
116 }
117
118 void SetBitmapIndex(int index)
119 {
120 mBitmapIndex = index;
121 ButtonBase::SetBitmap(theTheme.Bitmap(mBitmapIndex));
122 }
123
124 void SetBitmapLabelIndex(int index)
125 {
126 mLabelBitmapIndex = index;
127 ButtonBase::SetBitmapLabel(theTheme.Bitmap(mLabelBitmapIndex));
128 }
129 void SetBitmapPressedIndex(int index)
130 {
131 mPressedBitmapIndex = index;
132 ButtonBase::SetBitmapPressed(theTheme.Bitmap(mPressedBitmapIndex));
133 }
134
135 void SetBitmapCurrentIndex(int index)
136 {
137 mCurrentBitmapIndex = index;
138 ButtonBase::SetBitmapCurrent(theTheme.Bitmap(mCurrentBitmapIndex));
139 }
140
142 {
143 mBackgroundColorIndex = index;
144 if(index != -1)
145 ButtonBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
146 }
147
149 {
150 mTranslatableLabel = std::move(label);
151 ButtonBase::SetLabel(mTranslatableLabel->Translation());
152 }
153
154 void UpdatePrefs() override
155 {
158 ButtonBase::SetLabel(mTranslatableLabel->Translation());
159 }
160
161protected:
163 {
164 if (message.appearance)
165 {
166 if(mBitmapIndex != -1)
167 ButtonBase::SetBitmap(theTheme.Bitmap(mBitmapIndex));
168 if(mLabelBitmapIndex != -1)
169 ButtonBase::SetBitmapLabel(theTheme.Bitmap(mLabelBitmapIndex));
170 if(mPressedBitmapIndex != -1)
171 ButtonBase::SetBitmapPressed(theTheme.Bitmap(mPressedBitmapIndex));
172 if(mCurrentBitmapIndex != -1)
173 ButtonBase::SetBitmapCurrent(theTheme.Bitmap(mCurrentBitmapIndex));
174 if(mFocusBitmapIndex != -1)
175 ButtonBase::SetBitmapFocus(theTheme.Bitmap(mFocusBitmapIndex));
176
177 if(mBackgroundColorIndex != -1)
178 ButtonBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
179 ButtonBase::Refresh();
180 }
181 }
182};
183
184template<typename AButtonBase>
186 : public AButtonBase
187 , public PrefsListener
188{
190
191 std::map<int, std::array<int, AButton::AButtonStateCount>> mSets;
194 std::optional<TranslatableString> mTranslatableLabel;
195
196public:
197 template <typename... Args>
198 ThemedAButtonWrapper(Args&&... args) : AButtonBase( std::forward<Args>(args)... )
199 {
202 }
203
204 void SetImageIndices(int setIndex, int up, int over, int down, int overDown, int disabled)
205 {
206 mSets[setIndex] = { up, over, down, overDown, disabled };
207 AButtonBase::SetAlternateImages(
208 setIndex,
209 theTheme.Image(up),
210 theTheme.Image(over),
211 theTheme.Image(down),
212 theTheme.Image(overDown),
213 theTheme.Image(disabled)
214 );
215 }
216
218 {
219 mBackgroundColorIndex = index;
220 if(index != -1)
221 AButtonBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
222 }
223
225 {
226 mForegroundColorIndex = index;
227 if(index != -1)
228 AButtonBase::SetForegroundColour(theTheme.Colour(mForegroundColorIndex));
229 }
230
232 {
233 mTranslatableLabel = std::move(label);
234 AButtonBase::SetLabel(*mTranslatableLabel);
235 }
236
237 void UpdatePrefs() override
238 {
241 {
242 AButtonBase::SetLabel(*mTranslatableLabel);
243 AButtonBase::Refresh(false);
244 }
245 }
246
247private:
248
250 {
251 if (message.appearance)
252 {
253 for(const auto& p : mSets)
254 AButtonBase::SetAlternateImages(
255 p.first,
261 );
262
263 if(mBackgroundColorIndex != -1)
264 AButtonBase::SetBackgroundColour(theTheme.Colour(mBackgroundColorIndex));
265
266 if(mForegroundColorIndex != -1)
267 AButtonBase::SetForegroundColour(theTheme.Colour(mForegroundColorIndex));
268
269 AButtonBase::Refresh(false);
270 }
271 }
272
273};
TranslatableString label
Definition: TagsEditor.cpp:163
THEME_API Theme theTheme
Definition: Theme.cpp:82
@ AButtonUp
Definition: AButton.h:148
@ AButtonDis
Definition: AButton.h:152
@ AButtonOver
Definition: AButton.h:149
@ AButtonDown
Definition: AButton.h:150
@ AButtonOverDown
Definition: AButton.h:151
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:543
virtual void UpdatePrefs()=0
Definition: Prefs.cpp:124
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
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:112