KiCad PCB EDA Suite
cursor_store.cpp
Go to the documentation of this file.
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 
24 #include <cursor_store.h>
25 
26 #include <wx/bitmap.h>
27 #include <wx/debug.h>
28 
29 
41 {
42 #if defined( __WXMSW__ ) or defined( __WXMAC__ )
43 
44  wxBitmap img_bitmap(
45  reinterpret_cast<const char*>( aDef.m_image_data ), aDef.m_size.x, aDef.m_size.y );
46  wxBitmap msk_bitmap(
47  reinterpret_cast<const char*>( aDef.m_mask_data ), aDef.m_size.x, aDef.m_size.y );
48  img_bitmap.SetMask( new wxMask( msk_bitmap ) );
49 
50  wxImage image( img_bitmap.ConvertToImage() );
51 
52 #if defined( __WXMSW__ )
53  image.SetMaskColour( 255, 255, 255 );
54 #endif
55 
56  image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_X, aDef.m_hotspot.x );
57  image.SetOption( wxIMAGE_OPTION_CUR_HOTSPOT_Y, aDef.m_hotspot.y );
58 
59  return wxCursor{ image };
60 
61 #elif defined( __WXGTK__ ) or defined( __WXMOTIF__ )
62 
63  return wxCursor{
64  reinterpret_cast<const char*>( aDef.m_image_data ),
65  aDef.m_size.x,
66  aDef.m_size.y,
67  aDef.m_hotspot.x,
68  aDef.m_hotspot.y,
69  reinterpret_cast<const char*>( aDef.m_mask_data ),
70  };
71 
72 #else
73  wxASSERT_MSG( false, "Unknown platform for cursor construction." );
74  return wxNullCursor;
75 #endif
76 }
77 
78 
79 CURSOR_STORE::CURSOR_STORE( const std::vector<CURSOR_DEF>& aDefs )
80 {
81  for( const auto& def : aDefs )
82  {
83  m_store[def.m_id_key] = constructCursor( def );
84  }
85 }
86 
87 
88 const wxCursor& CURSOR_STORE::Get( int aIdKey ) const
89 {
90  const auto find_iter = m_store.find( aIdKey );
91 
92  if( find_iter != m_store.end() )
93  {
94  return find_iter->second;
95  }
96 
97  wxASSERT_MSG( false,
98  wxString::Format( "Could not find cursor with ID %d", static_cast<int>( aIdKey ) ) );
99  return wxNullCursor;
100 }
const unsigned char * m_mask_data
The mask data bitmap
Definition: cursor_store.h:54
wxCursor constructCursor(const CURSOR_STORE::CURSOR_DEF &aDef)
Construct a cursor for the given definition.
const unsigned char * m_image_data
The image data bitmap
Definition: cursor_store.h:51
Definition of a cursor.
Definition: cursor_store.h:45
wxSize m_size
The image size in pixels
Definition: cursor_store.h:57
wxPoint m_hotspot
The "hotspot" where the cursor "is" in the image
Definition: cursor_store.h:60
const wxCursor & Get(int aIdKey) const
Get a given cursor by its ID.
CURSOR_STORE(const std::vector< CURSOR_DEF > &aDefs)
Construct a store with a pre-set list of cursors.
std::map< int, wxCursor > m_store
Internal store of cursors by ID
Definition: cursor_store.h:82
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:205