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, see <https://www.gnu.org/licenses/>.
20 */
21
22
24#include <grid_tricks.h>
26#include <widgets/wx_grid.h>
28#include <bitmaps.h>
29#include <string_utils.h>
30
31
32#define INITIAL_HELP \
33 _( "Select an <b>Option Choice</b> in the listbox above, and then click the " \
34 "<b>Append Selected Option</b> button." )
35
36
38 const wxString& aNickname,
39 const std::map<std::string, UTF8>& aPluginOptions,
40 const wxString& aFormattedOptions,
41 wxString* aResult ) :
43 m_callers_options( aFormattedOptions ),
44 m_result( aResult ),
45 m_choices( aPluginOptions ),
48{
49 SetTitle( wxString::Format( _( "Options for Library '%s'" ), aNickname ) );
50
51 m_grid->SetSelectionMode( wxGrid::wxGridSelectionModes::wxGridSelectRows );
52
53 // add Cut, Copy, and Paste to wxGrid
54 m_grid->PushEventHandler( new GRID_TRICKS( m_grid ) );
55
56 // Option Choices Panel:
57 if( m_choices.size() )
58 {
59 unsigned int row = 0;
60
61 for( std::map<std::string, UTF8>::const_iterator it = m_choices.begin(); it != m_choices.end(); ++it, ++row )
62 {
63 wxString item = From_UTF8( it->first.c_str() );
64
65 m_listbox->InsertItems( 1, &item, row );
66 }
67 }
68
69 m_html->SetPage( m_initial_help );
70
71 // Configure button logos
74
75 // initial focus on the grid please.
77
79}
80
81
82DIALOG_PLUGIN_OPTIONS ::~DIALOG_PLUGIN_OPTIONS()
83{
84 // destroy GRID_TRICKS before m_grid.
85 m_grid->PopEventHandler( true );
86}
87
88
90{
91 if( !DIALOG_SHIM::TransferDataToWindow() )
92 return false;
93
94 // Fill the grid with existing aOptions
95 std::string options = TO_UTF8( m_callers_options );
96
97 std::map<std::string, UTF8> props = LIBRARY_TABLE::ParseOptions( options );
98
99 if( !props.empty() )
100 {
101 if( props.size() > static_cast<size_t>( m_grid->GetNumberRows() ) )
102 m_grid->AppendRows( props.size() - m_grid->GetNumberRows() );
103
104 int row = 0;
105
106 for( const auto& [key, value] : props )
107 {
108 m_grid->SetCellValue( row, 0, From_UTF8( key.c_str() ) );
109 m_grid->SetCellValue( row, 1, value );
110 }
111 }
112
113 return true;
114}
115
116
118{
119 if( !m_grid->CommitPendingChanges() )
120 return false;
121
122 if( !DIALOG_SHIM::TransferDataFromWindow() )
123 return false;
124
125 std::map<std::string, UTF8> props;
126 const int rowCount = m_grid->GetNumberRows();
127
128 for( int row = 0; row<rowCount; ++row )
129 {
130 std::string name = TO_UTF8( m_grid->GetCellValue( row, 0 ).Trim( false ).Trim() );
131 UTF8 value = m_grid->GetCellValue( row, 1 ).Trim( false ).Trim();
132
133 if( name.size() )
134 props[name] = value;
135 }
136
138 return true;
139}
140
141
143{
144 m_grid->AppendRows( 1 );
145 return m_grid->GetNumberRows() - 1;
146}
147
148
150{
151 int row = m_listbox->GetSelection();
152
153 if( row != wxNOT_FOUND )
154 {
155 wxString option = m_listbox->GetString( row );
156
157 for( row = 0; row < m_grid->GetNumberRows(); ++row )
158 {
159 wxString col0 = m_grid->GetCellValue( row, 0 );
160
161 if( !col0 ) // empty col0
162 break;
163 }
164
165 if( row == m_grid->GetNumberRows() )
166 row = appendRow();
167
168 m_grid->SetCellValue( row, 0, option );
169 m_grid_widths_dirty = true;
170 }
171
172 return row;
173}
174
175
176//-----<event handlers>------------------------------------------------------
177
179{
180 // change the help text based on the m_listbox selection:
181 if( event.IsSelection() )
182 {
183 std::string option = TO_UTF8( event.GetString() );
184
185 if( auto it = m_choices.find( option ); it != m_choices.end() )
186 m_html->SetPage( it->second );
187 else
188 m_html->SetPage( m_initial_help );
189 }
190}
191
192
194{
195 appendOption();
196}
197
198
200{
201 m_grid->OnAddRow(
202 [&]() -> std::pair<int, int>
203 {
204 return { appendOption(), -1 };
205 } );
206}
207
208
210{
211 m_grid->OnAddRow(
212 [&]() -> std::pair<int, int>
213 {
214 return { appendRow(), 0 };
215 } );
216}
217
218
220{
221 m_grid->OnDeleteRows(
222 [&]( int row )
223 {
224 m_grid->DeleteRows( row );
225 m_grid_widths_dirty = true;
226 } );
227}
228
229
231{
232 m_grid_widths_dirty = true;
233
234 aEvent.Skip();
235}
236
237
238void DIALOG_PLUGIN_OPTIONS::onUpdateUI( wxUpdateUIEvent& )
239{
240 if( m_grid_widths_dirty && !m_grid->IsCellEditControlShown() )
241 {
242 int width = m_grid->GetClientRect().GetWidth();
243
244 m_grid->AutoSizeColumn( 0 );
245 m_grid->SetColSize( 0, std::max( 72, m_grid->GetColSize( 0 ) ) );
246
247 m_grid->SetColSize( 1, std::max( 120, width - m_grid->GetColSize( 0 ) ) );
248
249 m_grid_widths_dirty = false;
250 }
251}
252
253
254void DIALOG_PLUGIN_OPTIONS::onSize( wxSizeEvent& aEvent )
255{
256 m_grid_widths_dirty = true;
257
258 aEvent.Skip();
259}
const char * name
wxBitmapBundle KiBitmapBundle(BITMAPS aBitmap, int aMinHeight)
Definition bitmap.cpp:106
DIALOG_PLUGIN_OPTIONS_BASE(wxWindow *parent, wxWindowID id=wxID_ANY, const wxString &title=wxEmptyString, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxSize(-1,-1), long style=wxCAPTION|wxCLOSE_BOX|wxDEFAULT_DIALOG_STYLE|wxMAXIMIZE_BOX|wxRESIZE_BORDER)
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
std::map< std::string, UTF8 > m_choices
const wxString & m_callers_options
void onUpdateUI(wxUpdateUIEvent &) override
void onSize(wxSizeEvent &aEvent) 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:79
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:57
static std::map< std::string, UTF8 > ParseOptions(const std::string &aOptionsList)
static UTF8 FormatOptions(const std::map< std::string, UTF8 > *aProperties)
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition utf8.h:67
wxString wx_str() const
Definition utf8.cpp:41
#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.