1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: DataVewCtrl wxWidgets sample
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
23 #include "wx/datetime.h"
26 #include "../sample.xpm"
29 #include "wx/dataview.h"
31 // -------------------------------------
33 // -------------------------------------
35 WX_DECLARE_LIST(wxDateTime
,wxArrayDate
);
36 #include <wx/listimpl.cpp>
37 WX_DEFINE_LIST(wxArrayDate
)
39 class MyTextModel
: public wxDataViewListModel
45 for (i
= 0; i
< 1000; i
++)
46 m_list
.Add( wxT("Test") );
47 for (i
= 0; i
< 500; i
++)
48 { m_bools
.Add( 0 ); m_bools
.Add( 1 ); }
49 for (i
= 0; i
< 500; i
++)
50 { m_colours
.Add( wxT("red") ); m_colours
.Add( wxT("green") ); }
51 for (i
= 0; i
< 1000; i
++)
52 { m_progress
.Add( i
/10 ); }
53 for (i
= 0; i
< 1000; i
++)
55 wxDateTime
*date
= new wxDateTime( wxDateTime::Now() );
56 m_dates
.Append( date
);
60 virtual size_t GetNumberOfRows()
62 virtual size_t GetNumberOfCols()
65 // as reported by wxVariant
66 virtual wxString
GetColType( size_t col
)
69 return wxT("datetime");
80 virtual void GetValue( wxVariant
&variant
, size_t col
, size_t row
)
84 variant
= (wxDateTime
) *m_dates
[row
];
88 variant
= (long) m_progress
[row
];
92 variant
= m_colours
[row
];
96 variant
= (bool) m_bools
[row
];
100 variant
= m_list
[row
];
105 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
109 virtual bool SetValue( wxVariant
&value
, size_t col
, size_t row
)
113 *m_dates
[row
] = value
.GetDateTime();
117 m_bools
[row
] = (int) value
.GetBool();
121 m_list
[row
] = value
.GetString();
126 wxArrayString m_list
;
128 wxArrayString m_colours
;
129 wxArrayInt m_progress
;
133 // -------------------------------------
135 // -------------------------------------
137 class MyCustomCell
: public wxDataViewCustomCell
141 wxDataViewCustomCell( wxT("string"), wxDATAVIEW_CELL_ACTIVATABLE
)
143 m_colour
= wxT("black");
145 bool SetValue( const wxVariant
&value
)
147 m_colour
= value
.GetString();
150 bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
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
);
158 dc
->SetBrush( *wxBLACK_BRUSH
);
159 dc
->DrawRectangle( rect
);
166 bool Activate( wxRect
WXUNUSED(rect
),
167 wxDataViewListModel
*WXUNUSED(model
),
168 size_t WXUNUSED(col
),
169 size_t WXUNUSED(row
) )
178 // -------------------------------------
179 // MyUnsortedTextModel
180 // -------------------------------------
182 class MyUnsortedTextModel
: public wxDataViewListModel
185 MyUnsortedTextModel()
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.") );
196 virtual size_t GetNumberOfRows() { return m_list
.GetCount(); }
197 virtual size_t GetNumberOfCols() { return 2; }
198 virtual wxString
GetColType( size_t WXUNUSED(col
) ) { return wxT("string"); }
199 virtual void GetValue( wxVariant
&variant
, size_t col
, size_t row
)
203 variant
= m_list
[row
];
207 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
210 virtual bool SetValue( wxVariant
&variant
, size_t col
, size_t row
)
214 m_list
[row
] = variant
.GetString();
221 void AppendRow( const wxString
&text
)
227 void PrependRow( const wxString
&text
)
229 m_list
.Insert( text
, 0 );
233 void InsertRowAt1( const wxString
&text
)
235 m_list
.Insert( text
, 1 );
239 void DeleteRow( size_t index
)
241 m_list
.RemoveAt( index
);
245 wxArrayString m_list
;
248 // -------------------------------------
250 // -------------------------------------
252 class MyApp
: public wxApp
258 // -------------------------------------
260 // -------------------------------------
262 class MyFrame
: public wxFrame
265 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
268 void OnQuit(wxCommandEvent
& event
);
269 void OnAbout(wxCommandEvent
& event
);
272 wxDataViewCtrl
* dataview_left
;
273 wxDataViewCtrl
* dataview_right
;
276 // -------------------------------------
278 // -------------------------------------
282 ID_APPEND_ROW_LEFT
= 1000,
289 ID_PREPEND_ROW_RIGHT
,
295 class MySortingFrame
: public wxFrame
298 MySortingFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
301 void OnQuit(wxCommandEvent
& event
);
302 void OnAbout(wxCommandEvent
& event
);
304 void OnAppendRowLeft(wxCommandEvent
& event
);
305 void OnPrependRowLeft(wxCommandEvent
& event
);
306 void OnInsertRowLeft(wxCommandEvent
& event
);
307 void OnDeleteRowLeft(wxCommandEvent
& event
);
308 void OnEditRowLeft(wxCommandEvent
& event
);
310 void OnAppendRowRight(wxCommandEvent
& event
);
311 void OnPrependRowRight(wxCommandEvent
& event
);
312 void OnInsertRowRight(wxCommandEvent
& event
);
313 void OnDeleteRowRight(wxCommandEvent
& event
);
314 void OnEditRowRight(wxCommandEvent
& event
);
317 wxDataViewCtrl
* dataview_left
;
318 wxDataViewCtrl
* dataview_right
;
320 MyUnsortedTextModel
*m_unsorted_model
;
322 DECLARE_EVENT_TABLE()
325 // -------------------------------------
327 // -------------------------------------
329 #define DYNAMIC_QUIT wxID_EXIT
330 #define DYNAMIC_ABOUT wxID_ABOUT
332 IMPLEMENT_APP (MyApp
)
334 bool MyApp::OnInit(void)
336 MyFrame
*frame
= new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
339 MySortingFrame
*frame2
= new MySortingFrame(NULL
, wxT("wxDataViewCtrl sorting test"), 10, 350, 600, 300);
347 // -------------------------------------
349 // -------------------------------------
351 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
352 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
354 SetIcon(wxICON(sample
));
356 wxMenu
*file_menu
= new wxMenu
;
358 file_menu
->Append(DYNAMIC_ABOUT
, _T("&About"));
359 file_menu
->Append(DYNAMIC_QUIT
, _T("E&xit"));
360 wxMenuBar
*menu_bar
= new wxMenuBar
;
361 menu_bar
->Append(file_menu
, _T("&File"));
362 SetMenuBar(menu_bar
);
364 // You used to have to do some casting for param 4, but now there are type-safe handlers
365 Connect( DYNAMIC_QUIT
, wxID_ANY
,
366 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MyFrame::OnQuit
) );
367 Connect( DYNAMIC_ABOUT
, wxID_ANY
,
368 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MyFrame::OnAbout
) );
372 wxPanel
*panel
= new wxPanel( this, wxID_ANY
);
375 // Left wxDataViewCtrl
376 dataview_left
= new wxDataViewCtrl( panel
, wxID_ANY
);
378 MyTextModel
*model
= new MyTextModel
;
379 dataview_left
->AssociateModel( model
);
381 dataview_left
->AppendTextColumn( wxT("first"), 0 );
382 dataview_left
->AppendTextColumn( wxT("second"), 1 );
384 wxDataViewTextCell
*text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
385 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_cell
, 2 );
386 dataview_left
->AppendColumn( column
);
388 dataview_left
->AppendToggleColumn( wxT("fourth"), 3 );
390 MyCustomCell
*custom_cell
= new MyCustomCell
;
391 column
= new wxDataViewColumn( wxT("custom"), custom_cell
, 4 );
392 dataview_left
->AppendColumn( column
);
394 dataview_left
->AppendProgressColumn( wxT("progress"), 5 );
396 dataview_left
->AppendDateColumn( wxT("date"), 6 );
398 // Right wxDataViewCtrl using the same model
399 dataview_right
= new wxDataViewCtrl( panel
, wxID_ANY
);
400 dataview_right
->AssociateModel( model
);
402 text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
403 column
= new wxDataViewColumn( wxT("editable"), text_cell
, 2 );
404 dataview_right
->AppendColumn( column
);
405 dataview_right
->AppendTextColumn( wxT("first"), 0 );
406 dataview_right
->AppendTextColumn( wxT("second"), 1 );
407 wxDataViewToggleCell
*toggle_cell
= new wxDataViewToggleCell( wxT("bool"), wxDATAVIEW_CELL_ACTIVATABLE
);
408 column
= new wxDataViewColumn( wxT("bool"), toggle_cell
, 3, 30 );
409 dataview_right
->AppendColumn( column
);
411 dataview_right
->AppendDateColumn( wxT("date"), 6 );
413 // layout dataview controls.
415 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
416 sizer
->Add( dataview_left
, 3, wxGROW
);
418 sizer
->Add( dataview_right
, 2, wxGROW
);
419 panel
->SetSizer( sizer
);
422 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
427 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
429 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control handling"),
430 _T("About DataView"), wxOK
);
435 // -------------------------------------
437 // -------------------------------------
439 BEGIN_EVENT_TABLE(MySortingFrame
,wxFrame
)
440 EVT_BUTTON( ID_APPEND_ROW_LEFT
, MySortingFrame::OnAppendRowLeft
)
441 EVT_BUTTON( ID_PREPEND_ROW_LEFT
, MySortingFrame::OnPrependRowLeft
)
442 EVT_BUTTON( ID_INSERT_ROW_LEFT
, MySortingFrame::OnInsertRowLeft
)
443 EVT_BUTTON( ID_DELETE_ROW_LEFT
, MySortingFrame::OnDeleteRowLeft
)
446 MySortingFrame::MySortingFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
447 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
449 SetIcon(wxICON(sample
));
451 wxMenu
*file_menu
= new wxMenu
;
453 file_menu
->Append(DYNAMIC_ABOUT
, _T("&About"));
454 file_menu
->Append(DYNAMIC_QUIT
, _T("E&xit"));
455 wxMenuBar
*menu_bar
= new wxMenuBar
;
456 menu_bar
->Append(file_menu
, _T("&File"));
457 SetMenuBar(menu_bar
);
459 // You used to have to do some casting for param 4, but now there are type-safe handlers
460 Connect( DYNAMIC_QUIT
, wxID_ANY
,
461 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MySortingFrame::OnQuit
) );
462 Connect( DYNAMIC_ABOUT
, wxID_ANY
,
463 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MySortingFrame::OnAbout
) );
468 // Left wxDataViewCtrl
469 dataview_left
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxDV_MULTIPLE
);
471 m_unsorted_model
= new MyUnsortedTextModel
;
472 dataview_left
->AssociateModel( m_unsorted_model
);
473 wxDataViewTextCell
*text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
474 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_cell
, 0 );
475 dataview_left
->AppendColumn( column
);
476 dataview_left
->AppendTextColumn( wxT("second"), 1 );
478 // Right wxDataViewCtrl using the sorting model
479 dataview_right
= new wxDataViewCtrl( this, wxID_ANY
);
480 wxDataViewSortedListModel
*sorted_model
=
481 new wxDataViewSortedListModel( m_unsorted_model
);
482 dataview_right
->AssociateModel( sorted_model
);
483 text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
484 column
= new wxDataViewColumn( wxT("editable"), text_cell
, 0 );
485 dataview_right
->AppendColumn( column
);
486 dataview_right
->AppendTextColumn( wxT("second"), 1 );
488 // layout dataview controls.
490 wxBoxSizer
*top_sizer
= new wxBoxSizer( wxHORIZONTAL
);
491 top_sizer
->Add( dataview_left
, 1, wxGROW
);
492 top_sizer
->Add(10,10);
493 top_sizer
->Add( dataview_right
, 1, wxGROW
);
495 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
496 button_sizer
->Add( 10, 10, 1 );
497 wxFlexGridSizer
*left_sizer
= new wxFlexGridSizer( 2 );
498 left_sizer
->Add( new wxButton( this, ID_APPEND_ROW_LEFT
, wxT("Append") ), 0, wxALL
, 5 );
499 left_sizer
->Add( new wxButton( this, ID_PREPEND_ROW_LEFT
, wxT("Prepend") ), 0, wxALL
, 5 );
500 left_sizer
->Add( new wxButton( this, ID_INSERT_ROW_LEFT
, wxT("Insert") ), 0, wxALL
, 5 );
501 left_sizer
->Add( new wxButton( this, ID_DELETE_ROW_LEFT
, wxT("Delete") ), 0, wxALL
, 5 );
502 left_sizer
->Add( new wxButton( this, ID_EDIT_ROW_LEFT
, wxT("Edit") ), 0, wxALL
, 5 );
503 button_sizer
->Add( left_sizer
);
504 button_sizer
->Add( 10, 10, 2 );
505 wxFlexGridSizer
*right_sizer
= new wxFlexGridSizer( 2 );
506 right_sizer
->Add( new wxButton( this, ID_APPEND_ROW_RIGHT
, wxT("Append") ), 0, wxALL
, 5 );
507 right_sizer
->Add( new wxButton( this, ID_PREPEND_ROW_RIGHT
, wxT("Prepend") ), 0, wxALL
, 5 );
508 right_sizer
->Add( new wxButton( this, ID_INSERT_ROW_RIGHT
, wxT("Insert") ), 0, wxALL
, 5 );
509 right_sizer
->Add( new wxButton( this, ID_DELETE_ROW_RIGHT
, wxT("Delete") ), 0, wxALL
, 5 );
510 right_sizer
->Add( new wxButton( this, ID_EDIT_ROW_RIGHT
, wxT("Edit") ), 0, wxALL
, 5 );
511 button_sizer
->Add( right_sizer
);
512 button_sizer
->Add( 10, 10, 1 );
514 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
515 main_sizer
->Add( top_sizer
, 1, wxGROW
);
516 main_sizer
->Add( button_sizer
, 0, wxGROW
);
518 SetSizer( main_sizer
);
521 void MySortingFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
526 void MySortingFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
528 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control sorting"),
529 _T("About DataView"), wxOK
);
534 void MySortingFrame::OnAppendRowLeft(wxCommandEvent
& WXUNUSED(event
))
536 wxTextEntryDialog
dialog( this, wxT("Enter text to append") );
537 if (dialog
.ShowModal() == wxID_OK
)
539 wxString value
= dialog
.GetValue();
541 m_unsorted_model
->AppendRow( value
);
545 void MySortingFrame::OnPrependRowLeft(wxCommandEvent
& WXUNUSED(event
))
547 wxTextEntryDialog
dialog( this, wxT("Enter text to prepend") );
548 if (dialog
.ShowModal() == wxID_OK
)
550 wxString value
= dialog
.GetValue();
552 m_unsorted_model
->PrependRow( value
);
556 void MySortingFrame::OnInsertRowLeft(wxCommandEvent
& WXUNUSED(event
))
558 wxTextEntryDialog
dialog( this, wxT("Enter text to insert at #2") );
559 if (dialog
.ShowModal() == wxID_OK
)
561 wxString value
= dialog
.GetValue();
563 m_unsorted_model
->InsertRowAt1( value
);
567 void MySortingFrame::OnDeleteRowLeft(wxCommandEvent
& WXUNUSED(event
))
569 m_unsorted_model
->DeleteRow( 1 );
572 void MySortingFrame::OnEditRowLeft(wxCommandEvent
& WXUNUSED(event
))
576 void MySortingFrame::OnAppendRowRight(wxCommandEvent
& WXUNUSED(event
))
580 void MySortingFrame::OnPrependRowRight(wxCommandEvent
& WXUNUSED(event
))
584 void MySortingFrame::OnInsertRowRight(wxCommandEvent
& WXUNUSED(event
))
588 void MySortingFrame::OnDeleteRowRight(wxCommandEvent
& WXUNUSED(event
))
592 void MySortingFrame::OnEditRowRight(wxCommandEvent
& WXUNUSED(event
))