File: Theme.cpp

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (1319 lines) | stat: -rw-r--r-- 40,438 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
/**********************************************************************

  Audacity: A Digital Audio Editor

  Theme.cpp

  James Crook

  Audacity is free software.
  This file is licensed under the wxWidgets license, see License.txt

********************************************************************//**

\class Theme
\brief Based on ThemeBase, Theme manages image and icon resources.

   Theme is a class which manages theme resources.
   It maps sets of ids to the resources and to names of the resources,
   so that they can be loaded/saved from files.

   Theme adds the Audacity specific images to ThemeBase.

\see \ref Themability

*//*****************************************************************//**

\class ThemeBase
\brief Theme management - Image loading and saving.

   Base for the Theme class. ThemeBase is a generic
   non-Audacity specific class.

\see \ref Themability

*//*****************************************************************//**

\class FlowPacker
\brief Packs rectangular boxes into a rectangle, using simple first fit.

This class is currently used by Theme to pack its images into the image
cache.  Perhaps someday we will improve FlowPacker and make it more flexible,
and use it for toolbar and window layouts too.

*//*****************************************************************//**

\class SourceOutputStream
\brief Allows us to capture output of the Save .png and 'pipe' it into
our own output function which gives a series of numbers.

This class is currently used by Theme to pack its images into the image
cache.  Perhaps someday we will improve FlowPacker and make it more flexible,
and use it for toolbar and window layouts too.

*//*****************************************************************/

#include "Theme.h"



#include <map>

#include <wx/wxprec.h>
#include <wx/brush.h>
#include <wx/pen.h>
#include <wx/file.h>
#include <wx/ffile.h>
#include <wx/txtstrm.h>
#include <wx/wfstream.h>
#include <wx/mstream.h>
#include <wx/settings.h>
#include <regex>

#include "AllThemeResources.h"
#include "BasicUI.h"
#include "FileNames.h"
#include "Prefs.h"
#include "ImageManipulation.h"
#include "Internat.h"
#include "MemoryX.h"

// theTheme is a global variable.
THEME_API Theme theTheme;

Theme::Theme(void)
{
}

Theme::~Theme(void)
{
}

namespace {
// Side of a square painted in one of the theme colours in image caches
constexpr int iColSize = 10;

wxString ThemeFilePrefix( teThemeType id )
{
   auto strings = wxSplit(id.GET(), L'-');
   wxString result;
   for (auto &string : strings)
      result += string.Capitalize();
   return result;
}

//! Has the side-effect of ensuring existence of the directory
FilePath ThemeSubdir(const FilePath &themeDir, Identifier id)
{
   return FileNames::MkDir(
      wxFileName( FileNames::MkDir(themeDir), id.GET() ).GetFullPath() );
}

//! Has the side-effect of ensuring existence of the directory
FilePath ThemeComponentsDir(const FilePath &themeDir, Identifier id)
{
   return FileNames::MkDir( wxFileName( ThemeSubdir(themeDir, id), wxT("Components") ).GetFullPath() );
}

constexpr auto ImageCacheFileName = L"ImageCache.png";

constexpr auto ImageMapFileName = L"ImageCache.htm";

constexpr auto ColorFileName = L"Colors.txt";

FilePath ThemeImageDefsAsCee(const FilePath &themeDir)
{
   return wxFileName( themeDir, wxT("ThemeImageDefsAsCee.h") ).GetFullPath();
}

constexpr auto ThemeCacheFileName = L"ThemeAsCeeCode.h";

FilePath ThemeComponent(const wxString &dir, const wxString &Str)
{
   return wxFileName( dir, Str, wxT("png") ).GetFullPath();
}
}

void Theme::EnsureInitialised()
{
   if ( !mpSet || mpSet->bInitialised )
      return;
   RegisterImagesAndColours();

#ifdef EXPERIMENTAL_EXTRA_THEME_RESOURCES
   extern void RegisterExtraThemeResources();
   RegisterExtraThemeResources();
#endif
}

FilePath ThemeBase::GetFilePath()
{
   if (mThemeDir.empty())
      SetFilePath(
         wxFileName(FileNames::DataDir(), wxT("Theme")).GetFullPath());
   return mThemeDir;
}

void ThemeBase::SetFilePath(const FilePath &path)
{
   mThemeDir = path;
}

bool ThemeBase::LoadPreferredTheme()
{
   Identifier theme = GUITheme().Read();
   theTheme.LoadTheme( theme );
   return true;
}

void Theme::RegisterImagesAndColours()
{
   if ( !mpSet || mpSet->bInitialised )
      return;
   mpSet->bInitialised = true;

// This initialises the variables e.g
// RegisterImage( myFlags, bmpRecordButton, some image, wxT("RecordButton"));
   int myFlags = resFlagPaired;
   NameSet allNames;
#define THEME_INITS
#include "AllThemeResources.h"


}

ThemeBase::ThemeBase(void)
{
}

ThemeBase::~ThemeBase(void)
{
}

using ThemeCacheLookup =
   std::map< EnumValueSymbol, const ThemeBase::RegisteredTheme& >;

static ThemeCacheLookup &GetThemeCacheLookup()
{
   static ThemeCacheLookup theMap;
   return theMap;
}

ThemeBase::RegisteredTheme::RegisteredTheme(
   EnumValueSymbol symbol, PreferredSystemAppearance preferredSystemAppearance,
   const std::vector<unsigned char> &data )
   : symbol{ symbol }
   , preferredSystemAppearance { preferredSystemAppearance }
   , data { data }
{
   GetThemeCacheLookup().emplace(symbol, *this);
}

