]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: dataview.cpp | |
3 | // Purpose: wxDataViewCtrl wxWidgets sample | |
4 | // Author: Robert Roebling | |
5 | // Modified by: Francesco Montorsi, Bo Yang | |
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 | #include "wx/splitter.h" | |
25 | #include "wx/aboutdlg.h" | |
26 | #include "wx/choicdlg.h" | |
27 | #include "wx/numdlg.h" | |
28 | #include "wx/dataview.h" | |
29 | #include "wx/spinctrl.h" | |
30 | ||
31 | #include "wx/ptr_shrd.h" | |
32 | #include "wx/vector.h" | |
33 | ||
34 | #ifndef __WXMSW__ | |
35 | #include "../sample.xpm" | |
36 | #endif | |
37 | ||
38 | #include "null.xpm" | |
39 | ||
40 | /* XPM */ | |
41 | static const char *small1_xpm[] = { | |
42 | /* columns rows colors chars-per-pixel */ | |
43 | "16 16 6 1", | |
44 | ". c Black", | |
45 | "o c #FFFFFF", | |
46 | "X c #000080", | |
47 | "O c #FFFF00", | |
48 | " c None", | |
49 | "+ c #FF0000", | |
50 | /* pixels */ | |
51 | " ", | |
52 | " ", | |
53 | " ", | |
54 | " ....... ", | |
55 | " .XXXXX. ", | |
56 | " .oXXXX. ", | |
57 | " .oXXX.......", | |
58 | ".....oXXX.OOOOO.", | |
59 | ".+++.XXXX.oOOOO.", | |
60 | ".o++......oOOOO.", | |
61 | ".o++++. .oOOOO.", | |
62 | ".o++++. .OOOOO.", | |
63 | ".+++++. .......", | |
64 | "....... ", | |
65 | " ", | |
66 | " " | |
67 | }; | |
68 | ||
69 | ||
70 | #define DEFAULT_ALIGN wxALIGN_LEFT | |
71 | #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES) | |
72 | ||
73 | // ------------------------------------- | |
74 | // MyMusicModel | |
75 | // ------------------------------------- | |
76 | ||
77 | /* | |
78 | Implement this data model | |
79 | Title Artist Year | |
80 | ------------------------------------------------------------- | |
81 | 1: My Music: | |
82 | 2: Pop music | |
83 | 3: You are not alone Michael Jackson 1995 | |
84 | 4: Take a bow Madonna 1994 | |
85 | 5: Classical music | |
86 | 6: Ninth Symphony Ludwig v. Beethoven 1824 | |
87 | 7: German Requiem Johannes Brahms 1868 | |
88 | */ | |
89 | ||
90 | ||
91 | ||
92 | class MyMusicModelNode; | |
93 | WX_DEFINE_ARRAY_PTR( MyMusicModelNode*, MyMusicModelNodes ); | |
94 | ||
95 | class MyMusicModelNode | |
96 | { | |
97 | public: | |
98 | MyMusicModelNode( MyMusicModelNode* parent, | |
99 | const wxString &title, const wxString &artist, int year ) | |
100 | { | |
101 | m_parent = parent; | |
102 | m_title = title; | |
103 | m_artist = artist; | |
104 | m_year = year; | |
105 | m_isContainer = false; | |
106 | } | |
107 | ||
108 | MyMusicModelNode( MyMusicModelNode* parent, | |
109 | const wxString &branch ) | |
110 | { | |
111 | m_parent = parent; | |
112 | m_title = branch; | |
113 | m_year = -1; | |
114 | m_isContainer = true; | |
115 | } | |
116 | ||
117 | ~MyMusicModelNode() | |
118 | { | |
119 | size_t count = m_children.GetCount(); | |
120 | size_t i; | |
121 | for (i = 0; i < count; i++) | |
122 | { | |
123 | MyMusicModelNode *child = m_children[i]; | |
124 | delete child; | |
125 | } | |
126 | } | |
127 | ||
128 | bool IsContainer() { return m_isContainer; } | |
129 | ||
130 | MyMusicModelNode* GetParent() { return m_parent; } | |
131 | MyMusicModelNodes &GetChildren() { return m_children; } | |
132 | MyMusicModelNode* GetNthChild( unsigned int n ) { return m_children.Item( n ); } | |
133 | void Insert( MyMusicModelNode* child, unsigned int n) { m_children.Insert( child, n); } | |
134 | void Append( MyMusicModelNode* child ) { m_children.Add( child ); } | |
135 | unsigned int GetChildCount() { return m_children.GetCount(); } | |
136 | ||
137 | public: | |
138 | wxString m_title; | |
139 | wxString m_artist; | |
140 | int m_year; | |
141 | ||
142 | private: | |
143 | MyMusicModelNode *m_parent; | |
144 | MyMusicModelNodes m_children; | |
145 | bool m_isContainer; | |
146 | }; | |
147 | ||
148 | ||
149 | class MyMusicModel: public wxDataViewModel | |
150 | { | |
151 | public: | |
152 | ||
153 | // constructor | |
154 | ||
155 | MyMusicModel() | |
156 | { | |
157 | m_root = new MyMusicModelNode( NULL, wxT("My Music" )); | |
158 | m_pop = new MyMusicModelNode( m_root, wxT("Pop music") ); | |
159 | m_root->Append( m_pop ); | |
160 | m_pop->Append( new MyMusicModelNode( m_pop, | |
161 | wxT("You are not alone"), wxT("Michael Jackson"), 1995 ) ); | |
162 | m_pop->Append( new MyMusicModelNode( m_pop, | |
163 | wxT("Take a bow"), wxT("Madonna"), 1994 ) ); | |
164 | m_classical = new MyMusicModelNode( m_root, wxT("Classical music") ); | |
165 | m_root->Append( m_classical ); | |
166 | m_classical->Append( new MyMusicModelNode( m_classical, | |
167 | wxT("Ninth symphony"), wxT("Ludwig van Beethoven"), 1824 ) ); | |
168 | m_classical->Append( new MyMusicModelNode( m_classical, | |
169 | wxT("German Requiem"), wxT("Johannes Brahms"), 1868 ) ); | |
170 | m_classicalMusicIsKnownToControl = false; | |
171 | } | |
172 | ||
173 | ~MyMusicModel() | |
174 | { | |
175 | delete m_root; | |
176 | } | |
177 | ||
178 | // helper method for wxLog | |
179 | ||
180 | wxString GetTitle( const wxDataViewItem &item ) | |
181 | { | |
182 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); | |
183 | if (!node) | |
184 | return wxEmptyString; | |
185 | ||
186 | return node->m_title; | |
187 | } | |
188 | ||
189 | // helper methods to change the model | |
190 | ||
191 | void AddToClassical( const wxString &title, const wxString &artist, int year ) | |
192 | { | |
193 | // add to data | |
194 | MyMusicModelNode *child_node = | |
195 | new MyMusicModelNode( m_classical, title, artist, year ); | |
196 | ||
197 | m_classical->Append( child_node ); | |
198 | ||
199 | if (m_classicalMusicIsKnownToControl) | |
200 | { | |
201 | // notify control | |
202 | wxDataViewItem child( (void*) child_node ); | |
203 | wxDataViewItem parent( (void*) m_classical ); | |
204 | ItemAdded( parent, child ); | |
205 | } | |
206 | } | |
207 | ||
208 | void Delete( const wxDataViewItem &item ) | |
209 | { | |
210 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); | |
211 | wxDataViewItem parent( node->GetParent() ); | |
212 | ||
213 | node->GetParent()->GetChildren().Remove( node ); | |
214 | delete node; | |
215 | ||
216 | // notify control | |
217 | ItemDeleted( parent, item ); | |
218 | } | |
219 | ||
220 | // override sorting to always sort branches ascendingly | |
221 | ||
222 | int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2, | |
223 | unsigned int column, bool ascending ) | |
224 | { | |
225 | if (IsContainer(item1) && IsContainer(item2)) | |
226 | { | |
227 | wxVariant value1,value2; | |
228 | GetValue( value1, item1, 0 ); | |
229 | GetValue( value2, item2, 0 ); | |
230 | ||
231 | wxString str1 = value1.GetString(); | |
232 | wxString str2 = value2.GetString(); | |
233 | int res = str1.Cmp( str2 ); | |
234 | if (res) return res; | |
235 | ||
236 | // items must be different | |
237 | unsigned long litem1 = (unsigned long) item1.GetID(); | |
238 | unsigned long litem2 = (unsigned long) item2.GetID(); | |
239 | ||
240 | return litem1-litem2; | |
241 | } | |
242 | ||
243 | return wxDataViewModel::Compare( item1, item2, column, ascending ); | |
244 | } | |
245 | ||
246 | // implementation of base class virtuals to define model | |
247 | ||
248 | virtual unsigned int GetColumnCount() const | |
249 | { | |
250 | return 3; | |
251 | } | |
252 | ||
253 | virtual wxString GetColumnType( unsigned int col ) const | |
254 | { | |
255 | if (col == 2) | |
256 | return wxT("long"); | |
257 | ||
258 | return wxT("string"); | |
259 | } | |
260 | ||
261 | virtual void GetValue( wxVariant &variant, | |
262 | const wxDataViewItem &item, unsigned int col ) const | |
263 | { | |
264 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); | |
265 | switch (col) | |
266 | { | |
267 | case 0: variant = node->m_title; break; | |
268 | case 1: variant = node->m_artist; break; | |
269 | case 2: variant = (long) node->m_year; break; | |
270 | default: | |
271 | { | |
272 | wxLogError( wxT("MyMusicModel::GetValue: wrong column" )); | |
273 | ||
274 | // provoke a crash when mouse button down | |
275 | wxMouseState state = wxGetMouseState(); | |
276 | if (state.ShiftDown()) | |
277 | { | |
278 | char *crash = 0; | |
279 | *crash = 0; | |
280 | } | |
281 | } | |
282 | } | |
283 | } | |
284 | ||
285 | virtual bool SetValue( const wxVariant &variant, | |
286 | const wxDataViewItem &item, unsigned int col ) | |
287 | { | |
288 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); | |
289 | switch (col) | |
290 | { | |
291 | case 0: node->m_title = variant.GetString(); return true; | |
292 | case 1: node->m_artist = variant.GetString(); return true; | |
293 | case 2: node->m_year = variant.GetLong(); return true; | |
294 | default: wxLogError( wxT("MyMusicModel::SetValue: wrong column") ); | |
295 | } | |
296 | return false; | |
297 | } | |
298 | ||
299 | virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const | |
300 | { | |
301 | // the invisble root node has no parent | |
302 | if (!item.IsOk()) | |
303 | return wxDataViewItem(0); | |
304 | ||
305 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); | |
306 | ||
307 | // "MyMusic" also has no parent | |
308 | if (node == m_root) | |
309 | return wxDataViewItem(0); | |
310 | ||
311 | return wxDataViewItem( (void*) node->GetParent() ); | |
312 | } | |
313 | ||
314 | virtual bool IsContainer( const wxDataViewItem &item ) const | |
315 | { | |
316 | // the invisble root node can have children (in | |
317 | // our model always "MyMusic") | |
318 | if (!item.IsOk()) | |
319 | return true; | |
320 | ||
321 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); | |
322 | return node->IsContainer(); | |
323 | } | |
324 | ||
325 | virtual unsigned int GetChildren( const wxDataViewItem &parent, wxDataViewItemArray &array ) const | |
326 | { | |
327 | MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID(); | |
328 | if (!node) | |
329 | { | |
330 | array.Add( wxDataViewItem( (void*) m_root ) ); | |
331 | return 1; | |
332 | } | |
333 | ||
334 | if (node == m_classical) | |
335 | { | |
336 | MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this; | |
337 | model->m_classicalMusicIsKnownToControl = true; | |
338 | } | |
339 | ||
340 | if (node->GetChildCount() == 0) | |
341 | { | |
342 | return 0; | |
343 | } | |
344 | ||
345 | unsigned int count = node->GetChildren().GetCount(); | |
346 | unsigned int pos; | |
347 | for (pos = 0; pos < count; pos++) | |
348 | { | |
349 | MyMusicModelNode *child = node->GetChildren().Item( pos ); | |
350 | array.Add( wxDataViewItem( (void*) child ) ); | |
351 | } | |
352 | return count; | |
353 | } | |
354 | ||
355 | // DnD | |
356 | ||
357 | virtual bool IsDraggable( const wxDataViewItem &item ) | |
358 | { | |
359 | // only drag items | |
360 | return (!IsContainer(item)); | |
361 | } | |
362 | ||
363 | virtual size_t GetDragDataSize( const wxDataViewItem &item, const wxDataFormat &WXUNUSED(format) ) | |
364 | { | |
365 | wxPrintf( "GetDragDataSize\n" ); | |
366 | ||
367 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); | |
368 | wxString data; | |
369 | data += node->m_title; data += wxT(" "); | |
370 | data += node->m_artist; | |
371 | return strlen( data.utf8_str() ) + 1; | |
372 | } | |
373 | virtual bool GetDragData( const wxDataViewItem &item, const wxDataFormat &WXUNUSED(format), | |
374 | void* dest, size_t WXUNUSED(size) ) | |
375 | { | |
376 | wxPrintf( "GetDragData\n" ); | |
377 | ||
378 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); | |
379 | wxString data; | |
380 | data += node->m_title; data += wxT(" "); | |
381 | data += node->m_artist; | |
382 | wxCharBuffer buffer( data.utf8_str() ); | |
383 | memcpy( dest, buffer, strlen(buffer)+1 ); | |
384 | return true; | |
385 | } | |
386 | ||
387 | private: | |
388 | MyMusicModelNode* m_root; | |
389 | MyMusicModelNode* m_pop; | |
390 | MyMusicModelNode* m_classical; | |
391 | bool m_classicalMusicIsKnownToControl; | |
392 | }; | |
393 | ||
394 | ||
395 | static int my_sort_reverse( int *v1, int *v2 ) | |
396 | { | |
397 | return *v2-*v1; | |
398 | } | |
399 | ||
400 | static int my_sort( int *v1, int *v2 ) | |
401 | { | |
402 | return *v1-*v2; | |
403 | } | |
404 | ||
405 | class MyListModel: public wxDataViewIndexListModel | |
406 | { | |
407 | public: | |
408 | MyListModel() : | |
409 | #ifdef __WXMAC__ | |
410 | wxDataViewIndexListModel( 1000 + 100 ) | |
411 | #else | |
412 | wxDataViewIndexListModel( 100000 + 100 ) | |
413 | #endif | |
414 | { | |
415 | #ifdef __WXMAC__ | |
416 | m_virtualItems = 1000; | |
417 | #else | |
418 | m_virtualItems = 100000; | |
419 | #endif | |
420 | ||
421 | unsigned int i; | |
422 | for (i = 0; i < 100; i++) | |
423 | { | |
424 | wxString str; | |
425 | str.Printf( wxT("row number %d"), i ); | |
426 | m_array.Add( str ); | |
427 | } | |
428 | ||
429 | m_icon = wxIcon( null_xpm ); | |
430 | } | |
431 | ||
432 | // helper methods to change the model | |
433 | ||
434 | void Prepend( const wxString &text ) | |
435 | { | |
436 | m_array.Insert( text, 0 ); | |
437 | RowPrepended(); | |
438 | } | |
439 | ||
440 | void DeleteItem( const wxDataViewItem &item ) | |
441 | { | |
442 | unsigned int row = GetRow( item ); | |
443 | if (row >= m_array.GetCount()) | |
444 | return; | |
445 | ||
446 | m_array.RemoveAt( row ); | |
447 | RowDeleted( row ); | |
448 | } | |
449 | ||
450 | void DeleteItems( const wxDataViewItemArray &items ) | |
451 | { | |
452 | wxArrayInt rows; | |
453 | unsigned int i; | |
454 | for (i = 0; i < items.GetCount(); i++) | |
455 | { | |
456 | unsigned int row = GetRow( items[i] ); | |
457 | if (row < m_array.GetCount()) | |
458 | rows.Add( row ); | |
459 | } | |
460 | ||
461 | // Sort in descending order so that the last | |
462 | // row will be deleted first. Otherwise the | |
463 | // remaining indeces would all be wrong. | |
464 | rows.Sort( my_sort_reverse ); | |
465 | for (i = 0; i < rows.GetCount(); i++) | |
466 | m_array.RemoveAt( rows[i] ); | |
467 | ||
468 | // This is just to test if wxDataViewCtrl can | |
469 | // cope with removing rows not sorted in | |
470 | // descending order | |
471 | rows.Sort( my_sort ); | |
472 | RowsDeleted( rows ); | |
473 | } | |
474 | ||
475 | void AddMany() | |
476 | { | |
477 | m_virtualItems += 1000; | |
478 | Reset( m_array.GetCount() + m_virtualItems ); | |
479 | } | |
480 | ||
481 | // implementation of base class virtuals to define model | |
482 | ||
483 | virtual unsigned int GetColumnCount() const | |
484 | { | |
485 | return 3; | |
486 | } | |
487 | ||
488 | virtual wxString GetColumnType( unsigned int col ) const | |
489 | { | |
490 | if (col == 1) | |
491 | return wxT("wxDataViewIconText"); | |
492 | ||
493 | return wxT("string"); | |
494 | } | |
495 | ||
496 | virtual unsigned int GetRowCount() | |
497 | { | |
498 | return m_array.GetCount(); | |
499 | } | |
500 | ||
501 | virtual void GetValue( wxVariant &variant, | |
502 | unsigned int row, unsigned int col ) const | |
503 | { | |
504 | if (col==0) | |
505 | { | |
506 | if (row >= m_array.GetCount()) | |
507 | { | |
508 | wxString str; | |
509 | str.Printf(wxT("row %d"), row - m_array.GetCount() ); | |
510 | variant = str; | |
511 | } | |
512 | else | |
513 | { | |
514 | variant = m_array[ row ]; | |
515 | } | |
516 | } else | |
517 | if (col==1) | |
518 | { | |
519 | wxDataViewIconText data( wxT("test"), m_icon ); | |
520 | variant << data; | |
521 | } else | |
522 | if (col==2) | |
523 | { | |
524 | if (row >= m_array.GetCount()) | |
525 | variant = wxT("plain"); | |
526 | else | |
527 | variant = wxT("blue"); | |
528 | } | |
529 | } | |
530 | ||
531 | virtual bool GetAttr( unsigned int row, unsigned int col, wxDataViewItemAttr &attr ) | |
532 | { | |
533 | if (col != 2) | |
534 | return false; | |
535 | ||
536 | if (row < m_array.GetCount()) | |
537 | { | |
538 | attr.SetColour( *wxBLUE ); | |
539 | attr.SetItalic( true ); | |
540 | } | |
541 | ||
542 | return true; | |
543 | } | |
544 | ||
545 | virtual bool SetValue( const wxVariant &variant, | |
546 | unsigned int row, unsigned int col ) | |
547 | { | |
548 | if (col == 0) | |
549 | { | |
550 | if (row >= m_array.GetCount()) | |
551 | return false; | |
552 | ||
553 | m_array[row] = variant.GetString(); | |
554 | return true; | |
555 | } | |
556 | ||
557 | return false; | |
558 | } | |
559 | ||
560 | wxArrayString m_array; | |
561 | wxIcon m_icon; | |
562 | int m_virtualItems; | |
563 | }; | |
564 | ||
565 | // ------------------------------------- | |
566 | // MyApp | |
567 | // ------------------------------------- | |
568 | ||
569 | class MyApp: public wxApp | |
570 | { | |
571 | public: | |
572 | bool OnInit(void); | |
573 | int OnExit(); | |
574 | }; | |
575 | ||
576 | // ------------------------------------- | |
577 | // MyFrame | |
578 | // ------------------------------------- | |
579 | ||
580 | class MyFrame : public wxFrame | |
581 | { | |
582 | public: | |
583 | MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h); | |
584 | ~MyFrame(); | |
585 | ||
586 | public: | |
587 | void OnQuit(wxCommandEvent& event); | |
588 | void OnAbout(wxCommandEvent& event); | |
589 | ||
590 | void OnAddMozart(wxCommandEvent& event); | |
591 | void OnDeleteMusic(wxCommandEvent& event); | |
592 | void OnDeleteYear(wxCommandEvent& event); | |
593 | ||
594 | void OnPrependList(wxCommandEvent& event); | |
595 | void OnDeleteList(wxCommandEvent& event); | |
596 | ||
597 | void OnValueChanged( wxDataViewEvent &event ); | |
598 | ||
599 | void OnActivated( wxDataViewEvent &event ); | |
600 | void OnExpanding( wxDataViewEvent &event ); | |
601 | void OnExpanded( wxDataViewEvent &event ); | |
602 | void OnCollapsing( wxDataViewEvent &event ); | |
603 | void OnCollapsed( wxDataViewEvent &event ); | |
604 | void OnSelectionChanged( wxDataViewEvent &event ); | |
605 | ||
606 | void OnEditingStarted( wxDataViewEvent &event ); | |
607 | void OnEditingDone( wxDataViewEvent &event ); | |
608 | ||
609 | void OnHeaderClick( wxDataViewEvent &event ); | |
610 | void OnHeaderRightClick( wxDataViewEvent &event ); | |
611 | void OnSorted( wxDataViewEvent &event ); | |
612 | ||
613 | void OnContextMenu( wxDataViewEvent &event ); | |
614 | ||
615 | void OnRightClick( wxMouseEvent &event ); | |
616 | void OnGoto( wxCommandEvent &event); | |
617 | void OnAddMany( wxCommandEvent &event); | |
618 | ||
619 | private: | |
620 | wxDataViewCtrl* m_musicCtrl; | |
621 | wxObjectDataPtr<MyMusicModel> m_music_model; | |
622 | ||
623 | wxDataViewCtrl* m_listCtrl; | |
624 | wxObjectDataPtr<MyListModel> m_list_model; | |
625 | ||
626 | wxDataViewColumn * m_col; | |
627 | ||
628 | wxTextCtrl * m_log; | |
629 | wxLog *m_logOld; | |
630 | ||
631 | private: | |
632 | DECLARE_EVENT_TABLE() | |
633 | }; | |
634 | ||
635 | // ------------------------------------- | |
636 | // MyApp | |
637 | // ------------------------------------- | |
638 | ||
639 | IMPLEMENT_APP(MyApp) | |
640 | ||
641 | bool MyApp::OnInit(void) | |
642 | { | |
643 | if ( !wxApp::OnInit() ) | |
644 | return false; | |
645 | ||
646 | // build the first frame | |
647 | MyFrame *frame = | |
648 | new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 40, 40, 800, 540); | |
649 | frame->Show(true); | |
650 | ||
651 | SetTopWindow(frame); | |
652 | return true; | |
653 | } | |
654 | ||
655 | int MyApp::OnExit() | |
656 | { | |
657 | return 0; | |
658 | } | |
659 | ||
660 | ||
661 | // ------------------------------------- | |
662 | // MyFrame | |
663 | // ------------------------------------- | |
664 | ||
665 | enum | |
666 | { | |
667 | // file menu | |
668 | ID_ABOUT = wxID_ABOUT, | |
669 | ID_EXIT = wxID_EXIT, | |
670 | ||
671 | ID_MUSIC_CTRL = 50, | |
672 | ||
673 | ID_ADD_MOZART = 100, | |
674 | ID_DELETE_MUSIC = 101, | |
675 | ID_DELETE_YEAR = 102, | |
676 | ||
677 | ID_PREPEND_LIST = 200, | |
678 | ID_DELETE_LIST = 201, | |
679 | ID_GOTO = 202, | |
680 | ID_ADD_MANY = 203 | |
681 | }; | |
682 | ||
683 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
684 | EVT_MENU( ID_ABOUT, MyFrame::OnAbout ) | |
685 | EVT_MENU( ID_EXIT, MyFrame::OnQuit ) | |
686 | EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart ) | |
687 | EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic ) | |
688 | EVT_BUTTON( ID_DELETE_YEAR, MyFrame::OnDeleteYear ) | |
689 | EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList ) | |
690 | EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList ) | |
691 | EVT_BUTTON( ID_GOTO, MyFrame::OnGoto) | |
692 | EVT_BUTTON( ID_ADD_MANY, MyFrame::OnAddMany) | |
693 | ||
694 | EVT_DATAVIEW_ITEM_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged ) | |
695 | ||
696 | EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated ) | |
697 | EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding) | |
698 | EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded) | |
699 | EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing) | |
700 | EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed) | |
701 | EVT_DATAVIEW_SELECTION_CHANGED(ID_MUSIC_CTRL, MyFrame::OnSelectionChanged) | |
702 | ||
703 | EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted) | |
704 | EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone) | |
705 | ||
706 | EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick) | |
707 | EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick) | |
708 | EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted) | |
709 | ||
710 | EVT_DATAVIEW_ITEM_CONTEXT_MENU(ID_MUSIC_CTRL, MyFrame::OnContextMenu) | |
711 | ||
712 | EVT_RIGHT_UP(MyFrame::OnRightClick) | |
713 | END_EVENT_TABLE() | |
714 | ||
715 | MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int h): | |
716 | wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) | |
717 | { | |
718 | m_log = NULL; | |
719 | m_col = NULL; | |
720 | ||
721 | SetIcon(wxICON(sample)); | |
722 | ||
723 | // build the menus: | |
724 | ||
725 | wxMenu *file_menu = new wxMenu; | |
726 | file_menu->Append(ID_ABOUT, wxT("&About")); | |
727 | file_menu->AppendSeparator(); | |
728 | file_menu->Append(ID_EXIT, wxT("E&xit")); | |
729 | ||
730 | wxMenuBar *menu_bar = new wxMenuBar; | |
731 | menu_bar->Append(file_menu, wxT("&File")); | |
732 | ||
733 | SetMenuBar(menu_bar); | |
734 | CreateStatusBar(); | |
735 | ||
736 | wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL ); | |
737 | ||
738 | wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL ); | |
739 | ||
740 | // MyMusic | |
741 | ||
742 | m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition, | |
743 | wxDefaultSize, wxDV_MULTIPLE ); | |
744 | ||
745 | m_music_model = new MyMusicModel; | |
746 | m_musicCtrl->AssociateModel( m_music_model.get() ); | |
747 | ||
748 | #if 0 | |
749 | // Call this and sorting is enabled | |
750 | // immediatly upon start up. | |
751 | wxDataViewColumn *col = m_musicCtrl->AppendTextColumn( wxT("Title"), 0, wxDATAVIEW_CELL_INERT, 200, | |
752 | DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE); | |
753 | col->SetSortOrder( true ); | |
754 | #else | |
755 | m_musicCtrl->AppendTextColumn(wxT("Title"),0,wxDATAVIEW_CELL_INERT,200,DEFAULT_ALIGN,wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE); | |
756 | #endif | |
757 | ||
758 | m_musicCtrl->AppendTextColumn( wxT("Artist"), 1, wxDATAVIEW_CELL_EDITABLE, 150, | |
759 | DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE); | |
760 | ||
761 | wxDataViewSpinRenderer *sr = new wxDataViewSpinRenderer( 0, 2010 ); | |
762 | wxDataViewColumn *column = new wxDataViewColumn( wxT("year"), sr, 2, -1, wxALIGN_CENTRE, wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE); | |
763 | m_musicCtrl->AppendColumn( column ); | |
764 | ||
765 | data_sizer->Add( m_musicCtrl, 3, wxGROW ); | |
766 | ||
767 | #if 1 | |
768 | ||
769 | // MyList | |
770 | ||
771 | m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, | |
772 | wxDefaultSize, wxDV_MULTIPLE | wxDV_ROW_LINES); | |
773 | ||
774 | m_list_model = new MyListModel; | |
775 | m_listCtrl->AssociateModel( m_list_model.get() ); | |
776 | ||
777 | m_listCtrl->AppendTextColumn (wxT("editable string"), 0, wxDATAVIEW_CELL_EDITABLE, 120 ); | |
778 | m_listCtrl->AppendIconTextColumn(wxT("icon"), 1, wxDATAVIEW_CELL_INERT, 60 ); | |
779 | ||
780 | wxDataViewTextRendererAttr *ra = new wxDataViewTextRendererAttr; | |
781 | column = new wxDataViewColumn(wxT("attributes"), ra, 2 ); | |
782 | m_listCtrl->AppendColumn( column ); | |
783 | ||
784 | data_sizer->Add( m_listCtrl, 2, wxGROW ); | |
785 | ||
786 | #endif | |
787 | ||
788 | main_sizer->Add( data_sizer, 2, wxGROW ); | |
789 | ||
790 | wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL ); | |
791 | ||
792 | button_sizer->Add( new wxButton( this, ID_ADD_MOZART, _("Add Mozart")), 0, wxALL, 10 ); | |
793 | button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC,_("Delete selected")), 0, wxALL, 10 ); | |
794 | button_sizer->Add( new wxButton( this, ID_DELETE_YEAR, _("Delete \"Year\" column")), 0, wxALL, 10 ); | |
795 | button_sizer->Add( 10, 10, 1 ); | |
796 | wxFlexGridSizer *grid_sizer = new wxFlexGridSizer( 2, 2 ); | |
797 | grid_sizer->Add( new wxButton( this, ID_PREPEND_LIST,_("Prepend")), 0, wxALL, 2 ); | |
798 | grid_sizer->Add( new wxButton( this, ID_DELETE_LIST, _("Delete selected")), 0, wxALL, 2 ); | |
799 | grid_sizer->Add( new wxButton( this, ID_GOTO, _("Goto 50")), 0, wxALL, 2 ); | |
800 | grid_sizer->Add( new wxButton( this, ID_ADD_MANY, _("Add 1000")), 0, wxALL, 2 ); | |
801 | button_sizer->Add( grid_sizer, 0, wxALL, 10 ); | |
802 | ||
803 | main_sizer->Add( button_sizer, 0, wxGROW, 0 ); | |
804 | ||
805 | wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL ); | |
806 | ||
807 | m_log = new wxTextCtrl( this, -1, wxString(), wxDefaultPosition, wxSize(100,200), wxTE_MULTILINE ); | |
808 | m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log)); | |
809 | wxLogMessage(_("This is the log window")); | |
810 | ||
811 | bottom_sizer->Add( m_log, 1, wxGROW ); | |
812 | ||
813 | // wxDataViewTreeStore | |
814 | ||
815 | wxDataViewCtrl *treectrl = new wxDataViewCtrl( this, -1, | |
816 | wxDefaultPosition, wxSize(100,200), wxDV_NO_HEADER ); | |
817 | ||
818 | wxDataViewTreeStore *store = new wxDataViewTreeStore; | |
819 | wxDataViewItem parent = store->AppendContainer( wxDataViewItem(0),wxT("Root 1"), wxIcon(small1_xpm) ); | |
820 | wxDataViewItem child = store->AppendItem( parent,wxT("Child 1"), wxIcon(small1_xpm) ); | |
821 | child = store->AppendItem( parent,wxT("Child 2"), wxIcon(small1_xpm) ); | |
822 | child = store->AppendItem( parent,wxT("Child 3, very long, long, long, long"), wxIcon(small1_xpm) ); | |
823 | treectrl->AssociateModel( store ); | |
824 | store->DecRef(); | |
825 | ||
826 | treectrl->AppendIconTextColumn(wxT("no label"), 0, wxDATAVIEW_CELL_INERT, -1 ); | |
827 | ||
828 | bottom_sizer->Add( treectrl, 1 ); | |
829 | ||
830 | // wxDataViewTreeCtrl | |
831 | ||
832 | wxDataViewTreeCtrl *treectrl2 = new wxDataViewTreeCtrl( this, -1, wxDefaultPosition, wxSize(100,200) ); | |
833 | ||
834 | wxImageList *ilist = new wxImageList( 16, 16 ); | |
835 | ilist->Add( wxIcon(small1_xpm) ); | |
836 | treectrl2->SetImageList( ilist ); | |
837 | ||
838 | parent = treectrl2->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 ); | |
839 | child = treectrl2->AppendItem( parent,wxT("Child 1"), 0 ); | |
840 | child = treectrl2->AppendItem( parent,wxT("Child 2"), 0 ); | |
841 | child = treectrl2->AppendItem( parent,wxT("Child 3, very long, long, long, long"), 0 ); | |
842 | ||
843 | bottom_sizer->Add( treectrl2, 1 ); | |
844 | ||
845 | // main sizer | |
846 | ||
847 | main_sizer->Add( bottom_sizer, 0, wxGROW ); | |
848 | ||
849 | SetSizer( main_sizer ); | |
850 | } | |
851 | ||
852 | MyFrame::~MyFrame() | |
853 | { | |
854 | delete wxLog::SetActiveTarget(m_logOld); | |
855 | } | |
856 | ||
857 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) | |
858 | { | |
859 | Close(true); | |
860 | } | |
861 | ||
862 | void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) ) | |
863 | { | |
864 | m_music_model->AddToClassical( wxT("Kleine Nachtmusik"), wxT("Wolfgang Mozart"), 1787 ); | |
865 | } | |
866 | ||
867 | void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) ) | |
868 | { | |
869 | wxDataViewItemArray items; | |
870 | int len = m_musicCtrl->GetSelections( items ); | |
871 | for( int i = 0; i < len; i ++ ) | |
872 | if (items[i].IsOk()) | |
873 | m_music_model->Delete( items[i] ); | |
874 | } | |
875 | ||
876 | void MyFrame::OnDeleteYear( wxCommandEvent& WXUNUSED(event) ) | |
877 | { | |
878 | m_musicCtrl->DeleteColumn( m_musicCtrl->GetColumn( 2 ) ); | |
879 | FindWindow( ID_DELETE_YEAR )->Disable(); | |
880 | } | |
881 | ||
882 | void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) ) | |
883 | { | |
884 | m_list_model->Prepend(wxT("Test")); | |
885 | } | |
886 | ||
887 | void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) ) | |
888 | { | |
889 | wxDataViewItemArray items; | |
890 | int len = m_listCtrl->GetSelections( items ); | |
891 | if (len > 0) | |
892 | m_list_model->DeleteItems( items ); | |
893 | } | |
894 | ||
895 | void MyFrame::OnValueChanged( wxDataViewEvent &event ) | |
896 | { | |
897 | if (!m_log) | |
898 | return; | |
899 | ||
900 | wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"), event.GetItem().GetID(), event.GetColumn() ); | |
901 | } | |
902 | ||
903 | void MyFrame::OnActivated( wxDataViewEvent &event ) | |
904 | { | |
905 | if(!m_log) | |
906 | return; | |
907 | ||
908 | wxString title = m_music_model->GetTitle( event.GetItem() ); | |
909 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title.GetData()); | |
910 | } | |
911 | ||
912 | void MyFrame::OnSelectionChanged( wxDataViewEvent &event ) | |
913 | { | |
914 | if(!m_log) | |
915 | return; | |
916 | ||
917 | wxString title = m_music_model->GetTitle( event.GetItem() ); | |
918 | if (title.empty()) | |
919 | title = wxT("None"); | |
920 | ||
921 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title.GetData() ); | |
922 | } | |
923 | ||
924 | void MyFrame::OnExpanding( wxDataViewEvent &event ) | |
925 | { | |
926 | if (!m_log) | |
927 | return; | |
928 | ||
929 | wxString title = m_music_model->GetTitle( event.GetItem() ); | |
930 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title.GetData() ); | |
931 | } | |
932 | ||
933 | ||
934 | void MyFrame::OnEditingStarted( wxDataViewEvent &event ) | |
935 | { | |
936 | if (!m_log) | |
937 | return; | |
938 | ||
939 | wxString title = m_music_model->GetTitle( event.GetItem() ); | |
940 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title.GetData() ); | |
941 | } | |
942 | ||
943 | void MyFrame::OnEditingDone( wxDataViewEvent &event ) | |
944 | { | |
945 | if (!m_log) | |
946 | return; | |
947 | ||
948 | wxString title = m_music_model->GetTitle( event.GetItem() ); | |
949 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title.GetData() ); | |
950 | } | |
951 | ||
952 | void MyFrame::OnExpanded( wxDataViewEvent &event ) | |
953 | { | |
954 | if (!m_log) | |
955 | return; | |
956 | ||
957 | wxString title = m_music_model->GetTitle( event.GetItem() ); | |
958 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title.GetData() ); | |
959 | } | |
960 | ||
961 | void MyFrame::OnCollapsing( wxDataViewEvent &event ) | |
962 | { | |
963 | if (!m_log) | |
964 | return; | |
965 | ||
966 | wxString title = m_music_model->GetTitle( event.GetItem() ); | |
967 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title.GetData() ); | |
968 | } | |
969 | ||
970 | void MyFrame::OnCollapsed( wxDataViewEvent &event ) | |
971 | { | |
972 | if (!m_log) | |
973 | return; | |
974 | ||
975 | wxString title = m_music_model->GetTitle( event.GetItem() ); | |
976 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title.GetData()); | |
977 | } | |
978 | ||
979 | void MyFrame::OnContextMenu( wxDataViewEvent &event ) | |
980 | { | |
981 | if (!m_log) | |
982 | return; | |
983 | ||
984 | wxString title = m_music_model->GetTitle( event.GetItem() ); | |
985 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title.GetData()); | |
986 | } | |
987 | ||
988 | void MyFrame::OnHeaderClick( wxDataViewEvent &event ) | |
989 | { | |
990 | if(!m_log) | |
991 | return; | |
992 | ||
993 | int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() ); | |
994 | ||
995 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos ); | |
996 | } | |
997 | ||
998 | void MyFrame::OnHeaderRightClick( wxDataViewEvent &event ) | |
999 | { | |
1000 | if(!m_log) | |
1001 | return; | |
1002 | ||
1003 | int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() ); | |
1004 | ||
1005 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos ); | |
1006 | } | |
1007 | ||
1008 | void MyFrame::OnSorted( wxDataViewEvent &event ) | |
1009 | { | |
1010 | if(!m_log) | |
1011 | return; | |
1012 | ||
1013 | int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() ); | |
1014 | ||
1015 | wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos ); | |
1016 | } | |
1017 | ||
1018 | void MyFrame::OnRightClick( wxMouseEvent &event ) | |
1019 | { | |
1020 | if(!m_log) | |
1021 | return; | |
1022 | ||
1023 | wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"), event.GetX(), event.GetY()); | |
1024 | } | |
1025 | ||
1026 | void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event)) | |
1027 | { | |
1028 | wxDataViewItem item = m_list_model->GetItem( 50 ); | |
1029 | m_listCtrl->EnsureVisible(item,m_col); | |
1030 | } | |
1031 | ||
1032 | void MyFrame::OnAddMany(wxCommandEvent& WXUNUSED(event)) | |
1033 | { | |
1034 | m_list_model->AddMany(); | |
1035 | } | |
1036 | ||
1037 | ||
1038 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) | |
1039 | { | |
1040 | wxAboutDialogInfo info; | |
1041 | info.SetName(_("DataView sample")); | |
1042 | info.SetDescription(_("This sample demonstrates the dataview control handling")); | |
1043 | info.SetCopyright(_T("(C) 2007 Robert Roebling")); | |
1044 | ||
1045 | wxAboutBox(info); | |
1046 | } | |
1047 |