KiCad PCB EDA Suite
Loading...
Searching...
No Matches
dialog_plugin_options.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) 2013 SoftPLC Corporation, Dick Hollenbeck <[email protected]>
5 * Copyright (C) 2013 CERN
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
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
28#include <grid_tricks.h>
29#include <lib_table_base.h>
30#include <widgets/wx_grid.h>
32#include <bitmaps.h>
33#include <string_utils.h>
34
35
36#define INITIAL_HELP \
37 _( "Select an <b>Option Choice</b> in the listbox above, and then click the " \
38 "<b>Append Selected Option</b> button." )
39
40
42 const wxString& aNickname,
43 const std::map<std::string, UTF8>& aPluginOptions,
44 const wxString& aFormattedOptions,
45 wxString* aResult ) :
47 m_callers_options( aFormattedOptions ),
48 m_result( aResult ),
49 m_choices( aPluginOptions ),
50 m_initial_help( INITIAL_HELP ),
51 m_grid_widths_dirty( true )
52{
53 SetTitle( wxString::Format( _( "Options for Library '%s'" ), aNickname ) );
54
55 m_grid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
56
57 // add Cut, Copy, and Paste to wxGrid
58 m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
59
60 // Option Choices Panel:
61 if( m_choices.size() )
62 {
63 unsigned int row = 0;
64
65 for( std::map<std::string, UTF8>::const_iterator it = m_choices.begin(); it != m_choices.end(); ++it, ++row )
66 {
67 wxString item = From_UTF8( it->first.c_str() );
68
69 m_listbox->InsertItems( 1, &item, row );
70 }
71 }
72
74
75 // Configure button logos
76 m_append_button->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
77 m_delete_button->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
78
79 // initial focus on the grid please.
81
83}
84
85
86DIALOG_PLUGIN_OPTIONS ::~DIALOG_PLUGIN_OPTIONS()
87{
88 // destroy GRID_TRICKS before m_grid.
89 m_grid->PopEventHandler( true );
90}
91
92
94{
95 if( !DIALOG_SHIM::TransferDataToWindow() )
96 return false;
97
98 // Fill the grid with existing aOptions
99 std::string options = TO_UTF8( m_callers_options );
100
101 std::map<std::string, UTF8>* props = LIB_TABLE::ParseOptions( options );
102
103 if( props )
104 {
105 if( (int) props->size() > m_grid->GetNumberRows() )
106 m_grid->AppendRows( props->size() - m_grid->GetNumberRows() );
107
108 int row = 0;
109
110 for( std::map<std::string, UTF8>::const_iterator it = props->begin(); it != props->end(); ++it, ++row )
111 {
112 m_grid->SetCellValue( row, 0, From_UTF8( it->first.c_str() ) );
113 m_grid->SetCellValue( row, 1, it->second );
114 }
115
116 delete props;
117 }
118
119 return true;
120}
121
122
124{
126 return false;
127
128 if( !DIALOG_SHIM::TransferDataFromWindow() )
129 return false;
130
131 std::map<std::string, UTF8> props;
132 const int rowCount = m_grid->GetNumberRows();
133
134 for( int row = 0; row<rowCount; ++row )
135 {
136 std::string name = TO_UTF8( m_grid->GetCellValue( row, 0 ).Trim( false ).Trim() );
137 UTF8 value = m_grid->GetCellValue( row, 1 ).Trim( false ).Trim();
138
139 if( name.size() )
140 props[name] = value;
141 }
142
144 return true;
145}
146
147
149{
150 m_grid->AppendRows( 1 );
151 return m_grid->GetNumberRows() - 1;
152}
153
154
156{
157 int row = m_listbox->GetSelection();
158
159 if( row != wxNOT_FOUND )
160 {
161 wxString option = m_listbox->GetString( row );
162
163 for( row = 0; row < m_grid->GetNumberRows(); ++row )
164 {
165 wxString col0 = m_grid->GetCellValue( row, 0 );
166
167 if( !col0 ) // empty col0
168 break;
169 }
170
171 if( row == m_grid->GetNumberRows() )
172 row = appendRow();
173
174 m_grid->SetCellValue( row, 0, option );
175 m_grid_widths_dirty = true;
176 }
177
178 return row;
179}
180
181
182//-----<event handlers>------------------------------------------------------
183
185{
186 // change the help text based on the m_listbox selection:
187 if( event.IsSelection() )
188 {
189 std::string option = TO_UTF8( event.GetString() );
190
191 if( auto it = m_choices.find( option ); it != m_choices.end() )
192 m_html->SetPage( it->second );
193 else
195 }
196}
197
198
200{
201 appendOption();
202}
203
204
206{
208 [&]() -> std::pair<int, int>
209 {
210 return { appendOption(), -1 };
211 } );
212}
213
214
216{
218 [&]() -> std::pair<int, int>
219 {
220 return { appendRow(), 0 };
221 } );
222}
223
224
226{
228 [&]( int row )
229 {
230 m_grid->DeleteRows( row );
231 m_grid_widths_dirty = true;
232 } );
233}
234
235
237{
238 m_grid_widths_dirty = true;
239
240 aEvent.Skip();
241}
242
243
244void DIALOG_PLUGIN_OPTIONS::onUpdateUI( wxUpdateUIEvent& )
245{
246 if( m_grid_widths_dirty && !m_grid->IsCellEditControlShown() )
247 {
248 int width = m_grid->GetClientRect().GetWidth();
249
250 m_grid->AutoSizeColumn( 0 );
251 m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) );
252
253 m_grid->SetColSize( 1, std::max( 120, width - m_grid->GetColSize( 0 ) ) );
254
255 m_grid_widths_dirty = false;
256 }
257}
258
259
260void DIALOG_PLUGIN_OPTIONS::onSize( wxSizeEvent& aEvent )
261{
262 m_grid_widths_dirty = true;
263
264 aEvent.Skip();
265}
const char * name
Definition: DXF_plotter.cpp:62
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition: bitmap.cpp:110
Class DIALOG_PLUGIN_OPTIONS_BASE.
DIALOG_PLUGIN_OPTIONS(wxWindow *aParent, const wxString &aNickname, const std::map< std::string, UTF8 > &aPluginOptions, const wxString &aFormattedOptions, wxString *aResult)
void onAppendOption(wxCommandEvent &) override
void onListBoxItemSelected(wxCommandEvent &event) override
void onDeleteRow(wxCommandEvent &) override
bool TransferDataToWindow() override
std::map< std::string, UTF8 > m_choices
const wxString & m_callers_options
void onUpdateUI(wxUpdateUIEvent &) override
void onSize(wxSizeEvent &aEvent) override
bool TransferDataFromWindow() override
void onGridCellChange(wxGridEvent &aEvent) override
void onListBoxItemDoubleClicked(wxCommandEvent &event) override
void onAppendRow(wxCommandEvent &) override
void SetInitialFocus(wxWindow *aWindow)
Sets the window (usually a wxTextCtrl) that should be focused when the dialog is shown.
Definition: dialog_shim.h:75
void SetupStandardButtons(std::map< int, wxString > aLabels={})
Add mouse and command handling (such as cut, copy, and paste) to a WX_GRID instance.
Definition: grid_tricks.h:61
bool SetPage(const wxString &aSource) override
Definition: html_window.cpp:50
static std::map< std::string, UTF8 > * ParseOptions(const std::string &aOptionsList)
Parses aOptionsList and places the result into a #PROPERTIES object which is returned.
static UTF8 FormatOptions(const std::map< std::string, UTF8 > *aProperties)
Returns a list of options from the aProperties parameter.
void SetBitmap(const wxBitmapBundle &aBmp)
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:72
wxString wx_str() const
Definition: utf8.cpp:45
void OnDeleteRows(const std::function< void(int row)> &aDeleter)
Handles a row deletion event.
Definition: wx_grid.cpp:704
void OnAddRow(const std::function< std::pair< int, int >()> &aAdder)
Definition: wx_grid.cpp:684
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:632
#define INITIAL_HELP
#define _(s)
wxString From_UTF8(const char *cstring)
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.
Definition: string_utils.h:429