ThemeBase::RegisteredTheme::~RegisteredTheme()
{
   GetThemeCacheLookup().erase(symbol);
}

void ThemeBase::SwitchTheme( teThemeType Theme )
{
   // Switch the active theme set
   mpSet = &mSets[Theme.empty() ? GUITheme().Read() : Theme];
   auto &resources = *mpSet;
   EnsureInitialised();

   const bool cbOkIfNotFound = true;

   if( !ReadImageCache( Theme, cbOkIfNotFound ) )
   {
      // THEN get the default set.
      ReadImageCache( GetFallbackThemeType(), !cbOkIfNotFound );

      // JKC: Now we could go on and load the individual images
      // on top of the default images using the commented out
      // code that follows...
      //
      // However, I think it is better to get the user to
      // build a NEW image cache, which they can do easily
      // from the Theme preferences tab.
#if 0
      // and now add any available component images.
      LoadComponents( cbOkIfNotFound );

      // JKC: I'm usure about doing this next step automatically.
      // Suppose the disk is write protected?
      // Is having the image cache created automatically
      // going to confuse users?  Do we need version specific names?
      // and now save the combined image as a cache for later use.
      // We should load the images a little faster in future as a result.
      CreateImageCache();
#endif
   }
}

/// This function is called to load the initial Theme images.
/// It does not though cause the GUI to refresh.
void ThemeBase::LoadTheme( teThemeType Theme )
{
   SwitchTheme( Theme );

   // Post-processing steps after loading of the cache

   // Two always overwritten images
   RotateImageInto( bmpRecordBeside, bmpRecordBelow, false );
   RotateImageInto( bmpRecordBesideDisabled, bmpRecordBelowDisabled, false );

   // Next line is not required as we haven't yet built the GUI
   // when this function is (or should be) called.
   // AColor::ApplyUpdatedImages();

   // The next step doesn't post-process the theme data.  It only modifies
   // system settings.  So it is not necessary to make it conditional on
   // preferences, for avoidance of errors in the use of Theme preferences.
   // (See commit 2020217)
   Publish({ mPreferredSystemAppearance });
}

void ThemeBase::RecolourBitmap( int iIndex, wxColour From, wxColour To )
{
   wxImage Image( Bitmap( iIndex ).ConvertToImage() );

   std::unique_ptr<wxImage> pResult = ChangeImageColour(
      &Image, From, To );
   ReplaceImage( iIndex, pResult.get() );
}

int ThemeBase::ColourDistance( wxColour & From, wxColour & To ){
   return 
      abs( From.Red() - To.Red() )
      + abs( From.Green() - To.Green() )
      + abs( From.Blue() - To.Blue() );
}

wxImage ThemeBase::MaskedImage( char const ** pXpm, char const ** pMask )
{
   wxBitmap Bmp1( pXpm );
   wxBitmap Bmp2( pMask );

//   wxLogDebug( wxT("Image 1: %i Image 2: %i"),
//      Bmp1.GetDepth(), Bmp2.GetDepth() );

   // We want a 24-bit-depth bitmap if all is working, but on some
   // platforms it might just return -1 (which means best available
   // or not relevant).
   // JKC: \todo check that we're not relying on 24 bit elsewhere.
   wxASSERT( Bmp1.GetDepth()==-1 || Bmp1.GetDepth()==24);
   wxASSERT( Bmp1.GetDepth()==-1 || Bmp2.GetDepth()==24);

   int i,nBytes;
   nBytes = Bmp1.GetWidth() * Bmp1.GetHeight();
   wxImage Img1( Bmp1.ConvertToImage());
   wxImage Img2( Bmp2.ConvertToImage());

//   unsigned char *src = Img1.GetData();
   unsigned char *mk = Img2.GetData();
   //wxImage::setAlpha requires memory allocated with malloc, not NEW
   MallocString<unsigned char> alpha{
      static_cast<unsigned char*>(malloc( nBytes )) };

   // Extract alpha channel from second XPM.
   for(i=0;i<nBytes;i++)
   {
      alpha[i] = mk[0];
      mk+=3;
   }

   Img1.SetAlpha( alpha.release() );

   //dmazzoni: the top line does not work on wxGTK
   //wxBitmap Result( Img1, 32 );
   //wxBitmap Result( Img1 );

   return Img1;
}

// Legacy function to allow use of an XPM where no theme image was defined.
// Bit depth and mask needs review.
// Note that XPMs don't offer translucency, so unsuitable for a round shape overlay, 
// for example.
void ThemeBase::RegisterImage( NameSet &allNames,
   int &flags, int &iIndex, char const ** pXpm, const wxString & Name )
{
   wxBitmap Bmp( pXpm );
   wxImage Img( Bmp.ConvertToImage() );
   // The next line recommended by http://forum.audacityteam.org/viewtopic.php?f=50&t=96765
   Img.SetMaskColour(0xDE, 0xDE, 0xDE);
   Img.InitAlpha();

   //dmazzoni: the top line does not work on wxGTK
   //wxBitmap Bmp2( Img, 32 );
   //wxBitmap Bmp2( Img );

   RegisterImage( allNames, flags, iIndex, Img, Name );
}

