forked from Sneeds-Feed-and-Seed/sneedacity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheme.cpp
1337 lines (1152 loc) · 38.5 KB
/
Theme.cpp
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
/**********************************************************************
Sneedacity: A Digital Audio Editor
Theme.cpp
James Crook
Sneedacity 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 Sneedacity 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-Sneedacity 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.
*//*****************************************************************//**
\class auStaticText
\brief is like wxStaticText, except it can be themed. wxStaticText
can't be.
*//*****************************************************************/
#include "Theme.h"
#include <wx/wxprec.h>
#include <wx/dcclient.h>
#include <wx/image.h>
#include <wx/file.h>
#include <wx/ffile.h>
#include <wx/mstream.h>
#include <wx/settings.h>
#include "AllThemeResources.h" // can remove this later, only needed for 'XPMS_RETIRED'.
#include "FileNames.h"
#include "Prefs.h"
#include "ImageManipulation.h"
#include "Internat.h"
#include "MemoryX.h"
#include "widgets/SneedacityMessageBox.h"
// JKC: First get the MAC specific images.
// As we've disabled USE_AQUA_THEME, we need to name each file we use.
//
// JKC: Mac Hackery.
// These #defines are very temporary. We want to ensure the Mac XPM names don't collide with
// the PC XPM names, so we do some #defines and later undo them.
// Use the same trick wherever we need to avoid name collisions.
// All this will vanish when the XPMs are eliminated.
// Indeed XPMS_RETIRED the #ifndef ensures we're already not using any of it.
#ifndef XPMS_RETIRED
// This step should mean that we get PC/Linux images only
// except where we EXPLICITLY request otherwise.
#undef USE_AQUA_THEME
// This step ensures we treat the cursors as 32x32 even on Mac.
// We're not yet creating the cursors from the theme, so
// all this ensures is that the sizing on PC and Mac stays in step.
#define CURSORS_SIZE32
#define DownButton MacDownButton
#define HiliteButton MacHiliteButton
#define UpButton MacUpButton
#define Down MacDown
#define Hilite MacHilite
#define Up MacUp
#define Slider MacSlider
#define SliderThumb MacSliderThumb
#include "../images/Aqua/HiliteButtonSquare.xpm"
#include "../images/Aqua/UpButtonSquare.xpm"
#include "../images/Aqua/DownButtonSquare.xpm"
#include "../images/Aqua/Slider.xpm"
#include "../images/Aqua/SliderThumb.xpm"
#include "../images/Aqua/Down.xpm"
#include "../images/Aqua/Hilite.xpm"
#include "../images/Aqua/Up.xpm"
#if 0
// These ones aren't used...
#include "../images/Aqua/DownButtonStripes.xpm"
#include "../images/Aqua/DownButtonWhite.xpm"
#include "../images/Aqua/HiliteButtonStripes.xpm"
#include "../images/Aqua/HiliteButtonWhite.xpm"
#include "../images/Aqua/UpButtonStripes.xpm"
#include "../images/Aqua/UpButtonWhite.xpm"
#endif
#undef DownButton
#undef UpButton
#undef HiliteButton
#undef Down
#undef Hilite
#undef Up
#undef Slider
#undef SliderThumb
//-- OK now on to includes for Linux/PC images.
#include "../images/PostfishButtons.h"
#include "../images/ControlButtons.h"
#define HAVE_SHARED_BUTTONS
#include "../images/EditButtons.h"
#include "../images/MixerImages.h"
#include "../images/Cursors.h"
#include "../images/ToolBarButtons.h"
#include "../images/TranscriptionButtons.h"
#include "../images/ToolsButtons.h"
#include "../images/ExpandingToolBar/ToolBarToggle.xpm"
#include "../images/ExpandingToolBar/ToolBarTarget.xpm"
#include "../images/ExpandingToolBar/ToolBarGrabber.xpm"
#define Slider VolumeSlider
#define SliderThumb VolumeSliderThumb
#include "../images/ControlButtons/Slider.xpm"
#include "../images/ControlButtons/SliderThumb.xpm"
#undef Slider
#undef SliderThumb
// A different slider's thumb.
#include "../images/SliderThumb.xpm"
#include "../images/SliderThumbAlpha.xpm"
// Include files to get the default images
//#include "../images/Aqua.xpm"
#include "../images/Arrow.xpm"
#include "../images/GlyphImages.h"
#include "../images/UploadImages.h"
#include "../images/SneedacityLogoWithName.xpm"
//#include "../images/SneedacityLogo.xpm"
#include "../images/SneedacityLogo48x48.xpm"
#endif
// Include the ImageCache...
static const unsigned char DarkImageCacheAsData[] = {
#include "DarkThemeAsCeeCode.h"
};
static const unsigned char LightImageCacheAsData[] = {
#include "LightThemeAsCeeCode.h"
};
static const unsigned char ClassicImageCacheAsData[] = {
#include "ClassicThemeAsCeeCode.h"
};
static const unsigned char HiContrastImageCacheAsData[] = {
#include "HiContrastThemeAsCeeCode.h"
};
// theTheme is a global variable.
SNEEDACITY_DLL_API Theme theTheme;
Theme::Theme(void)
{
mbInitialised=false;
}
Theme::~Theme(void)
{
}
void Theme::EnsureInitialised()
{
if( mbInitialised )
return;
RegisterImages();
RegisterColours();
#ifdef EXPERIMENTAL_EXTRA_THEME_RESOURCES
extern void RegisterExtraThemeResources();
RegisterExtraThemeResources();
#endif
LoadPreferredTheme();
}
bool ThemeBase::LoadPreferredTheme()
{
// DA: Default themes differ.
auto theme = GUITheme.Read();
theTheme.LoadTheme( theTheme.ThemeTypeOfTypeName( theme ) );
return true;
}
void Theme::RegisterImages()
{
if( mbInitialised )
return;
mbInitialised = true;
// This initialises the variables e.g
// RegisterImage( bmpRecordButton, some image, wxT("RecordButton"));
#define THEME_INITS
#include "AllThemeResources.h"
}
void Theme::RegisterColours()
{
}
ThemeBase::ThemeBase(void)
{
bRecolourOnLoad = false;
bIsUsingSystemTextColour = false;
}
ThemeBase::~ThemeBase(void)
{
}
/// This function is called to load the initial Theme images.
/// It does not though cause the GUI to refresh.
void ThemeBase::LoadTheme( teThemeType Theme )
{
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
}
RotateImageInto( bmpRecordBeside, bmpRecordBelow, false );
RotateImageInto( bmpRecordBesideDisabled, bmpRecordBelowDisabled, false );
if( bRecolourOnLoad )
RecolourTheme();
wxColor Back = theTheme.Colour( clrTrackInfo );
wxColor CurrentText = theTheme.Colour( clrTrackPanelText );
wxColor DesiredText = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
int TextColourDifference = ColourDistance( CurrentText, DesiredText );
bIsUsingSystemTextColour = ( TextColourDifference == 0 );
// Theming is very accepting of alternative text colours. They just need to
// have decent contrast to the background colour, if we're blending themes.
if( !bIsUsingSystemTextColour ){
int ContrastLevel = ColourDistance( Back, DesiredText );
bIsUsingSystemTextColour = bRecolourOnLoad && (ContrastLevel > 250);
if( bIsUsingSystemTextColour )
Colour( clrTrackPanelText ) = DesiredText;
}
bRecolourOnLoad = false;
// Next line is not required as we haven't yet built the GUI
// when this function is (or should be) called.
// ApplyUpdatedImages();
}
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() );
}
// This function coerces a theme to be more like the system colours.
// Only used for built in themes. For custom themes a user
// will choose a better theme for them and just not use a mismatching one.
void ThemeBase::RecolourTheme()
{
wxColour From = Colour( clrMedium );
#if defined( __WXGTK__ )
wxColour To = wxSystemSettings::GetColour( wxSYS_COLOUR_BACKGROUND );
#else
wxColour To = wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE );
#endif
// only recolour if recolouring is slight.
int d = ColourDistance( From, To );
// Don't recolour if difference is too big.
if( d > 120 )
return;
// A minor tint difference from standard does not need
// to be recouloured either. Includes case of d==0 which is nothing
// needs to be done.
if( d < 40 )
return;
Colour( clrMedium ) = To;
RecolourBitmap( bmpUpButtonLarge, From, To );
RecolourBitmap( bmpDownButtonLarge, From, To );
RecolourBitmap( bmpHiliteButtonLarge, From, To );
RecolourBitmap( bmpUpButtonSmall, From, To );
RecolourBitmap( bmpDownButtonSmall, From, To );
RecolourBitmap( bmpHiliteButtonSmall, From, To );
Colour( clrTrackInfo ) = To;
RecolourBitmap( bmpUpButtonExpand, From, To );
}
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( int &iIndex, char const ** pXpm, const wxString & Name )
{
wxASSERT( iIndex == -1 ); // Don't initialise same bitmap twice!
wxBitmap Bmp( pXpm );
wxImage Img( Bmp.ConvertToImage() );
// The next line recommended by http://forum.sneedacityteam.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( iIndex, Img, Name );
}
void ThemeBase::RegisterImage( int &iIndex, const wxImage &Image, const wxString & Name )
{
wxASSERT( iIndex == -1 ); // Don't initialise same bitmap twice!
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();
mBitmaps.push_back( wxBitmap( TempImage ) );
#else
mBitmaps.push_back( wxBitmap( Image ) );
#endif
mBitmapNames.push_back( Name );
mBitmapFlags.push_back( mFlow.mFlags );
mFlow.mFlags &= ~resFlagSkip;
iIndex = mBitmaps.size() - 1;
}
void ThemeBase::RegisterColour( int &iIndex, const wxColour &Clr, const wxString & Name )
{
wxASSERT( iIndex == -1 ); // Don't initialise same colour twice!
mColours.push_back( Clr );
mColourNames.push_back( Name );
iIndex = mColours.size() - 1;
}
void FlowPacker::Init(int width)
{
mFlags = resFlagPaired;
mOldFlags = mFlags;
mxCacheWidth = width;
myPos = 0;
myPosBase =0;
myHeight = 0;
iImageGroupSize = 1;
SetNewGroup(1);
mBorderWidth = 0;
}
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 )
{
// DA: Naming of output sourcery
#ifdef EXPERIMENTAL_DA
File.Write( wxT("/// @file DarkThemeAsCeeCode.h\r\n") );
#else
File.Write( wxT("/// @file ThemeAsCeeCode.h\r\n") );
#endif
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 bmpSneedacityLogo. Use double width + 10.
const int ImageCacheWidth = 440;
const int ImageCacheHeight = 836;
void ThemeBase::CreateImageCache( bool bBinarySave )
{
EnsureInitialised();
wxBusyCursor busy;
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();
}
int i;
mFlow.Init( ImageCacheWidth );
mFlow.mBorderWidth =1;
//#define IMAGE_MAP
#ifdef IMAGE_MAP
wxLogDebug( wxT("<img src=\"ImageCache.png\" usemap=\"#map1\">" ));
wxLogDebug( wxT("<map name=\"map1\">") );
#endif
// Save the bitmaps
for(i = 0;i < (int)mImages.size();i++)
{
wxImage &SrcImage = mImages[i];
mFlow.mFlags = mBitmapFlags[i];
if( (mBitmapFlags[i] & resFlagInternal)==0)
{
mFlow.GetNextPosition( SrcImage.GetWidth(), SrcImage.GetHeight());
ImageCache.SetRGB( mFlow.Rect(), 0xf2, 0xb0, 0x27 );
if( (mFlow.mFlags & resFlagSkip) == 0 )
PasteSubImage( &ImageCache, &SrcImage,
mFlow.mxPos + mFlow.mBorderWidth,
mFlow.myPos + mFlow.mBorderWidth);
else
ImageCache.SetRGB( mFlow.RectInner(), 1,1,1);
#ifdef IMAGE_MAP
// No href in html. Uses title not alt.
wxRect R( mFlow.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;
mFlow.SetColourGroup();
const int iColSize = 10;
for(i = 0; i < (int)mColours.size(); i++)
{
mFlow.GetNextPosition( iColSize, iColSize );
wxColour c = mColours[i];
ImageCache.SetRGB( mFlow.Rect() , 0xf2, 0xb0, 0x27 );
ImageCache.SetRGB( mFlow.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( mFlow.mxPos + x, mFlow.myPos+y, 255);
}
}
#ifdef IMAGE_MAP
// No href in html. Uses title not alt.
wxRect R( mFlow.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
int j;
for(i=0;i<ImageCacheWidth;i++)
for(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
// IF bBinarySave, THEN saving to a normal PNG file.
if( bBinarySave )
{
const auto &FileName = FileNames::ThemeCachePng();
// 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 };
SneedacityMessageBox( message );
return;
}
#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 ))
{
SneedacityMessageBox(
XO("Sneedacity could not write file:\n %s.")
.Format( FileName ));
return;
}
SneedacityMessageBox(
/* 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. Sneedacity can load and save alternative
themes. */
XO("Theme written to:\n %s.")
.Format( FileName ));
}
// ELSE saving to a C code textual version.
else
{
SourceOutputStream OutStream;
const auto &FileName = FileNames::ThemeCacheAsCee( );
if( !OutStream.OpenFile( FileName ))
{
SneedacityMessageBox(
XO("Sneedacity could not open file:\n %s\nfor writing.")
.Format( FileName ));
return;
}
if( !ImageCache.SaveFile(OutStream, wxBITMAP_TYPE_PNG ) )
{
SneedacityMessageBox(
XO("Sneedacity could not write images to file:\n %s.")
.Format( FileName ));
return;
}
SneedacityMessageBox(
/* i18n-hint "Cee" means the C computer programming language */
XO("Theme as Cee code written to:\n %s.")
.Format( FileName ));
}
}
/// Writes an html file with an image map of the ImageCache
/// Very handy for seeing what each part is for.
void ThemeBase::WriteImageMap( )
{
EnsureInitialised();
wxBusyCursor busy;
int i;
mFlow.Init( ImageCacheWidth );
mFlow.mBorderWidth = 1;
wxFFile File( FileNames::ThemeCacheHtm(), 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(i = 0; i < (int)mImages.size(); i++)
{
wxImage &SrcImage = mImages[i];
mFlow.mFlags = mBitmapFlags[i];
if( (mBitmapFlags[i] & resFlagInternal)==0)
{
mFlow.GetNextPosition( SrcImage.GetWidth(), SrcImage.GetHeight());
// No href in html. Uses title not alt.
wxRect R( mFlow.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.
mFlow.SetColourGroup();
const int iColSize = 10;
for(i = 0; i < (int)mColours.size(); i++)
{
mFlow.GetNextPosition( iColSize, iColSize );
// No href in html. Uses title not alt.
wxRect R( mFlow.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( )
{
EnsureInitialised();
wxBusyCursor busy;
int i;
wxFFile File( FileNames::ThemeImageDefsAsCee(), wxT("wb") );
if( !File.IsOpened() )
return;
teResourceFlags PrevFlags = (teResourceFlags)-1;
for(i = 0; i < (int)mImages.size(); i++)
{
wxImage &SrcImage = 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.
#ifdef EXPERIMENTAL_DA
return themeDark;
#else
return themeLight;
#endif
}
teThemeType ThemeBase::ThemeTypeOfTypeName( const wxString & Name )
{
static const wxArrayStringEx aThemes{
"classic" ,
"dark" ,
"light" ,
"high-contrast" ,
"custom" ,
};
int themeIx = make_iterator_range( aThemes ).index( Name );
if( themeIx < 0 )
return GetFallbackThemeType();
return (teThemeType)themeIx;
}
/// Reads an image cache including images, cursors and colours.
/// @param bBinaryRead if true means read from an external binary file.
/// otherwise the data is taken from a compiled in 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)
{
EnsureInitialised();
wxImage ImageCache;
wxBusyCursor busy;
// Ensure we have an alpha channel...
// if( !ImageCache.HasAlpha() )
// {
// ImageCache.InitAlpha();
// }
gPrefs->Read(wxT("/GUI/BlendThemes"), &bRecolourOnLoad, true);
if( type == themeFromFile )
{
const auto &FileName = FileNames::ThemeCachePng();
if( !wxFileExists( FileName ))
{
if( bOkIfNotFound )
return false; // did not load the images, so return false.
SneedacityMessageBox(
XO("Sneedacity could not find file:\n %s.\nTheme not loaded.")
.Format( FileName ));
return false;
}
if( !ImageCache.LoadFile( FileName, wxBITMAP_TYPE_PNG ))
{
SneedacityMessageBox(
/* i18n-hint: Do not translate png. It is the name of a file format.*/
XO("Sneedacity 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;
switch( type ){
default:
case themeClassic :
ImageSize = sizeof(ClassicImageCacheAsData);
pImage = ClassicImageCacheAsData;
break;
case themeLight :
ImageSize = sizeof(LightImageCacheAsData);
pImage = LightImageCacheAsData;
break;
case themeDark :
ImageSize = sizeof(DarkImageCacheAsData);
pImage = DarkImageCacheAsData;
break;
case themeHiContrast :
ImageSize = sizeof(HiContrastImageCacheAsData);
pImage = HiContrastImageCacheAsData;
break;
}
//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.
SneedacityMessageBox(
XO(
"Sneedacity 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());