#include "wx/listctrl.h"
#include "wx/timer.h" // for wxStopWatch
#include "wx/colordlg.h" // for wxGetColourFromUser
+#include "wx/settings.h"
+#include "wx/sysopt.h"
#include "listtest.h"
EVT_MENU(LIST_FREEZE, MyFrame::OnFreeze)
EVT_MENU(LIST_THAW, MyFrame::OnThaw)
EVT_MENU(LIST_TOGGLE_LINES, MyFrame::OnToggleLines)
+ EVT_MENU(LIST_MAC_USE_GENERIC, MyFrame::OnToggleMacUseGeneric)
EVT_UPDATE_UI(LIST_SHOW_COL_INFO, MyFrame::OnUpdateShowColInfo)
EVT_UPDATE_UI(LIST_TOGGLE_MULTI_SEL, MyFrame::OnUpdateToggleMultiSel)
EVT_LIST_CACHE_HINT(LIST_CTRL, MyListCtrl::OnCacheHint)
+#if USE_CONTEXT_MENU
+ EVT_CONTEXT_MENU(MyListCtrl::OnContextMenu)
+#endif
EVT_CHAR(MyListCtrl::OnChar)
+
+ EVT_RIGHT_DOWN(MyListCtrl::OnRightClick)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
bool MyApp::OnInit()
{
// Create the main frame window
- MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"), 50, 50, 450, 340);
+ MyFrame *frame = new MyFrame(wxT("wxListCtrl Test"));
// Show the frame
frame->Show(true);
}
// My frame constructor
-MyFrame::MyFrame(const wxChar *title, int x, int y, int w, int h)
- : wxFrame(NULL, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
+MyFrame::MyFrame(const wxChar *title)
+ : wxFrame(NULL, wxID_ANY, title)
{
m_listCtrl = NULL;
m_logWindow = NULL;
m_smallVirtual = false;
+ if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_SMALL)
+ SetSize(wxSize(450, 340));
+
// Give it an icon
SetIcon( wxICON(mondrian) );
menuView->Append(LIST_SMALL_ICON_TEXT_VIEW, _T("Small icon &view with text\tF6"));
menuView->Append(LIST_VIRTUAL_VIEW, _T("&Virtual view\tF7"));
menuView->Append(LIST_SMALL_VIRTUAL_VIEW, _T("Small virtual vie&w\tF8"));
+#ifdef __WXMAC__
+ menuView->AppendCheckItem(LIST_MAC_USE_GENERIC, _T("Mac: Use Generic Control"));
+#endif
wxMenu *menuList = new wxMenu;
menuList->Append(LIST_FOCUS_LAST, _T("&Make last item current\tCtrl-L"));
m_listCtrl->SetSingleStyle(wxLC_HRULES | wxLC_VRULES, event.IsChecked());
}
+void MyFrame::OnToggleMacUseGeneric(wxCommandEvent& event)
+{
+ wxSystemOptions::SetOption(wxT("mac.listctrl.always_use_generic"), event.IsChecked());
+}
+
void MyFrame::OnFocusLast(wxCommandEvent& WXUNUSED(event))
{
long index = m_listCtrl->GetItemCount() - 1;
m_listCtrl->SetColumnWidth( 1, wxLIST_AUTOSIZE );
m_listCtrl->SetColumnWidth( 2, wxLIST_AUTOSIZE );
+ // Set images in columns
+ m_listCtrl->SetItemColumnImage(1, 1, 0);
+
+ wxListItem info;
+ info.SetImage(0);
+ info.SetId(3);
+ info.SetColumn(2);
+ m_listCtrl->SetItem(info);
+
// test SetItemFont too
m_listCtrl->SetItemFont(0, *wxITALIC_FONT);
}
{
wxStopWatch sw;
- size_t itemCount = m_listCtrl->GetItemCount();
+ int itemCount = m_listCtrl->GetItemCount();
m_listCtrl->DeleteAllItems();
void MyListCtrl::OnDeleteItem(wxListEvent& event)
{
LogEvent(event, _T("OnDeleteItem"));
+ wxLogMessage( wxT("Number of items when delete event is sent: %d"), GetItemCount() );
}
void MyListCtrl::OnDeleteAllItems(wxListEvent& event)
{
wxListItem info;
info.m_itemId = event.GetIndex();
+ if ( info.m_itemId == -1 )
+ {
+ // no item
+ break;
+ }
+
GetItem(info);
wxListItemAttr *attr = info.GetAttributes();
}
}
+void MyListCtrl::OnRightClick(wxMouseEvent& event)
+{
+ if ( !event.ControlDown() )
+ {
+ event.Skip();
+ return;
+ }
+
+ int flags;
+ long subitem;
+ long item = HitTest(event.GetPosition(), flags, &subitem);
+
+ wxString where;
+ switch ( flags )
+ {
+ case wxLIST_HITTEST_ABOVE: where = _T("above"); break;
+ case wxLIST_HITTEST_BELOW: where = _T("below"); break;
+ case wxLIST_HITTEST_NOWHERE: where = _T("nowhere near"); break;
+ case wxLIST_HITTEST_ONITEMICON: where = _T("on icon of"); break;
+ case wxLIST_HITTEST_ONITEMLABEL: where = _T("on label of"); break;
+ case wxLIST_HITTEST_ONITEMRIGHT: where = _T("right on"); break;
+ case wxLIST_HITTEST_TOLEFT: where = _T("to the left of"); break;
+ case wxLIST_HITTEST_TORIGHT: where = _T("to the right of"); break;
+ default: where = _T("not clear exactly where on"); break;
+ }
+
+ wxLogMessage(_T("Right double click %s item %ld, subitem %ld"),
+ where.c_str(), item, subitem);
+}
+
void MyListCtrl::LogEvent(const wxListEvent& event, const wxChar *eventName)
{
wxLogMessage(_T("Item %ld: %s (item text = %s, data = %ld)"),
}
}
-int MyListCtrl::OnGetItemImage(long WXUNUSED(item)) const
+int MyListCtrl::OnGetItemColumnImage(long item, long column) const
{
- return 0;
+ if (!column)
+ return 0;
+
+ if (!(item %3) && column == 1)
+ return 0;
+
+ return -1;
}
wxListItemAttr *MyListCtrl::OnGetItemAttr(long item) const
SetItemData(tmp, i);
buf.Printf(_T("Col 1, item %d"), i);
- SetItem(i, 1, buf);
+ SetItem(tmp, 1, buf);
buf.Printf(_T("Item %d in column 2"), i);
- SetItem(i, 2, buf);
+ SetItem(tmp, 2, buf);
+}
+
+#if USE_CONTEXT_MENU
+void MyListCtrl::OnContextMenu(wxContextMenuEvent& event)
+{
+ wxPoint point = event.GetPosition();
+ // If from keyboard
+ if (point.x == -1 && point.y == -1) {
+ wxSize size = GetSize();
+ point.x = size.x / 2;
+ point.y = size.y / 2;
+ } else {
+ point = ScreenToClient(point);
+ }
+ ShowContextMenu(point);
+}
+#endif
+
+void MyListCtrl::ShowContextMenu(const wxPoint& pos)
+{
+ wxMenu menu;
+
+ menu.Append(wxID_ABOUT, _T("&About"));
+ menu.AppendSeparator();
+ menu.Append(wxID_EXIT, _T("E&xit"));
+
+ PopupMenu(&menu, pos.x, pos.y);
}