void ThemeBase::RegisterImage( NameSet &allNames,
   int &flags, int &iIndex, const wxImage &Image, const wxString & Name )
{
   auto &resources = *mpSet;
   resources.mImages.push_back( Image );

#ifdef __APPLE__
   // On Mac, bitmaps with alpha don't work.
   // So we convert to a mask and use that.
   // It isn't quite as good, as alpha gives smoother edges.
   //[Does not affect the large control buttons, as for those we do
   // the blending ourselves anyway.]
   wxImage TempImage( Image );
   TempImage.ConvertAlphaToMask();
   resources.mBitmaps.push_back( wxBitmap( TempImage ) );
#else
   resources.mBitmaps.push_back( wxBitmap( Image ) );
#endif

   flags &= ~resFlagSkip;
   auto index = resources.mBitmaps.size() - 1;
   if (iIndex == -1) {
      // First time assignment of global variable identifying an image
      iIndex = index;
      mBitmapNames.push_back( Name );
      mBitmapFlags.push_back( flags );
      wxASSERT(allNames.insert(Name).second);
   }
   else {
      // If revisiting for another theme set,
      // images should be re-done in the same sequence
      wxASSERT(iIndex == index);
      wxASSERT(mBitmapNames[index] == Name);
      wxASSERT(mBitmapFlags[index] == flags);
   }
}

void ThemeBase::RegisterColour( NameSet &allNames,
   int &iIndex, const wxColour &Clr, const wxString & Name )
{
   auto &resources = *mpSet;
   resources.mColours.push_back( Clr );
   auto index = resources.mColours.size() - 1;
   if (iIndex == -1) {
      // First time assignment of global variable identifying a colour
      iIndex = index;
      mColourNames.push_back( Name );
      wxASSERT(allNames.insert(Name).second);
   }
   else {
      // If revisiting for another theme set,
      // colours should be re-done in the same sequence
      wxASSERT(iIndex == index);
      wxASSERT(mColourNames[index] == Name);
   }
}

FlowPacker::FlowPacker(int width)
   : mxCacheWidth{ width }
{
   SetNewGroup(1);
}

void FlowPacker::SetNewGroup( int iGroupSize )
{
   myPosBase +=myHeight * iImageGroupSize;
   mxPos =0;
   mOldFlags = mFlags;
   iImageGroupSize = iGroupSize;
   iImageGroupIndex = -1;
   mComponentWidth=0;
}

void FlowPacker::SetColourGroup( )
{
   myPosBase = 750;
   mxPos =0;
   mOldFlags = mFlags;
   iImageGroupSize = 1;
   iImageGroupIndex = -1;
   mComponentWidth=0;
   myHeight = 11;
}

void FlowPacker::GetNextPosition( int xSize, int ySize )
{
   xSize += 2*mBorderWidth;
   ySize += 2*mBorderWidth;
   // if the height has increased, then we are on a NEW group.
   if(( ySize > myHeight )||(((mFlags ^ mOldFlags )& ~resFlagSkip)!=0))
   {
      SetNewGroup( ((mFlags & resFlagPaired)!=0) ? 2 : 1 );
      myHeight = ySize;
//      mFlags &= ~resFlagNewLine;
//      mOldFlags = mFlags;
   }

   iImageGroupIndex++;
   if( iImageGroupIndex == iImageGroupSize )
   {
      iImageGroupIndex = 0;
      mxPos += mComponentWidth;
   }

   if(mxPos > (mxCacheWidth - xSize ))
   {
      SetNewGroup(iImageGroupSize);
      iImageGroupIndex++;
      myHeight = ySize;
   }
   myPos = myPosBase + iImageGroupIndex * myHeight;

   mComponentWidth = xSize;
   mComponentHeight = ySize;
}

wxRect FlowPacker::Rect()
{
   return wxRect( mxPos, myPos, mComponentWidth, mComponentHeight);
}

wxRect FlowPacker::RectInner()
{
   return Rect().Deflate( mBorderWidth, mBorderWidth );
}

void FlowPacker::RectMid( int &x, int &y )
{
   x = mxPos + mComponentWidth/2;
   y = myPos + mComponentHeight/2;
}


/// \brief Helper class based on wxOutputStream used to get a png file in text format
///
/// The trick used here is that wxWidgets can write a PNG image to a stream.
/// By writing to a custom stream, we get to see each byte of data in turn, convert
/// it to text, put in commas, and then write that out to our own text stream.
class SourceOutputStream final : public wxOutputStream
{
public:
   SourceOutputStream(){;};
   int OpenFile(const FilePath & Filename);
   virtual ~SourceOutputStream();

protected:
   size_t OnSysWrite(const void *buffer, size_t bufsize) override;
   wxFile File;
   int nBytes;
};

/// Opens the file and also adds a standard comment at the start of it.
int SourceOutputStream::OpenFile(const FilePath & Filename)
{
   nBytes = 0;
   bool bOk;
   bOk = File.Open( Filename, wxFile::write );
   if( bOk )
   {
      File.Write( wxString::Format(
         wxT("///   @file %s\r\n"), wxFileName(Filename).GetFullName()));
      File.Write( wxT("///   @brief This file was Auto-Generated.\r\n") );
      File.Write( wxT("///\r\n") );
      File.Write( wxT("///   It is included by Theme.cpp.\r\n") );
      File.Write( wxT("///   Only check this into Git if you've read and understood the guidelines!\r\n\r\n   ") );
   }
   return bOk;
}

/// This is the 'callback' function called with each write of PNG data
/// to the stream.  This is where we conveet to text and add commas.
size_t SourceOutputStream::OnSysWrite(const void *buffer, size_t bufsize)
{
   wxString Temp;
   for(int i=0;i<(int)bufsize;i++)
   {
      // Write one byte with a comma
      Temp = wxString::Format( wxT("%i,"),(int)(((unsigned char*)buffer)[i]) );
      File.Write( Temp );
      nBytes++;
      // New line if more than 20 bytes written since last time.
      if( (nBytes %20)==0 )
      {
         File.Write( wxT("\r\n   "));
      }
   }
   return bufsize;
}

