You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
5.5 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.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. #ifndef WIDGETS_WIDGET_SAVE_RESTORE__H
  24. #define WIDGETS_WIDGET_SAVE_RESTORE__H
  25. #include <base_units.h>
  26. #include <vector>
  27. class wxCheckBox;
  28. class wxChoice;
  29. class wxNotebook;
  30. class wxRadioBox;
  31. class wxRadioButton;
  32. class wxString;
  33. class wxTextCtrl;
  34. class UNIT_BINDER;
  35. class EDA_ANGLE;
  36. class WIDGET_SAVE_RESTORE
  37. {
  38. public:
  39. WIDGET_SAVE_RESTORE( const EDA_IU_SCALE& aIuScale, bool& aValidFlag ) :
  40. m_valid( aValidFlag )
  41. {
  42. }
  43. /**
  44. * Bind a radiobox to a choice.
  45. */
  46. void Add( wxRadioBox& ctrl, long& dest );
  47. /**
  48. * Bind a radio button to a binary choice
  49. */
  50. void Add( wxRadioButton& ctrl, bool& dest );
  51. /**
  52. * Bind a check box to a binary choice
  53. */
  54. void Add( wxCheckBox& ctrl, bool& dest );
  55. /**
  56. * Bind a text ctrl to a string: the control value is stored directly
  57. * into the string
  58. */
  59. void Add( wxTextCtrl& ctrl, wxString& dest );
  60. /**
  61. * Bind a text ctrl to a integer: the control value is converted to and
  62. * from integer on save/restore.
  63. */
  64. void Add( wxTextCtrl& ctrl, long& dest );
  65. /**
  66. * Bind a text ctrl to a double: the control value is converted to and
  67. * from double on save/restore.
  68. */
  69. void Add( wxTextCtrl& ctrl, double& dest );
  70. /**
  71. * Bind a control managed by a #UNIT_BINDER into a integer
  72. */
  73. void Add( UNIT_BINDER& ctrl, long& dest );
  74. /**
  75. * Bind a control managed by a #UNIT_BINDER into an angle
  76. */
  77. void Add( UNIT_BINDER& ctrl, EDA_ANGLE& dest );
  78. /**
  79. * Bind a choice control into a choice (agnostic to the actual
  80. * meaning of the choice)
  81. */
  82. void Add( wxChoice& ctrl, long& dest );
  83. /**
  84. * Bind a notebook tab choice to an integer
  85. */
  86. void Add( wxNotebook& ctrl, long& dest );
  87. /**
  88. * Read values of all bound controls into the internally-stored
  89. * references to the underlying data.
  90. */
  91. void ReadConfigFromControls();
  92. /**
  93. * Restore the values from the internally-stored references to the underlying
  94. * data to each bound control.
  95. */
  96. void RestoreConfigToControls();
  97. private:
  98. /**
  99. * Recognised parameters types (encodes an implicit widget type,
  100. * data type and appropriate conversion).
  101. */
  102. enum class WIDGET_CTRL_TYPE_T
  103. {
  104. TEXT,
  105. TEXT_INTEGER,
  106. TEXT_DOUBLE,
  107. UNIT_BINDER,
  108. UNIT_BINDER_ANGLE,
  109. CHECKBOX,
  110. RADIOBUTTON,
  111. RADIOBOX,
  112. CHOICE,
  113. TAB
  114. };
  115. union CONTROL {
  116. CONTROL( wxCheckBox* aCtrl ) :
  117. m_checkbox( aCtrl )
  118. {
  119. }
  120. CONTROL( wxRadioButton* aCtrl ) :
  121. m_radiobutton( aCtrl )
  122. {
  123. }
  124. CONTROL( wxChoice* aCtrl ) :
  125. m_choice( aCtrl )
  126. {
  127. }
  128. CONTROL( wxNotebook* aCtrl ) :
  129. m_notebook( aCtrl )
  130. {
  131. }
  132. CONTROL( wxRadioBox* aCtrl ) :
  133. m_radiobox( aCtrl )
  134. {
  135. }
  136. CONTROL( wxTextCtrl* aCtrl ) :
  137. m_textctrl( aCtrl )
  138. {
  139. }
  140. CONTROL( UNIT_BINDER* aCtrl ) :
  141. m_unit_binder( aCtrl )
  142. {
  143. }
  144. wxCheckBox* m_checkbox;
  145. wxChoice* m_choice;
  146. wxNotebook* m_notebook;
  147. wxRadioBox* m_radiobox;
  148. wxRadioButton* m_radiobutton;
  149. wxTextCtrl* m_textctrl;
  150. UNIT_BINDER* m_unit_binder;
  151. };
  152. union DATA {
  153. DATA( long* aDest ) :
  154. m_long( aDest )
  155. {
  156. }
  157. DATA( bool* aDest ) :
  158. m_bool( aDest )
  159. {
  160. }
  161. DATA( wxString* aDest ) :
  162. m_str( aDest )
  163. {
  164. }
  165. DATA( double* aDest ) :
  166. m_double( aDest )
  167. {
  168. }
  169. DATA( EDA_ANGLE* aDest ) :
  170. m_angle( aDest )
  171. {
  172. }
  173. long* m_long;
  174. bool* m_bool;
  175. wxString* m_str;
  176. double* m_double;
  177. EDA_ANGLE* m_angle;
  178. };
  179. /**
  180. * Struct that represents a single bound control
  181. */
  182. struct WIDGET_CTRL_T
  183. {
  184. template <typename CTRL_T, typename DEST_T>
  185. WIDGET_CTRL_T( WIDGET_CTRL_TYPE_T aType, CTRL_T& aCtrl, DEST_T& aDest ) :
  186. m_type( aType ),
  187. m_control( &aCtrl ),
  188. m_dest( &aDest )
  189. {
  190. }
  191. WIDGET_CTRL_TYPE_T m_type;
  192. CONTROL m_control;
  193. DATA m_dest;
  194. };
  195. std::vector<WIDGET_CTRL_T> m_ctrls;
  196. bool& m_valid;
  197. };
  198. #endif // WIDGETS_WIDGET_SAVE_RESTORE__H