]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
3fb311897b8f631bacb7910ef2036473bc42fa29
[wxWidgets.git] / samples / dataview / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dataview.cpp
3 // Purpose: DataVewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 06/01/06
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #ifndef __WXMSW__
24 #include "mondrian.xpm"
25 #endif
26
27 #include "wx/dataview.h"
28
29 // -------------------------------------
30 // MyTextModel
31 // -------------------------------------
32
33 class MyTextModel: public wxDataViewListModel
34 {
35 public:
36 MyTextModel()
37 {
38 size_t i;
39 for (i = 0; i < 1000; i++)
40 m_list.Add( wxT("Test") );
41 for (i = 0; i < 500; i++)
42 { m_bools.Add( 0 ); m_bools.Add( 1 ); }
43 for (i = 0; i < 500; i++)
44 { m_colours.Add( wxT("red") ); m_colours.Add( wxT("green") ); }
45 }
46
47 virtual size_t GetNumberOfRows()
48 { return 1000; }
49 virtual size_t GetNumberOfCols()
50 { return 5; }
51
52 // as reported by wxVariant
53 virtual wxString GetColType( size_t col )
54 {
55 if (col == 3)
56 return wxT("bool");
57
58 return wxT("string");
59 }
60
61 virtual wxVariant GetValue( size_t col, size_t row )
62 {
63 if (col == 3)
64 {
65 return (bool) m_bools[row];
66 } else
67 if (col == 4)
68 {
69 return m_colours[row];
70 }
71 if (col == 2)
72 {
73 return m_list[row];
74 }
75 else
76 {
77 wxString tmp;
78 tmp.Printf( wxT("item(%d;%d)"), (int)row, (int)col );
79 return tmp;
80 }
81 }
82 virtual bool SetValue( wxVariant &value, size_t col, size_t row )
83 {
84 if (col == 3)
85 {
86 m_bools[row] = (int) value.GetBool();
87 } else
88 if (col == 2)
89 {
90 m_list[row] = value.GetString();
91 }
92 return true;
93 }
94
95 wxArrayString m_list;
96 wxArrayInt m_bools;
97 wxArrayString m_colours;
98 };
99
100 // -------------------------------------
101 // MyCustomCell
102 // -------------------------------------
103
104 class MyCustomCell: public wxDataViewCustomCell
105 {
106 public:
107 MyCustomCell() :
108 wxDataViewCustomCell( wxT("string"), wxDATAVIEW_CELL_INERT )
109 {
110 m_colour = wxT("black");
111 }
112 bool SetValue( const wxVariant &value )
113 {
114 m_colour = value.GetString();
115 return true;
116 }
117 bool Render( wxRect rect, wxDC *dc, int state )
118 {
119 dc->SetPen( *wxBLACK_PEN );
120 if (m_colour == wxT("red"))
121 dc->SetBrush( *wxRED_BRUSH );
122 else if (m_colour == wxT("green"))
123 dc->SetBrush( *wxGREEN_BRUSH );
124 else
125 dc->SetBrush( *wxBLACK_BRUSH );
126 dc->DrawRectangle( rect );
127 return true;
128 }
129 wxSize GetSize()
130 {
131 return wxSize(20,8);
132 }
133
134 private:
135 wxString m_colour;
136 };
137
138 // -------------------------------------
139 // MyApp
140 // -------------------------------------
141
142 class MyApp: public wxApp
143 {
144 public:
145 bool OnInit(void);
146 };
147
148 // -------------------------------------
149 // MyFrame
150 // -------------------------------------
151
152 class MyFrame: public wxFrame
153 {
154 public:
155 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
156
157 public:
158 void OnQuit(wxCommandEvent& event);
159 void OnAbout(wxCommandEvent& event);
160
161 private:
162 wxDataViewCtrl* dataview_left;
163 wxDataViewCtrl* dataview_right;
164 };
165
166 // -------------------------------------
167 // MyApp
168 // -------------------------------------
169
170 #define DYNAMIC_QUIT wxID_EXIT
171 #define DYNAMIC_ABOUT wxID_ABOUT
172
173 IMPLEMENT_APP (MyApp)
174
175 bool MyApp::OnInit(void)
176 {
177 MyFrame *frame = new MyFrame(NULL, _T("Dynamic wxWidgets App"), 50, 50, 600, 340);
178
179 frame->Show(true);
180
181 SetTopWindow(frame);
182
183 return true;
184 }
185
186 // -------------------------------------
187 // MyFrame
188 // -------------------------------------
189
190 MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
191 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
192 {
193 #ifdef __WXMSW__
194 SetIcon(wxIcon(_T("mondrian")));
195 #else
196 SetIcon(wxIcon(mondrian_xpm));
197 #endif
198
199 wxMenu *file_menu = new wxMenu;
200
201 file_menu->Append(DYNAMIC_ABOUT, _T("&About"));
202 file_menu->Append(DYNAMIC_QUIT, _T("E&xit"));
203 wxMenuBar *menu_bar = new wxMenuBar;
204 menu_bar->Append(file_menu, _T("&File"));
205 SetMenuBar(menu_bar);
206
207 // You used to have to do some casting for param 4, but now there are type-safe handlers
208 Connect( DYNAMIC_QUIT, wxID_ANY,
209 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnQuit) );
210 Connect( DYNAMIC_ABOUT, wxID_ANY,
211 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnAbout) );
212
213 CreateStatusBar();
214
215
216 // Left wxDataViewCtrl
217 dataview_left = new wxDataViewCtrl( this, -1 );
218
219 MyTextModel *model = new MyTextModel;
220 dataview_left->AssociateModel( model );
221
222 dataview_left->AppendTextColumn( wxT("first"), 0 );
223 dataview_left->AppendTextColumn( wxT("second"), 1 );
224
225 wxDataViewTextCell *text_cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE );
226 wxDataViewColumn *column = new wxDataViewColumn( wxT("editable"), text_cell, 2 );
227 dataview_left->AppendColumn( column );
228
229 dataview_left->AppendToggleColumn( wxT("fourth"), 3 );
230
231 MyCustomCell *custom_cell = new MyCustomCell;
232 column = new wxDataViewColumn( wxT("custom"), custom_cell, 4 );
233 dataview_left->AppendColumn( column );
234
235 // Right wxDataViewCtrl using the same model
236 dataview_right = new wxDataViewCtrl( this, -1 );
237 dataview_right->AssociateModel( model );
238
239 text_cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE );
240 column = new wxDataViewColumn( wxT("editable"), text_cell, 2 );
241 dataview_right->AppendColumn( column );
242 dataview_right->AppendTextColumn( wxT("first"), 0 );
243 dataview_right->AppendTextColumn( wxT("second"), 1 );
244 wxDataViewToggleCell *toggle_cell = new wxDataViewToggleCell( wxT("bool"), wxDATAVIEW_CELL_EDITABLE );
245 column = new wxDataViewColumn( wxT("bool"), toggle_cell, 3 );
246 dataview_right->AppendColumn( column );
247
248 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
249 sizer->Add( dataview_left, 1, wxGROW );
250 sizer->Add(10,10);
251 sizer->Add( dataview_right, 1, wxGROW );
252 SetSizer( sizer );
253 }
254
255 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
256 {
257 Close(true);
258 }
259
260 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
261 {
262 wxMessageDialog dialog(this, _T("This demonstrates the dataview control handling"),
263 _T("About DataView"), wxOK);
264
265 dialog.ShowModal();
266 }
267
268