EVT_MENU(LIST_DESELECT_ALL, MyFrame::OnDeselectAll)
EVT_MENU(LIST_SELECT_ALL, MyFrame::OnSelectAll)
EVT_MENU(LIST_DELETE_ALL, MyFrame::OnDeleteAll)
+ EVT_MENU(LIST_SORT, MyFrame::OnSort)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(MyListCtrl, wxListCtrl)
EVT_LIST_ITEM_DESELECTED(LIST_CTRL, MyListCtrl::OnDeselected)
EVT_LIST_KEY_DOWN(LIST_CTRL, MyListCtrl::OnListKeyDown)
EVT_LIST_ITEM_ACTIVATED(LIST_CTRL, MyListCtrl::OnActivated)
+ EVT_LIST_COL_CLICK(LIST_CTRL, MyListCtrl::OnColClick)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
+int wxCALLBACK MyCompareFunction(long item1, long item2, long sortData)
+{
+ // inverse the order
+ return item1 < item2;
+}
+
// `Main program' equivalent, creating windows and returning main app frame
bool MyApp::OnInit(void)
{
file_menu->Append(LIST_DESELECT_ALL, "&Deselect All");
file_menu->Append(LIST_SELECT_ALL, "S&elect All");
file_menu->AppendSeparator();
+ file_menu->Append(LIST_SORT, "&Sort\tCtrl-S");
+ file_menu->AppendSeparator();
file_menu->Append(LIST_DELETE_ALL, "Delete &all items");
file_menu->AppendSeparator();
file_menu->Append(BUSY_ON, "&Busy cursor on");
// Make a panel with a message
frame->m_listCtrl = new MyListCtrl(frame, LIST_CTRL, wxPoint(0, 0), wxSize(400, 200),
- wxLC_LIST|wxSUNKEN_BORDER);
+ wxLC_LIST|wxSUNKEN_BORDER|wxLC_EDIT_LABELS );
// wxLC_LIST|wxLC_USER_TEXT|wxSUNKEN_BORDER); // wxLC_USER_TEXT requires app to supply all text on demand
frame->m_logWindow = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxSize(400, 200), wxTE_MULTILINE|wxSUNKEN_BORDER);
m_listCtrl->InsertColumn(1, "Column 2"); // , wxLIST_FORMAT_LEFT, 140);
m_listCtrl->InsertColumn(2, "One More Column (2)"); // , wxLIST_FORMAT_LEFT, 140);
- for ( int i = 0; i < 3000; i++ )
+ for ( int i = 0; i < 300; i++ )
{
wxChar buf[50];
wxSprintf(buf, _T("This is item %d"), i);
long tmp = m_listCtrl->InsertItem(i, buf, 0);
+ m_listCtrl->SetItemData(tmp, i);
wxSprintf(buf, _T("Col 1, item %d"), i);
tmp = m_listCtrl->SetItem(i, 1, buf);
tmp = m_listCtrl->SetItem(i, 2, buf);
}
+ // we leave all mask fields to 0 and only change the colour
+ wxListItem item;
+ item.m_itemId = 0;
+ item.SetTextColour(*wxRED);
+ m_listCtrl->SetItem( item );
+
+ item.m_itemId = 2;
+ item.SetTextColour(*wxGREEN);
+ m_listCtrl->SetItem( item );
+ item.m_itemId = 4;
+ item.SetTextColour(*wxLIGHT_GREY);
+ item.SetFont(*wxITALIC_FONT);
+ item.SetBackgroundColour(*wxRED);
+ m_listCtrl->SetItem( item );
+
+ m_listCtrl->SetTextColour(*wxBLUE);
+ m_listCtrl->SetBackgroundColour(*wxLIGHT_GREY);
+
m_listCtrl->SetColumnWidth( 0, wxLIST_AUTOSIZE );
m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
}
}
+void MyFrame::OnSort(wxCommandEvent& WXUNUSED(event))
+{
+ m_listCtrl->SortItems(MyCompareFunction, 0);
+}
+
void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event))
{
(void)wxGetElapsedTime(TRUE);
// MyListCtrl
-void MyListCtrl::OnBeginDrag(wxListEvent& WXUNUSED(event))
+void MyListCtrl::OnColClick(wxListEvent& event)
{
if ( !wxGetApp().GetTopWindow() )
return;
if ( !text )
return;
- text->WriteText("OnBeginDrag\n");
+ wxString msg;
+ msg.Printf( "OnColumnClick at %d.\n", event.GetColumn() );
+ text->WriteText(msg);
}
-void MyListCtrl::OnBeginRDrag(wxListEvent& WXUNUSED(event))
+void MyListCtrl::OnBeginDrag(wxListEvent& event)
{
if ( !wxGetApp().GetTopWindow() )
return;
wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
if ( !text )
return;
- text->WriteText("OnBeginRDrag\n");
+
+ wxString msg;
+ msg.Printf( "OnBeginDrag at %d,%d.\n", event.m_pointDrag.x, event.m_pointDrag.y );
+ text->WriteText(msg);
}
-void MyListCtrl::OnBeginLabelEdit(wxListEvent& WXUNUSED(event))
+void MyListCtrl::OnBeginRDrag(wxListEvent& event)
{
if ( !wxGetApp().GetTopWindow() )
return;
if ( !text )
return;
- text->WriteText("OnBeginLabelEdit\n");
+ wxString msg;
+ msg.Printf( "OnBeginRDrag at %d,%d.\n", event.m_pointDrag.x, event.m_pointDrag.y );
+ text->WriteText(msg);
}
-void MyListCtrl::OnEndLabelEdit(wxListEvent& WXUNUSED(event))
+void MyListCtrl::OnBeginLabelEdit(wxListEvent& event)
{
if ( !wxGetApp().GetTopWindow() )
return;
if ( !text )
return;
- text->WriteText("OnEndLabelEdit\n");
+ text->WriteText("OnBeginLabelEdit: ");
+ text->WriteText(event.m_item.m_text);
+ text->WriteText("\n");
+}
+
+void MyListCtrl::OnEndLabelEdit(wxListEvent& event)
+{
+ if ( !wxGetApp().GetTopWindow() )
+ return;
+
+ wxTextCtrl *text = ((MyFrame *)wxGetApp().GetTopWindow())->m_logWindow;
+ if ( !text )
+ return;
+
+ text->WriteText("OnEndLabelEdit: ");
+ text->WriteText(event.m_item.m_text);
+ text->WriteText("\n");
}
void MyListCtrl::OnDeleteItem(wxListEvent& WXUNUSED(event))
text->WriteText("OnDeleteItem\n");
}
-void MyListCtrl::OnGetInfo(wxListEvent& /*event*/)
+void MyListCtrl::OnGetInfo(wxListEvent& event)
{
if ( !wxGetApp().GetTopWindow() )
return;
if ( !text )
return;
- text->WriteText("OnGetInfo\n");
-
-/*
- ostream str(text);
+ text->WriteText("OnGetInfo\n");
- str << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
+ (*text) << "OnGetInfo (" << event.m_item.m_itemId << ", " << event.m_item.m_col << ")";
if ( event.m_item.m_mask & wxLIST_MASK_STATE )
- str << " wxLIST_MASK_STATE";
+ (*text) << " wxLIST_MASK_STATE";
if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
- str << " wxLIST_MASK_TEXT";
+ (*text) << " wxLIST_MASK_TEXT";
if ( event.m_item.m_mask & wxLIST_MASK_IMAGE )
- str << " wxLIST_MASK_IMAGE";
+ (*text) << " wxLIST_MASK_IMAGE";
if ( event.m_item.m_mask & wxLIST_MASK_DATA )
- str << " wxLIST_MASK_DATA";
+ (*text) << " wxLIST_MASK_DATA";
if ( event.m_item.m_mask & wxLIST_SET_ITEM )
- str << " wxLIST_SET_ITEM";
+ (*text) << " wxLIST_SET_ITEM";
if ( event.m_item.m_mask & wxLIST_MASK_WIDTH )
- str << " wxLIST_MASK_WIDTH";
+ (*text) << " wxLIST_MASK_WIDTH";
if ( event.m_item.m_mask & wxLIST_MASK_FORMAT )
- str << " wxLIST_MASK_WIDTH";
+ (*text) << " wxLIST_MASK_WIDTH";
if ( event.m_item.m_mask & wxLIST_MASK_TEXT )
{
event.m_item.m_text = "My callback text";
}
- str << "\n";
- str.flush();
-*/
+ (*text) << "\n";
}
void MyListCtrl::OnSetInfo(wxListEvent& WXUNUSED(event))
text->WriteText("OnSetInfo\n");
}
-void MyListCtrl::OnSelected(wxListEvent& WXUNUSED(event))
+void MyListCtrl::OnSelected(wxListEvent& event)
{
if ( !wxGetApp().GetTopWindow() )
return;
if ( !text )
return;
+ if ( GetWindowStyle() & wxLC_REPORT )
+ {
+ wxListItem info;
+ info.m_itemId = event.m_itemIndex;
+ info.m_col = 1;
+ info.m_mask = wxLIST_MASK_TEXT;
+ if ( GetItem(info) )
+ {
+ *text << "Value of the 2nd field of the selected item: "
+ << info.m_text << '\n';
+ }
+ else
+ {
+ wxFAIL_MSG("wxListCtrl::GetItem() failed");
+ }
+ }
+
text->WriteText("OnSelected\n");
}