/// Destructor.  We close our text stream in here.
SourceOutputStream::~SourceOutputStream()
{
   File.Write( wxT("\r\n") );
   File.Close();
}


// Must be wide enough for bmpAudacityLogo. Use double width + 10.
const int ImageCacheWidth = 440;

const int ImageCacheHeight = 836;

void ThemeBase::CreateImageCache()
{
   ValueRestorer cleanup{ mpSet };
   for (auto &[key, data] : GetThemeCacheLookup())
      if (!CreateOneImageCache(key.Internal(), true))
         // Some file failed to save, message was given
         return;
   BasicUI::ShowMessageBox(
/* i18n-hint: A theme is a consistent visual style across an application's
graphical user interface, including choices of colors, and similarity of images
such as those on button controls.  Audacity can load and save alternative
themes. */
      XO("Themes written to:\n  %s/*/%s.")
         .Format( GetFilePath(), ImageCacheFileName ));
}

bool ThemeBase::CreateOneImageCache( teThemeType id, bool bBinarySave )
{
   SwitchTheme( id );
   auto &resources = *mpSet;

   wxImage ImageCache( ImageCacheWidth, ImageCacheHeight );
   ImageCache.SetRGB( wxRect( 0,0,ImageCacheWidth, ImageCacheHeight), 1,1,1);//Not-quite black.

   // Ensure we have an alpha channel...
   if( !ImageCache.HasAlpha() )
   {
      ImageCache.InitAlpha();
   }

   FlowPacker context{ ImageCacheWidth };

//#define IMAGE_MAP
#ifdef IMAGE_MAP
   wxLogDebug( wxT("<img src=\"ImageCache.png\" usemap=\"#map1\">" ));
   wxLogDebug( wxT("<map name=\"map1\">") );
#endif

   // Save the bitmaps
   for (size_t i = 0; i < resources.mImages.size() ; ++i)
   {
      wxImage &SrcImage = resources.mImages[i];
      context.mFlags = mBitmapFlags[i];
      if( !(mBitmapFlags[i] & resFlagInternal) )
      {
         context.GetNextPosition( SrcImage.GetWidth(), SrcImage.GetHeight());
         ImageCache.SetRGB( context.Rect(), 0xf2, 0xb0, 0x27 );
         if( !(context.mFlags & resFlagSkip) )
            PasteSubImage( &ImageCache, &SrcImage, 
               context.mxPos + context.mBorderWidth,
               context.myPos + context.mBorderWidth);
         else
            ImageCache.SetRGB( context.RectInner(), 1,1,1);
#ifdef IMAGE_MAP
         // No href in html.  Uses title not alt.
         wxRect R( context.Rect() );
         wxLogDebug( wxT("<area title=\"Bitmap:%s\" shape=rect coords=\"%i,%i,%i,%i\">"),
            mBitmapNames[i],
            R.GetLeft(), R.GetTop(), R.GetRight(), R.GetBottom() );
#endif
      }
   }

   // Now save the colours.
   int x,y;

   context.SetColourGroup();
   for (size_t i = 0; i < resources.mColours.size(); ++i)
   {
      context.GetNextPosition( iColSize, iColSize );
      wxColour c = resources.mColours[i];
      ImageCache.SetRGB( context.Rect() , 0xf2, 0xb0, 0x27 );
      ImageCache.SetRGB( context.RectInner() , c.Red(), c.Green(), c.Blue() );

      // YUCK!  No function in wxWidgets to set a rectangle of alpha...
      for(x=0;x<iColSize;x++)
      {
         for(y=0;y<iColSize;y++)
         {
            ImageCache.SetAlpha( context.mxPos + x, context.myPos+y, 255);
         }
      }
#ifdef IMAGE_MAP
      // No href in html.  Uses title not alt.
      wxRect R( context.Rect() );
      wxLogDebug( wxT("<area title=\"Colour:%s\" shape=rect coords=\"%i,%i,%i,%i\">"),
         mColourNames[i],
         R.GetLeft(), R.GetTop(), R.GetRight(), R.GetBottom() );
#endif
   }
#if TEST_CARD
   for ( unsigned i = 0; i < ImageCacheWidth; ++i)
      for ( unsigned j = 0; j < ImageCacheHeight; ++j) {
         int r = j &0xff;
         int g = i &0xff;
         int b = (j >> 8) | ((i>>4)&0xf0);
         wxRect R( i,j, 1, 1);
         ImageCache.SetRGB( R, r, g, b );
         ImageCache.SetAlpha( i,j, 255);
   }
#endif

#ifdef IMAGE_MAP
   wxLogDebug( "</map>" );
#endif

   using namespace BasicUI;

   // IF bBinarySave, THEN saving to a normal PNG file.
   if( bBinarySave )
   {
      auto dir = ThemeSubdir(GetFilePath(), id);
      auto FileName = wxFileName{ dir, ImageCacheFileName }.GetFullPath();

      // Perhaps we should prompt the user if they are overwriting
      // an existing theme cache?
#if 0
      if( wxFileExists( FileName ))
      {
         auto message =
//          XO(
//"Theme cache file:\n  %s\nalready exists.\nAre you sure you want to replace it?")
//             .Format( FileName );
            TranslatableString{ FileName };
         ShowMessageBox( message );
         return false;
      }
#endif
#if 0
      // Deliberate policy to use the fast/cheap blocky pixel-multiplication
      // algorithm, as this introduces no artifacts on repeated scale up/down.
      ImageCache.Rescale( 
         ImageCache.GetWidth()*4,
         ImageCache.GetHeight()*4,
         wxIMAGE_QUALITY_NEAREST );
#endif
      if( !ImageCache.SaveFile( FileName, wxBITMAP_TYPE_PNG ))
      {
         ShowMessageBox(
            XO("Audacity could not write file:\n  %s.")
               .Format( FileName ));
         return false;
      }
   }
   // ELSE saving to a C code textual version.
   else
   {
      // Generated header file is not put in the sub-directory for
      // the theme, but is instead distinguished by a prefix on the file name.
      // So the header can simply be copied into the source code tree.
      auto dir = GetFilePath();
      SourceOutputStream OutStream;
      auto name = ThemeFilePrefix(id) + ThemeCacheFileName;
      auto FileName = wxFileName{ dir, name }.GetFullPath();
      if( !OutStream.OpenFile( FileName ))
      {
         ShowMessageBox(
            XO("Audacity could not open file:\n  %s\nfor writing.")
               .Format( FileName ));
         return false;
      }
      if( !ImageCache.SaveFile(OutStream, wxBITMAP_TYPE_PNG ) )
      {
         ShowMessageBox(
            XO("Audacity could not write images to file:\n  %s.")
               .Format( FileName ));
         return false;
      }
   }
   return true;
}

