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