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"
31 #include "wx/dataview.h"
33 // -------------------------------------
35 // -------------------------------------
37 WX_DECLARE_LIST(wxDateTime
,wxArrayDate
);
38 #include <wx/listimpl.cpp>
39 WX_DEFINE_LIST(wxArrayDate
)
41 class MyTextModel
: public wxDataViewListModel
47 for (i
= 0; i
< 1000; i
++)
48 m_list
.Add( wxT("Test") );
49 for (i
= 0; i
< 500; i
++)
50 { m_bools
.Add( 0 ); m_bools
.Add( 1 ); }
51 for (i
= 0; i
< 500; i
++)
52 { m_colours
.Add( wxT("red") ); m_colours
.Add( wxT("green") ); }
53 for (i
= 0; i
< 1000; i
++)
54 { m_progress
.Add( i
/10 ); }
55 for (i
= 0; i
< 1000; i
++)
57 wxDateTime
*date
= new wxDateTime( wxDateTime::Now() );
58 m_dates
.Append( date
);
62 virtual unsigned int GetNumberOfRows()
64 virtual unsigned int GetNumberOfCols()
67 // as reported by wxVariant
68 virtual wxString
GetColType( unsigned int col
)
71 return wxT("datetime");
82 virtual void GetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
)
86 variant
= (wxDateTime
) *m_dates
[row
];
90 variant
= (long) m_progress
[row
];
94 variant
= m_colours
[row
];
98 variant
= (bool) m_bools
[row
];
102 variant
= m_list
[row
];
107 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
111 virtual bool SetValue( wxVariant
&value
, unsigned int col
, unsigned int row
)
115 *m_dates
[row
] = value
.GetDateTime();
119 m_bools
[row
] = (int) value
.GetBool();
123 m_list
[row
] = value
.GetString();
128 wxArrayString m_list
;
130 wxArrayString m_colours
;
131 wxArrayInt m_progress
;
135 // -------------------------------------
137 // -------------------------------------
139 class MyCustomCell
: public wxDataViewCustomCell
143 wxDataViewCustomCell( wxT("string"), wxDATAVIEW_CELL_ACTIVATABLE
)
145 m_colour
= wxT("black");
147 bool SetValue( const wxVariant
&value
)
149 m_colour
= value
.GetString();
152 bool Render( wxRect rect
, wxDC
*dc
, int WXUNUSED(state
) )
154 dc
->SetPen( *wxBLACK_PEN
);
155 if (m_colour
== wxT("red"))
156 dc
->SetBrush( *wxRED_BRUSH
);
157 else if (m_colour
== wxT("green"))
158 dc
->SetBrush( *wxGREEN_BRUSH
);
160 dc
->SetBrush( *wxBLACK_BRUSH
);
161 dc
->DrawRectangle( rect
);
168 bool Activate( wxRect
WXUNUSED(rect
),
169 wxDataViewListModel
*WXUNUSED(model
),
170 unsigned int WXUNUSED(col
),
171 unsigned int WXUNUSED(row
) )
180 // -------------------------------------
181 // MyUnsortedTextModel
182 // -------------------------------------
184 class MyUnsortedTextModel
: public wxDataViewListModel
187 MyUnsortedTextModel()
189 m_list
.Add( wxT("This") );
190 m_list
.Add( wxT("is") );
191 m_list
.Add( wxT("an") );
192 m_list
.Add( wxT("unsorted") );
193 m_list
.Add( wxT("list") );
194 m_list
.Add( wxT("of") );
195 m_list
.Add( wxT("words.") );
197 m_bitmap
= wxBitmap( null_xpm
);
200 virtual unsigned int GetNumberOfRows() { return m_list
.GetCount(); }
201 virtual unsigned int GetNumberOfCols() { return 2; }
202 virtual wxString
GetColType( unsigned int WXUNUSED(col
) ) { return wxT("string"); }
203 virtual void GetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
)
207 variant
= m_list
[row
];
210 if ((col
== 2) || (col
== 3))
216 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
219 virtual bool SetValue( wxVariant
&variant
, unsigned int col
, unsigned int row
)
223 m_list
[row
] = variant
.GetString();
230 void AppendRow( const wxString
&text
)
236 void PrependRow( const wxString
&text
)
238 m_list
.Insert( text
, 0 );
242 void InsertRowAt1( const wxString
&text
)
244 m_list
.Insert( text
, 1 );
248 void DeleteRow( unsigned int index
)
250 m_list
.RemoveAt( index
);
254 wxArrayString m_list
;
258 // -------------------------------------
260 // -------------------------------------
262 class MyApp
: public wxApp
268 // -------------------------------------
270 // -------------------------------------
272 class MyFrame
: public wxFrame
275 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
278 void OnQuit(wxCommandEvent
& event
);
279 void OnAbout(wxCommandEvent
& event
);
282 wxDataViewCtrl
* dataview_left
;
283 wxDataViewCtrl
* dataview_right
;
286 // -------------------------------------
288 // -------------------------------------
292 ID_APPEND_ROW_LEFT
= 1000,
299 ID_PREPEND_ROW_RIGHT
,
308 class MySortingFrame
: public wxFrame
311 MySortingFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
315 void OnQuit(wxCommandEvent
& event
);
316 void OnAbout(wxCommandEvent
& event
);
318 void OnAppendRowLeft(wxCommandEvent
& event
);
319 void OnPrependRowLeft(wxCommandEvent
& event
);
320 void OnInsertRowLeft(wxCommandEvent
& event
);
321 void OnDeleteRowLeft(wxCommandEvent
& event
);
322 void OnEditRowLeft(wxCommandEvent
& event
);
324 void OnAppendRowRight(wxCommandEvent
& event
);
325 void OnPrependRowRight(wxCommandEvent
& event
);
326 void OnInsertRowRight(wxCommandEvent
& event
);
327 void OnDeleteRowRight(wxCommandEvent
& event
);
328 void OnEditRowRight(wxCommandEvent
& event
);
330 void OnSelectedUnsorted(wxDataViewEvent
&event
);
331 void OnSelectedSorted(wxDataViewEvent
&event
);
334 wxDataViewCtrl
* dataview_left
;
335 wxDataViewCtrl
* dataview_right
;
338 wxTextCtrl
*m_logWindow
;
340 MyUnsortedTextModel
*m_unsorted_model
;
342 DECLARE_EVENT_TABLE()
345 // -------------------------------------
347 // -------------------------------------
349 #define DYNAMIC_QUIT wxID_EXIT
350 #define DYNAMIC_ABOUT wxID_ABOUT
352 IMPLEMENT_APP (MyApp
)
354 bool MyApp::OnInit(void)
356 MyFrame
*frame
= new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
359 MySortingFrame
*frame2
= new MySortingFrame(NULL
, wxT("wxDataViewCtrl sorting test"), 10, 150, 600, 500);
367 // -------------------------------------
369 // -------------------------------------
371 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
372 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
374 SetIcon(wxICON(sample
));
376 wxMenu
*file_menu
= new wxMenu
;
378 file_menu
->Append(DYNAMIC_ABOUT
, _T("&About"));
379 file_menu
->Append(DYNAMIC_QUIT
, _T("E&xit"));
380 wxMenuBar
*menu_bar
= new wxMenuBar
;
381 menu_bar
->Append(file_menu
, _T("&File"));
382 SetMenuBar(menu_bar
);
384 // You used to have to do some casting for param 4, but now there are type-safe handlers
385 Connect( DYNAMIC_QUIT
, wxID_ANY
,
386 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MyFrame::OnQuit
) );
387 Connect( DYNAMIC_ABOUT
, wxID_ANY
,
388 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MyFrame::OnAbout
) );
392 wxPanel
*panel
= new wxPanel( this, wxID_ANY
);
395 // Left wxDataViewCtrl
396 dataview_left
= new wxDataViewCtrl( panel
, wxID_ANY
);
398 MyTextModel
*model
= new MyTextModel
;
399 dataview_left
->AssociateModel( model
);
401 dataview_left
->AppendTextColumn( wxT("first"), 0 );
402 dataview_left
->AppendTextColumn( wxT("second"), 1 );
404 wxDataViewTextCell
*text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
405 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_cell
, 2 );
406 dataview_left
->AppendColumn( column
);
408 dataview_left
->AppendToggleColumn( wxT("fourth"), 3 );
410 MyCustomCell
*custom_cell
= new MyCustomCell
;
411 column
= new wxDataViewColumn( wxT("custom"), custom_cell
, 4 );
412 dataview_left
->AppendColumn( column
);
414 dataview_left
->AppendProgressColumn( wxT("progress"), 5 );
416 dataview_left
->AppendDateColumn( wxT("date"), 6 );
418 // Right wxDataViewCtrl using the same model
419 dataview_right
= new wxDataViewCtrl( panel
, wxID_ANY
);
420 dataview_right
->AssociateModel( model
);
422 text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
423 column
= new wxDataViewColumn( wxT("editable"), text_cell
, 2 );
424 dataview_right
->AppendColumn( column
);
425 dataview_right
->AppendTextColumn( wxT("first"), 0 );
426 dataview_right
->AppendTextColumn( wxT("second"), 1 );
427 wxDataViewToggleCell
*toggle_cell
= new wxDataViewToggleCell( wxT("bool"), wxDATAVIEW_CELL_ACTIVATABLE
);
428 column
= new wxDataViewColumn( wxT("bool"), toggle_cell
, 3, 30 );
429 dataview_right
->AppendColumn( column
);
431 dataview_right
->AppendDateColumn( wxT("date"), 6 );
433 // layout dataview controls.
435 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
436 sizer
->Add( dataview_left
, 3, wxGROW
);
438 sizer
->Add( dataview_right
, 2, wxGROW
);
439 panel
->SetSizer( sizer
);
442 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
447 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
449 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control handling"),
450 _T("About DataView"), wxOK
);
455 // -------------------------------------
457 // -------------------------------------
459 BEGIN_EVENT_TABLE(MySortingFrame
,wxFrame
)
460 EVT_BUTTON( ID_APPEND_ROW_LEFT
, MySortingFrame::OnAppendRowLeft
)
461 EVT_BUTTON( ID_PREPEND_ROW_LEFT
, MySortingFrame::OnPrependRowLeft
)
462 EVT_BUTTON( ID_INSERT_ROW_LEFT
, MySortingFrame::OnInsertRowLeft
)
463 EVT_BUTTON( ID_DELETE_ROW_LEFT
, MySortingFrame::OnDeleteRowLeft
)
464 EVT_DATAVIEW_ROW_SELECTED( ID_SORTED
, MySortingFrame::OnSelectedSorted
)
465 EVT_DATAVIEW_ROW_SELECTED( ID_UNSORTED
, MySortingFrame::OnSelectedUnsorted
)
468 MySortingFrame::MySortingFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
469 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
473 SetIcon(wxICON(sample
));
475 wxMenu
*file_menu
= new wxMenu
;
477 file_menu
->Append(DYNAMIC_ABOUT
, _T("&About"));
478 file_menu
->Append(DYNAMIC_QUIT
, _T("E&xit"));
479 wxMenuBar
*menu_bar
= new wxMenuBar
;
480 menu_bar
->Append(file_menu
, _T("&File"));
481 SetMenuBar(menu_bar
);
483 // You used to have to do some casting for param 4, but now there are type-safe handlers
484 Connect( DYNAMIC_QUIT
, wxID_ANY
,
485 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MySortingFrame::OnQuit
) );
486 Connect( DYNAMIC_ABOUT
, wxID_ANY
,
487 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MySortingFrame::OnAbout
) );
492 // Left wxDataViewCtrl
493 dataview_left
= new wxDataViewCtrl( this, ID_UNSORTED
, wxDefaultPosition
, wxDefaultSize
, wxDV_MULTIPLE
);
495 m_unsorted_model
= new MyUnsortedTextModel
;
496 dataview_left
->AssociateModel( m_unsorted_model
);
497 wxDataViewTextCell
*text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
498 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_cell
, 0 );
499 dataview_left
->AppendColumn( column
);
500 dataview_left
->AppendTextColumn( wxT("second"), 1 );
501 dataview_left
->AppendColumn( new wxDataViewColumn( wxT("icon"), new wxDataViewBitmapCell
, 2, 25 ) );
502 dataview_left
->AppendColumn( new wxDataViewColumn( wxT("icon"), new wxDataViewBitmapCell
, 3, 25 ) );
504 // Right wxDataViewCtrl using the sorting model
505 dataview_right
= new wxDataViewCtrl( this, ID_SORTED
);
507 wxDataViewSortedListModel
*sorted_model
=
508 new wxDataViewSortedListModel( m_unsorted_model
);
509 dataview_right
->AssociateModel( sorted_model
);
510 text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
511 column
= new wxDataViewColumn( wxT("editable"), text_cell
, 0, -1, wxDATAVIEW_COL_SORTABLE
|wxDATAVIEW_COL_RESIZABLE
);
512 dataview_right
->AppendColumn( column
);
514 dataview_right
->AppendTextColumn( wxT("second"), 1 );
516 // layout dataview controls.
518 wxBoxSizer
*top_sizer
= new wxBoxSizer( wxHORIZONTAL
);
519 top_sizer
->Add( dataview_left
, 1, wxGROW
);
520 top_sizer
->Add(10,10);
521 top_sizer
->Add( dataview_right
, 1, wxGROW
);
523 wxBoxSizer
*button_sizer
= new wxBoxSizer( wxHORIZONTAL
);
524 button_sizer
->Add( 10, 10, 1 );
525 wxFlexGridSizer
*left_sizer
= new wxFlexGridSizer( 2 );
526 left_sizer
->Add( new wxButton( this, ID_APPEND_ROW_LEFT
, wxT("Append") ), 0, wxALL
, 5 );
527 left_sizer
->Add( new wxButton( this, ID_PREPEND_ROW_LEFT
, wxT("Prepend") ), 0, wxALL
, 5 );
528 left_sizer
->Add( new wxButton( this, ID_INSERT_ROW_LEFT
, wxT("Insert") ), 0, wxALL
, 5 );
529 left_sizer
->Add( new wxButton( this, ID_DELETE_ROW_LEFT
, wxT("Delete second") ), 0, wxALL
, 5 );
530 left_sizer
->Add( new wxButton( this, ID_EDIT_ROW_LEFT
, wxT("Edit") ), 0, wxALL
, 5 );
531 button_sizer
->Add( left_sizer
);
532 button_sizer
->Add( 10, 10, 2 );
533 wxFlexGridSizer
*right_sizer
= new wxFlexGridSizer( 2 );
534 right_sizer
->Add( new wxButton( this, ID_APPEND_ROW_RIGHT
, wxT("Append") ), 0, wxALL
, 5 );
535 right_sizer
->Add( new wxButton( this, ID_PREPEND_ROW_RIGHT
, wxT("Prepend") ), 0, wxALL
, 5 );
536 right_sizer
->Add( new wxButton( this, ID_INSERT_ROW_RIGHT
, wxT("Insert") ), 0, wxALL
, 5 );
537 right_sizer
->Add( new wxButton( this, ID_DELETE_ROW_RIGHT
, wxT("Delete second") ), 0, wxALL
, 5 );
538 right_sizer
->Add( new wxButton( this, ID_EDIT_ROW_RIGHT
, wxT("Edit") ), 0, wxALL
, 5 );
539 button_sizer
->Add( right_sizer
);
540 button_sizer
->Add( 10, 10, 1 );
542 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
543 main_sizer
->Add( top_sizer
, 1, wxGROW
);
544 main_sizer
->Add( button_sizer
, 0, wxGROW
);
546 m_logWindow
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
547 wxDefaultPosition
, wxDefaultSize
,
548 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
549 main_sizer
->Add( 20,20 );
550 main_sizer
->Add( m_logWindow
, 1, wxGROW
);
552 m_logOld
= wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow
));
554 SetSizer( main_sizer
);
557 MySortingFrame::~MySortingFrame()
559 delete wxLog::SetActiveTarget(m_logOld
);
562 void MySortingFrame::OnSelectedUnsorted(wxDataViewEvent
&event
)
564 wxLogMessage( wxT("OnSelected from unsorted list, selected %d"), (int) event
.GetRow() );
567 void MySortingFrame::OnSelectedSorted(wxDataViewEvent
&event
)
569 wxLogMessage( wxT("OnSelected from sorted list, selected %d"), (int) event
.GetRow() );
572 void MySortingFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
577 void MySortingFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
579 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control sorting"),
580 _T("About DataView"), wxOK
);
585 void MySortingFrame::OnAppendRowLeft(wxCommandEvent
& WXUNUSED(event
))
587 wxTextEntryDialog
dialog( this, wxT("Enter text to append") );
588 if (dialog
.ShowModal() == wxID_OK
)
590 wxString value
= dialog
.GetValue();
592 m_unsorted_model
->AppendRow( value
);
596 void MySortingFrame::OnPrependRowLeft(wxCommandEvent
& WXUNUSED(event
))
598 wxTextEntryDialog
dialog( this, wxT("Enter text to prepend") );
599 if (dialog
.ShowModal() == wxID_OK
)
601 wxString value
= dialog
.GetValue();
603 m_unsorted_model
->PrependRow( value
);
607 void MySortingFrame::OnInsertRowLeft(wxCommandEvent
& WXUNUSED(event
))
609 wxTextEntryDialog
dialog( this, wxT("Enter text to insert before second") );
610 if (dialog
.ShowModal() == wxID_OK
)
612 wxString value
= dialog
.GetValue();
614 m_unsorted_model
->InsertRowAt1( value
);
618 void MySortingFrame::OnDeleteRowLeft(wxCommandEvent
& WXUNUSED(event
))
620 m_unsorted_model
->DeleteRow( 1 );
623 void MySortingFrame::OnEditRowLeft(wxCommandEvent
& WXUNUSED(event
))
627 void MySortingFrame::OnAppendRowRight(wxCommandEvent
& WXUNUSED(event
))
631 void MySortingFrame::OnPrependRowRight(wxCommandEvent
& WXUNUSED(event
))
635 void MySortingFrame::OnInsertRowRight(wxCommandEvent
& WXUNUSED(event
))
639 void MySortingFrame::OnDeleteRowRight(wxCommandEvent
& WXUNUSED(event
))
643 void MySortingFrame::OnEditRowRight(wxCommandEvent
& WXUNUSED(event
))