void ThemeBase::WriteImageMap( )
{
   ValueRestorer cleanup{ mpSet };
   for (auto &[key, data] : GetThemeCacheLookup())
      WriteOneImageMap(key.Internal());
}

/// Writes an html file with an image map of the ImageCache
/// Very handy for seeing what each part is for.
void ThemeBase::WriteOneImageMap( teThemeType id )
{
   SwitchTheme( id );
   auto &resources = *mpSet;

   FlowPacker context{ ImageCacheWidth };
   
   auto dir = ThemeSubdir(GetFilePath(), id);
   auto FileName = wxFileName{ dir, ImageMapFileName }.GetFullPath();
   wxFFile File( FileName, wxT("wb") );// I'll put in NEW lines explicitly.
   if( !File.IsOpened() )
      return;

   File.Write( wxT("<html>\r\n"));
   File.Write( wxT("<body bgcolor=\"303030\">\r\n"));
   wxString Temp = wxString::Format( wxT("<img src=\"ImageCache.png\" width=\"%i\" usemap=\"#map1\">\r\n" ), ImageCacheWidth );
   File.Write( Temp );
   File.Write( wxT("<map name=\"map1\">\r\n") );

   for (size_t i = 0; i < resources.mImages.size(); ++i)
   {
      wxImage &SrcImage = resources.mImages[i];
      context.mFlags = mBitmapFlags[i];
      if( !(mBitmapFlags[i] & resFlagInternal) )
      {
         context.GetNextPosition( SrcImage.GetWidth(), SrcImage.GetHeight());
         // No href in html.  Uses title not alt.
         wxRect R( context.RectInner() );
         File.Write( wxString::Format(
            wxT("<area title=\"Bitmap:%s\" shape=rect coords=\"%i,%i,%i,%i\">\r\n"),
            mBitmapNames[i],
            R.GetLeft(), R.GetTop(), R.GetRight(), R.GetBottom()) );
      }
   }
   // Now save the colours.
   context.SetColourGroup();
   for (size_t i = 0; i < resources.mColours.size(); ++i)
   {
      context.GetNextPosition( iColSize, iColSize );
      // No href in html.  Uses title not alt.
      wxRect R( context.RectInner() );
      File.Write( wxString::Format( wxT("<area title=\"Colour:%s\" shape=rect coords=\"%i,%i,%i,%i\">\r\n"),
         mColourNames[i],
         R.GetLeft(), R.GetTop(), R.GetRight(), R.GetBottom()) );
   }
   File.Write( wxT("</map>\r\n") );
   File.Write( wxT("</body>\r\n"));
   File.Write( wxT("</html>\r\n"));
   // File will be closed automatically.
}

/// Writes a series of Macro definitions that can be used in the include file.
void ThemeBase::WriteImageDefs( )
{
   // The generated image defs macro calls depend only on sizes of images,
   // not contents, and so there is only one file good across all themes.

   auto &resources = *mpSet;
   EnsureInitialised();

   wxFFile File( ThemeImageDefsAsCee(GetFilePath()), wxT("wb") );
   if( !File.IsOpened() )
      return;
   teResourceFlags PrevFlags = (teResourceFlags)-1;
   for (size_t i = 0; i < resources.mImages.size(); ++i)
   {
      wxImage &SrcImage = resources.mImages[i];
      // No href in html.  Uses title not alt.
      if( PrevFlags != mBitmapFlags[i] )
      {
         PrevFlags = (teResourceFlags)mBitmapFlags[i];
         int t = (int)PrevFlags;
         wxString Temp;
         if( t==0 ) Temp = wxT(" resFlagNone ");
         if( t & resFlagPaired )   Temp += wxT(" resFlagPaired ");
         if( t & resFlagCursor )   Temp += wxT(" resFlagCursor ");
         if( t & resFlagNewLine )  Temp += wxT(" resFlagNewLine ");
         if( t & resFlagInternal ) Temp += wxT(" resFlagInternal ");
         Temp.Replace( wxT("  "), wxT(" | ") );

         File.Write( wxString::Format( wxT("\r\n   SET_THEME_FLAGS( %s );\r\n"),
            Temp ));
      }
      File.Write( wxString::Format(
         wxT("   DEFINE_IMAGE( bmp%s, wxImage( %i, %i ), wxT(\"%s\"));\r\n"),
         mBitmapNames[i],
         SrcImage.GetWidth(),
         SrcImage.GetHeight(),
         mBitmapNames[i]
         ));
   }
}


