]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/treectrl/treectrl.cpp
don't crash when invalid colour is set as fg/bg colour
[wxWidgets.git] / samples / treectrl / treectrl.cpp
index 15a8cb2a6d759c82b401393b9a323e7a5beaaccd..749bc0d3c9251a9daa3f8e62275b9e95607a533d 100644 (file)
@@ -9,9 +9,12 @@
 // Licence:     wxWindows license
 /////////////////////////////////////////////////////////////////////////////
 
+// important: the #pragma argument must be different from treectrl.cpp,
+// otherwise gcc gets confused (as there is also treectrl.cpp in the library
+// which has identical #pragma) and the sample crashes on startup!
 #ifdef __GNUG__
-  #pragma implementation
-  #pragma interface
+  #pragma interface "treetest.cpp"
+  #pragma implementation "treetest.cpp"
 #endif
 
 // For compilers that support precompilation, includes "wx/wx.h".
@@ -95,7 +98,11 @@ BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(TreeTest_ToggleIcon, MyFrame::OnToggleIcon)
 END_EVENT_TABLE()
 
+#if USE_GENERIC_TREECTRL
+BEGIN_EVENT_TABLE(MyTreeCtrl, wxGenericTreeCtrl)
+#else
 BEGIN_EVENT_TABLE(MyTreeCtrl, wxTreeCtrl)
+#endif
     EVT_TREE_BEGIN_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginDrag)
     EVT_TREE_BEGIN_RDRAG(TreeTest_Ctrl, MyTreeCtrl::OnBeginRDrag)
     EVT_TREE_END_DRAG(TreeTest_Ctrl, MyTreeCtrl::OnEndDrag)
@@ -110,6 +117,9 @@ 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_RIGHT_UP(MyTreeCtrl::OnRMouseUp)
     EVT_TREE_SEL_CHANGED(TreeTest_Ctrl, MyTreeCtrl::OnSelChanged)
     EVT_TREE_SEL_CHANGING(TreeTest_Ctrl, MyTreeCtrl::OnSelChanging)
     EVT_TREE_KEY_DOWN(TreeTest_Ctrl, MyTreeCtrl::OnTreeKeyDown)
@@ -145,6 +155,7 @@ MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
     // Give it an icon
     SetIcon(wxICON(mondrian));
 
+#if wxUSE_MENUS
     // Make a menubar
     wxMenu *file_menu = new wxMenu,
            *tree_menu = new wxMenu,
@@ -205,17 +216,18 @@ MyFrame::MyFrame(const wxString& title, int x, int y, int w, int h)
     SetMenuBar(menu_bar);
 
     menu_bar->Check(TreeTest_ToggleImages, TRUE);
+#endif // wxUSE_MENUS
 
     m_treeCtrl = new MyTreeCtrl(this, TreeTest_Ctrl,
                                 wxDefaultPosition, wxDefaultSize,
-                                wxTR_HAS_BUTTONS |
+                                wxTR_MAC_BUTTONS | wxTR_NO_LINES |
                                 wxTR_EDIT_LABELS |
 #ifndef NO_VARIABLE_HEIGHT
                                 wxTR_HAS_VARIABLE_ROW_HEIGHT |
 #endif
                                 wxSUNKEN_BORDER);
 
-    m_treeCtrl->SetBackgroundColour(wxColour(204, 205, 79));
+//    m_treeCtrl->SetBackgroundColour( *wxLIGHT_GREY );
 
     m_textCtrl = new wxTextCtrl(this, -1, "",
                                 wxDefaultPosition, wxDefaultSize,
@@ -428,7 +440,18 @@ void MyFrame::OnSetImageSize(wxCommandEvent& event)
 
 void MyFrame::OnToggleImages(wxCommandEvent& event)
 {
-    wxGetApp().SetShowImages(!wxGetApp().ShowImages());
+    if ( wxGetApp().ShowImages() )
+    {
+        m_treeCtrl->CreateImageList(-1);
+
+        wxGetApp().SetShowImages(FALSE);
+    }
+    else
+    {
+        m_treeCtrl->CreateImageList();
+
+        wxGetApp().SetShowImages(TRUE);
+    }
 
     OnRecreate(event);
 }
@@ -499,7 +522,11 @@ void MyFrame::OnToggleIcon(wxCommandEvent& WXUNUSED(event))
 }
 
 // MyTreeCtrl implementation
