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