teThemeType ThemeBase::GetFallbackThemeType(){
// Fallback must be an internally supported type,
// to guarantee it is found.
   return "light";
}

/// Reads an image cache including images, cursors and colours.
/// @param type if empty means read from an external binary file.
///   otherwise the data is taken from a block of memory.
/// @param bOkIfNotFound if true means do not report absent file.
/// @return true iff we loaded the images.
bool ThemeBase::ReadImageCache( teThemeType type, bool bOkIfNotFound)
{
   auto &resources = *mpSet;
   EnsureInitialised();
   wxImage ImageCache;

   // Ensure we have an alpha channel...
//   if( !ImageCache.HasAlpha() )
//   {
//      ImageCache.InitAlpha();
//   }

   using namespace BasicUI;

   if( type.empty() || type == "custom" )
   {
      mPreferredSystemAppearance = PreferredSystemAppearance::Light;

      // Take the image cache file for the theme chosen in preferences
      auto dir = ThemeSubdir(GetFilePath(), GUITheme().Read());
      const auto &FileName =
         wxFileName{ dir, ImageCacheFileName }.GetFullPath();
      if( !wxFileExists( FileName ))
      {
         if( bOkIfNotFound )
            return false; // did not load the images, so return false.
         ShowMessageBox(
            XO("Audacity could not find file:\n  %s.\nTheme not loaded.")
               .Format( FileName ));
         return false;
      }
      if( !ImageCache.LoadFile( FileName, wxBITMAP_TYPE_PNG ))
      {
         ShowMessageBox(
            /* i18n-hint: Do not translate png.  It is the name of a file format.*/
            XO("Audacity could not load file:\n  %s.\nBad png format perhaps?")
               .Format( FileName ));
         return false;
      }
   }
   // ELSE we are reading from internal storage.
   else
   {
      size_t ImageSize = 0;
      const unsigned char * pImage = nullptr;
      auto &lookup = GetThemeCacheLookup();
      auto iter = lookup.find({type, {}});
      if (const auto end = lookup.end(); iter == end) {
         iter = lookup.find({"classic", {}});
         wxASSERT(iter != end);
      }

      mPreferredSystemAppearance = iter->second.preferredSystemAppearance;

      ImageSize = iter->second.data.size();
      if (ImageSize == 0)
         // This must be the image compiler
         return true;

      pImage = iter->second.data.data();
      //wxLogDebug("Reading ImageCache %p size %i", pImage, ImageSize );
      wxMemoryInputStream InternalStream( pImage, ImageSize );

      if( !ImageCache.LoadFile( InternalStream, wxBITMAP_TYPE_PNG ))
      {
         // If we get this message, it means that the data in file
         // was not a valid png image.
         // Most likely someone edited it by mistake,
         // Or some experiment is being tried with NEW formats for it.
         ShowMessageBox(
            XO(
"Audacity could not read its default theme.\nPlease report the problem."));
         return false;
      }
      //wxLogDebug("Read %i by %i", ImageCache.GetWidth(), ImageCache.GetHeight() );
   }

   // Resize a large image down.
   if( ImageCache.GetWidth() > ImageCacheWidth ){
      int h = ImageCache.GetHeight() * ((1.0*ImageCacheWidth)/ImageCache.GetWidth());
      ImageCache.Rescale(  ImageCacheWidth, h );
   }
   FlowPacker context{ ImageCacheWidth };
   // Load the bitmaps
   for (size_t i = 0; i < resources.mImages.size(); ++i)
   {
      wxImage &Image = resources.mImages[i];
      context.mFlags = mBitmapFlags[i];
      if( !(mBitmapFlags[i] & resFlagInternal) )
      {
         context.GetNextPosition( Image.GetWidth(),Image.GetHeight() );
         wxRect R = context.RectInner();
         //wxLogDebug( "[%i, %i, %i, %i, \"%s\"], ", R.x, R.y, R.width, R.height, mBitmapNames[i].c_str() );
         Image = GetSubImageWithAlpha( ImageCache, context.RectInner() );
         resources.mBitmaps[i] = wxBitmap(Image);
      }
   }
   if( !ImageCache.HasAlpha() )
      ImageCache.InitAlpha();

//   return true; //To not load colours..
   // Now load the colours.
   int x,y;
   context.SetColourGroup();
   wxColour TempColour;
   for (size_t i = 0; i < resources.mColours.size(); ++i)
   {
      context.GetNextPosition( iColSize, iColSize );
      context.RectMid( x, y );
      wxRect R = context.RectInner();
      //wxLogDebug( "[%i, %i, %i, %i, \"%s\"], ", R.x, R.y, R.width, R.height, mColourNames[i].c_str() );
      // Only change the colour if the alpha is opaque.
      // This allows us to add NEW colours more easily.
      if( ImageCache.GetAlpha(x,y ) > 128 )
      {
         TempColour = wxColour(
            ImageCache.GetRed( x,y),
            ImageCache.GetGreen( x,y),
            ImageCache.GetBlue(x,y));
         /// \todo revisit this hack which makes adding NEW colours easier
         /// but which prevents a colour of (1,1,1) from being added.
         /// find an alternative way to make adding NEW colours easier.
         /// e.g. initialise the background to translucent, perhaps.
         if( TempColour != wxColour(1,1,1) )
            resources.mColours[i] = TempColour;
      }
   }
   return true;
}

void ThemeBase::LoadThemeComponents( bool bOkIfNotFound )
{
   ValueRestorer cleanup{ mpSet };
   for (auto &[key, data] : GetThemeCacheLookup())
      LoadOneThemeComponents( key.Internal(), bOkIfNotFound );
}

