From f99b80612af934f090d358ce00f7c342557d76c4 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 12 Sep 2019 18:51:36 +0200 Subject: [PATCH] Add a simple test of wxDataViewIndexListModel to the sample This is useful to investigate inconsistencies in its behaviour between the generic and the native GTK versions that can't be easily checked in the automatic unit tests. --- samples/dataview/dataview.cpp | 73 +++++++++++++++++++++++++++++++++-- samples/dataview/mymodels.h | 35 +++++++++++++++++ 2 files changed, 105 insertions(+), 3 deletions(-) diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index 8c5065aed8..cb6023bd58 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -118,6 +118,9 @@ private: void OnAddTreeItem(wxCommandEvent& event); void OnAddTreeContainerItem(wxCommandEvent& event); + void OnIndexListUseEnglish(wxCommandEvent&) { FillIndexList(Lang_English); } + void OnIndexListUseFrench(wxCommandEvent&) { FillIndexList(Lang_French); } + void OnValueChanged( wxDataViewEvent &event ); void OnActivated( wxDataViewEvent &event ); @@ -160,6 +163,11 @@ private: // helper used by both OnDeleteSelected() and OnDataViewChar() void DeleteSelectedItems(); + // helper for the index list model fills the model with the weekday names + // in the specified language + enum Lang { Lang_English, Lang_French }; + void FillIndexList(Lang lang); + wxNotebook* m_notebook; @@ -171,16 +179,18 @@ private: Page_ListStore, Page_TreeStore, Page_VarHeight, + Page_IndexList, Page_Max }; wxDataViewCtrl* m_ctrl[Page_Max]; - // the models associated with the first two DVC: + // Some of the models associated with the controls: wxObjectDataPtr m_music_model; wxObjectDataPtr m_long_music_model; wxObjectDataPtr m_list_model; + wxObjectDataPtr m_index_list_model; // other data: @@ -427,7 +437,11 @@ enum ID_DELETE_TREE_ITEM = 400, ID_DELETE_ALL_TREE_ITEMS = 401, ID_ADD_TREE_ITEM = 402, - ID_ADD_TREE_CONTAINER_ITEM = 403 + ID_ADD_TREE_CONTAINER_ITEM = 403, + + // Index list model page + ID_INDEX_LIST_USE_ENGLISH = 500, + ID_INDEX_LIST_USE_FRENCH }; wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) @@ -474,6 +488,9 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_BUTTON( ID_ADD_TREE_ITEM, MyFrame::OnAddTreeItem ) EVT_BUTTON( ID_ADD_TREE_CONTAINER_ITEM, MyFrame::OnAddTreeContainerItem ) + EVT_BUTTON( ID_INDEX_LIST_USE_ENGLISH, MyFrame::OnIndexListUseEnglish ) + EVT_BUTTON( ID_INDEX_LIST_USE_FRENCH, MyFrame::OnIndexListUseFrench ) + EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged ) EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated ) @@ -672,6 +689,24 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int fifthPanelSz->Add(m_ctrl[Page_VarHeight], 1, wxGROW | wxALL, 5); fifthPanel->SetSizerAndFit(fifthPanelSz); + // page showing the indexed list model + // ----------------------------------- + + wxPanel* sixthPanel = new wxPanel(m_notebook, wxID_ANY); + + BuildDataViewCtrl(sixthPanel, Page_IndexList); + + wxBoxSizer *button_sizer6 = new wxBoxSizer(wxHORIZONTAL); + button_sizer6->Add(new wxButton(sixthPanel, ID_INDEX_LIST_USE_ENGLISH, "&English"), + wxSizerFlags().DoubleBorder()); + button_sizer6->Add(new wxButton(sixthPanel, ID_INDEX_LIST_USE_FRENCH, "&French"), + wxSizerFlags().DoubleBorder()); + + wxSizer *sixthPanelSz = new wxBoxSizer(wxVERTICAL); + sixthPanelSz->Add(m_ctrl[Page_IndexList], wxSizerFlags(1).Expand().Border()); + sixthPanelSz->Add(button_sizer6); + sixthPanel->SetSizerAndFit(sixthPanelSz); + // complete GUI // ------------ @@ -680,7 +715,8 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int m_notebook->AddPage(secondPanel, "MyListModel"); m_notebook->AddPage(thirdPanel, "wxDataViewListCtrl"); m_notebook->AddPage(fourthPanel, "wxDataViewTreeCtrl"); - m_notebook->AddPage(fifthPanel, "wxDataViewTreeCtrl Variable line height"); + m_notebook->AddPage(fifthPanel, "Variable line height"); + m_notebook->AddPage(sixthPanel, "MyIndexListModel"); wxSizer* mainSizer = new wxBoxSizer(wxVERTICAL); @@ -929,6 +965,21 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l m_ctrl[Page_VarHeight]->AppendColumn(column1); } break; + + case Page_IndexList: + { + m_ctrl[Page_IndexList] = new wxDataViewCtrl(parent, wxID_ANY, + wxDefaultPosition, + wxDefaultSize, + style); + + m_index_list_model = new MyIndexListModel; + m_ctrl[Page_IndexList]->AssociateModel(m_index_list_model.get()); + m_ctrl[Page_IndexList]->AppendTextColumn("String", 0); + + FillIndexList(Lang_English); + } + break; } } @@ -1616,3 +1667,19 @@ void MyFrame::OnSortByFirstColumn(wxCommandEvent& event) else col->UnsetAsSortKey(); } + +// ---------------------------------------------------------------------------- +// Index list model page +// ---------------------------------------------------------------------------- + +void MyFrame::FillIndexList(Lang lang) +{ + const int DAYS_PER_WEEK = 7; + const wxString weekdays[2][DAYS_PER_WEEK] = + { + { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }, + { "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam", "Dim" }, + }; + + m_index_list_model->Fill(wxArrayString(DAYS_PER_WEEK, weekdays[lang])); +} diff --git a/samples/dataview/mymodels.h b/samples/dataview/mymodels.h index db089197a9..3a7f5735da 100644 --- a/samples/dataview/mymodels.h +++ b/samples/dataview/mymodels.h @@ -269,3 +269,38 @@ class MyListStoreDerivedModel : public wxDataViewListStore public: virtual bool IsEnabledByRow(unsigned int row, unsigned int col) const wxOVERRIDE; }; + +// ---------------------------------------------------------------------------- +// MyIndexListModel +// ---------------------------------------------------------------------------- + +class MyIndexListModel : public wxDataViewIndexListModel +{ +public: + MyIndexListModel() { } + + void Fill(const wxArrayString& strings) + { + m_strings = strings; + + Reset(m_strings.size()); + } + + // Implement base class pure virtual methods. + unsigned GetColumnCount() const wxOVERRIDE { return 1; } + wxString GetColumnType(unsigned) const wxOVERRIDE { return "string"; } + unsigned GetCount() const wxOVERRIDE { return m_strings.size(); } + void GetValueByRow(wxVariant& val, unsigned row, unsigned) const wxOVERRIDE + { + val = m_strings[row]; + } + bool SetValueByRow(const wxVariant&, unsigned, unsigned) wxOVERRIDE + { + return false; + } + +private: + wxArrayString m_strings; + + wxDECLARE_NO_COPY_CLASS(MyIndexListModel); +};