X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a6aa377bb38c4a2802cb3a0f382a7c7b09f8337a..8584ae1cd63a315f30450ba8a0136f1470f025c3:/samples/treectrl/treetest.cpp diff --git a/samples/treectrl/treetest.cpp b/samples/treectrl/treetest.cpp index a2dc2ebd70..5cebdf70ee 100644 --- a/samples/treectrl/treetest.cpp +++ b/samples/treectrl/treetest.cpp @@ -50,10 +50,16 @@ #include "state4.xpm" #include "state5.xpm" +#include "unchecked.xpm" +#include "checked.xpm" + #ifndef __WXMSW__ #include "../sample.xpm" #endif +static const int NUM_CHILDREN_PER_LEVEL = 5; +static const int NUM_LEVELS = 2; + // verify that the item is ok and insult the user if it is not #define CHECK_ITEM( item ) if ( !item.IsOk() ) { \ wxMessageBox(wxT("Please select some item first!"), \ @@ -112,6 +118,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame) MENU_LINK(SetImageSize) MENU_LINK(CollapseAndReset) MENU_LINK(EnsureVisible) + MENU_LINK(SetFocus) MENU_LINK(AddItem) MENU_LINK(InsertItem) MENU_LINK(IncIndent) @@ -262,6 +269,7 @@ MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h) tree_menu->Append(TreeTest_SortRev, wxT("Sort in reversed order")); tree_menu->AppendSeparator(); tree_menu->Append(TreeTest_EnsureVisible, wxT("Make the last item &visible")); + tree_menu->Append(TreeTest_SetFocus, wxT("Set &focus to the tree")); tree_menu->AppendSeparator(); tree_menu->Append(TreeTest_IncIndent, wxT("Add 5 points to indentation\tAlt-I")); tree_menu->Append(TreeTest_DecIndent, wxT("Reduce indentation by 5 points\tAlt-R")); @@ -619,7 +627,7 @@ void MyFrame::OnDeleteAll(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnRecreate(wxCommandEvent& event) { OnDeleteAll(event); - m_treeCtrl->AddTestItemsToTree(5, 2); + m_treeCtrl->AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL, NUM_LEVELS); } void MyFrame::OnSetImageSize(wxCommandEvent& WXUNUSED(event)) @@ -677,6 +685,11 @@ void MyFrame::OnToggleAlternateStates(wxCommandEvent& WXUNUSED(event)) m_treeCtrl->SetAlternateStates(!alternateStates); m_treeCtrl->CreateStateImageList(); + + // normal states < alternate states + // so we must reset broken states + if ( alternateStates ) + m_treeCtrl->ResetBrokenStateImages(); } void MyFrame::OnToggleButtons(wxCommandEvent& WXUNUSED(event)) @@ -705,6 +718,11 @@ void MyFrame::OnEnsureVisible(wxCommandEvent& WXUNUSED(event)) m_treeCtrl->DoEnsureVisible(); } +void MyFrame::OnSetFocus(wxCommandEvent& WXUNUSED(event)) +{ + m_treeCtrl->SetFocus(); +} + void MyFrame::OnInsertItem(wxCommandEvent& WXUNUSED(event)) { int image = wxGetApp().ShowImages() ? MyTreeCtrl::TreeCtrlIcon_File : -1; @@ -837,7 +855,7 @@ MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id, CreateStateImageList(); // Add some items to the tree - AddTestItemsToTree(5, 2); + AddTestItemsToTree(NUM_CHILDREN_PER_LEVEL, NUM_LEVELS); } void MyTreeCtrl::CreateImageList(int size) @@ -925,35 +943,18 @@ void MyTreeCtrl::CreateStateImageList(bool del) } else { - wxRendererNative& renderer = wxRendererNative::Get(); - - wxSize size(renderer.GetCheckBoxSize(this)); - - // make an state checkbox image list - states = new wxImageList(size.GetWidth(), size.GetHeight(), true); - - wxBitmap checkBmp(size.GetWidth(), size.GetHeight()); - wxRect rect(size); - - // create no checked image - { - // first create bitmap in a memory DC - wxMemoryDC memDC(checkBmp); - memDC.Clear(); - // then draw a check mark into it - renderer.DrawCheckBox(this, memDC, rect, 0); - } // select checkBmp out of memDC - - states->Add(checkBmp); - - // create checked image - { - wxMemoryDC memDC(checkBmp); - memDC.Clear(); - renderer.DrawCheckBox(this, memDC, rect, wxCONTROL_CHECKED); - } + wxIcon icons[2]; + icons[0] = wxIcon(unchecked_xpm); + icons[1] = wxIcon(checked_xpm); + + int width = icons[0].GetWidth(), + height = icons[0].GetHeight(); + + // Make an state image list containing small icons + states = new wxImageList(width, height, true); - states->Add(checkBmp); + for ( size_t i = 0; i < WXSIZEOF(icons); i++ ) + states->Add(icons[i]); } AssignStateImageList(states); @@ -1104,9 +1105,13 @@ void MyTreeCtrl::AddTestItemsToTree(size_t numChildren, SetItemTextColour(id, *wxBLUE); id = GetNextChild(rootId, cookie); - id = GetNextChild(rootId, cookie); - SetItemTextColour(id, *wxRED); - SetItemBackgroundColour(id, *wxLIGHT_GREY); + if ( id ) + id = GetNextChild(rootId, cookie); + if ( id ) + { + SetItemTextColour(id, *wxRED); + SetItemBackgroundColour(id, *wxLIGHT_GREY); + } } void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, @@ -1166,6 +1171,29 @@ void MyTreeCtrl::DoToggleState(const wxTreeItemId& item) } } +void MyTreeCtrl::DoResetBrokenStateImages(const wxTreeItemId& idParent, + wxTreeItemIdValue cookie, int state) +{ + wxTreeItemId id; + + if ( !cookie ) + id = GetFirstChild(idParent, cookie); + else + id = GetNextChild(idParent, cookie); + + if ( !id.IsOk() ) + return; + + int curState = GetItemState(id); + if ( curState != wxTREE_ITEMSTATE_NONE && curState > state ) + SetItemState(id, state); + + if (ItemHasChildren(id)) + DoResetBrokenStateImages(id, 0, state); + + DoResetBrokenStateImages(idParent, cookie, state); +} + void MyTreeCtrl::LogEvent(const wxChar *name, const wxTreeEvent& event) { wxTreeItemId item = event.GetItem(); @@ -1482,6 +1510,8 @@ void MyTreeCtrl::OnContextMenu(wxContextMenuEvent& event) wxPoint pt = event.GetPosition(); wxLogMessage(wxT("OnContextMenu at screen coords (%i, %i)"), pt.x, pt.y); + + event.Skip(); } void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)