Audacity  3.0.3
Theme.h
Go to the documentation of this file.
1 /**********************************************************************
2 
3  Audacity: A Digital Audio Editor
4 
5  Theme.h
6 
7  James Crook
8 
9  Audacity is free software.
10  This file is licensed under the wxWidgets license, see License.txt
11 
12 **********************************************************************/
13 
14 #ifndef __AUDACITY_THEME__
15 #define __AUDACITY_THEME__
16 
17 #include <map>
18 #include <vector>
19 #include <wx/arrstr.h>
20 #include <wx/defs.h>
21 #include <wx/gdicmn.h>
23 
24 #include "Prefs.h"
25 
28 
31 {
32  Light,
33  Dark,
35 };
36 
37 class wxArrayString;
38 class wxBitmap;
39 class wxColour;
40 class wxImage;
41 class wxPen;
42 
43 class ChoiceSetting;
44 
45 // JKC: will probably change name from 'teBmps' to 'tIndexBmp';
46 using teBmps = int;
47 
49 {
53 };
54 
56 {
57  resFlagNone =0x00,
61  resFlagInternal = 0x08, // For image manipulation. Don't save or load.
62  resFlagSkip = 0x10
63 };
64 
66 class THEME_API FlowPacker
67 {
68 public:
69  explicit FlowPacker(int width);
71  void GetNextPosition( int xSize, int ySize );
72  void SetNewGroup( int iGroupSize );
73  void SetColourGroup( );
74  wxRect Rect();
75  wxRect RectInner();
76  void RectMid( int &x, int &y );
77 
78  // These 4 should become private again...
79  int mFlags = resFlagPaired;
80  int mxPos = 0;
81  int myPos = 0;
82  int myHeight = 0;
83  int mBorderWidth = 1;
84 
85 private:
86  int iImageGroupSize = 1;
87  int iImageGroupIndex = -1;
88  int mOldFlags = resFlagPaired;
89  int myPosBase = 0;
90  int mxCacheWidth = 0;
91 
92  int mComponentWidth = 0;
93  int mComponentHeight = 0;
94 
95 };
96 
97 struct ThemeSet
98 {
99  // wxImage, wxBitmap copy cheaply using reference counting
100  std::vector<wxImage> mImages;
101  std::vector<wxBitmap> mBitmaps;
102  wxArrayString mBitmapNames;
103  std::vector<int> mBitmapFlags;
104 
105  std::vector<wxColour> mColours;
106  wxArrayString mColourNames;
107 
108  bool bInitialised = false;
109  bool bRecolourOnLoad = false; // Request to recolour.
110 };
111 
112 class THEME_API ThemeBase /* not final */
113 {
114 public:
115  ThemeBase(void);
116  ThemeBase ( const ThemeBase & ) = delete;
117  ThemeBase &operator =( const ThemeBase & ) = delete;
118 public:
119  virtual ~ThemeBase(void);
120 
121 public:
122  virtual void EnsureInitialised() = 0;
123 
124  // Typically statically constructed:
125  struct THEME_API RegisteredTheme {
127  PreferredSystemAppearance preferredSystemAppearance,
128  const std::vector<unsigned char> &data
130  );
131  ~RegisteredTheme();
132 
135  const std::vector<unsigned char>& data;
136  };
137 
138  void SwitchTheme( teThemeType Theme );
139  void LoadTheme( teThemeType Theme );
140  void RegisterImage( int &flags, int &iIndex,char const** pXpm, const wxString & Name);
141  void RegisterImage( int &flags, int &iIndex, const wxImage &Image, const wxString & Name );
142  void RegisterColour( int &iIndex, const wxColour &Clr, const wxString & Name );
143 
144  teThemeType GetFallbackThemeType();
145  void CreateImageCache();
146  bool CreateOneImageCache(teThemeType id, bool bBinarySave);
147  bool ReadImageCache( teThemeType type = {}, bool bOkIfNotFound=false);
148  void LoadThemeComponents( bool bOkIfNotFound =false);
149  void LoadOneThemeComponents( teThemeType id, bool bOkIfNotFound = false);
150  void SaveThemeComponents();
151  bool SaveOneThemeComponents( teThemeType id );
152  void SaveThemeAsCode();
153  void WriteImageDefs( );
154  void WriteImageMap( );
155  void WriteOneImageMap( teThemeType id );
156  static bool LoadPreferredTheme();
157  void RecolourBitmap( int iIndex, wxColour From, wxColour To );
158  void RecolourTheme();
159 
160  int ColourDistance( wxColour & From, wxColour & To );
161  wxColour & Colour( int iIndex );
162  wxBitmap & Bitmap( int iIndex );
163  wxImage & Image( int iIndex );
164  wxSize ImageSize( int iIndex );
165 
166  void ReplaceImage( int iIndex, wxImage * pImage );
167  void RotateImageInto( int iTo, int iFrom, bool bClockwise );
168 
169  void SetBrushColour( wxBrush & Brush, int iIndex );
170  void SetPenColour( wxPen & Pen, int iIndex );
171 
172  // Utility function that combines a bitmap and a mask, both in XPM format.
173  wxImage MaskedImage( char const ** pXpm, char const ** pMask );
174  // Utility function that takes a 32 bit bitmap and makes it into an image.
175  wxImage MakeImageWithAlpha( wxBitmap & Bmp );
176 
179  SetOnPreferredSystemAppearanceChanged(OnPreferredSystemAppearanceChanged handler);
180 
181  // Reclaim resources after finished with theme editing
182  void DeleteUnusedThemes();
183 
184 protected:
185  // wxImage, wxBitmap copy cheaply using reference counting
186  std::vector<wxImage> mImages;
187  std::vector<wxBitmap> mBitmaps;
188  wxArrayString mBitmapNames;
189  std::vector<int> mBitmapFlags;
190 
191  std::vector<wxColour> mColours;
192  wxArrayString mColourNames;
193 
196 
197  std::map<Identifier, ThemeSet> mSets;
198  ThemeSet *mpSet = nullptr;
199 };
200 
201 class THEME_API Theme final : public ThemeBase
202 {
203 public:
204  Theme(void);
205 public:
206  ~Theme(void);
207 public:
208  void EnsureInitialised() override;
209  void RegisterImagesAndColours();
210 };
211 
212 extern THEME_API Theme theTheme;
213 
214 extern THEME_API BoolSetting
216 ;
217 
218 extern THEME_API ChoiceSetting
219  &GUITheme()
220 ;
221 
222 #endif // __AUDACITY_THEME__
teBmps
int teBmps
Definition: ImageManipulation.h:48
ThemeBase::mImages
std::vector< wxImage > mImages
Definition: Theme.h:186
resFlagPaired
@ resFlagPaired
Definition: Theme.h:58
teResourceFlags
teResourceFlags
Definition: Theme.h:56
ThemeSet
Definition: Theme.h:98
Theme
Based on ThemeBase, Theme manages image and icon resources.
Definition: Theme.h:202
ThemeBase::mBitmaps
std::vector< wxBitmap > mBitmaps
Definition: Theme.h:187
ThemeSet::bInitialised
bool bInitialised
Definition: Theme.h:108
resFlagSkip
@ resFlagSkip
Definition: Theme.h:62
PreferredSystemAppearance
PreferredSystemAppearance
A system theme, that matches selected theme best (only works on macOS with builtin themes).
Definition: Theme.h:31
BoolSetting
This specialization of Setting for bool adds a Toggle method to negate the saved value.
Definition: Prefs.h:204
resTypeBitmap
@ resTypeBitmap
Definition: Theme.h:51
ComponentInterfaceSymbol.h
GUITheme
THEME_API ChoiceSetting & GUITheme()
Definition: Theme.cpp:1247
PreferredSystemAppearance::Dark
@ Dark
ComponentInterfaceSymbol
ComponentInterfaceSymbol pairs a persistent string identifier used internally with an optional,...
Definition: ComponentInterfaceSymbol.h:27
ThemeSet::mImages
std::vector< wxImage > mImages
Definition: Theme.h:100
ThemeBase::mSets
std::map< Identifier, ThemeSet > mSets
Definition: Theme.h:197
resFlagInternal
@ resFlagInternal
Definition: Theme.h:61
ThemeBase::mColours
std::vector< wxColour > mColours
Definition: Theme.h:191
Identifier
An explicitly nonlocalized string, not meant for the user to see.
Definition: Identifier.h:22
resFlagNone
@ resFlagNone
Definition: Theme.h:57
ThemeBase::RegisteredTheme
Definition: Theme.h:125
ThemeSet::mColourNames
wxArrayString mColourNames
Definition: Theme.h:106
ChoiceSetting
Definition: Prefs.h:270
ThemeBase::mBitmapNames
wxArrayString mBitmapNames
Definition: Theme.h:188
ThemeSet::mBitmapFlags
std::vector< int > mBitmapFlags
Definition: Theme.h:103
ThemeBase::RegisteredTheme::data
const std::vector< unsigned char > & data
Definition: Theme.h:135
PreferredSystemAppearance::HighContrastDark
@ HighContrastDark
ThemeBase::mColourNames
wxArrayString mColourNames
Definition: Theme.h:192
resTypeColour
@ resTypeColour
Definition: Theme.h:50
theTheme
THEME_API Theme theTheme
Definition: Theme.cpp:79
ThemeBase
Theme management - Image loading and saving.
Definition: Theme.h:113
FlowPacker::~FlowPacker
~FlowPacker()
Definition: Theme.h:70
ThemeSet::mBitmaps
std::vector< wxBitmap > mBitmaps
Definition: Theme.h:101
FlowPacker
A cursor for iterating the theme bitmap.
Definition: Theme.h:67
ThemeBase::EnsureInitialised
virtual void EnsureInitialised()=0
ThemeBase::mOnPreferredSystemAppearanceChanged
OnPreferredSystemAppearanceChanged mOnPreferredSystemAppearanceChanged
Definition: Theme.h:195
ThemeBase::OnPreferredSystemAppearanceChanged
std::function< void(PreferredSystemAppearance)> OnPreferredSystemAppearanceChanged
Definition: Theme.h:177
ThemeBase::RegisteredTheme::symbol
const EnumValueSymbol symbol
Definition: Theme.h:133
ThemeBase::ThemeBase
ThemeBase(const ThemeBase &)=delete
ThemeBase::RegisteredTheme::preferredSystemAppearance
const PreferredSystemAppearance preferredSystemAppearance
Definition: Theme.h:134
resTypeImage
@ resTypeImage
Definition: Theme.h:52
ThemeSet::bRecolourOnLoad
bool bRecolourOnLoad
Definition: Theme.h:109
PreferredSystemAppearance::Light
@ Light
ThemeSet::mBitmapNames
wxArrayString mBitmapNames
Definition: Theme.h:102
ThemeBase::mBitmapFlags
std::vector< int > mBitmapFlags
Definition: Theme.h:189
Prefs.h
GUIBlendThemes
THEME_API BoolSetting GUIBlendThemes
Definition: Theme.cpp:1303
ThemeSet::mColours
std::vector< wxColour > mColours
Definition: Theme.h:105
resFlagCursor
@ resFlagCursor
Definition: Theme.h:59
resFlagNewLine
@ resFlagNewLine
Definition: Theme.h:60
teResourceType
teResourceType
The index of a bitmap resource in Theme Resources.
Definition: Theme.h:49