Missed update to private test repo version.
[dcpomatic.git] / hacks / data_view_model.cc
1 #include <wx/wx.h>
2 #include <wx/dataview.h>
3 #include <boost/bind.hpp>
4 #include <vector>
5
6 using std::cout;
7
8 class Model : public wxDataViewModel
9 {
10 public:
11         Model ()
12         {
13                 _content.push_back ("cock");
14                 _content.push_back ("piss");
15                 _content.push_back ("partridge");
16
17                 update ("");
18         }
19
20         unsigned int GetColumnCount () const {
21                 return 1;
22         }
23
24         wxString GetColumnType (unsigned int) const {
25                 return "string";
26         }
27
28         void GetValue (wxVariant& val, const wxDataViewItem& item, unsigned int column) const {
29                 val = wxVariant (_content[((size_t) item.GetID()) - 1]);
30         }
31
32         bool SetValue (const wxVariant &, const wxDataViewItem &, unsigned int) {
33                 return true;
34         }
35
36         wxDataViewItem GetParent(const wxDataViewItem& item) const {
37                 return wxDataViewItem (0);
38         }
39
40         bool IsContainer(const wxDataViewItem &) const {
41                 return false;
42         }
43
44         unsigned GetChildren(const wxDataViewItem& item, wxDataViewItemArray& children) const {
45                 if (item.IsOk ()) {
46                         return 0;
47                 }
48
49                 for (int i = 0; i < _data.GetCount(); ++i) {
50                         children.Add (_data[i]);
51                 }
52
53                 return _data.GetCount ();
54         }
55
56         void set_search (wxTextCtrl* search)
57         {
58                 Cleared ();
59                 update (search->GetValue ());
60         }
61
62         void update (wxString search)
63         {
64                 _data.Clear ();
65                 Cleared ();
66
67                 for (size_t i = 0; i < _content.size(); ++i) {
68                         if (search.IsEmpty() || _content[i].Find(search) != wxNOT_FOUND) {
69                                 _data.Add (wxDataViewItem ((void *) (i + 1)));
70                         }
71                 }
72
73                 ItemsAdded (wxDataViewItem (0), _data);
74         }
75
76 private:
77         std::vector<wxString> _content;
78         wxDataViewItemArray _data;
79 };
80
81 class App : public wxApp
82 {
83 public:
84         bool OnInit () {
85                 if (!wxApp::OnInit()) {
86                         return false;
87                 }
88
89                 wxFrame* frame = new wxFrame (0, wxID_ANY, "Test");
90
91                 wxDataViewCtrl* ctrl = new wxDataViewCtrl (frame, wxID_ANY, wxDefaultPosition, wxSize (300, 600), wxDV_NO_HEADER);
92                 wxDataViewTextRenderer* renderer = new wxDataViewTextRenderer ("string", wxDATAVIEW_CELL_INERT);
93                 wxDataViewColumn* column = new wxDataViewColumn ("string", renderer, 0, 100, wxAlignment(wxALIGN_LEFT));
94                 Model* model = new Model;
95                 ctrl->AssociateModel (model);
96                 ctrl->AppendColumn (column);
97                 ctrl->SetExpanderColumn (column);
98
99                 wxTextCtrl* search = new wxTextCtrl (frame, wxID_ANY);
100                 search->Bind (wxEVT_TEXT, boost::bind (&Model::set_search, model, search));
101
102                 wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
103                 sizer->Add (search);
104                 sizer->Add (ctrl);
105                 frame->SetSizerAndFit (sizer);
106                 frame->Show (true);
107                 return true;
108         }
109 };
110
111 IMPLEMENT_APP(App)