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