You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
3.0 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 KiCad Developers, see CHANGELOG.TXT for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <cursor_store.h>
  24. #include <wx/bitmap.h>
  25. #include <wx/debug.h>
  26. /**
  27. * Construct a cursor for the given definition.
  28. *
  29. * How to do this depends on the platform, see
  30. * http://docs.wxwidgets.org/trunk/classwx_cursor.html
  31. *
  32. * @param aDef the cursor definition
  33. * @return a newly constructed cursor if the platform is supported,
  34. * else wxNullCursor
  35. */
  36. wxCursor constructCursor( const CURSOR_STORE::CURSOR_DEF& aDef )
  37. {
  38. #if defined( __WXMSW__ ) or defined( __WXMAC__ )
  39. wxBitmap img_bitmap(
  40. reinterpret_cast<const char*>( aDef.m_image_data ), aDef.m_size.x, aDef.m_size.y );
  41. wxBitmap msk_bitmap(
  42. reinterpret_cast<const char*>( aDef.m_mask_data ), aDef.m_size.x, aDef.m_size.y );
  43. img_bitmap.SetMask( new wxMask( msk_bitmap ) );
  44. wxImage image( img_bitmap.ConvertToImage() );
  45. #if defined( __WXMSW__ )
  46. image.SetMaskColour( 255, 255, 255 );
  47. #endif
  48. image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_X, aDef.m_hotspot.x );
  49. image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_Y, aDef.m_hotspot.y );
  50. return wxCursor{ image };
  51. #elif defined( __WXGTK__ ) or defined( __WXMOTIF__ )
  52. return wxCursor{
  53. reinterpret_cast<const char*>( aDef.m_image_data ),
  54. aDef.m_size.x,
  55. aDef.m_size.y,
  56. aDef.m_hotspot.x,
  57. aDef.m_hotspot.y,
  58. reinterpret_cast<const char*>( aDef.m_mask_data ),
  59. };
  60. #else
  61. wxASSERT_MSG( false, "Unknown platform for cursor construction." );
  62. return wxNullCursor;
  63. #endif
  64. }
  65. CURSOR_STORE::CURSOR_STORE( const std::vector<CURSOR_DEF>& aDefs )
  66. {
  67. for( const auto& def : aDefs )
  68. {
  69. m_store[def.m_id_key] = constructCursor( def );
  70. }
  71. }
  72. const wxCursor& CURSOR_STORE::Get( int aIdKey ) const
  73. {
  74. const auto find_iter = m_store.find( aIdKey );
  75. if( find_iter != m_store.end() )
  76. {
  77. return find_iter->second;
  78. }
  79. wxASSERT_MSG( false,
  80. wxString::Format( "Could not find cursor with ID %d", static_cast<int>( aIdKey ) ) );
  81. return wxNullCursor;
  82. }