]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
Added stubs for variaous tests.
[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 WX_DECLARE_LIST(wxDateTime,wxArrayDate);
34 #include <wx/listimpl.cpp>
35 WX_DEFINE_LIST(wxArrayDate);
36
37 class MyTextModel: public wxDataViewListModel
38 {
39 public:
40 MyTextModel()
41 {
42 size_t i;
43 for (i = 0; i < 1000; i++)
44 m_list.Add( wxT("Test") );
45 for (i = 0; i < 500; i++)
46 { m_bools.Add( 0 ); m_bools.Add( 1 ); }
47 for (i = 0; i < 500; i++)
48 { m_colours.Add( wxT("red") ); m_colours.Add( wxT("green") ); }
49 for (i = 0; i < 1000; i++)
50 { m_progress.Add( i/10 ); }
51 for (i = 0; i < 1000; i++)
52 {
53 wxDateTime *date = new wxDateTime( wxDateTime::Now() );
54 m_dates.Append( date );
55 }
56 }
57
58 virtual size_t GetNumberOfRows()
59 { return 1000; }
60 virtual size_t GetNumberOfCols()
61 { return 7; }
62
63 // as reported by wxVariant
64 virtual wxString GetColType( size_t col )
65 {
66 if (col == 6)
67 return wxT("datetime");
68
69 if (col == 5)
70 return wxT("long");
71
72 if (col == 3)
73 return wxT("bool");
74
75 return wxT("string");
76 }
77
78 virtual wxVariant GetValue( size_t col, size_t row )
79 {
80 if (col == 6)
81 {
82 return (wxDateTime) *m_dates[row];
83 } else
84 if (col == 5)
85 {
86 return (long) m_progress[row];
87 } else
88 if (col == 4)
89 {
90 return m_colours[row];
91 } else
92 if (col == 3)
93 {
94 return (bool) m_bools[row];
95 } else
96 if (col == 2)
97 {
98 return m_list[row];
99 }
100 else
101 {
102 wxString tmp;
103 tmp.Printf( wxT("item(%d;%d)"), (int)row, (int)col );
104 return tmp;
105 }
106 }
107 virtual bool SetValue( wxVariant &value, size_t col, size_t row )
108 {
109 if (col == 6)
110 {
111 *m_dates[row] = value.GetDateTime();
112 } else
113 if (col == 3)
114 {
115 m_bools[row] = (int) value.GetBool();
116 } else
117 if (col == 2)
118 {
119 m_list[row] = value.GetString();
120 }
121 return true;
122 }
123
124 wxArrayString m_list;
125 wxArrayInt m_bools;
126 wxArrayString m_colours;
127 wxArrayInt m_progress;
128 wxArrayDate m_dates;
129 };
130
131 // -------------------------------------
132 // MyCustomCell
133 // -------------------------------------
134
135 class MyCustomCell: public wxDataViewCustomCell
136 {
137 public:
138 MyCustomCell() :
139 wxDataViewCustomCell( wxT("string"), wxDATAVIEW_CELL_ACTIVATABLE )
140 {
141 m_colour = wxT("black");
142 }
143 bool SetValue( const wxVariant &value )
144 {
145 m_colour = value.GetString();
146 return true;
147 }
148 bool Render( wxRect rect, wxDC *dc, int state )
149 {
150 dc->SetPen( *wxBLACK_PEN );
151 if (m_colour == wxT("red"))
152 dc->SetBrush( *wxRED_BRUSH );
153 else if (m_colour == wxT("green"))
154 dc->SetBrush( *wxGREEN_BRUSH );
155 else
156 dc->SetBrush( *wxBLACK_BRUSH );
157 dc->DrawRectangle( rect );
158 return true;
159 }
160 wxSize GetSize()
161 {
162 return wxSize(20,8);
163 }
164 bool Activate( wxRect rect,
165 wxDataViewListModel *model, size_t col, size_t row )
166 {
167 return false;
168 }
169
170 private:
171 wxString m_colour;
172 };
173
174 // -------------------------------------
175 // MyUnsortedTextModel
176 // -------------------------------------
177
178 class MyUnsortedTextModel: public wxDataViewListModel
179 {
180 public:
181 MyUnsortedTextModel()
182 {
183 m_list.Add( wxT("This") );
184 m_list.Add( wxT("is") );
185 m_list.Add( wxT("an") );
186 m_list.Add( wxT("unsorted") );
187 m_list.Add( wxT("list") );
188 m_list.Add( wxT("of") );
189 m_list.Add( wxT("words.") );
190 }
191
192 virtual size_t GetNumberOfRows() { return m_list.GetCount(); }
193 virtual size_t GetNumberOfCols() { return 2; }
194 virtual wxString GetColType( size_t col ) { return wxT("string"); }
195 virtual wxVariant GetValue( size_t col, size_t row )
196 {
197 if (col == 0)
198 return m_list[row];
199 wxString tmp;
200 tmp.Printf( wxT("item(%d;%d)"), (int)row, (int)col );
201 return tmp;
202 }
203 virtual bool SetValue( wxVariant &variant, size_t col, size_t row )
204 {
205 if (col == 0)
206 {
207 m_list[row] = variant.GetString();
208 return true;
209 }
210 return false;
211
212 }
213
214 wxArrayString m_list;
215 };
216
217 // -------------------------------------
218 // MyApp
219 // -------------------------------------
220
221 class MyApp: public wxApp
222 {
223 public:
224 bool OnInit(void);
225 };
226
227 // -------------------------------------
228 // MyFrame
229 // -------------------------------------
230
231 class MyFrame: public wxFrame
232 {
233 public:
234 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
235
236 public:
237 void OnQuit(wxCommandEvent& event);
238 void OnAbout(wxCommandEvent& event);
239
240 private:
241 wxDataViewCtrl* dataview_left;
242 wxDataViewCtrl* dataview_right;
243 };
244
245 // -------------------------------------
246 // MySortingFrame
247 // -------------------------------------
248
249 enum my_events
250 {
251 ID_APPEND_ROW_LEFT = 1000,
252 ID_PREPEND_ROW_LEFT,
253 ID_INSERT_ROW_LEFT,
254 ID_DELETE_ROW_LEFT,
255 ID_EDIT_ROW_LEFT,
256
257 ID_APPEND_ROW_RIGHT,
258 ID_PREPEND_ROW_RIGHT,
259 ID_INSERT_ROW_RIGHT,
260 ID_DELETE_ROW_RIGHT,
261 ID_EDIT_ROW_RIGHT
262 };
263
264 class MySortingFrame: public wxFrame
265 {
266 public:
267 MySortingFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
268
269 public:
270 void OnQuit(wxCommandEvent& event);
271 void OnAbout(wxCommandEvent& event);
272
273 void OnAppendRowLeft(wxCommandEvent& event);
274 void OnPrependRowLeft(wxCommandEvent& event);
275 void OnInsertRowLeft(wxCommandEvent& event);
276 void OnDeleteRowLeft(wxCommandEvent& event);
277 void OnEditRowLeft(wxCommandEvent& event);
278
279 void OnAppendRowRight(wxCommandEvent& event);
280 void OnPrependRowRight(wxCommandEvent& event);
281 void OnInsertRowRight(wxCommandEvent& event);
282 void OnDeleteRowRight(wxCommandEvent& event);
283 void OnEditRowRight(wxCommandEvent& event);
284
285 private:
286 wxDataViewCtrl* dataview_left;
287 wxDataViewCtrl* dataview_right;
288
289 DECLARE_EVENT_TABLE()
290 };
291
292 // -------------------------------------
293 // MyApp
294 // -------------------------------------
295
296 #define DYNAMIC_QUIT wxID_EXIT
297 #define DYNAMIC_ABOUT wxID_ABOUT
298
299 IMPLEMENT_APP (MyApp)
300
301 bool MyApp::OnInit(void)
302 {
303 MyFrame *frame = new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
304 frame->Show(true);
305
306 MySortingFrame *frame2 = new MySortingFrame(NULL, wxT("wxDataViewCtrl sorting test"), 10, 350, 600, 300);
307 frame2->Show(true);
308
309 SetTopWindow(frame);
310
311 return true;
312 }
313
314 // -------------------------------------
315 // MyFrame
316 // -------------------------------------
317
318 MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
319 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
320 {
321 #ifdef __WXMSW__
322 SetIcon(wxIcon(_T("mondrian")));
323 #else
324 SetIcon(wxIcon(mondrian_xpm));
325 #endif
326
327 wxMenu *file_menu = new wxMenu;
328
329 file_menu->Append(DYNAMIC_ABOUT, _T("&About"));
330 file_menu->Append(DYNAMIC_QUIT, _T("E&xit"));
331 wxMenuBar *menu_bar = new wxMenuBar;
332 menu_bar->Append(file_menu, _T("&File"));
333 SetMenuBar(menu_bar);
334
335 // You used to have to do some casting for param 4, but now there are type-safe handlers
336 Connect( DYNAMIC_QUIT, wxID_ANY,
337 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnQuit) );
338 Connect( DYNAMIC_ABOUT, wxID_ANY,
339 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnAbout) );
340
341 CreateStatusBar();
342
343
344 // Left wxDataViewCtrl
345 dataview_left = new wxDataViewCtrl( this, -1 );
346
347 MyTextModel *model = new MyTextModel;
348 dataview_left->AssociateModel( model );
349
350 dataview_left->AppendTextColumn( wxT("first"), 0 );
351 dataview_left->AppendTextColumn( wxT("second"), 1 );
352
353 wxDataViewTextCell *text_cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE );
354 wxDataViewColumn *column = new wxDataViewColumn( wxT("editable"), text_cell, 2 );
355 dataview_left->AppendColumn( column );
356
357 dataview_left->AppendToggleColumn( wxT("fourth"), 3 );
358
359 MyCustomCell *custom_cell = new MyCustomCell;
360 column = new wxDataViewColumn( wxT("custom"), custom_cell, 4 );
361 dataview_left->AppendColumn( column );
362
363 dataview_left->AppendProgressColumn( wxT("progress"), 5 );
364
365 dataview_left->AppendDateColumn( wxT("date"), 6 );
366
367 // Right wxDataViewCtrl using the same model
368 dataview_right = new wxDataViewCtrl( this, -1 );
369 dataview_right->AssociateModel( model );
370
371 text_cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE );
372 column = new wxDataViewColumn( wxT("editable"), text_cell, 2 );
373 dataview_right->AppendColumn( column );
374 dataview_right->AppendTextColumn( wxT("first"), 0 );
375 dataview_right->AppendTextColumn( wxT("second"), 1 );
376 wxDataViewToggleCell *toggle_cell = new wxDataViewToggleCell( wxT("bool"), wxDATAVIEW_CELL_EDITABLE );
377 column = new wxDataViewColumn( wxT("bool"), toggle_cell, 3 );
378 dataview_right->AppendColumn( column );
379
380 dataview_right->AppendDateColumn( wxT("date"), 6 );
381
382 // layout dataview controls.
383
384 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
385 sizer->Add( dataview_left, 3, wxGROW );
386 sizer->Add(10,10);
387 sizer->Add( dataview_right, 2, wxGROW );
388 SetSizer( sizer );
389 }
390
391 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
392 {
393 Close(true);
394 }
395
396 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
397 {
398 wxMessageDialog dialog(this, _T("This demonstrates the dataview control handling"),
399 _T("About DataView"), wxOK);
400
401 dialog.ShowModal();
402 }
403
404 // -------------------------------------
405 // MySortingFrame
406 // -------------------------------------
407
408 BEGIN_EVENT_TABLE(MySortingFrame,wxFrame)
409 EVT_BUTTON( ID_APPEND_ROW_LEFT, MySortingFrame::OnAppendRowLeft )
410 END_EVENT_TABLE()
411
412 MySortingFrame::MySortingFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
413 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
414 {
415 #ifdef __WXMSW__
416 SetIcon(wxIcon(_T("mondrian")));
417 #else
418 SetIcon(wxIcon(mondrian_xpm));
419 #endif
420
421 wxMenu *file_menu = new wxMenu;
422
423 file_menu->Append(DYNAMIC_ABOUT, _T("&About"));
424 file_menu->Append(DYNAMIC_QUIT, _T("E&xit"));
425 wxMenuBar *menu_bar = new wxMenuBar;
426 menu_bar->Append(file_menu, _T("&File"));
427 SetMenuBar(menu_bar);
428
429 // You used to have to do some casting for param 4, but now there are type-safe handlers
430 Connect( DYNAMIC_QUIT, wxID_ANY,
431 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MySortingFrame::OnQuit) );
432 Connect( DYNAMIC_ABOUT, wxID_ANY,
433 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MySortingFrame::OnAbout) );
434
435 CreateStatusBar();
436
437
438 // Left wxDataViewCtrl
439 dataview_left = new wxDataViewCtrl( this, -1 );
440
441 MyUnsortedTextModel *model = new MyUnsortedTextModel;
442 dataview_left->AssociateModel( model );
443 wxDataViewTextCell *text_cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE );
444 wxDataViewColumn *column = new wxDataViewColumn( wxT("editable"), text_cell, 0 );
445 dataview_left->AppendColumn( column );
446 dataview_left->AppendTextColumn( wxT("second"), 1 );
447
448 // Right wxDataViewCtrl using the sorting model
449 dataview_right = new wxDataViewCtrl( this, -1 );
450 wxDataViewSortedListModel *sorted_model =
451 new wxDataViewSortedListModel( model );
452 dataview_right->AssociateModel( sorted_model );
453 text_cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE );
454 column = new wxDataViewColumn( wxT("editable"), text_cell, 0 );
455 dataview_right->AppendColumn( column );
456 dataview_right->AppendTextColumn( wxT("second"), 1 );
457
458 // layout dataview controls.
459
460 wxBoxSizer *top_sizer = new wxBoxSizer( wxHORIZONTAL );
461 top_sizer->Add( dataview_left, 1, wxGROW );
462 top_sizer->Add(10,10);
463 top_sizer->Add( dataview_right, 1, wxGROW );
464
465 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
466 button_sizer->Add( 10, 10, 1 );
467 wxFlexGridSizer *left_sizer = new wxFlexGridSizer( 2 );
468 left_sizer->Add( new wxButton( this, ID_APPEND_ROW_LEFT, wxT("Append") ), 0, wxALL, 5 );
469 left_sizer->Add( new wxButton( this, ID_PREPEND_ROW_LEFT, wxT("Prepend") ), 0, wxALL, 5 );
470 left_sizer->Add( new wxButton( this, ID_INSERT_ROW_LEFT, wxT("Insert") ), 0, wxALL, 5 );
471 left_sizer->Add( new wxButton( this, ID_DELETE_ROW_LEFT, wxT("Delete") ), 0, wxALL, 5 );
472 left_sizer->Add( new wxButton( this, ID_EDIT_ROW_LEFT, wxT("Edit") ), 0, wxALL, 5 );
473 button_sizer->Add( left_sizer );
474 button_sizer->Add( 10, 10, 2 );
475 wxFlexGridSizer *right_sizer = new wxFlexGridSizer( 2 );
476 right_sizer->Add( new wxButton( this, ID_APPEND_ROW_RIGHT, wxT("Append") ), 0, wxALL, 5 );
477 right_sizer->Add( new wxButton( this, ID_PREPEND_ROW_RIGHT, wxT("Prepend") ), 0, wxALL, 5 );
478 right_sizer->Add( new wxButton( this, ID_INSERT_ROW_RIGHT, wxT("Insert") ), 0, wxALL, 5 );
479 right_sizer->Add( new wxButton( this, ID_DELETE_ROW_RIGHT, wxT("Delete") ), 0, wxALL, 5 );
480 right_sizer->Add( new wxButton( this, ID_EDIT_ROW_RIGHT, wxT("Edit") ), 0, wxALL, 5 );
481 button_sizer->Add( right_sizer );
482 button_sizer->Add( 10, 10, 1 );
483
484 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
485 main_sizer->Add( top_sizer, 1, wxGROW );
486 main_sizer->Add( button_sizer, 0, wxGROW );
487
488 SetSizer( main_sizer );
489 }
490
491 void MySortingFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
492 {
493 Close(true);
494 }
495
496 void MySortingFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
497 {
498 wxMessageDialog dialog(this, _T("This demonstrates the dataview control sorting"),
499 _T("About DataView"), wxOK);
500
501 dialog.ShowModal();
502 }
503
504 void MySortingFrame::OnAppendRowLeft(wxCommandEvent& event)
505 {
506 }
507
508 void MySortingFrame::OnPrependRowLeft(wxCommandEvent& event)
509 {
510 }
511
512 void MySortingFrame::OnInsertRowLeft(wxCommandEvent& event)
513 {
514 }
515
516 void MySortingFrame::OnDeleteRowLeft(wxCommandEvent& event)
517 {
518 }
519
520 void MySortingFrame::OnEditRowLeft(wxCommandEvent& event)
521 {
522 }
523
524 void MySortingFrame::OnAppendRowRight(wxCommandEvent& event)
525 {
526 }
527
528 void MySortingFrame::OnPrependRowRight(wxCommandEvent& event)
529 {
530 }
531
532 void MySortingFrame::OnInsertRowRight(wxCommandEvent& event)
533 {
534 }
535
536 void MySortingFrame::OnDeleteRowRight(wxCommandEvent& event)
537 {
538 }
539
540 void MySortingFrame::OnEditRowRight(wxCommandEvent& event)
541 {
542 }
543