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