summaryrefslogtreecommitdiff
path: root/hacks/data_view_model.cc
blob: 9dd2436917db752954d005e20f1130ede585aa42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <wx/wx.h>
#include <wx/dataview.h>
#include <boost/bind.hpp>
#include <vector>

using std::cout;

class Model : public wxDataViewModel
{
public:
	Model ()
	{
		_content.push_back ("cock");
		_content.push_back ("piss");
		_content.push_back ("partridge");

		update ("");
	}

	unsigned int GetColumnCount () const {
		return 1;
	}

	wxString GetColumnType (unsigned int) const {
		return "string";
	}

	void GetValue (wxVariant& val, const wxDataViewItem& item, unsigned int column) const {
		val = wxVariant (_content[((size_t) item.GetID()) - 1]);
	}

	bool SetValue (const wxVariant &, const wxDataViewItem &, unsigned int) {
		return true;
	}

        wxDataViewItem GetParent(const wxDataViewItem& item) const {
		return wxDataViewItem (0);
        }

        bool IsContainer(const wxDataViewItem &) const {
		return false;
        }

        unsigned GetChildren(const wxDataViewItem& item, wxDataViewItemArray& children) const {
		if (item.IsOk ()) {
			return 0;
		}

		for (int i = 0; i < _data.GetCount(); ++i) {
			children.Add (_data[i]);
		}

		return _data.GetCount ();
        }

	void set_search (wxTextCtrl* search)
	{
		Cleared ();
		update (search->GetValue ());
	}

	void update (wxString search)
	{
		_data.Clear ();
		Cleared ();

		for (size_t i = 0; i < _content.size(); ++i) {
			if (search.IsEmpty() || _content[i].Find(search) != wxNOT_FOUND) {
				_data.Add (wxDataViewItem ((void *) (i + 1)));
			}
		}

		ItemsAdded (wxDataViewItem (0), _data);
	}

private:
	std::vector<wxString> _content;
	wxDataViewItemArray _data;
};

class App : public wxApp
{
public:
        bool OnInit () {
		if (!wxApp::OnInit()) {
			return false;
		}

		wxFrame* frame = new wxFrame (0, wxID_ANY, "Test");

		wxDataViewCtrl* ctrl = new wxDataViewCtrl (frame, wxID_ANY, wxDefaultPosition, wxSize (300, 600), wxDV_NO_HEADER);
		wxDataViewTextRenderer* renderer = new wxDataViewTextRenderer ("string", wxDATAVIEW_CELL_INERT);
		wxDataViewColumn* column = new wxDataViewColumn ("string", renderer, 0, 100, wxAlignment(wxALIGN_LEFT));
		Model* model = new Model;
		ctrl->AssociateModel (model);
		ctrl->AppendColumn (column);
		ctrl->SetExpanderColumn (column);

		wxTextCtrl* search = new wxTextCtrl (frame, wxID_ANY);
		search->Bind (wxEVT_COMMAND_TEXT_UPDATED, boost::bind (&Model::set_search, model, search));

		wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
		sizer->Add (search);
		sizer->Add (ctrl);
		frame->SetSizerAndFit (sizer);
		frame->Show (true);
		return true;
        }
};

IMPLEMENT_APP(App)