X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b29903d4854625650ae678c0515acc76facba1ee..ae200bfcb537c475708959cb034be66eb0f5dcd4:/samples/treectrl/treetest.cpp diff --git a/samples/treectrl/treetest.cpp b/samples/treectrl/treetest.cpp index 6ab96a4285..40705972a7 100644 --- a/samples/treectrl/treetest.cpp +++ b/samples/treectrl/treetest.cpp @@ -9,11 +9,6 @@ // Licence: wxWindows license ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ - #pragma implementation - #pragma interface -#endif - // For compilers that support precompilation, includes "wx/wx.h". #include "wx/wxprec.h" @@ -32,8 +27,7 @@ #include "wx/image.h" #include "wx/imaglist.h" #include "wx/treectrl.h" - -#include "math.h" +#include "wx/math.h" #ifdef __WIN32__ // this is not supported by native control @@ -137,13 +131,22 @@ BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl) EVT_TREE_ITEM_EXPANDING(TreeTest_Ctrl, MyTreeCtrl::OnItemExpanding) EVT_TREE_ITEM_COLLAPSED(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsed) EVT_TREE_ITEM_COLLAPSING(TreeTest_Ctrl, MyTreeCtrl::OnItemCollapsing) - //EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRightClick) - EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu) EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged) EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging) EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown) EVT_TREE_ITEM_ACTIVATED(TreeTest_Ctrl, MyTreeCtrl::OnItemActivated) + + // so many differents ways to handle right mouse button clicks... + EVT_CONTEXT_MENU(MyTreeCtrl::OnContextMenu) + // EVT_TREE_ITEM_MENU is the preferred event for creating context menus + // on a tree control, because it includes the point of the click or item, + // meaning that no additional placement calculations are required. + EVT_TREE_ITEM_MENU(TreeTest_Ctrl, MyTreeCtrl::OnItemMenu) + EVT_TREE_ITEM_RIGHT_CLICK(TreeTest_Ctrl, MyTreeCtrl::OnItemRClick) + + EVT_RIGHT_DOWN(MyTreeCtrl::OnRMouseDown) + EVT_RIGHT_UP(MyTreeCtrl::OnRMouseUp) EVT_RIGHT_DCLICK(MyTreeCtrl::OnRMouseDClick) END_EVENT_TABLE() @@ -363,9 +366,9 @@ void MyFrame::OnIdle(wxIdleEvent& event) void MyFrame::OnSize(wxSizeEvent& event) { - if ( m_treeCtrl + if ( m_treeCtrl #if wxUSE_LOG - && m_textCtrl + && m_textCtrl #endif // wxUSE_LOG ) { @@ -411,7 +414,7 @@ void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event)) static wxString s_text; s_text = wxGetTextFromUser(wxT("New name: "), wxT("Tree sample question"), s_text, this); - if ( !s_text.IsEmpty() ) + if ( !s_text.empty() ) { m_treeCtrl->SetItemText(item, s_text); } @@ -758,10 +761,6 @@ void MyTreeCtrl::CreateButtonsImageList(int WXUNUSED(size)) #endif } -MyTreeCtrl::~MyTreeCtrl() -{ -} - int MyTreeCtrl::OnCompareItems(const wxTreeItemId& item1, const wxTreeItemId& item2) { @@ -932,8 +931,6 @@ void LogKeyEvent(const wxChar *name, const wxKeyEvent& event) case WXK_MENU: key = wxT("MENU"); break; case WXK_PAUSE: key = wxT("PAUSE"); break; case WXK_CAPITAL: key = wxT("CAPITAL"); break; - case WXK_PRIOR: key = wxT("PRIOR"); break; - case WXK_NEXT: key = wxT("NEXT"); break; case WXK_END: key = wxT("END"); break; case WXK_HOME: key = wxT("HOME"); break; case WXK_LEFT: key = wxT("LEFT"); break; @@ -1002,7 +999,6 @@ void LogKeyEvent(const wxChar *name, const wxKeyEvent& event) case WXK_NUMPAD_UP: key = wxT("NUMPAD_UP"); break; case WXK_NUMPAD_RIGHT: key = wxT("NUMPAD_RIGHT"); break; case WXK_NUMPAD_DOWN: key = wxT("NUMPAD_DOWN"); break; - case WXK_NUMPAD_PRIOR: key = wxT("NUMPAD_PRIOR"); break; case WXK_NUMPAD_PAGEUP: key = wxT("NUMPAD_PAGEUP"); break; case WXK_NUMPAD_PAGEDOWN: key = wxT("NUMPAD_PAGEDOWN"); break; case WXK_NUMPAD_END: key = wxT("NUMPAD_END"); break; @@ -1018,7 +1014,7 @@ void LogKeyEvent(const wxChar *name, const wxKeyEvent& event) default: { - if ( wxIsprint((int)keycode) ) + if ( keycode < 128 && wxIsprint((int)keycode) ) key.Printf(wxT("'%c'"), (char)keycode); else if ( keycode > 0 && keycode < 27 ) key.Printf(_("Ctrl-%c"), wxT('A') + keycode - 1); @@ -1109,6 +1105,11 @@ void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event) event.Veto(); } + else if ( itemId == GetRootItem() ) + { + // test that it is possible to change the text of the item being edited + SetItemText(itemId, _T("Editing root item")); + } } void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event) @@ -1152,26 +1153,35 @@ void MyTreeCtrl::OnItemActivated(wxTreeEvent& event) wxLogMessage(wxT("OnItemActivated")); } -void MyTreeCtrl::OnItemRightClick(wxTreeEvent& event) +void MyTreeCtrl::OnItemMenu(wxTreeEvent& event) { - ShowMenu(event.GetItem(), event.GetPoint()); + wxTreeItemId itemId = event.GetItem(); + MyTreeItemData *item = itemId.IsOk() ? (MyTreeItemData *)GetItemData(itemId) + : NULL; + + wxLogMessage(wxT("OnItemMenu for item \"%s\""), item ? item->GetDesc() + : _T("")); + + event.Skip(); } void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event) { wxPoint pt = event.GetPosition(); - wxTreeItemId item = GetSelection(); + wxTreeItemId item; + if ( !HasFlag(wxTR_MULTIPLE) ) + item = GetSelection(); wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt.x, pt.y); - //check if event was generated by keyboard (MSW-specific?) - if (pt.x==-1 && pt.y==-1) //(this is how MSW indicates it) + // check if event was generated by keyboard (MSW-specific?) + if ( pt.x == -1 && pt.y == -1 ) //(this is how MSW indicates it) { - //attempt to guess where to show the menu - if (item.IsOk()) - { - //if an item was clicked, show menu to the right of it + // attempt to guess where to show the menu + if ( item.IsOk() ) + { + // if an item was clicked, show menu to the right of it wxRect rect; - GetBoundingRect(item, rect, true); //true = only the label + GetBoundingRect(item, rect, true /* only the label */); pt = wxPoint(rect.GetRight(), rect.GetTop()); } else @@ -1179,12 +1189,11 @@ void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event) pt = wxPoint(0, 0); } } - else + else // event was generated by mouse, use supplied coords { - //event was generated by mouse, use supplied coords pt = ScreenToClient(pt); } - + ShowMenu(item, pt); } @@ -1209,6 +1218,32 @@ void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt) #endif // wxUSE_MENUS } +void MyTreeCtrl::OnItemRClick(wxTreeEvent& event) +{ + wxTreeItemId itemId = event.GetItem(); + MyTreeItemData *item = itemId.IsOk() ? (MyTreeItemData *)GetItemData(itemId) + : NULL; + + wxLogMessage(wxT("Item \"%s\" right clicked"), item ? item->GetDesc() + : _T("")); + + event.Skip(); +} + +void MyTreeCtrl::OnRMouseDown(wxMouseEvent& event) +{ + wxLogMessage(wxT("Right mouse button down")); + + event.Skip(); +} + +void MyTreeCtrl::OnRMouseUp(wxMouseEvent& event) +{ + wxLogMessage(wxT("Right mouse button up")); + + event.Skip(); +} + void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event) { wxTreeItemId id = HitTest(event.GetPosition()); @@ -1220,6 +1255,8 @@ void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event) if ( item ) wxLogMessage(wxT("Item '%s' under mouse"), item->GetDesc()); } + + event.Skip(); } static inline const wxChar *Bool2String(bool b)