X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ea91ff8e339b0bfdff5a81161b5fb2e2b9441801..02fd8b9b6186623ed61019ac7e69ed9a4ef16773:/samples/dataview/dataview.cpp?ds=sidebyside diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index 4d24a1ea48..dc7fbb846d 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -74,15 +74,15 @@ static const char *small1_xpm[] = { /* Implement this data model - Title Artist Year -------------------------------------------------------------- + Title Artist Year Judgement +-------------------------------------------------------------------------- 1: My Music: 2: Pop music - 3: You are not alone Michael Jackson 1995 - 4: Take a bow Madonna 1994 + 3: You are not alone Michael Jackson 1995 good + 4: Take a bow Madonna 1994 good 5: Classical music - 6: Ninth Symphony Ludwig v. Beethoven 1824 - 7: German Requiem Johannes Brahms 1868 + 6: Ninth Symphony Ludwig v. Beethoven 1824 good + 7: German Requiem Johannes Brahms 1868 good */ @@ -100,6 +100,7 @@ public: m_title = title; m_artist = artist; m_year = year; + m_quality = "good"; m_isContainer = false; } @@ -136,6 +137,7 @@ public: wxString m_title; wxString m_artist; int m_year; + wxString m_quality; private: MyMusicModelNode *m_parent; @@ -255,7 +257,7 @@ public: virtual unsigned int GetColumnCount() const { - return 5; + return 6; } virtual wxString GetColumnType( unsigned int col ) const @@ -275,14 +277,15 @@ public: case 0: variant = node->m_title; break; case 1: variant = node->m_artist; break; case 2: variant = (long) node->m_year; break; - case 3: + case 3: variant = node->m_quality; break; + case 4: // wxMac doesn't conceal the popularity progress renderer, return 0 for containers if (IsContainer(item)) variant = (long) 0; else variant = (long) 80; // all music is very 80% popular break; - case 4: + case 5: // Make size of red square depend on year if (GetYear(item) < 1900) variant = (long) 35; @@ -313,6 +316,7 @@ public: case 0: node->m_title = variant.GetString(); return true; case 1: node->m_artist = variant.GetString(); return true; case 2: node->m_year = variant.GetLong(); return true; + case 3: node->m_quality = variant.GetString(); return true; default: wxLogError( wxT("MyMusicModel::SetValue: wrong column") ); } return false; @@ -526,7 +530,7 @@ public: return m_array.GetCount(); } - virtual void GetValue( wxVariant &variant, + virtual void GetValueByRow( wxVariant &variant, unsigned int row, unsigned int col ) const { if (col==0) @@ -556,7 +560,7 @@ public: } } - virtual bool GetAttr( unsigned int row, unsigned int col, wxDataViewItemAttr &attr ) + virtual bool GetAttrByRow( unsigned int row, unsigned int col, wxDataViewItemAttr &attr ) { if (col != 2) return false; @@ -570,7 +574,7 @@ public: return true; } - virtual bool SetValue( const wxVariant &variant, + virtual bool SetValueByRow( const wxVariant &variant, unsigned int row, unsigned int col ) { if (col == 0) @@ -626,7 +630,8 @@ public: virtual wxSize GetSize() const { - return wxSize(60,m_height); + //return wxSize(60,m_height); + return wxSize(60,20); } virtual bool SetValue( const wxVariant &value ) @@ -814,6 +819,8 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int SetMenuBar(menu_bar); CreateStatusBar(); + + wxPanel *panel = new wxPanel( this, -1 ); wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL ); @@ -821,7 +828,7 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int // MyMusic - m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition, + m_musicCtrl = new wxDataViewCtrl( panel, ID_MUSIC_CTRL, wxDefaultPosition, wxSize(400,200), wxDV_MULTIPLE|wxDV_VARIABLE_LINE_HEIGHT ); m_music_model = new MyMusicModel; @@ -829,38 +836,46 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int wxDataViewTextRenderer *tr = new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_INERT ); wxDataViewColumn *column0 = new wxDataViewColumn( wxT("title"), tr, 0, 200, wxALIGN_LEFT, - wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE ); + wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_RESIZABLE ); m_musicCtrl->AppendColumn( column0 ); #if 0 // Call this and sorting is enabled // immediatly upon start up. - column0->SetSortOrder( true ); + column0->SetAsSortKey(); #endif tr = new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE ); wxDataViewColumn *column1 = new wxDataViewColumn( wxT("artist"), tr, 1, 150, wxALIGN_LEFT, wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE ); + column1->SetMinWidth(150); // this column can't be resized to be smaller m_musicCtrl->AppendColumn( column1 ); -#if 1 wxDataViewSpinRenderer *sr = new wxDataViewSpinRenderer( 0, 2010, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT ); - wxDataViewColumn *column2 = new wxDataViewColumn( wxT("year"), sr, 2, 80, wxALIGN_LEFT, - wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE ); + wxDataViewColumn *column2 = new wxDataViewColumn( wxT("year"), sr, 2, 60, wxALIGN_LEFT, + wxDATAVIEW_COL_SORTABLE | wxDATAVIEW_COL_REORDERABLE ); m_musicCtrl->AppendColumn( column2 ); - m_musicCtrl->AppendProgressColumn( wxT("popularity"), 3, wxDATAVIEW_CELL_INERT, 80 ); + wxArrayString choices; + choices.Add( "good" ); + choices.Add( "bad" ); + choices.Add( "lousy" ); + wxDataViewChoiceRenderer *c = new wxDataViewChoiceRenderer( choices, wxDATAVIEW_CELL_EDITABLE, wxALIGN_RIGHT ); + wxDataViewColumn *column3 = new wxDataViewColumn( wxT("rating"), c, 3, 100, wxALIGN_LEFT, + wxDATAVIEW_COL_REORDERABLE | wxDATAVIEW_COL_RESIZABLE ); + m_musicCtrl->AppendColumn( column3 ); + + m_musicCtrl->AppendProgressColumn( wxT("popularity"), 4, wxDATAVIEW_CELL_INERT, 80 ); MyCustomRenderer *cr = new MyCustomRenderer( wxDATAVIEW_CELL_ACTIVATABLE, wxALIGN_RIGHT ); - wxDataViewColumn *column3 = new wxDataViewColumn( wxT("custom"), cr, 4, -1, wxALIGN_LEFT, + wxDataViewColumn *column4 = new wxDataViewColumn( wxT("custom"), cr, 5, -1, wxALIGN_LEFT, wxDATAVIEW_COL_RESIZABLE ); - m_musicCtrl->AppendColumn( column3 ); -#endif + m_musicCtrl->AppendColumn( column4 ); data_sizer->Add( m_musicCtrl, 3, wxGROW ); // MyList - m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, + m_listCtrl = new wxDataViewCtrl( panel, wxID_ANY, wxDefaultPosition, wxSize(400,200), wxDV_MULTIPLE | wxDV_ROW_LINES); m_list_model = new MyListModel; @@ -875,8 +890,8 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int #endif wxDataViewTextRendererAttr *ra = new wxDataViewTextRendererAttr; - wxDataViewColumn *column4 = new wxDataViewColumn(wxT("attributes"), ra, 2 ); - m_listCtrl->AppendColumn( column4 ); + wxDataViewColumn *column5 = new wxDataViewColumn(wxT("attributes"), ra, 2 ); + m_listCtrl->AppendColumn( column5 ); data_sizer->Add( m_listCtrl, 2, wxGROW ); @@ -884,68 +899,69 @@ MyFrame::MyFrame(wxFrame *frame, const wxString &title, int x, int y, int w, int wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL ); - button_sizer->Add( new wxButton( this, ID_ADD_MOZART, _("Add Mozart")), 0, wxALL, 10 ); - button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC,_("Delete selected")), 0, wxALL, 10 ); - button_sizer->Add( new wxButton( this, ID_DELETE_YEAR, _("Delete \"Year\" column")), 0, wxALL, 10 ); - button_sizer->Add( new wxButton( this, ID_SELECT_NINTH, _("Select Ninth")), 0, wxALL, 10 ); + button_sizer->Add( new wxButton( panel, ID_ADD_MOZART, _("Add Mozart")), 0, wxALL, 10 ); + button_sizer->Add( new wxButton( panel, ID_DELETE_MUSIC,_("Delete selected")), 0, wxALL, 10 ); + button_sizer->Add( new wxButton( panel, ID_DELETE_YEAR, _("Delete \"Year\" column")), 0, wxALL, 10 ); + button_sizer->Add( new wxButton( panel, ID_SELECT_NINTH, _("Select Ninth")), 0, wxALL, 10 ); button_sizer->Add( 10, 10, 1 ); wxFlexGridSizer *grid_sizer = new wxFlexGridSizer( 2, 2 ); - grid_sizer->Add( new wxButton( this, ID_PREPEND_LIST,_("Prepend")), 0, wxALL, 2 ); - grid_sizer->Add( new wxButton( this, ID_DELETE_LIST, _("Delete selected")), 0, wxALL, 2 ); - grid_sizer->Add( new wxButton( this, ID_GOTO, _("Goto 50")), 0, wxALL, 2 ); - grid_sizer->Add( new wxButton( this, ID_ADD_MANY, _("Add 1000")), 0, wxALL, 2 ); + grid_sizer->Add( new wxButton( panel, ID_PREPEND_LIST,_("Prepend")), 0, wxALL, 2 ); + grid_sizer->Add( new wxButton( panel, ID_DELETE_LIST, _("Delete selected")), 0, wxALL, 2 ); + grid_sizer->Add( new wxButton( panel, ID_GOTO, _("Goto 50")), 0, wxALL, 2 ); + grid_sizer->Add( new wxButton( panel, ID_ADD_MANY, _("Add 1000")), 0, wxALL, 2 ); button_sizer->Add( grid_sizer, 0, wxALL, 10 ); main_sizer->Add( button_sizer, 0, wxGROW, 0 ); wxBoxSizer *bottom_sizer = new wxBoxSizer( wxHORIZONTAL ); - m_log = new wxTextCtrl( this, -1, wxString(), wxDefaultPosition, wxSize(100,200), wxTE_MULTILINE ); + m_log = new wxTextCtrl( panel, -1, wxString(), wxDefaultPosition, wxSize(100,200), wxTE_MULTILINE ); m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log)); wxLogMessage(_("This is the log window")); bottom_sizer->Add( m_log, 1, wxGROW ); -#if 1 - // wxDataViewTreeStore - - wxDataViewCtrl *treectrl = new wxDataViewCtrl( this, -1, - wxDefaultPosition, wxSize(100,200), wxDV_NO_HEADER ); - - wxDataViewTreeStore *store = new wxDataViewTreeStore; - wxDataViewItem parent = store->AppendContainer( wxDataViewItem(0),wxT("Root 1"), wxIcon(small1_xpm) ); - wxDataViewItem child = store->AppendItem( parent,wxT("Child 1"), wxIcon(small1_xpm) ); - child = store->AppendItem( parent,wxT("Child 2"), wxIcon(small1_xpm) ); - child = store->AppendItem( parent,wxT("Child 3, very long, long, long, long"), wxIcon(small1_xpm) ); - treectrl->AssociateModel( store ); - store->DecRef(); - - treectrl->AppendIconTextColumn( wxT("no label"), 0, wxDATAVIEW_CELL_INERT, -1, (wxAlignment) 0, - wxDATAVIEW_COL_RESIZABLE ); - - bottom_sizer->Add( treectrl, 1 ); + // wxDataViewListCtrl + + wxDataViewListCtrl *listctrl = new wxDataViewListCtrl( panel, -1, + wxDefaultPosition, wxSize(100,200) ); + + listctrl->AppendToggleCol( wxT("Toggle") ); + listctrl->AppendTextCol( wxT("Text") ); + + wxVector data; + data.push_back( true ); + data.push_back( "row 1" ); + listctrl->AppendItem( data ); + + data.clear(); + data.push_back( false ); + data.push_back( "row 3" ); + listctrl->AppendItem( data ); + + bottom_sizer->Add( listctrl, 1 ); + // wxDataViewTreeCtrl - wxDataViewTreeCtrl *treectrl2 = new wxDataViewTreeCtrl( this, -1, wxDefaultPosition, wxSize(100,200) ); + wxDataViewTreeCtrl *treectrl2 = new wxDataViewTreeCtrl( panel, -1, wxDefaultPosition, wxSize(100,200) ); wxImageList *ilist = new wxImageList( 16, 16 ); ilist->Add( wxIcon(small1_xpm) ); treectrl2->SetImageList( ilist ); - parent = treectrl2->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 ); - child = treectrl2->AppendItem( parent,wxT("Child 1"), 0 ); - child = treectrl2->AppendItem( parent,wxT("Child 2"), 0 ); - child = treectrl2->AppendItem( parent,wxT("Child 3, very long, long, long, long"), 0 ); + wxDataViewItem parent2 = treectrl2->AppendContainer( wxDataViewItem(0),wxT("Root 1"), 0 ); + wxDataViewItem child2 = treectrl2->AppendItem( parent2, wxT("Child 1"), 0 ); + child2 = treectrl2->AppendItem( parent2, wxT("Child 2"), 0 ); + child2 = treectrl2->AppendItem( parent2, wxT("Child 3, very long, long, long, long"), 0 ); bottom_sizer->Add( treectrl2, 1 ); -#endif // main sizer main_sizer->Add( bottom_sizer, 0, wxGROW ); - SetSizer( main_sizer ); + panel->SetSizer( main_sizer ); } MyFrame::~MyFrame() @@ -996,18 +1012,12 @@ void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) ) m_list_model->DeleteItems( items ); } -#ifndef __WXMAC__ -#define ENABLE_LOGGING -#endif - void MyFrame::OnValueChanged( wxDataViewEvent &event ) { if (!m_log) return; -#ifdef ENABLE_LOGGING wxLogMessage( wxT("EVT_DATAVIEW_ITEM_VALUE_CHANGED, Item Id: %d; Column: %d"), event.GetItem().GetID(), event.GetColumn() ); -#endif } void MyFrame::OnActivated( wxDataViewEvent &event ) @@ -1016,9 +1026,10 @@ void MyFrame::OnActivated( wxDataViewEvent &event ) return; wxString title = m_music_model->GetTitle( event.GetItem() ); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item: %s"), title ); -#endif + + if (m_musicCtrl->IsExpanded( event.GetItem() )) + wxLogMessage(wxT("Item: %s is expanded"), title ); } void MyFrame::OnSelectionChanged( wxDataViewEvent &event ) @@ -1030,13 +1041,7 @@ void MyFrame::OnSelectionChanged( wxDataViewEvent &event ) if (title.empty()) title = wxT("None"); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, First selected Item: %s"), title ); -#else - wxString text; - text.Printf( wxT("wxEVT_COMMAND_DATAVIEW_SELECTION_CHANGED, Item: %s\n"), title ); - m_log->WriteText( text ); -#endif } void MyFrame::OnExpanding( wxDataViewEvent &event ) @@ -1045,13 +1050,7 @@ void MyFrame::OnExpanding( wxDataViewEvent &event ) return; wxString title = m_music_model->GetTitle( event.GetItem() ); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s"), title ); -#else - wxString text; - text.Printf( wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item: %s\n"), title ); - m_log->WriteText( text ); -#endif } @@ -1061,9 +1060,7 @@ void MyFrame::OnEditingStarted( wxDataViewEvent &event ) return; wxString title = m_music_model->GetTitle( event.GetItem() ); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item: %s"), title ); -#endif } void MyFrame::OnEditingDone( wxDataViewEvent &event ) @@ -1072,9 +1069,7 @@ void MyFrame::OnEditingDone( wxDataViewEvent &event ) return; wxString title = m_music_model->GetTitle( event.GetItem() ); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item: %s"), title ); -#endif } void MyFrame::OnExpanded( wxDataViewEvent &event ) @@ -1083,13 +1078,7 @@ void MyFrame::OnExpanded( wxDataViewEvent &event ) return; wxString title = m_music_model->GetTitle( event.GetItem() ); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s"), title ); -#else - wxString text; - text.Printf( wxT("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item: %s\n"), title ); - m_log->WriteText( text ); -#endif } void MyFrame::OnCollapsing( wxDataViewEvent &event ) @@ -1098,13 +1087,7 @@ void MyFrame::OnCollapsing( wxDataViewEvent &event ) return; wxString title = m_music_model->GetTitle( event.GetItem() ); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s"), title ); -#else - wxString text; - text.Printf( wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item: %s\n"), title ); - m_log->WriteText( text ); -#endif } void MyFrame::OnCollapsed( wxDataViewEvent &event ) @@ -1113,13 +1096,7 @@ void MyFrame::OnCollapsed( wxDataViewEvent &event ) return; wxString title = m_music_model->GetTitle( event.GetItem() ); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s"),title); -#else - wxString text; - text.Printf( wxT("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item: %s\n"), title ); - m_log->WriteText( text ); -#endif } void MyFrame::OnContextMenu( wxDataViewEvent &event ) @@ -1128,9 +1105,7 @@ void MyFrame::OnContextMenu( wxDataViewEvent &event ) return; wxString title = m_music_model->GetTitle( event.GetItem() ); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_ITEM_CONTEXT_MENU, Item: %s"),title ); -#endif wxMenu menu; menu.Append( 1, wxT("entry 1") ); @@ -1144,14 +1119,16 @@ void MyFrame::OnContextMenu( wxDataViewEvent &event ) void MyFrame::OnHeaderClick( wxDataViewEvent &event ) { + // we need to skip the event to let the default behaviour of sorting by + // this column when it is clicked to take place + event.Skip(); + if(!m_log) return; int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() ); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column position: %d"), pos ); -#endif } void MyFrame::OnHeaderRightClick( wxDataViewEvent &event ) @@ -1161,9 +1138,7 @@ void MyFrame::OnHeaderRightClick( wxDataViewEvent &event ) int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() ); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column position: %d"), pos ); -#endif } void MyFrame::OnSorted( wxDataViewEvent &event ) @@ -1173,9 +1148,7 @@ void MyFrame::OnSorted( wxDataViewEvent &event ) int pos = m_musicCtrl->GetColumnPosition( event.GetDataViewColumn() ); -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column position: %d"), pos ); -#endif } void MyFrame::OnRightClick( wxMouseEvent &event ) @@ -1183,9 +1156,7 @@ void MyFrame::OnRightClick( wxMouseEvent &event ) if(!m_log) return; -#ifdef ENABLE_LOGGING wxLogMessage(wxT("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d"), event.GetX(), event.GetY()); -#endif } void MyFrame::OnGoto(wxCommandEvent& WXUNUSED(event))