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"
24 #include "mondrian.xpm"
27 #include "wx/dataview.h"
29 // -------------------------------------
31 // -------------------------------------
33 WX_DECLARE_LIST(wxDateTime
,wxArrayDate
);
34 #include <wx/listimpl.cpp>
35 WX_DEFINE_LIST(wxArrayDate
);
37 class MyTextModel
: public wxDataViewListModel
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
++)
53 wxDateTime
*date
= new wxDateTime( wxDateTime::Now() );
54 m_dates
.Append( date
);
58 virtual size_t GetNumberOfRows()
60 virtual size_t GetNumberOfCols()
63 // as reported by wxVariant
64 virtual wxString
GetColType( size_t col
)
67 return wxT("datetime");
78 virtual void GetValue( wxVariant
&variant
, size_t col
, size_t row
)
82 variant
= (wxDateTime
) *m_dates
[row
];
86 variant
= (long) m_progress
[row
];
90 variant
= m_colours
[row
];
94 variant
= (bool) m_bools
[row
];
98 variant
= m_list
[row
];
103 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
107 virtual bool SetValue( wxVariant
&value
, size_t col
, size_t row
)
111 *m_dates
[row
] = value
.GetDateTime();
115 m_bools
[row
] = (int) value
.GetBool();
119 m_list
[row
] = value
.GetString();
124 wxArrayString m_list
;
126 wxArrayString m_colours
;
127 wxArrayInt m_progress
;
131 // -------------------------------------
133 // -------------------------------------
135 class MyCustomCell
: public wxDataViewCustomCell
139 wxDataViewCustomCell( wxT("string"), wxDATAVIEW_CELL_ACTIVATABLE
)
141 m_colour
= wxT("black");
143 bool SetValue( const wxVariant
&value
)
145 m_colour
= value
.GetString();
148 bool Render( wxRect rect
, wxDC
*dc
, int state
)
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
);
156 dc
->SetBrush( *wxBLACK_BRUSH
);
157 dc
->DrawRectangle( rect
);
164 bool Activate( wxRect rect
,
165 wxDataViewListModel
*model
, size_t col
, size_t row
)
174 // -------------------------------------
175 // MyUnsortedTextModel
176 // -------------------------------------
178 class MyUnsortedTextModel
: public wxDataViewListModel
181 MyUnsortedTextModel()
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.") );
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 void GetValue( wxVariant
&variant
, size_t col
, size_t row
)
199 variant
= m_list
[row
];
203 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
206 virtual bool SetValue( wxVariant
&variant
, size_t col
, size_t row
)
210 m_list
[row
] = variant
.GetString();
217 wxArrayString m_list
;
220 // -------------------------------------
222 // -------------------------------------
224 class MyApp
: public wxApp
230 // -------------------------------------
232 // -------------------------------------
234 class MyFrame
: public wxFrame
237 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
240 void OnQuit(wxCommandEvent
& event
);
241 void OnAbout(wxCommandEvent
& event
);
244 wxDataViewCtrl
* dataview_left
;
245 wxDataViewCtrl
* dataview_right
;
248 // -------------------------------------
250 // -------------------------------------
254 ID_APPEND_ROW_LEFT
= 1000,
261 ID_PREPEND_ROW_RIGHT
,
267 class MySortingFrame
: public wxFrame
270 MySortingFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
273 void OnQuit(wxCommandEvent
& event
);
274 void OnAbout(wxCommandEvent
& event
);
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
);
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
);
289 wxDataViewCtrl
* dataview_left
;
290 wxDataViewCtrl
* dataview_right
;
292 DECLARE_EVENT_TABLE()
295 // -------------------------------------
297 // -------------------------------------
299 #define DYNAMIC_QUIT wxID_EXIT
300 #define DYNAMIC_ABOUT wxID_ABOUT
302 IMPLEMENT_APP (MyApp
)
304 bool MyApp::OnInit(void)
306 MyFrame
*frame
= new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
309 MySortingFrame
*frame2
= new MySortingFrame(NULL
, wxT("wxDataViewCtrl sorting test"), 10, 350, 600, 300);
317 // -------------------------------------
319 // -------------------------------------
321 MyFrame::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
))
325 SetIcon(wxIcon(_T("mondrian")));
327 SetIcon(wxIcon(mondrian_xpm
));
330 wxMenu
*file_menu
= new wxMenu
;
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
);
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
) );
347 // Left wxDataViewCtrl
348 dataview_left
= new wxDataViewCtrl( this, -1 );
350 MyTextModel
*model
= new MyTextModel
;
351 dataview_left
->AssociateModel( model
);
353 dataview_left
->AppendTextColumn( wxT("first"), 0 );
354 dataview_left
->AppendTextColumn( wxT("second"), 1 );
356 wxDataViewTextCell
*text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
357 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_cell
, 2 );
358 dataview_left
->AppendColumn( column
);
360 dataview_left
->AppendToggleColumn( wxT("fourth"), 3 );
362 MyCustomCell
*custom_cell
= new MyCustomCell
;
363 column
= new wxDataViewColumn( wxT("custom"), custom_cell
, 4 );
364 dataview_left
->AppendColumn( column
);
366 dataview_left
->AppendProgressColumn( wxT("progress"), 5 );
368 dataview_left
->AppendDateColumn( wxT("date"), 6 );
370 // Right wxDataViewCtrl using the same model
371 dataview_right
= new wxDataViewCtrl( this, -1 );
372 dataview_right
->AssociateModel( model
);
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 );
381 dataview_right
->AppendColumn( column
);
383 dataview_right
->AppendDateColumn( wxT("date"), 6 );
385 // layout dataview controls.
387 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
388 sizer
->Add( dataview_left
, 3, wxGROW
);
390 sizer
->Add( dataview_right
, 2, wxGROW
);
394 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
399 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
401 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control handling"),
402 _T("About DataView"), wxOK
);
407 // -------------------------------------
409 // -------------------------------------
411 BEGIN_EVENT_TABLE(MySortingFrame
,wxFrame
)
412 EVT_BUTTON( ID_APPEND_ROW_LEFT
, MySortingFrame::OnAppendRowLeft
)
415 MySortingFrame::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
))
419 SetIcon(wxIcon(_T("mondrian")));
421 SetIcon(wxIcon(mondrian_xpm
));
424 wxMenu
*file_menu
= new wxMenu
;
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
);
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
) );
441 // Left wxDataViewCtrl
442 dataview_left
= new wxDataViewCtrl( this, -1 );
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 );
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 );
461 // layout dataview controls.
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
);
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 );
487 wxBoxSizer
*main_sizer
= new wxBoxSizer( wxVERTICAL
);
488 main_sizer
->Add( top_sizer
, 1, wxGROW
);
489 main_sizer
->Add( button_sizer
, 0, wxGROW
);
491 SetSizer( main_sizer
);
494 void MySortingFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
499 void MySortingFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
501 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control sorting"),
502 _T("About DataView"), wxOK
);
507 void MySortingFrame::OnAppendRowLeft(wxCommandEvent
& event
)
511 void MySortingFrame::OnPrependRowLeft(wxCommandEvent
& event
)
515 void MySortingFrame::OnInsertRowLeft(wxCommandEvent
& event
)
519 void MySortingFrame::OnDeleteRowLeft(wxCommandEvent
& event
)
523 void MySortingFrame::OnEditRowLeft(wxCommandEvent
& event
)
527 void MySortingFrame::OnAppendRowRight(wxCommandEvent
& event
)
531 void MySortingFrame::OnPrependRowRight(wxCommandEvent
& event
)
535 void MySortingFrame::OnInsertRowRight(wxCommandEvent
& event
)
539 void MySortingFrame::OnDeleteRowRight(wxCommandEvent
& event
)
543 void MySortingFrame::OnEditRowRight(wxCommandEvent
& event
)