From 07d613d0d2ff691d62d813f00312398022b8e48d Mon Sep 17 00:00:00 2001 From: Ian McInerney Date: Sat, 8 Apr 2023 00:07:33 +0100 Subject: [PATCH] Subclass wxDataViewCtrl to include our helpers It is cleaner to just extend the class with these functions rather than having the separate functions. --- common/CMakeLists.txt | 2 +- common/widgets/lib_tree.cpp | 12 ++-- .../wx_dataviewctrl.cpp} | 38 +++++----- include/widgets/lib_tree.h | 18 ++--- include/widgets/wx_dataviewctrl.h | 70 +++++++++++++++++++ include/wxdataviewctrl_helpers.h | 67 ------------------ 6 files changed, 105 insertions(+), 102 deletions(-) rename common/{wxdataviewctrl_helpers.cpp => widgets/wx_dataviewctrl.cpp} (64%) create mode 100644 include/widgets/wx_dataviewctrl.h delete mode 100644 include/wxdataviewctrl_helpers.h diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 31a877107d..412a175566 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -206,6 +206,7 @@ set( COMMON_WIDGET_SRCS widgets/wx_busy_indicator.cpp widgets/wx_collapsible_pane.cpp widgets/wx_combobox.cpp + widgets/wx_dataviewctrl.cpp widgets/wx_ellipsized_static_text.cpp widgets/wx_grid.cpp widgets/wx_html_report_box.cpp @@ -387,7 +388,6 @@ set( COMMON_SRCS validators.cpp wildcards_and_files_ext.cpp drawing_sheet/ds_painter.cpp - wxdataviewctrl_helpers.cpp wx_stl_compat.cpp wx_filename.cpp xnode.cpp diff --git a/common/widgets/lib_tree.cpp b/common/widgets/lib_tree.cpp index d500acdfe9..818ddd74f7 100644 --- a/common/widgets/lib_tree.cpp +++ b/common/widgets/lib_tree.cpp @@ -25,15 +25,15 @@ #include #include #include -#include -#include #include #include #include #include #include -#include +#include #include +#include +#include #include constexpr int RECENT_SEARCHES_MAX = 10; @@ -106,7 +106,7 @@ LIB_TREE::LIB_TREE( wxWindow* aParent, const wxString& aRecentSearchesKey, LIB_T // Tree control int dvFlags = ( aFlags & MULTISELECT ) ? wxDV_MULTIPLE : wxDV_SINGLE; - m_tree_ctrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, dvFlags ); + m_tree_ctrl = new WX_DATAVIEWCTRL( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, dvFlags ); m_adapter->AttachTo( m_tree_ctrl ); if( aFlags & DETAILS ) @@ -488,12 +488,12 @@ void LIB_TREE::onQueryCharHook( wxKeyEvent& aKeyStroke ) { case WXK_UP: updateRecentSearchMenu(); - selectIfValid( GetPrevItem( *m_tree_ctrl, sel ) ); + selectIfValid( m_tree_ctrl->GetPrevItem( sel ) ); break; case WXK_DOWN: updateRecentSearchMenu(); - selectIfValid( GetNextItem( *m_tree_ctrl, sel ) ); + selectIfValid( m_tree_ctrl->GetNextItem( sel ) ); break; case WXK_ADD: diff --git a/common/wxdataviewctrl_helpers.cpp b/common/widgets/wx_dataviewctrl.cpp similarity index 64% rename from common/wxdataviewctrl_helpers.cpp rename to common/widgets/wx_dataviewctrl.cpp index 8b60c17c65..81ad5acedf 100644 --- a/common/wxdataviewctrl_helpers.cpp +++ b/common/widgets/wx_dataviewctrl.cpp @@ -1,7 +1,7 @@ /* * This program source code file is part of KiCad, a free EDA CAD application. * - * Copyright (C) 2017-2022 KiCad Developers, see AUTHORS.txt for contributors. + * Copyright (C) 2017-2023 KiCad Developers, see AUTHORS.txt for contributors. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the @@ -17,21 +17,21 @@ * with this program. If not, see . */ +#include #include -#include -wxDataViewItem GetPrevItem( wxDataViewCtrl const& aView, wxDataViewItem const& aItem ) +wxDataViewItem WX_DATAVIEWCTRL::GetPrevItem( wxDataViewItem const& aItem ) { - wxDataViewItem prevItem = GetPrevSibling( aView, aItem ); + wxDataViewItem prevItem = GetPrevSibling( aItem ); if( !prevItem.IsOk() ) { - prevItem = aView.GetModel()->GetParent( aItem ); + prevItem = GetModel()->GetParent( aItem ); } - else if( aView.IsExpanded( prevItem ) ) + else if( IsExpanded( prevItem ) ) { wxDataViewItemArray children; - aView.GetModel()->GetChildren( prevItem, children ); + GetModel()->GetChildren( prevItem, children ); prevItem = children[children.size() - 1]; } @@ -39,7 +39,7 @@ wxDataViewItem GetPrevItem( wxDataViewCtrl const& aView, wxDataViewItem const& a } -wxDataViewItem GetNextItem( wxDataViewCtrl const& aView, wxDataViewItem const& aItem ) +wxDataViewItem WX_DATAVIEWCTRL::GetNextItem( wxDataViewItem const& aItem ) { wxDataViewItem nextItem; wxDataViewItem invalid; @@ -48,7 +48,7 @@ wxDataViewItem GetNextItem( wxDataViewCtrl const& aView, wxDataViewItem const& a { // No selection. Select the first. wxDataViewItemArray children; - aView.GetModel()->GetChildren( aItem, children ); + GetModel()->GetChildren( aItem, children ); if( children.size() ) return children[0]; @@ -56,10 +56,10 @@ wxDataViewItem GetNextItem( wxDataViewCtrl const& aView, wxDataViewItem const& a return invalid; } - if( aView.IsExpanded( aItem ) ) + if( IsExpanded( aItem ) ) { wxDataViewItemArray children; - aView.GetModel()->GetChildren( aItem, children ); + GetModel()->GetChildren( aItem, children ); if( children.size() ) return children[0]; @@ -69,9 +69,9 @@ wxDataViewItem GetNextItem( wxDataViewCtrl const& aView, wxDataViewItem const& a else { // Walk up levels until we find one that has a next sibling. - for( wxDataViewItem walk = aItem; walk.IsOk(); walk = aView.GetModel()->GetParent( walk ) ) + for( wxDataViewItem walk = aItem; walk.IsOk(); walk = GetModel()->GetParent( walk ) ) { - nextItem = GetNextSibling( aView, walk ); + nextItem = GetNextSibling( walk ); if( nextItem.IsOk() ) break; @@ -82,13 +82,13 @@ wxDataViewItem GetNextItem( wxDataViewCtrl const& aView, wxDataViewItem const& a } -wxDataViewItem GetPrevSibling( wxDataViewCtrl const& aView, wxDataViewItem const& aItem ) +wxDataViewItem WX_DATAVIEWCTRL::GetPrevSibling( wxDataViewItem const& aItem ) { wxDataViewItemArray siblings; wxDataViewItem invalid; - wxDataViewItem parent = aView.GetModel()->GetParent( aItem ); + wxDataViewItem parent = GetModel()->GetParent( aItem ); - aView.GetModel()->GetChildren( parent, siblings ); + GetModel()->GetChildren( parent, siblings ); for( size_t i = 0; i < siblings.size(); ++i ) { @@ -105,13 +105,13 @@ wxDataViewItem GetPrevSibling( wxDataViewCtrl const& aView, wxDataViewItem const } -wxDataViewItem GetNextSibling( wxDataViewCtrl const& aView, wxDataViewItem const& aItem ) +wxDataViewItem WX_DATAVIEWCTRL::GetNextSibling( wxDataViewItem const& aItem ) { wxDataViewItemArray siblings; wxDataViewItem invalid; - wxDataViewItem parent = aView.GetModel()->GetParent( aItem ); + wxDataViewItem parent = GetModel()->GetParent( aItem ); - aView.GetModel()->GetChildren( parent, siblings ); + GetModel()->GetChildren( parent, siblings ); for( size_t i = 0; i < siblings.size(); ++i ) { diff --git a/include/widgets/lib_tree.h b/include/widgets/lib_tree.h index 9dd215b14d..9be994abbe 100644 --- a/include/widgets/lib_tree.h +++ b/include/widgets/lib_tree.h @@ -28,8 +28,8 @@ #include #include #include +#include -class wxDataViewCtrl; class wxTextCtrl; class wxHtmlLinkEvent; class wxSearchCtrl; @@ -216,16 +216,16 @@ protected: wxObjectDataPtr m_adapter; - wxSearchCtrl* m_query_ctrl; - wxDataViewCtrl* m_tree_ctrl; - HTML_WINDOW* m_details_ctrl; - wxTimer* m_debounceTimer; - bool m_inTimerEvent; + wxSearchCtrl* m_query_ctrl; + WX_DATAVIEWCTRL* m_tree_ctrl; + HTML_WINDOW* m_details_ctrl; + wxTimer* m_debounceTimer; + bool m_inTimerEvent; - LIB_ID m_last_libid; - wxString m_recentSearchesKey; + LIB_ID m_last_libid; + wxString m_recentSearchesKey; - bool m_skipNextRightClick; + bool m_skipNextRightClick; }; ///< Custom event sent when a new symbol is preselected diff --git a/include/widgets/wx_dataviewctrl.h b/include/widgets/wx_dataviewctrl.h new file mode 100644 index 0000000000..d104ef7edf --- /dev/null +++ b/include/widgets/wx_dataviewctrl.h @@ -0,0 +1,70 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2017-2023 KiCad Developers, see AUTHORS.txt for contributors. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef WX_DATAVIEWCTRL_H_ +#define WX_DATAVIEWCTRL_H_ + +#include + +/** + * Extension of the wxDataViewCtrl to include some helper functions for working with items. + * These should probably be sent upstream, since they may be useful to others, but for now + * just extend the class with them ourselves. + */ +class WX_DATAVIEWCTRL : public wxDataViewCtrl +{ +public: + // Just take all constructors + using wxDataViewCtrl::wxDataViewCtrl; + + /** + * Get the previous item in list order. + * + * @param aItem a valid item in the control's model + * @return the item before aItem, or an invalid item if aItem is at the top. + */ + wxDataViewItem GetPrevItem( wxDataViewItem const& aItem ); + + /** + * Get the next item in list order. + * + * @param aItem a valid item in the control's model + * @return the item after aItem, or an invalid item if aItem is at the bottom. + */ + wxDataViewItem GetNextItem( wxDataViewItem const& aItem ); + + /** + * Get the previous sibling of an item. + * + * @param aItem a valid item in the control's model + * @return the sibling before aItem, or an invalid item if aItem has no siblings before it. + */ + wxDataViewItem GetPrevSibling( wxDataViewItem const& aItem ); + + /** + * Get the next sibling of an item. + * + * @param aItem a valid item in the control's model + * @return the sibling after aItem, or an invalid item if aItem has no siblings after it. + */ + wxDataViewItem GetNextSibling( wxDataViewItem const& aItem ); + +}; + +#endif // WX_DATAVIEWCTRL_H_ diff --git a/include/wxdataviewctrl_helpers.h b/include/wxdataviewctrl_helpers.h deleted file mode 100644 index ce7fc3dabf..0000000000 --- a/include/wxdataviewctrl_helpers.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * This program source code file is part of KiCad, a free EDA CAD application. - * - * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors. - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program. If not, see . - */ - -/** - * @file - * wxDataViewCtrl helper functions. These are functions that should be methods - * of wxDataViewCtrl, but aren't. - */ - -#ifndef WXDATAVIEWCTRL_HELPERS_H -#define WXDATAVIEWCTRL_HELPERS_H - -#include - -/** - * Get the previous item in list order. - * - * @param aView - a wxDataViewCtrl with valid model - * @param aItem - a valid item in the model - * @return the item before aItem, or an invalid item if aItem is at the top. - */ -wxDataViewItem GetPrevItem( wxDataViewCtrl const& aView, wxDataViewItem const& aItem ); - -/** - * Get the next item in list order. - * - * @param aView - a wxDataViewCtrl with valid model - * @param aItem - a valid item in the model - * @return the item after aItem, or an invalid item if aItem is at the bottom. - */ -wxDataViewItem GetNextItem( wxDataViewCtrl const& aView, wxDataViewItem const& aItem ); - -/** - * Get the previous sibling of an item. - * - * @param aView - awxDataViewCtrl with valid model - * @param aItem - a valid item in the model - * @return the sibling before aItem, or an invalid item if aItem has no siblings before it. - */ -wxDataViewItem GetPrevSibling( wxDataViewCtrl const& aView, wxDataViewItem const& aItem ); - -/** - * Get the next sibling of an item. - * - * @param aView - awxDataViewCtrl with valid model - * @param aItem - a valid item in the model - * @return the sibling after aItem, or an invalid item if aItem has no siblings after it. - */ -wxDataViewItem GetNextSibling( wxDataViewCtrl const& aView, wxDataViewItem const& aItem ); - -#endif // WXDATAVIEWCTRL_HELPERS_H