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 (C) 2013-2023 KiCad Developers, see change_log.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 <b>Append Selected Option</b> button." )
38
39
41 const wxString& aNickname,
42 const STRING_UTF8_MAP& aPluginOptions,
43 const wxString& aFormattedOptions,
44 wxString* aResult ) :
46 m_callers_options( aFormattedOptions ),
47 m_result( aResult ),
48 m_choices( aPluginOptions ),
49 m_initial_help( INITIAL_HELP ),
50 m_grid_widths_dirty( true )
51{
52 SetTitle( wxString::Format( _( "Options for Library '%s'" ), aNickname ) );
53
54 // Give a bit more room for combobox editors
55 m_grid->SetDefaultRowSize( m_grid->GetDefaultRowSize() + 4 );
56
57 m_grid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
58
59 // add Cut, Copy, and Paste to wxGrid
60 m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
61
62 // Option Choices Panel:
63 if( m_choices.size() )
64 {
65 unsigned int row = 0;
66
67 for( STRING_UTF8_MAP::const_iterator it = m_choices.begin(); it != m_choices.end();
68 ++it, ++row )
69 {
70 wxString item = From_UTF8( it->first.c_str() );
71
72 m_listbox->InsertItems( 1, &item, row );
73 }
74 }
75
77
78 // Configure button logos
79 m_append_button->SetBitmap( KiBitmapBundle( BITMAPS::small_plus ) );
80 m_delete_button->SetBitmap( KiBitmapBundle( BITMAPS::small_trash ) );
81
82 // initial focus on the grid please.
84
86}
87
88
89DIALOG_PLUGIN_OPTIONS ::~DIALOG_PLUGIN_OPTIONS()
90{
91 // destroy GRID_TRICKS before m_grid.
92 m_grid->PopEventHandler( true );
93}
94
95
97{
98 if( !DIALOG_SHIM::TransferDataToWindow() )
99 return false;
100
101 // Fill the grid with existing aOptions
102 std::string options = TO_UTF8( m_callers_options );
103
104 STRING_UTF8_MAP* props = LIB_TABLE::ParseOptions( options );
105
106 if( props )
107 {
108 if( (int) props->size() > m_grid->GetNumberRows() )
109 m_grid->AppendRows( props->size() - m_grid->GetNumberRows() );
110
111 int row = 0;
112
113 for( STRING_UTF8_MAP::const_iterator it = props->begin(); it != props->end();
114 ++it, ++row )
115 {
116 m_grid->SetCellValue( row, 0, From_UTF8( it->first.c_str() ) );
117 m_grid->SetCellValue( row, 1, it->second );
118 }
119
120 delete props;
121 }
122
123 return true;
124}
125
126
128{
130 return false;
131
132 if( !DIALOG_SHIM::TransferDataFromWindow() )
133 return false;
134
135 STRING_UTF8_MAP props;
136 const int rowCount = m_grid->GetNumberRows();
137
138 for( int row = 0; row<rowCount; ++row )
139 {
140 std::string name = TO_UTF8( m_grid->GetCellValue( row, 0 ).Trim( false ).Trim() );
141 UTF8 value = m_grid->GetCellValue( row, 1 ).Trim( false ).Trim();
142
143 if( name.size() )
144 {
145 props[name] = value;
146 }
147 }
148
150 return true;
151}
152
153
155{
156 int row = m_grid->GetNumberRows();
157
158 m_grid->AppendRows( 1 );
159
160 // wx documentation is wrong, SetGridCursor does not make visible.
161 m_grid->MakeCellVisible( row, 0 );
162 m_grid->SetGridCursor( row, 0 );
163
164 return row;
165}
166
167
169{
170 int selected_row = m_listbox->GetSelection();
171 if( selected_row != wxNOT_FOUND )
172 {
173 wxString option = m_listbox->GetString( selected_row );
174
175 int row_count = m_grid->GetNumberRows();
176 int row;
177
178 for( row=0; row<row_count; ++row )
179 {
180 wxString col0 = m_grid->GetCellValue( row, 0 );
181
182 if( !col0 ) // empty col0
183 break;
184 }
185
186 if( row == row_count )
187 row = appendRow();
188
189 m_grid->SetCellValue( row, 0, option );
190 m_grid_widths_dirty = true;
191 }
192}
193
194
195//-----<event handlers>------------------------------------------------------
196
198{
199 // change the help text based on the m_listbox selection:
200 if( event.IsSelection() )
201 {
202 std::string option = TO_UTF8( event.GetString() );
203 UTF8 help_text;
204
205 if( m_choices.Value( option.c_str(), &help_text ) )
206 m_html->SetPage( help_text );
207 else
209 }
210}
211
212
214{
215 appendOption();
216}
217
218
220{
222 return;
223
224 appendOption();
225}
226
227
229{
231 return;
232
233 appendRow();
234}
235
236
238{
240 return;
241
242 int curRow = m_grid->GetGridCursorRow();
243
244 m_grid->DeleteRows( curRow );
245 m_grid_widths_dirty = true;
246
247 curRow = std::max( 0, curRow - 1 );
248 m_grid->MakeCellVisible( curRow, m_grid->GetGridCursorCol() );
249 m_grid->SetGridCursor( curRow, m_grid->GetGridCursorCol() );
250}
251
252
254{
255 m_grid_widths_dirty = true;
256
257 aEvent.Skip();
258}
259
260
261void DIALOG_PLUGIN_OPTIONS::onUpdateUI( wxUpdateUIEvent& )
262{
263 if( m_grid_widths_dirty && !m_grid->IsCellEditControlShown() )
264 {
265 int width = m_grid->GetClientRect().GetWidth();
266
267 m_grid->AutoSizeColumn( 0 );
268 m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) );
269
270 m_grid->SetColSize( 1, std::max( 120, width - m_grid->GetColSize( 0 ) ) );
271
272 m_grid_widths_dirty = false;
273 }
274}
275
276
277void DIALOG_PLUGIN_OPTIONS::onSize( wxSizeEvent& aEvent )
278{
279 m_grid_widths_dirty = true;
280
281 aEvent.Skip();
282}
const char * name
Definition: DXF_plotter.cpp:57
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap)
Definition: bitmap.cpp:110
Class DIALOG_PLUGIN_OPTIONS_BASE.
void onAppendOption(wxCommandEvent &) override
void onListBoxItemSelected(wxCommandEvent &event) override
void onDeleteRow(wxCommandEvent &) override
bool TransferDataToWindow() override
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
DIALOG_PLUGIN_OPTIONS(wxWindow *aParent, const wxString &aNickname, const STRING_UTF8_MAP &aPluginOptions, const wxString &aFormattedOptions, wxString *aResult)
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:97
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:38
static STRING_UTF8_MAP * ParseOptions(const std::string &aOptionsList)
Parses aOptionsList and places the result into a #PROPERTIES object which is returned.
static UTF8 FormatOptions(const STRING_UTF8_MAP *aProperties)
Returns a list of options from the aProperties parameter.
void SetBitmap(const wxBitmapBundle &aBmp)
A name/value tuple with unique names and optional values.
bool Value(const char *aName, UTF8 *aFetchedValue=nullptr) const
Fetch a property by aName and returns true if that property was found, else false.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:71
wxString wx_str() const
Definition: utf8.cpp:45
bool CommitPendingChanges(bool aQuietMode=false)
Close any open cell edit controls.
Definition: wx_grid.cpp:462
#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:391