void ThemeBase::LoadOneThemeComponents( teThemeType id, bool bOkIfNotFound )
{
   SwitchTheme( id );
   auto &resources = *mpSet;
   // IF directory doesn't exist THEN return early.
   const auto dir = ThemeComponentsDir(GetFilePath(), id);
   if( !wxDirExists( dir ))
      return;

   using namespace BasicUI;

   int n=0;
   FilePath FileName;
   for (size_t i = 0; i < resources.mImages.size(); ++i)
   {
      if( !(mBitmapFlags[i] & resFlagInternal) )
      {
         FileName = ThemeComponent( dir, mBitmapNames[i] );
         if( wxFileExists( FileName ))
         {
            if( !resources.mImages[i].LoadFile( FileName, wxBITMAP_TYPE_PNG ))
            {
               ShowMessageBox(
                  XO(
               /* i18n-hint: Do not translate png.  It is the name of a file format.*/
"Audacity could not load file:\n  %s.\nBad png format perhaps?")
                     .Format( FileName ));
               return;
            }
            /// JKC: \bug (wxWidgets) A png may have been saved with alpha, but when you
            /// load it, it comes back with a mask instead!  (well I guess it is more
            /// efficient).  Anyway, we want alpha and not a mask, so we call InitAlpha,
            /// and that transfers the mask into the alpha channel, and we're done.
            if( ! resources.mImages[i].HasAlpha() )
            {
               // wxLogDebug( wxT("File %s lacked alpha"), mBitmapNames[i] );
               resources.mImages[i].InitAlpha();
            }
            resources.mBitmaps[i] = wxBitmap( resources.mImages[i] );
            n++;
         }
      }
   }

   // Now read complete information about the colors from one text file
   {
      const auto fName = wxFileName{ dir, ColorFileName }.GetFullPath();
      wxTextFile file{ fName };
      file.Open();
      if (!file.IsOpened())
         ShowMessageBox( XO("Couldn't read from file: %s").Format( fName ) );
      else {
         ++n;
         // Scan the line for name and #xxxxxx;
         static const std::wregex expr{
            LR"(^ *([_[:alnum:]]+).*#([0-9a-fA-F]{6});)" };
         const auto begin = mColourNames.begin(),
             end = mColourNames.end();
         std::unordered_set<wxString> names;
         for (auto str = file.GetFirstLine();
              !file.Eof(); str = file.GetNextLine()) {
            if (std::wsmatch match;
                regex_search( str.ToStdWstring(), match, expr )) {
               const wxString name{ match[1] };
               if (!names.insert(name).second)
                  ShowMessageBox( Verbatim("Ignoring duplicate color name: %s")
                     .Format( name ) );
               else if (const auto iter = std::find(begin, end, name);
                  iter == end)
                  ShowMessageBox(
                     Verbatim("Unrecognized color name: %s").Format( name ) );
               else {
                  auto rrggbb =
                     static_cast<unsigned>(stoi(match[2], nullptr, 16));
                  unsigned char rr = (rrggbb >> 16) & 0xffu;
                  unsigned char gg = (rrggbb >> 8) & 0xffu;
                  unsigned char bb = rrggbb & 0xffu;
                  resources.mColours[iter - begin] = { rr, gg, bb };
               }
            }
         }
      }
   }

   if( n==0 )
   {
      if( bOkIfNotFound )
         return;
      ShowMessageBox(
         XO(
"None of the expected theme component files\n were found in:\n  %s.")
            .Format( dir ));
   }
}

void ThemeBase::SaveThemeComponents()
{
   ValueRestorer cleanup{ mpSet };
   for (auto &[key, data] : GetThemeCacheLookup())
      if (!SaveOneThemeComponents( key.Internal() ))
         // Some file failed to save, message was given
         return;
   BasicUI::ShowMessageBox(
      XO("Themes written to:\n  %s/*/Components/.").Format(GetFilePath()));
}

bool ThemeBase::SaveOneThemeComponents( teThemeType id )
{
   using namespace BasicUI;
   SwitchTheme( id );
   auto &resources = *mpSet;
   // IF directory doesn't exist THEN create it
   const auto dir = ThemeComponentsDir(GetFilePath(), id);
   if( !wxDirExists( dir ))
   {
      /// \bug 1 in wxWidgets documentation; wxMkDir returns false if
      /// directory didn't exist, even if it successfully creates it.
      /// so we create and then test if it exists instead.
      /// \bug 2 in wxWidgets documentation; wxMkDir has only one argument
      /// under MSW
#ifdef __WXMSW__
      wxMkDir( dir.fn_str() );
#else
      wxMkDir( dir.fn_str(), 0700 );
#endif
      if( !wxDirExists( dir ))
      {
         ShowMessageBox(
            XO("Could not create directory:\n  %s")
               .Format( dir ) );
         return false;
      }
   }

   int n=0;
   FilePath FileName;
   for (size_t i = 0; i < resources.mImages.size(); ++i)
   {
      if( !(mBitmapFlags[i] & resFlagInternal) )
      {
         FileName = ThemeComponent( dir, mBitmapNames[i] );
         if( wxFileExists( FileName ))
         {
            ++n;
            break;
         }
      }
   }

   if( wxFileExists( ThemeComponent( dir, ColorFileName ) ) )
      ++n;

   using namespace BasicUI;

   if (n > 0)
   {
      auto result =
         ShowMessageBox(
            XO(
"Some required files in:\n  %s\nwere already present. Overwrite?")
               .Format( dir ),
            MessageBoxOptions{}
               .ButtonStyle(Button::YesNo)
               .DefaultIsNo());
      if (result == MessageBoxResult::No)
         return false;
   }

   for (size_t i = 0; i < resources.mImages.size(); ++i)
   {
      if( !(mBitmapFlags[i] & resFlagInternal) )
      {
         FileName = ThemeComponent( dir, mBitmapNames[i] );
         if( !resources.mImages[i].SaveFile( FileName, wxBITMAP_TYPE_PNG ))
         {
            ShowMessageBox(
               XO("Audacity could not save file:\n  %s")
                  .Format( FileName ));
            return false;
         }
      }
   }

   // Now write complete information about the colors in one text file
   {
      const auto fName = wxFileName{ dir, ColorFileName }.GetFullPath();
      wxFileOutputStream ffStream{ fName };
      if (!ffStream.IsOk()) {
         ShowMessageBox( XO("Couldn't write to file: %s").Format( fName ) );
         return false;
      }
      wxTextOutputStream ss{ffStream};
      // Open with, for instance, ".darkTheme {"
      ss << "." << id.GET() << "Theme {\n";
      for (size_t i = 0; i < resources.mColours.size(); ++i) {
         const auto &colour = resources.mColours[i];
         ss
            // Write names, aligning the colons in a column,
            // followed by #rrggbb;
            << wxString::Format("%30s: #", mColourNames[i])
            << wxString::Format("%02x", colour.Red())
            << wxString::Format("%02x", colour.Green())
            << wxString::Format("%02x", colour.Blue())
            << ";\n";
      }
      ss << "}";
   }

   return true;
}

