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.

172 lines
5.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2016 Chris Pavlina <pavlina.chris@gmail.com>
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef __widget_hotkey_list__
  25. #define __widget_hotkey_list__
  26. #include <unordered_map>
  27. #include <utility>
  28. #include <vector>
  29. #include <wx/treelist.h>
  30. #include <wx/dataview.h>
  31. #include <hotkeys_basic.h>
  32. #include <hotkey_store.h>
  33. class WIDGET_HOTKEY_CLIENT_DATA;
  34. class WIDGET_HOTKEY_LIST : public wxTreeListCtrl
  35. {
  36. public:
  37. /**
  38. * Create a #WIDGET_HOTKEY_LIST.
  39. *
  40. * @param aParent is the parent widget.
  41. * @param aHotkeys is the #EDA_HOTKEY_CONFIG data: a hotkey store is constructed from this.
  42. */
  43. WIDGET_HOTKEY_LIST( wxWindow* aParent, HOTKEY_STORE& aHotkeyStore, bool aReadOnly );
  44. /**
  45. * Apply a filter string to the hotkey list, selecting which hotkeys to show.
  46. *
  47. * @param aFilterStr the string to filter by.
  48. */
  49. void ApplyFilterString( const wxString& aFilterStr );
  50. /**
  51. * Set hotkeys in the control to default or original values.
  52. *
  53. * @param aResetToDefault if true, reset to the defaults inherent to the hotkeys, else
  54. * reset to the value they had when the dialog was invoked.
  55. */
  56. void ResetAllHotkeys( bool aResetToDefault );
  57. /**
  58. * Load the hotkey data from the store into the control.
  59. *
  60. * @return true if the operation was successful.
  61. */
  62. bool TransferDataToControl();
  63. /**
  64. * Save the hotkey data from the control.
  65. *
  66. * @return true if the operation was successful.
  67. */
  68. bool TransferDataFromControl();
  69. /**
  70. * Map a keypress event to the correct key code for use as a hotkey.
  71. */
  72. static long MapKeypressToKeycode( const wxKeyEvent& aEvent );
  73. protected:
  74. /**
  75. * Prompt the user for a new hotkey given a list item.
  76. */
  77. void editItem( wxTreeListItem aItem, int aEditId );
  78. /**
  79. * Reset the item to either the default, the value when the dialog was opened, or none.
  80. */
  81. void resetItem( wxTreeListItem aItem, int aResetId );
  82. /**
  83. * Handle activation of a row.
  84. */
  85. void onActivated( wxTreeListEvent& aEvent );
  86. /**
  87. * Handle right-click on a row.
  88. */
  89. void onContextMenu( wxTreeListEvent& aEvent );
  90. /**
  91. * Handle activation of a context menu item.
  92. */
  93. void onMenu( wxCommandEvent& aEvent );
  94. /**
  95. * Check if we can set a hotkey, and prompt the user if there is a conflict between keys.
  96. * The key code should already have been checked that it's not for the same entry as it's
  97. * current in, or else this method will prompt for the self-change.
  98. *
  99. * The method will do conflict resolution depending on aSectionTag.
  100. * g_CommonSectionTag means the key code must only be checked with the aSectionTag section
  101. * and g_CommonSectionTag section.
  102. *
  103. * @param aKey is the key to check.
  104. * @param aActionName is the name of the action into which the key is proposed to be installed.
  105. *
  106. * @return true if the user accepted the overwrite or no conflict existed.
  107. */
  108. bool resolveKeyConflicts( TOOL_ACTION* aAction, long aKey );
  109. private:
  110. /**
  111. * Return the #WIDGET_HOTKEY_CLIENT_DATA for the given item, or NULL if the item is invalid.
  112. */
  113. WIDGET_HOTKEY_CLIENT_DATA* getHKClientData( wxTreeListItem aItem );
  114. /**
  115. * Refresh the visible text on the widget from the rows' client data objects.
  116. */
  117. void updateFromClientData();
  118. /**
  119. * Update the items shown in the widget based on a given filter string.
  120. *
  121. * @param aFilterStr the string to filter with. Empty means no filter.
  122. */
  123. void updateShownItems( const wxString& aFilterStr );
  124. /**
  125. * Attempt to change the given hotkey to the given key code.
  126. *
  127. * If the hotkey conflicts, the user is prompted to change anyway (and in doing so, unset
  128. * the conflicting key), or cancel the attempt.
  129. *
  130. * @param aHotkey the change-able hotkey to try to change.
  131. * @param aKey the key code to change it to.
  132. * @param alternate Change the secondary hotkey.
  133. */
  134. void changeHotkey( HOTKEY& aHotkey, long aKey, bool alternate );
  135. /**
  136. * Recalculate column widths after model has changed.
  137. */
  138. void updateColumnWidths();
  139. private:
  140. HOTKEY_STORE& m_hk_store;
  141. bool m_readOnly;
  142. std::unordered_map<long, wxString> m_reservedHotkeys;
  143. wxTreeListItem m_context_menu_item;
  144. };
  145. #endif // __widget_hotkey_list__