+#if USE_GENERIC_TREECTRL
+IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxGenericTreeCtrl)
+#else
 IMPLEMENT_DYNAMIC_CLASS(MyTreeCtrl, wxTreeCtrl)
+#endif
 
 MyTreeCtrl::MyTreeCtrl(wxWindow *parent, const wxWindowID id,
                        const wxPoint& pos, const wxSize& size,
@@ -522,42 +549,42 @@ void MyTreeCtrl::CreateImageList(int size)
     if ( size == -1 )
     {
         m_imageListNormal = NULL;
-
-        return;
     }
+    else
+    {
+        // Make an image list containing small icons
+        m_imageListNormal = new wxImageList(size, size, TRUE);
 
-    // Make an image list containing small icons
-    m_imageListNormal = new wxImageList(size, size, TRUE);
-
-    // should correspond to TreeCtrlIcon_xxx enum
+        // should correspond to TreeCtrlIcon_xxx enum
 #if defined(__WXMSW__) && defined(__WIN16__)
-    m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
-    m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
-    m_imageListNormal->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE));
-    m_imageListNormal->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE));
-    m_imageListNormal->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE));
-#else
-    wxIcon icons[5];
-    icons[0] = wxICON(icon1);
-    icons[1] = wxICON(icon2);
-    icons[2] = wxICON(icon3);
-    icons[3] = wxICON(icon4);
-    icons[4] = wxICON(icon5);
-
-    int sizeOrig = icons[0].GetWidth();
-    for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
-    {
-        if ( size == sizeOrig )
+        m_imageListNormal->Add(wxBitmap("bitmap1", wxBITMAP_TYPE_BMP_RESOURCE));
+        m_imageListNormal->Add(wxBitmap("bitmap2", wxBITMAP_TYPE_BMP_RESOURCE));
+        m_imageListNormal->Add(wxBitmap("bitmap3", wxBITMAP_TYPE_BMP_RESOURCE));
+        m_imageListNormal->Add(wxBitmap("bitmap4", wxBITMAP_TYPE_BMP_RESOURCE));
+        m_imageListNormal->Add(wxBitmap("bitmap5", wxBITMAP_TYPE_BMP_RESOURCE));
+#else // !MSW
+        wxIcon icons[5];
+        icons[0] = wxICON(icon1);
+        icons[1] = wxICON(icon2);
+        icons[2] = wxICON(icon3);
+        icons[3] = wxICON(icon4);
+        icons[4] = wxICON(icon5);
+
+        int sizeOrig = icons[0].GetWidth();
+        for ( size_t i = 0; i < WXSIZEOF(icons); i++ )
         {
-            m_imageListNormal->Add(icons[i]);
-        }
-        else
-        {
-            m_imageListNormal->Add(wxImage(icons[i]).Rescale(size, size).
-                                    ConvertToBitmap());
+            if ( size == sizeOrig )
+            {
+                m_imageListNormal->Add(icons[i]);
+            }
+            else
+            {
+                m_imageListNormal->Add(wxImage(icons[i]).Rescale(size, size).
+                                        ConvertToBitmap());
+            }
         }
+#endif // MSW/!MSW
     }
-#endif
 
     SetImageList(m_imageListNormal);
 }
@@ -819,6 +846,38 @@ void MyTreeCtrl::OnItemActivated(wxTreeEvent& event)
     wxLogMessage("OnItemActivated");
 }
 
+void MyTreeCtrl::OnItemRightClick(wxTreeEvent& event)
+{
+    ShowMenu(event.GetItem(), event.GetPoint());
+}
+
+void MyTreeCtrl::OnRMouseUp(wxMouseEvent& event)
+{
+    wxPoint pt = event.GetPosition();
+    ShowMenu(HitTest(pt), pt);
+}
+
+void MyTreeCtrl::ShowMenu(wxTreeItemId id, const wxPoint& pt)
+{
+    wxString title;
+    if ( id.IsOk() )
+    {
+        title << _T("Menu for ") << GetItemText(id);
+    }
+    else
+    {
+        title = _T("Menu for no particular item");
+    }
+
+#if wxUSE_MENUS
+    wxMenu menu(title);
+    menu.Append(TreeTest_About, _T("&About..."));
+    menu.Append(TreeTest_Dump, _T("&Dump"));
+
+    PopupMenu(&menu, pt);
+#endif // wxUSE_MENUS
+}
+
 void MyTreeCtrl::OnRMouseDClick(wxMouseEvent& event)
 {
     wxTreeItemId id = HitTest(event.GetPosition());