]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/treectrl/treetest.cpp
Added wxNB_RIGHT,LEFT,BOTTOM flags for tab placement
[wxWidgets.git] / samples / treectrl / treetest.cpp
index 93d51cd80d74f6ea7d720625933e76a78f4f8815..8335f176b7604a75b60fe7a432b5c17ae61c0c28 100644 (file)
   #include "wx/wx.h"
 #endif
 
-// under Windows the icons are in the .rc file
-#ifndef __WXMSW__
-  #include "icon1.xpm"
-  #include "icon2.xpm"
-  #include "mondrian.xpm"
-#endif
-
 #include "wx/log.h"
 
+#include "wx/image.h"
 #include "wx/imaglist.h"
 #include "wx/treectrl.h"
 
 #include "math.h"
 
+//#ifdef __WXMSW__
+    #define NO_MULTIPLE_SELECTION
+    #define NO_VARIABLE_HEIGHT
+//#endif
+
 #include "treetest.h"
 
+// under Windows the icons are in the .rc file
+#ifndef __WXMSW__
+#ifdef NO_VARIABLE_HEIGHT
+  #include "icon1.xpm"
+#endif
+  #include "icon2.xpm"
+  #include "mondrian.xpm"
+#endif
+
 // verify that the item is ok and insult the user if it is not
 #define CHECK_ITEM( item ) if ( !item.IsOk() ) {                            \
                              wxMessageBox("Please select some item first!", \
@@ -54,6 +62,11 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(TreeTest_Quit, MyFrame::OnQuit)
     EVT_MENU(TreeTest_About, MyFrame::OnAbout)
     EVT_MENU(TreeTest_Dump, MyFrame::OnDump)
+#ifndef NO_MULTIPLE_SELECTION
+    EVT_MENU(TreeTest_DumpSelected, MyFrame::OnDumpSelected)
+    EVT_MENU(TreeTest_Select, MyFrame::OnSelect)
+    EVT_MENU(TreeTest_Unselect, MyFrame::OnUnselect)
+#endif // NO_MULTIPLE_SELECTION
     EVT_MENU(TreeTest_Rename, MyFrame::OnRename)
     EVT_MENU(TreeTest_Sort, MyFrame::OnSort)
     EVT_MENU(TreeTest_SortRev, MyFrame::OnSortRev)
@@ -70,6 +83,7 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(TreeTest_DecIndent, MyFrame::OnDecIndent)
     EVT_MENU(TreeTest_IncSpacing, MyFrame::OnIncSpacing)
     EVT_MENU(TreeTest_DecSpacing, MyFrame::OnDecSpacing)
+    EVT_MENU(TreeTest_ToggleIcon, MyFrame::OnToggleIcon)
 END_EVENT_TABLE()
 
 BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
@@ -147,11 +161,20 @@ MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
     tree_menu->Append(TreeTest_DecSpacing, "Reduce spacing by 5 points\tCtrl-R");
 
     item_menu->Append(TreeTest_Dump, "&Dump item children");
-    item_menu->AppendSeparator();
     item_menu->Append(TreeTest_Rename, "&Rename item...");
+
     item_menu->AppendSeparator();
     item_menu->Append(TreeTest_Bold, "Make item &bold");
     item_menu->Append(TreeTest_UnBold, "Make item &not bold");
+    item_menu->AppendSeparator();
+    item_menu->Append(TreeTest_ToggleIcon, "Toggle the items &icon");
+
+#ifndef NO_MULTIPLE_SELECTION
+    item_menu->AppendSeparator();
+    item_menu->Append(TreeTest_DumpSelected, "Dump selected items\tAlt-D");
+    item_menu->Append(TreeTest_Select, "Select current item\tAlt-S");
+    item_menu->Append(TreeTest_Unselect, "Unselect everything\tAlt-U");
+#endif
 
     wxMenuBar *menu_bar = new wxMenuBar;
     menu_bar->Append(file_menu, "&File");
@@ -161,9 +184,15 @@ MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
 
     m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
                                 wxDefaultPosition, wxDefaultSize,
-                                wxTR_HAS_BUTTONS | wxSUNKEN_BORDER
-                               | wxTR_MULTIPLE
-                               );
+                                wxTR_HAS_BUTTONS |
+                                wxTR_EDIT_LABELS |
+#ifndef NO_MULTIPLE_SELECTION
+                               wxTR_MULTIPLE |
+#endif
+#ifndef NO_VARIABLE_HEIGHT
+                               wxTR_HAS_VARIABLE_ROW_HEIGHT |
+#endif
+                               wxSUNKEN_BORDER);
     wxTextCtrl *textCtrl = new wxTextCtrl(this, -1, "",
                                 wxDefaultPosition, wxDefaultSize,
                                 wxTE_MULTILINE | wxSUNKEN_BORDER);
