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.

145 lines
4.2 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. #include <dialogs/eda_view_switcher.h>
  24. #ifdef __WXGTK__
  25. #define LIST_BOX_H_PADDING 20
  26. #define LIST_BOX_V_PADDING 8
  27. #else
  28. #define LIST_BOX_H_PADDING 10
  29. #define LIST_BOX_V_PADDING 5
  30. #endif
  31. EDA_VIEW_SWITCHER::EDA_VIEW_SWITCHER( wxWindow* aParent, const wxArrayString& aItems,
  32. wxKeyCode aCtrlKey ) :
  33. EDA_VIEW_SWITCHER_BASE( aParent ),
  34. m_tabState( true ),
  35. m_receivingEvents( false ),
  36. m_ctrlKey( aCtrlKey )
  37. {
  38. m_listBox->InsertItems( aItems, 0 );
  39. m_listBox->SetSelection( std::min( 0, (int) m_listBox->GetCount() - 1 ) );
  40. int width = 0;
  41. int height = 0;
  42. for( const wxString& item : aItems )
  43. {
  44. wxSize extents = m_listBox->GetTextExtent( item );
  45. width = std::max( width, extents.x );
  46. height += extents.y + LIST_BOX_V_PADDING;
  47. }
  48. m_listBox->SetMinSize( wxSize( width + LIST_BOX_H_PADDING,
  49. height + ( LIST_BOX_V_PADDING * 2 ) ) );
  50. SetInitialFocus( m_listBox );
  51. // this line fixes an issue on Linux Ubuntu using Unity (dialog not shown),
  52. // and works fine on all systems
  53. GetSizer()->Fit( this );
  54. Centre();
  55. }
  56. bool EDA_VIEW_SWITCHER::Show( bool aShow )
  57. {
  58. if( !aShow )
  59. m_receivingEvents = false;
  60. bool ret = DIALOG_SHIM::Show( aShow );
  61. if( aShow )
  62. {
  63. m_receivingEvents = true;
  64. // Force the dialog to always be centered over the window
  65. Centre();
  66. }
  67. return ret;
  68. }
  69. // OK, this is *really* annoying, but wxWidgets doesn't give us key-down events while the
  70. // control key is being held down. So we can't use OnKeyDown() or OnCharHook() and instead
  71. // must rely on watching key states in TryBefore().
  72. //
  73. // Just checking the state of the tab key is tempting, but then we'll think it's been hit
  74. // several times when it's actually just a key-down followed by a redraw or idle event.
  75. //
  76. // So we have to keep a state machine of the tab key.
  77. //
  78. bool EDA_VIEW_SWITCHER::TryBefore( wxEvent& aEvent )
  79. {
  80. if( !m_receivingEvents )
  81. {
  82. return DIALOG_SHIM::TryBefore( aEvent );
  83. }
  84. // Check for tab key leading edge
  85. if( !m_tabState && wxGetKeyState( WXK_TAB ) )
  86. {
  87. m_tabState = true;
  88. int idx = m_listBox->GetSelection();
  89. if( wxGetKeyState( WXK_SHIFT ) && m_ctrlKey != WXK_SHIFT )
  90. {
  91. if( --idx < 0 )
  92. m_listBox->SetSelection( (int) m_listBox->GetCount() - 1 );
  93. else
  94. m_listBox->SetSelection( idx );
  95. }
  96. else
  97. {
  98. if( ++idx >= (int) m_listBox->GetCount() )
  99. m_listBox->SetSelection( 0 );
  100. else
  101. m_listBox->SetSelection( idx );
  102. }
  103. return true;
  104. }
  105. // Check for tab key trailing edge
  106. if( m_tabState && !wxGetKeyState( WXK_TAB ) )
  107. {
  108. m_tabState = false;
  109. }
  110. // Check for control key trailing edge
  111. if( !wxGetKeyState( m_ctrlKey ) )
  112. {
  113. wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK ) );
  114. }
  115. else if( wxGetKeyState( WXK_ESCAPE ) )
  116. {
  117. wxPostEvent( this, wxCommandEvent( wxEVT_COMMAND_BUTTON_CLICKED, wxID_CANCEL ) );
  118. }
  119. return DIALOG_SHIM::TryBefore( aEvent );
  120. }