libraries/lib-theme/Theme.cpp
/// 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.
Reads an image cache including images, cursors and colours.
Reads an image cache including images cursors and colours .
bool ThemeBase::ReadImageCache( teThemeType type, bool bOkIfNotFound)
{
   EnsureInitialised();
   wxImage ImageCache;
   bRecolourOnLoad = GUIBlendThemes.Read();
   using namespace BasicUI;
   if( type.empty() )
   {
      mPreferredSystemAppearance = PreferredSystemAppearance::Light;
      const auto &FileName = FileNames::ThemeCachePng();
      if( !wxFileExists( FileName ))
      {
         if( bOkIfNotFound )
            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(
            XO("Audacity could not load file:\n  %s.\nBad png format perhaps?")
               .Format( FileName ));
         return false;
      }
   }
   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();
      pImage = iter->second.data.data();
      wxMemoryInputStream InternalStream( pImage, ImageSize );
      if( !ImageCache.LoadFile( InternalStream, wxBITMAP_TYPE_PNG ))
      {
         ShowMessageBox(
            XO(
"Audacity could not read its default theme.\nPlease report the problem."));
         return false;
      }
   }
   if( ImageCache.GetWidth() > ImageCacheWidth ){
      int h = ImageCache.GetHeight() * ((1.0*ImageCacheWidth)/ImageCache.GetWidth());
      ImageCache.Rescale(  ImageCacheWidth, h );
   }
   int i;
   FlowPacker context{ ImageCacheWidth };
   for(i = 0; i < (int)mImages.size(); i++)
   {
      wxImage &Image = mImages[i];
      context.mFlags = mBitmapFlags[i];
      if( (mBitmapFlags[i] & resFlagInternal)==0)
      {
         context.GetNextPosition( Image.GetWidth(),Image.GetHeight() );
         wxRect R = context.RectInner();
         Image = GetSubImageWithAlpha( ImageCache, context.RectInner() );
         mBitmaps[i] = wxBitmap(Image);
      }
   }
   if( !ImageCache.HasAlpha() )
      ImageCache.InitAlpha();
   int x,y;
   context.SetColourGroup();
   wxColour TempColour;
   const int iColSize=10;
   for(i = 0; i < (int)mColours.size(); i++)
   {
      context.GetNextPosition( iColSize, iColSize );
      context.RectMid( x, y );
      wxRect R = context.RectInner();
      if( ImageCache.GetAlpha(x,y ) > 128 )
      {
         TempColour = wxColour(
            ImageCache.GetRed( x,y),
            ImageCache.GetGreen( x,y),
            ImageCache.GetBlue(x,y));
         if( TempColour != wxColour(1,1,1) )
            mColours[i] = TempColour;
      }
   }
   return true;
}
bool ThemeBase :: ReadImageCache ( teThemeType type , bool bOkIfNotFound ) { EnsureInitialised ( ) ; wxImage ImageCache ; bRecolourOnLoad = GUIBlendThemes . Read ( ) ; using namespace BasicUI ; if ( type . empty ( ) ) { mPreferredSystemAppearance = PreferredSystemAppearance :: Light ; const auto & FileName = FileNames :: ThemeCachePng ( ) ; if ( ! wxFileExists ( FileName ) ) { if ( bOkIfNotFound ) return false ; ShowMessageBox ( XO ( " \n \n " ) . Format ( FileName ) ) ; return false ; } if ( ! ImageCache . LoadFile ( FileName , wxBITMAP_TYPE_PNG ) ) { ShowMessageBox ( XO ( " \n \n " ) . Format ( FileName ) ) ; return false ; } } 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 ( { " " , { } } ) ; wxASSERT ( iter != end ) ; } mPreferredSystemAppearance = iter -> second . preferredSystemAppearance ; ImageSize = iter -> second . data . size ( ) ; pImage = iter -> second . data . data ( ) ; wxMemoryInputStream InternalStream ( pImage , ImageSize ) ; if ( ! ImageCache . LoadFile ( InternalStream , wxBITMAP_TYPE_PNG ) ) { ShowMessageBox ( XO ( " \n " ) ) ; return false ; } } if ( ImageCache . GetWidth ( ) > ImageCacheWidth ) { int h = ImageCache . GetHeight ( ) * ( ( 1.0 * ImageCacheWidth ) / ImageCache . GetWidth ( ) ) ; ImageCache . Rescale ( ImageCacheWidth , h ) ; } int i ; FlowPacker context { ImageCacheWidth } ; for ( i = 0 ; i < ( int ) mImages . size ( ) ; i ++ ) { wxImage & Image = mImages [ i ] ; context . mFlags = mBitmapFlags [ i ] ; if ( ( mBitmapFlags [ i ] & resFlagInternal ) == 0 ) { context . GetNextPosition ( Image . GetWidth ( ) , Image . GetHeight ( ) ) ; wxRect R = context . RectInner ( ) ; Image = GetSubImageWithAlpha ( ImageCache , context . RectInner ( ) ) ; mBitmaps [ i ] = wxBitmap ( Image ) ; } } if ( ! ImageCache . HasAlpha ( ) ) ImageCache . InitAlpha ( ) ; int x , y ; context . SetColourGroup ( ) ; wxColour TempColour ; const int iColSize = 10 ; for ( i = 0 ; i < ( int ) mColours . size ( ) ; i ++ ) { context . GetNextPosition ( iColSize , iColSize ) ; context . RectMid ( x , y ) ; wxRect R = context . RectInner ( ) ; if ( ImageCache . GetAlpha ( x , y ) > 128 ) { TempColour = wxColour ( ImageCache . GetRed ( x , y ) , ImageCache . GetGreen ( x , y ) , ImageCache . GetBlue ( x , y ) ) ; if ( TempColour != wxColour ( 1 , 1 , 1 ) ) mColours [ i ] = TempColour ; } } return true ; }
Reads an image cache including images, cursors and colours.
Reads an image cache including images cursors and colours .
// Ensure we have an alpha channel... //   if( !ImageCache.HasAlpha() ) //   { //      ImageCache.InitAlpha(); //   } // did not load the images, so return false. /* i18n-hint: Do not translate png.  It is the name of a file format.*/ // ELSE we are reading from internal storage. //wxLogDebug("Reading ImageCache %p size %i", pImage, ImageSize ); // 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. //wxLogDebug("Read %i by %i", ImageCache.GetWidth(), ImageCache.GetHeight() ); // Resize a large image down. // Load the bitmaps //wxLogDebug( "[%i, %i, %i, %i, \"%s\"], ", R.x, R.y, R.width, R.height, mBitmapNames[i].c_str() ); //   return true; //To not load colours.. // Now load the colours. //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. /// \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.