@@ -211,8 +240,7 @@ void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
 {
     wxMessageBox("Tree test sample\n"
-                 "Julian Smart (c) 1997,\n"
-                 "Vadim Zeitlin (c) 1998",
+                 "(c) Julian Smart 1997, Vadim Zeitlin 1998",
                  "About tree test",
                  wxOK | wxICON_INFORMATION, this);
 }
@@ -223,6 +251,8 @@ void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
 
     CHECK_ITEM( item );
 
+    // old code - now we edit in place
+#if 0
     static wxString s_text;
     s_text = wxGetTextFromUser("New name: ", "Tree sample question",
             s_text, this);
@@ -230,6 +260,10 @@ void MyFrame::OnRename(wxCommandEvent& WXUNUSED(event))
     {
         m_treeCtrl->SetItemText(item, s_text);
     }
+#endif // 0
+
+    // TODO demonstrate creating a custom edit control...
+    (void)m_treeCtrl->EditLabel(item);
 }
 
 void MyFrame::DoSort(bool reverse)
@@ -250,6 +284,33 @@ void MyFrame::OnDump(wxCommandEvent& WXUNUSED(event))
     m_treeCtrl->GetItemsRecursively(root, -1);
 }
 
+#ifndef NO_MULTIPLE_SELECTION
+
+void MyFrame::OnDumpSelected(wxCommandEvent& WXUNUSED(event))
+{
+    wxArrayTreeItemIds array;
+
+    size_t count = m_treeCtrl->GetSelections(array);
+    wxLogMessage(_T("%u items selected"), count);
+
+    for ( size_t n = 0; n < count; n++ )
+    {
+        wxLogMessage("\t%s", m_treeCtrl->GetItemText(array.Item(n)).c_str());
+    }
+}
+
+void MyFrame::OnSelect(wxCommandEvent& event)
+{
+    m_treeCtrl->SelectItem(m_treeCtrl->GetSelection());
+}
+
+void MyFrame::OnUnselect(wxCommandEvent& event)
+{
+    m_treeCtrl->UnselectAll();
+}
+
+#endif // NO_MULTIPLE_SELECTION
+
 void MyFrame::DoSetBold(bool bold)
 {
     wxTreeItemId item = m_treeCtrl->GetSelection();
@@ -306,8 +367,8 @@ void MyFrame::OnAddItem(wxCommandEvent& WXUNUSED(event))
     text.Printf("Item #%d", ++s_num);
 
     m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(),
-                           text,
-                           MyTreeCtrl::TreeCtrlIcon_File);
+                           text /*,
+                           MyTreeCtrl::TreeCtrlIcon_File */ );
 }
 
 void MyFrame::OnIncIndent(wxCommandEvent& WXUNUSED(event))
@@ -338,6 +399,15 @@ void MyFrame::OnDecSpacing(wxCommandEvent& WXUNUSED(event))
         m_treeCtrl->SetSpacing( indent-5 );
 }
 
+void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
+{
+    wxTreeItemId item = m_treeCtrl->GetSelection();
+
+    CHECK_ITEM( item );
+
+    m_treeCtrl->DoToggleIcon(item);
+}
+
 // MyTreeCtrl implementation
 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
 
