]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/treelist/treelist.cpp
eliminate possibility of configure enabling gnomeprint with GTK3
[wxWidgets.git] / samples / treelist / treelist.cpp
index fc614dc596e7d166bba36106d77b5413e47bfacd..081a594e683f4dd1f3d4fd228a7b6b0ae3574dfe 100644 (file)
 // Resources
 // ----------------------------------------------------------------------------
 
 // Resources
 // ----------------------------------------------------------------------------
 
-#if !defined(__WXMSW__) && !defined(__WXPM__)
+#ifndef wxHAS_IMAGES_IN_RESOURCES
     #include "../sample.xpm"
 #endif
 
 // ----------------------------------------------------------------------------
     #include "../sample.xpm"
 #endif
 
 // ----------------------------------------------------------------------------
-// Constants for menu items
+// Constants
 // ----------------------------------------------------------------------------
 
 // ----------------------------------------------------------------------------
 
+// Menu items.
 enum
 {
     Id_MultiSelect = 100,
 enum
 {
     Id_MultiSelect = 100,
+    Id_FlatList,
 
     Id_Checkboxes_Start,
     Id_NoCheckboxes = Id_Checkboxes_Start,
 
     Id_Checkboxes_Start,
     Id_NoCheckboxes = Id_Checkboxes_Start,
@@ -71,6 +73,89 @@ enum
     Id_Select_HTMLDocs
 };
 
     Id_Select_HTMLDocs
 };
 
+// Tree list columns.
+enum
+{
+    Col_Component,
+    Col_Files,
+    Col_Size
+};
+
+// ----------------------------------------------------------------------------
+// Custom comparator for tree list items comparison
+// ----------------------------------------------------------------------------
+
+// This is a toy class as in a real program you would have the original numeric
+// data somewhere and wouldn't need to parse it back from strings presumably.
+// Nevertheless it shows how to implement a custom comparator which is needed
+// if you want to sort by a column with non-textual contents.
+class MyComparator : public wxTreeListItemComparator
+{
+public:
+    virtual int
+    Compare(wxTreeListCtrl* treelist,
+            unsigned column,
+            wxTreeListItem item1,
+            wxTreeListItem item2)
+    {
+        wxString text1 = treelist->GetItemText(item1, column),
+                 text2 = treelist->GetItemText(item2, column);
+
+        switch ( column )
+        {
+            case Col_Component:
+                // Simple alphabetical comparison is fine for those.
+                return text1.CmpNoCase(text2);
+
+            case Col_Files:
+                // Compare strings as numbers.
+                return GetNumFilesFromText(text1) - GetNumFilesFromText(text2);
+
+            case Col_Size:
+                // Compare strings as numbers but also take care of "KiB" and
+                // "MiB" suffixes.
+                return GetSizeFromText(text1) - GetSizeFromText(text2);
+        }
+
+        wxFAIL_MSG( "Sorting on unknown column?" );
+
+        return 0;
+    }
+
+private:
+    // Return the number of files handling special value "many". Notice that
+    // the returned value is signed to allow using it in subtraction above.
+    int GetNumFilesFromText(const wxString& text) const
+    {
+        unsigned long n;
+        if ( !text.ToULong(&n) )
+        {
+            if ( text == "many" )
+                n = 9999;
+            else
+                n = 0;
+        }
+
+        return n;
+    }
+
+    // Return the size in KiB from a string with either KiB or MiB suffix.
+    int GetSizeFromText(const wxString& text) const
+    {
+        wxString size;
+        unsigned factor = 1;
+        if ( text.EndsWith(" MiB", &size) )
+            factor = 1024;
+        else if ( !text.EndsWith(" KiB", &size) )
+            return 0;
+
+        unsigned long n = 0;
+        size.ToULong(&n);
+
+        return n*factor;
+    }
+};
+
 // ----------------------------------------------------------------------------
 // Application class
 // ----------------------------------------------------------------------------
 // ----------------------------------------------------------------------------
 // Application class
 // ----------------------------------------------------------------------------
@@ -94,6 +179,7 @@ public:
 private:
     // Event handlers for the menu and wxTreeListCtrl events.
     void OnMultiSelect(wxCommandEvent& event);
 private:
     // Event handlers for the menu and wxTreeListCtrl events.
     void OnMultiSelect(wxCommandEvent& event);
+    void OnFlatList(wxCommandEvent& event);
     void OnCheckboxes(wxCommandEvent& event);
     void OnDumpSelection(wxCommandEvent& event);
     void OnCheckHTMLDocs(wxCommandEvent& event);
     void OnCheckboxes(wxCommandEvent& event);
     void OnDumpSelection(wxCommandEvent& event);
     void OnCheckHTMLDocs(wxCommandEvent& event);
@@ -138,10 +224,14 @@ private:
 
     wxTreeListCtrl* m_treelist;
 
 
     wxTreeListCtrl* m_treelist;
 
+    MyComparator m_comparator;
+
     wxTreeListItem m_itemHTMLDocs;
 
     wxLog* m_oldLogTarget;
 
     wxTreeListItem m_itemHTMLDocs;
 
     wxLog* m_oldLogTarget;
 
+    bool m_isFlat;
+
     wxDECLARE_EVENT_TABLE();
 };
 
     wxDECLARE_EVENT_TABLE();
 };
 
@@ -171,6 +261,7 @@ bool MyApp::OnInit()
 
 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(Id_MultiSelect, MyFrame::OnMultiSelect)
 
 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
     EVT_MENU(Id_MultiSelect, MyFrame::OnMultiSelect)
