KiCad PCB EDA Suite
widget_save_restore.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 
25 
26 #include <wx/checkbox.h>
27 #include <wx/choice.h>
28 #include <wx/notebook.h>
29 #include <wx/radiobox.h>
30 #include <wx/textctrl.h>
31 
32 #include <widgets/unit_binder.h>
33 
34 
35 void WIDGET_SAVE_RESTORE::Add( wxRadioBox& ctrl, long& dest )
36 {
37  m_ctrls.emplace_back( WIDGET_CTRL_TYPE_T::RADIOBOX, ctrl, dest );
38 }
39 
40 
41 void WIDGET_SAVE_RESTORE::Add( wxCheckBox& ctrl, bool& dest )
42 {
43  m_ctrls.emplace_back( WIDGET_CTRL_TYPE_T::CHECKBOX, ctrl, dest );
44 }
45 
46 
47 void WIDGET_SAVE_RESTORE::Add( wxTextCtrl& ctrl, wxString& dest )
48 {
49  m_ctrls.emplace_back( WIDGET_CTRL_TYPE_T::TEXT, ctrl, dest );
50 }
51 
52 
53 void WIDGET_SAVE_RESTORE::Add( wxTextCtrl& ctrl, long& dest )
54 {
55  m_ctrls.emplace_back( WIDGET_CTRL_TYPE_T::TEXT_INTEGER, ctrl, dest );
56 }
57 
58 
59 void WIDGET_SAVE_RESTORE::Add( wxTextCtrl& ctrl, double& dest )
60 {
61  m_ctrls.emplace_back( WIDGET_CTRL_TYPE_T::TEXT_DOUBLE, ctrl, dest );
62 }
63 
64 
65 void WIDGET_SAVE_RESTORE::Add( UNIT_BINDER& ctrl, long& dest )
66 {
67  m_ctrls.emplace_back( WIDGET_CTRL_TYPE_T::UNIT_BINDER, ctrl, dest );
68 }
69 
70 
71 void WIDGET_SAVE_RESTORE::Add( wxChoice& ctrl, long& dest )
72 {
73  m_ctrls.emplace_back( WIDGET_CTRL_TYPE_T::CHOICE, ctrl, dest );
74 }
75 
76 
77 void WIDGET_SAVE_RESTORE::Add( wxNotebook& ctrl, long& dest )
78 {
79  m_ctrls.emplace_back( WIDGET_CTRL_TYPE_T::TAB, ctrl, dest );
80 }
81 
82 
84 {
85  for( auto& ctrl : m_ctrls )
86  {
87  switch( ctrl.m_type )
88  {
90  *ctrl.m_dest.m_bool = ctrl.m_control.m_checkbox->GetValue();
91  break;
92 
94  *ctrl.m_dest.m_str = ctrl.m_control.m_textctrl->GetValue();
95  break;
96 
98  ctrl.m_control.m_textctrl->GetValue().ToLong( ctrl.m_dest.m_long );
99  break;
100 
102  ctrl.m_control.m_textctrl->GetValue().ToDouble( ctrl.m_dest.m_double );
103  break;
104 
106  *ctrl.m_dest.m_long = ctrl.m_control.m_unit_binder->GetValue();
107  break;
108 
110  *ctrl.m_dest.m_long = ctrl.m_control.m_choice->GetSelection();
111  break;
112 
114  *ctrl.m_dest.m_long = ctrl.m_control.m_radiobox->GetSelection();
115  break;
116 
118  *ctrl.m_dest.m_long = ctrl.m_control.m_notebook->GetSelection();
119  break;
120  }
121  }
122 
123  m_valid = true;
124 }
125 
126 
128 {
129  if( !m_valid )
130  return;
131 
132  for( auto& ctrl : m_ctrls )
133  {
134  switch( ctrl.m_type )
135  {
137  ctrl.m_control.m_checkbox->SetValue( *ctrl.m_dest.m_bool );
138  break;
139 
141  ctrl.m_control.m_textctrl->SetValue( *ctrl.m_dest.m_str );
142  break;
143 
145  ctrl.m_control.m_textctrl->SetValue( wxString::Format( "%ld", *ctrl.m_dest.m_long ) );
146  break;
147 
149  ctrl.m_control.m_textctrl->SetValue( wxString::Format( "%f", *ctrl.m_dest.m_double ) );
150  break;
151 
153  ctrl.m_control.m_unit_binder->SetValue( *ctrl.m_dest.m_long );
154  break;
155 
157  ctrl.m_control.m_choice->SetSelection( *ctrl.m_dest.m_long );
158  break;
159 
161  ctrl.m_control.m_radiobox->SetSelection( *ctrl.m_dest.m_long );
162  break;
163 
165  ctrl.m_control.m_notebook->SetSelection( *ctrl.m_dest.m_long );
166  break;
167  }
168  }
169 }
std::vector< WIDGET_CTRL_T > m_ctrls
void RestoreConfigToControls()
Restore the values from the internally-stored references to the underlying data to each bound control...
void ReadConfigFromControls()
Read values of all bound controls into the internally-stored references to the underlying data.
void Add(wxRadioBox &ctrl, long &dest)
Bind a radiobox to a choice.
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