@@ -346,6 +416,15 @@ MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
                        long style)
           : wxTreeCtrl(parent, id, pos, size, style)
 {
+#ifndef NO_VARIABLE_HEIGHT
+#if wxUSE_LIBJPEG
+    wxImage::AddHandler(new wxJPEGHandler);
+    wxImage image;
+
+    image.LoadFile(wxString("horse.jpg"), wxBITMAP_TYPE_JPEG );
+#endif
+#endif
+
     m_reverseSort = FALSE;
 
     // Make an image list containing small icons
@@ -355,10 +434,18 @@ MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
 #if defined(__WXMSW__) && defined(__WIN16__)
     // This is required in 16-bit Windows mode only because we can't load a specific (16x16)
     // icon image, so it comes out stretched
+#  ifndef NO_VARIABLE_HEIGHT
+    m_imageListNormal->Add(image.ConvertToBitmap());
+#  else
     m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
+#  endif
     m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
 #else
+#  ifndef NO_VARIABLE_HEIGHT
+    m_imageListNormal->Add(image.ConvertToBitmap());
+#  else
     m_imageListNormal->Add(wxICON(icon1));
+#  endif
     m_imageListNormal->Add(wxICON(icon2));
 #endif
 
@@ -441,7 +528,7 @@ void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie)
     if(id <= 0)
         return;
 
-    wxString text=GetItemText(id);
+    wxString text = GetItemText(id);
     wxLogMessage(text);
 
     if (ItemHasChildren(id))
@@ -450,6 +537,14 @@ void MyTreeCtrl::GetItemsRecursively(const wxTreeItemId& idParent, long cookie)
     GetItemsRecursively(idParent, cookie);
 }
 
+void MyTreeCtrl::DoToggleIcon(const wxTreeItemId& item)
+{
+    int image = GetItemImage(item) == TreeCtrlIcon_Folder ? TreeCtrlIcon_File
+                                                          : TreeCtrlIcon_Folder;
+
+    SetItemImage(item, image);
+}
+
 
 // avoid repetition
 #define TREE_EVENT_HANDLER(name)                            \
@@ -460,8 +555,6 @@ void MyTreeCtrl::name(wxTreeEvent& WXUNUSED(event))         \
 
 TREE_EVENT_HANDLER(OnBeginDrag)
 TREE_EVENT_HANDLER(OnBeginRDrag)
-TREE_EVENT_HANDLER(OnBeginLabelEdit)
-TREE_EVENT_HANDLER(OnEndLabelEdit)
 TREE_EVENT_HANDLER(OnDeleteItem)
 TREE_EVENT_HANDLER(OnGetInfo)
 TREE_EVENT_HANDLER(OnSetInfo)
@@ -473,13 +566,40 @@ TREE_EVENT_HANDLER(OnSelChanging)
 
 #undef TREE_EVENT_HANDLER
 
+void MyTreeCtrl::OnBeginLabelEdit(wxTreeEvent& event)
+{
+    wxLogMessage("OnBeginLabelEdit");
+
+    // for testing, prevent this items label editing
+    wxTreeItemId itemId = event.GetItem();
+    if ( IsTestItem(itemId) )
+    {
+        wxMessageBox("You can't edit this item.");
+
+        event.Veto();
+    }
+}
+
+void MyTreeCtrl::OnEndLabelEdit(wxTreeEvent& event)
+{
+    wxLogMessage("OnEndLabelEdit");
+
+    // don't allow anything except letters in the labels
+    if ( !event.GetLabel().IsWord() )
+    {
+        wxMessageBox("The label should contain only letters.");
+
+        event.Veto();
+    }
+}
+
 void MyTreeCtrl::OnItemCollapsing(wxTreeEvent& event)
 {
     wxLogMessage("OnItemCollapsing");
 
     // for testing, prevent the user from collapsing the first child folder
     wxTreeItemId itemId = event.GetItem();
-    if ( GetParent(itemId) == GetRootItem() && !GetPrevSibling(itemId) )
+    if ( IsTestItem(itemId) )
     {
         wxMessageBox("You can't collapse this item.");