+    EVT_MENU(Id_FlatList, MyFrame::OnFlatList)
     EVT_MENU_RANGE(Id_Checkboxes_Start, Id_Checkboxes_End,
                    MyFrame::OnCheckboxes)
 
     EVT_MENU_RANGE(Id_Checkboxes_Start, Id_Checkboxes_End,
                    MyFrame::OnCheckboxes)
 
@@ -194,6 +285,8 @@ MyFrame::MyFrame()
        : wxFrame(NULL, wxID_ANY, "wxWidgets tree/list control sample",
                  wxDefaultPosition, wxSize(600, 450))
 {
        : wxFrame(NULL, wxID_ANY, "wxWidgets tree/list control sample",
                  wxDefaultPosition, wxSize(600, 450))
 {
+    m_isFlat = false;
+
     // Create menus and status bar.
     SetIcon(wxICON(sample));
 
     // Create menus and status bar.
     SetIcon(wxICON(sample));
 
@@ -211,6 +304,8 @@ MyFrame::MyFrame()
                                "&3-state checkboxes\tCtrl-3");
     treeStyle->AppendRadioItem(Id_CheckboxesUser3State,
                                "&User-settable 3-state checkboxes\tCtrl-4");
                                "&3-state checkboxes\tCtrl-3");
     treeStyle->AppendRadioItem(Id_CheckboxesUser3State,
                                "&User-settable 3-state checkboxes\tCtrl-4");
+    treeStyle->AppendSeparator();
+    treeStyle->AppendCheckItem(Id_FlatList, "&Flat list");
 
     wxMenu* treeOper = new wxMenu;
     treeOper->Append(Id_DumpSelection, "&Dump selection\tCtrl-D");
 
     wxMenu* treeOper = new wxMenu;
     treeOper->Append(Id_DumpSelection, "&Dump selection\tCtrl-D");
@@ -295,24 +390,23 @@ wxTreeListCtrl* MyFrame::CreateTreeListCtrl(long style)
                                   style);
     tree->SetImageList(m_imageList);
 
                                   style);
     tree->SetImageList(m_imageList);
 
-    enum
-    {
-        Col_Component,
-        Col_Files,
-        Col_Size
-    };
-
-    tree->AppendColumn("Component");
+    tree->AppendColumn("Component",
+                       wxCOL_WIDTH_AUTOSIZE,
+                       wxALIGN_LEFT,
+                       wxCOL_RESIZABLE | wxCOL_SORTABLE);
     tree->AppendColumn("# Files",
                        tree->WidthFor("1,000,000"),
     tree->AppendColumn("# Files",
                        tree->WidthFor("1,000,000"),
-                       wxALIGN_RIGHT);
+                       wxALIGN_RIGHT,
+                       wxCOL_RESIZABLE | wxCOL_SORTABLE);
     tree->AppendColumn("Size",
                        tree->WidthFor("1,000,000 KiB"),
     tree->AppendColumn("Size",
                        tree->WidthFor("1,000,000 KiB"),
-                       wxALIGN_RIGHT);
+                       wxALIGN_RIGHT,
+                       wxCOL_RESIZABLE | wxCOL_SORTABLE);
 
     // Define a shortcut to save on typing here.
     #define ADD_ITEM(item, parent, files, size) \
 
     // Define a shortcut to save on typing here.
     #define ADD_ITEM(item, parent, files, size) \
-        wxTreeListItem item = tree->AppendItem(parent, #item, \
+        wxTreeListItem item = tree->AppendItem(m_isFlat ? root : parent, \
+                                               #item, \
                                                Icon_FolderClosed, \
                                                Icon_FolderOpened); \
         tree->SetItemText(item, Col_Files, files); \
                                                Icon_FolderClosed, \
                                                Icon_FolderOpened); \
         tree->SetItemText(item, Col_Files, files); \
@@ -341,6 +435,9 @@ wxTreeListCtrl* MyFrame::CreateTreeListCtrl(long style)
     // Remember this one for subsequent tests.
     m_itemHTMLDocs = HTML;
 
     // Remember this one for subsequent tests.
     m_itemHTMLDocs = HTML;
 
+    // Set a custom comparator to compare strings containing numbers correctly.
+    tree->SetItemComparator(&m_comparator);
+
     return tree;
 }
 
     return tree;
 }
 
@@ -367,6 +464,13 @@ void MyFrame::OnMultiSelect(wxCommandEvent& event)
     RecreateTreeListCtrl(style);
 }
 
     RecreateTreeListCtrl(style);
 }
 
+void MyFrame::OnFlatList(wxCommandEvent& event)
+{
+    m_isFlat = event.IsChecked();
+
+    RecreateTreeListCtrl(m_treelist->GetWindowStyle());
+}
+
 void MyFrame::OnCheckboxes(wxCommandEvent& event)
 {
     long style = m_treelist->GetWindowStyle();
 void MyFrame::OnCheckboxes(wxCommandEvent& event)
 {
     long style = m_treelist->GetWindowStyle();
@@ -499,7 +603,14 @@ const char* MyFrame::CheckedStateString(wxCheckBoxState state)
 
 void MyFrame::OnSelectionChanged(wxTreeListEvent& event)
 {
 
 void MyFrame::OnSelectionChanged(wxTreeListEvent& event)
 {
-    wxLogMessage("Selection changed to \"%s\"", DumpItem(event.GetItem()));
+    const char* msg;
+
+    if ( m_treelist->HasFlag(wxTL_MULTIPLE) )
+        msg = "Selection of the \"%s\" item changed.";
+    else
+        msg = "Selection changed, now is \"%s\".";
+
+    wxLogMessage(msg, DumpItem(event.GetItem()));
 }
 
 void MyFrame::OnItemExpanding(wxTreeListEvent& event)
 }
 
 void MyFrame::OnItemExpanding(wxTreeListEvent& event)