X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9a83f860948059b0273b5cc6d9e43fadad3ebfca..f46e6a1ef9541a3d13c38a114870658d3b4e004b:/samples/listctrl/listtest.cpp diff --git a/samples/listctrl/listtest.cpp b/samples/listctrl/listtest.cpp index 96afe78e75..052260bff0 100644 --- a/samples/listctrl/listtest.cpp +++ b/samples/listctrl/listtest.cpp @@ -6,7 +6,7 @@ // Created: 04/01/98 // RCS-ID: $Id$ // Copyright: (c) Julian Smart -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// // For compilers that support precompilation, includes "wx/wx.h". @@ -20,11 +20,11 @@ #include "wx/wx.h" #endif -#if !defined(__WXMSW__) && !defined(__WXPM__) - #include "mondrian.xpm" +#ifndef wxHAS_IMAGES_IN_RESOURCES + #include "../sample.xpm" #endif -#ifndef __WXMSW__ +#ifndef wxHAS_IMAGES_IN_RESOURCES #include "bitmaps/toolbrai.xpm" #include "bitmaps/toolchar.xpm" #include "bitmaps/tooldata.xpm" @@ -70,13 +70,13 @@ const wxChar *SMALL_VIRTUAL_VIEW_ITEMS[][2] = static const int NUM_ICONS = 9; int wxCALLBACK -MyCompareFunction(long item1, long item2, wxIntPtr WXUNUSED(sortData)) +MyCompareFunction(wxIntPtr item1, wxIntPtr item2, wxIntPtr WXUNUSED(sortData)) { // inverse the order if (item1 < item2) - return -1; - if (item1 > item2) return 1; + if (item1 > item2) + return -1; return 0; } @@ -100,8 +100,6 @@ bool MyApp::OnInit() // Show the frame frame->Show(true); - SetTopWindow(frame); - return true; } @@ -139,6 +137,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(LIST_SORT, MyFrame::OnSort) EVT_MENU(LIST_SET_FG_COL, MyFrame::OnSetFgColour) EVT_MENU(LIST_SET_BG_COL, MyFrame::OnSetBgColour) + EVT_MENU(LIST_ROW_LINES, MyFrame::OnSetRowLines) EVT_MENU(LIST_TOGGLE_MULTI_SEL, MyFrame::OnToggleMultiSel) EVT_MENU(LIST_SHOW_COL_INFO, MyFrame::OnShowColInfo) EVT_MENU(LIST_SHOW_SEL_INFO, MyFrame::OnShowSelInfo) @@ -150,13 +149,19 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze) EVT_MENU(LIST_THAW, MyFrame::OnThaw) EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines) + EVT_MENU(LIST_TOGGLE_HEADER, MyFrame::OnToggleHeader) + EVT_MENU(LIST_TOGGLE_BELL, MyFrame::OnToggleBell) #ifdef __WXOSX__ EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric) #endif // __WXOSX__ EVT_MENU(LIST_FIND, MyFrame::OnFind) - EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo) + EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateUIEnableInReport) + EVT_UPDATE_UI(LIST_TOGGLE_HEADER, MyFrame::OnUpdateUIEnableInReport) + EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel) + EVT_UPDATE_UI(LIST_TOGGLE_HEADER, MyFrame::OnUpdateToggleHeader) + EVT_UPDATE_UI(LIST_ROW_LINES, MyFrame::OnUpdateRowLines) END_EVENT_TABLE() // My frame constructor @@ -169,13 +174,13 @@ MyFrame::MyFrame(const wxChar *title) m_numListItems = 10; // Give it an icon - SetIcon( wxICON(mondrian) ); + SetIcon(wxICON(sample)); // Make an image list containing large icons m_imageListNormal = new wxImageList(32, 32, true); m_imageListSmall = new wxImageList(16, 16, true); -#ifdef __WXMSW__ +#ifdef wxHAS_IMAGES_IN_RESOURCES m_imageListNormal->Add( wxIcon(wxT("icon1"), wxBITMAP_TYPE_ICO_RESOURCE) ); m_imageListNormal->Add( wxIcon(wxT("icon2"), wxBITMAP_TYPE_ICO_RESOURCE) ); m_imageListNormal->Add( wxIcon(wxT("icon3"), wxBITMAP_TYPE_ICO_RESOURCE) ); @@ -251,12 +256,17 @@ MyFrame::MyFrame(const wxChar *title) menuList->Append(LIST_THAW, wxT("Tha&w\tCtrl-W")); menuList->AppendSeparator(); menuList->AppendCheckItem(LIST_TOGGLE_LINES, wxT("Toggle &lines\tCtrl-I")); - menuList->Append(LIST_TOGGLE_MULTI_SEL, wxT("&Multiple selection\tCtrl-M"), - wxT("Toggle multiple selection"), true); + menuList->AppendCheckItem(LIST_TOGGLE_MULTI_SEL, + wxT("&Multiple selection\tCtrl-M")); + menuList->Check(LIST_TOGGLE_MULTI_SEL, true); + menuList->AppendCheckItem(LIST_TOGGLE_HEADER, "Toggle &header\tCtrl-H"); + menuList->Check(LIST_TOGGLE_HEADER, true); + menuList->AppendCheckItem(LIST_TOGGLE_BELL, "Toggle &bell on no match"); wxMenu *menuCol = new wxMenu; menuCol->Append(LIST_SET_FG_COL, wxT("&Foreground colour...")); menuCol->Append(LIST_SET_BG_COL, wxT("&Background colour...")); + menuCol->AppendCheckItem(LIST_ROW_LINES, wxT("Alternating colours")); wxMenuBar *menubar = new wxMenuBar; menubar->Append(menuFile, wxT("&File")); @@ -276,7 +286,7 @@ MyFrame::MyFrame(const wxChar *title) #ifdef __WXMSW__ // this is useful to know specially when debugging :) - wxLogMessage("Your version of comctl32.dll is: %d", + wxLogMessage("Your version of comctl32.dll is: %d", wxApp::GetComCtl32Version()); #endif @@ -330,7 +340,7 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) { wxMessageDialog dialog(this, wxT("List test sample\nJulian Smart (c) 1997"), - wxT("About list test"), wxOK|wxCANCEL); + wxT("About list test")); dialog.ShowModal(); } @@ -354,6 +364,18 @@ void MyFrame::OnToggleLines(wxCommandEvent& event) m_listCtrl->SetSingleStyle(wxLC_HRULES | wxLC_VRULES, event.IsChecked()); } +void MyFrame::OnToggleHeader(wxCommandEvent& event) +{ + wxLogMessage("%s the header", event.IsChecked() ? "Showing" : "Hiding"); + + m_listCtrl->ToggleWindowStyle(wxLC_NO_HEADER); +} + +void MyFrame::OnToggleBell(wxCommandEvent& event) +{ + m_listCtrl->EnableBellOnNoMatch(event.IsChecked()); +} + #ifdef __WXOSX__ void MyFrame::OnToggleMacUseGeneric(wxCommandEvent& event) @@ -456,10 +478,16 @@ void MyFrame::RecreateList(long flags, bool withText) default: wxFAIL_MSG( wxT("unknown listctrl mode") ); } + + wxMenuBar* const mb = GetMenuBar(); + if ( mb ) + m_listCtrl->EnableBellOnNoMatch(mb->IsChecked(LIST_TOGGLE_BELL)); } DoSize(); + GetMenuBar()->Check(LIST_ROW_LINES, false); + m_logWindow->Clear(); } @@ -559,8 +587,14 @@ void MyFrame::InitWithIconItems(bool withText, bool sameIcon) if ( withText ) { - m_listCtrl->InsertItem(i, wxString::Format(wxT("Label %d"), i), - image); + // Make labels of different widths to test the layout. + wxString label; + if ( !(i % 5) ) + label.Printf("Longer label %d", i); + else + label.Printf("Label %d", i); + + m_listCtrl->InsertItem(i, label, image); } else { @@ -632,14 +666,14 @@ void MyFrame::InitWithVirtualItems() if ( m_smallVirtual ) { - m_listCtrl->InsertColumn(0, wxT("Animal")); - m_listCtrl->InsertColumn(1, wxT("Sound")); + m_listCtrl->AppendColumn(wxT("Animal")); + m_listCtrl->AppendColumn(wxT("Sound")); m_listCtrl->SetItemCount(WXSIZEOF(SMALL_VIRTUAL_VIEW_ITEMS)); } else { - m_listCtrl->InsertColumn(0, wxT("First Column")); - m_listCtrl->InsertColumn(1, wxT("Second Column")); + m_listCtrl->AppendColumn(wxT("First Column")); + m_listCtrl->AppendColumn(wxT("Second Column")); m_listCtrl->SetColumnWidth(0, 150); m_listCtrl->SetColumnWidth(1, 150); m_listCtrl->SetItemCount(1000000); @@ -778,7 +812,7 @@ void MyFrame::OnShowColInfo(wxCommandEvent& WXUNUSED(event)) } } -void MyFrame::OnUpdateShowColInfo(wxUpdateUIEvent& event) +void MyFrame::OnUpdateUIEnableInReport(wxUpdateUIEvent& event) { event.Enable( (m_listCtrl->GetWindowStyleFlag() & wxLC_REPORT) != 0 ); } @@ -799,7 +833,17 @@ void MyFrame::OnToggleMultiSel(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnUpdateToggleMultiSel(wxUpdateUIEvent& event) { - event.Check((m_listCtrl->GetWindowStyleFlag() & wxLC_SINGLE_SEL) == 0); + event.Check(!m_listCtrl->HasFlag(wxLC_SINGLE_SEL)); +} + +void MyFrame::OnUpdateToggleHeader(wxUpdateUIEvent& event) +{ + event.Check(!m_listCtrl->HasFlag(wxLC_NO_HEADER)); +} + +void MyFrame::OnUpdateRowLines(wxUpdateUIEvent& event) +{ + event.Enable(m_listCtrl->HasFlag(wxLC_VIRTUAL)); } void MyFrame::OnSetFgColour(wxCommandEvent& WXUNUSED(event)) @@ -814,6 +858,12 @@ void MyFrame::OnSetBgColour(wxCommandEvent& WXUNUSED(event)) m_listCtrl->Refresh(); } +void MyFrame::OnSetRowLines(wxCommandEvent& event) +{ + m_listCtrl->EnableAlternateRowColours(event.IsChecked()); + m_listCtrl->Refresh(); +} + void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event)) { m_listCtrl->InsertItem(m_listCtrl->GetItemCount(), wxT("Appended item")); @@ -821,16 +871,26 @@ void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnEdit(wxCommandEvent& WXUNUSED(event)) { - long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, - wxLIST_STATE_FOCUSED); - - if ( itemCur != -1 ) + // demonstrate cancelling editing: this currently is wxMSW-only +#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) + if ( m_listCtrl->GetEditControl() ) { - m_listCtrl->EditLabel(itemCur); + m_listCtrl->EndEditLabel(true); } - else + else // start editing +#endif // __WXMSW__ { - m_logWindow->WriteText(wxT("No item to edit")); + long itemCur = m_listCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, + wxLIST_STATE_FOCUSED); + + if ( itemCur != -1 ) + { + m_listCtrl->EditLabel(itemCur); + } + else + { + m_logWindow->WriteText(wxT("No item to edit")); + } } } @@ -1063,6 +1123,13 @@ void MyListCtrl::OnListKeyDown(wxListEvent& event) { long item; + if ( !wxGetKeyState(WXK_SHIFT) ) + { + LogEvent(event, wxT("OnListKeyDown")); + event.Skip(); + return; + } + switch ( event.GetKeyCode() ) { case 'C': // colorize @@ -1117,8 +1184,8 @@ void MyListCtrl::OnListKeyDown(wxListEvent& event) } break; - case '1': // show sub item bounding rectangle - case '2': + case '1': // show sub item bounding rectangle for the given column + case '2': // (and icon/label rectangle if Shift/Ctrl is pressed) case '3': case '4': // this column is invalid but we want to test it too if ( InReportView() ) @@ -1126,7 +1193,14 @@ void MyListCtrl::OnListKeyDown(wxListEvent& event) int subItem = event.GetKeyCode() - '1'; item = event.GetIndex(); wxRect r; - if ( !GetSubItemRect(item, subItem, r) ) + + int code = wxLIST_RECT_BOUNDS; + if ( wxGetKeyState(WXK_SHIFT) ) + code = wxLIST_RECT_ICON; + else if ( wxGetKeyState(WXK_CONTROL) ) + code = wxLIST_RECT_LABEL; + + if ( !GetSubItemRect(item, subItem, r, code) ) { wxLogError(wxT("Failed to retrieve rect of item %ld column %d"), item, subItem + 1); break; @@ -1197,26 +1271,7 @@ void MyListCtrl::OnChar(wxKeyEvent& event) { wxLogMessage(wxT("Got char event.")); - switch ( event.GetKeyCode() ) - { - case 'n': - case 'N': - case 'c': - case 'C': - case 'r': - case 'R': - case 'u': - case 'U': - case 'd': - case 'D': - case 'i': - case 'I': - // these are the keys we process ourselves - break; - - default: - event.Skip(); - } + event.Skip(); } void MyListCtrl::OnRightClick(wxMouseEvent& event) @@ -1289,7 +1344,7 @@ wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const return &s_attrHighlight; } - return item % 2 ? NULL : (wxListItemAttr *)&m_attr; + return wxListCtrl::OnGetItemAttr(item); } void MyListCtrl::InsertItemInReportView(int i)