1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015 CERN
5  * Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  * @author Maciej Suminski <maciej.suminski@cern.ch>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
24  */
25 
26 #include <tool/grid_menu.h>
27 #include <id.h>
28 #include <eda_draw_frame.h>
29 #include <settings/app_settings.h>
30 #include <tool/actions.h>
31 #include <bitmaps.h>
32 #include <base_units.h>
33 
34 using namespace std::placeholders;
35 
GRID_MENU(EDA_DRAW_FRAME * aParent)36 GRID_MENU::GRID_MENU( EDA_DRAW_FRAME* aParent ) :
37         ACTION_MENU( true ),
38         m_parent( aParent )
39 {
40     SetTitle( _( "Grid" ) );
41     SetIcon( BITMAPS::grid_select );
42 
43     APP_SETTINGS_BASE* settings = m_parent->config();
44     wxArrayString      gridsList;
45     int                i = ID_POPUP_GRID_START;
46 
47     BuildChoiceList( &gridsList, settings, m_parent );
48 
49     for( const wxString& grid : gridsList )
50         Append( i++, grid, wxEmptyString, wxITEM_CHECK );
51 }
52 
53 
eventHandler(const wxMenuEvent & aEvent)54 OPT_TOOL_EVENT GRID_MENU::eventHandler( const wxMenuEvent& aEvent )
55 {
56     OPT_TOOL_EVENT event( ACTIONS::gridPreset.MakeEvent() );
57     event->SetParameter( (intptr_t) aEvent.GetId() - ID_POPUP_GRID_START );
58     return event;
59 }
60 
61 
update()62 void GRID_MENU::update()
63 {
64     APP_SETTINGS_BASE* settings = m_parent->config();
65     unsigned int       current = settings->m_Window.grid.last_size_idx;
66     wxArrayString      gridsList;
67 
68     GRID_MENU::BuildChoiceList( &gridsList, settings, m_parent );
69 
70     for( unsigned int i = 0; i < GetMenuItemCount(); ++i )
71     {
72         wxMenuItem* menuItem = FindItemByPosition( i );
73 
74         menuItem->SetItemLabel( gridsList[ i ] );  // Refresh label in case units have changed
75         menuItem->Check( i == current );           // Refresh checkmark
76     }
77 }
78 
BuildChoiceList(wxArrayString * aGridsList,APP_SETTINGS_BASE * aCfg,EDA_DRAW_FRAME * aParent)79 void GRID_MENU::BuildChoiceList( wxArrayString* aGridsList, APP_SETTINGS_BASE* aCfg,
80                                  EDA_DRAW_FRAME* aParent )
81 {
82     wxString msg;
83 
84     EDA_UNITS primaryUnit;
85     EDA_UNITS secondaryUnit;
86 
87     aParent->GetUnitPair( primaryUnit, secondaryUnit );
88 
89     for( const wxString& gridSize : aCfg->m_Window.grid.sizes )
90     {
91         int val = (int) ValueFromString( EDA_UNITS::MILLIMETRES, gridSize );
92 
93         msg.Printf( _( "Grid: %s (%s)" ),
94                     MessageTextFromValue( primaryUnit, val ),
95                     MessageTextFromValue( secondaryUnit, val ) );
96 
97         aGridsList->Add( msg );
98     }
99 
100     if( !aCfg->m_Window.grid.user_grid_x.empty() )
101     {
102         int val = (int) ValueFromString( EDA_UNITS::INCHES, aCfg->m_Window.grid.user_grid_x );
103 
104         msg.Printf( _( "User grid: %s (%s)" ),
105                     MessageTextFromValue( primaryUnit, val ),
106                     MessageTextFromValue( secondaryUnit, val ) );
107 
108         aGridsList->Add( msg );
109     }
110 }
111