void ThemeBase::SaveThemeAsCode()
{
   ValueRestorer cleanup{ mpSet };
   for (auto &[key, data] : GetThemeCacheLookup()) {
      // false indicates not using standard binary method.
      if (!CreateOneImageCache(key.Internal(), false))
         // Some file failed to save, message was given
         return;
   }
   BasicUI::ShowMessageBox(
      /* i18n-hint "Cee" means the C computer programming language */
      XO("Themes as Cee code written to:\n  %s/*%s.")
         .Format( GetFilePath(), ThemeCacheFileName ));
}

wxImage ThemeBase::MakeImageWithAlpha( wxBitmap & Bmp )
{
   // BUG in wxWidgets.  Conversion from BMP to image does not preserve alpha.
   wxImage image( Bmp.ConvertToImage() );
   return image;
}

void ThemeBase::DeleteUnusedThemes()
{
   auto iter = mSets.begin(), end = mSets.end();
   while (iter != end) {
      if (mpSet == &iter->second)
         ++iter;
      else
         iter = mSets.erase(iter);
   }
}

wxColour & ThemeBase::Colour( int iIndex )
{
   wxASSERT( iIndex >= 0 );
   auto &resources = *mpSet;
   EnsureInitialised();
   return resources.mColours[iIndex];
}

void ThemeBase::SetBrushColour( wxBrush & Brush, int iIndex )
{
   wxASSERT( iIndex >= 0 );
   Brush.SetColour( Colour( iIndex ));
}

void ThemeBase::SetPenColour(   wxPen & Pen, int iIndex )
{
   wxASSERT( iIndex >= 0 );
   Pen.SetColour( Colour( iIndex ));
}

wxBitmap & ThemeBase::Bitmap( int iIndex )
{
   wxASSERT( iIndex >= 0 );
   auto &resources = *mpSet;
   EnsureInitialised();
   return resources.mBitmaps[iIndex];
}

wxImage  & ThemeBase::Image( int iIndex )
{
   wxASSERT( iIndex >= 0 );
   auto &resources = *mpSet;
   EnsureInitialised();
   return resources.mImages[iIndex];
}
wxSize  ThemeBase::ImageSize( int iIndex )
{
   wxASSERT( iIndex >= 0 );
   auto &resources = *mpSet;
   EnsureInitialised();
   wxImage & Image = resources.mImages[iIndex];
   return wxSize( Image.GetWidth(), Image.GetHeight());
}

/// Replaces both the image and the bitmap.
void ThemeBase::ReplaceImage( int iIndex, wxImage * pImage )
{
   Image( iIndex ) = *pImage;
   Bitmap( iIndex ) = wxBitmap( *pImage );
}

void ThemeBase::RotateImageInto( int iTo, int iFrom, bool bClockwise )
{
   wxImage img(theTheme.Bitmap( iFrom ).ConvertToImage() );
   wxImage img2 = img.Rotate90( bClockwise );
   ReplaceImage( iTo, &img2 );
}

ChoiceSetting &GUITheme()
{
   auto symbols = []{
      std::vector<EnumValueSymbol> symbols;

      // Gather registered themes
      for (const auto &[symbol, data] : GetThemeCacheLookup())
         symbols.emplace_back(symbol);

      // Sort the names, with built-in themes to the front,
      // conserving the ordering that was used in 3.1.0; otherwise
      // sorting other registered themes alphabetically by identifier
      static const Identifier names[] = {
         "classic", "light", "dark", "high-contrast", "modern"
      };
      static auto index = [](const EnumValueSymbol &symbol){
         auto begin = std::begin(names), end = std::end(names);
         return std::find(begin, end, symbol.Internal()) - begin;
      };
      std::stable_sort( symbols.begin(), symbols.end(),
         [](auto &a, auto &b){ return index(a) < index(b); } );

      // Last, custom
      symbols.emplace_back(
         /* i18n-hint: user defined */
         "custom", XO("Custom")
      );
   
      return symbols;
   };

   constexpr int defaultTheme = 1; // "light"

   static ChoiceSetting setting {
      wxT("/GUI/Theme"), symbols(), defaultTheme
   };

   return setting;
}