[PATCH setup 10/13] Use indents in category view

Jon Turney jon.turney@dronecode.org.uk
Sun Aug 5 22:11:00 GMT 2018


We keep around an empty imagelist for 1x1 images, to reset the indent when
not in tree view mode

(This isn't quite right as the listview will allocate a 1-pixel space before
column 0 for those images, but this seems the best we can do as 0x0
imagelists aren't allowed...)
---
 ListView.cc         | 29 +++++++++++++++++++++--------
 ListView.h          |  7 ++++++-
 PickCategoryLine.cc |  6 ++++++
 PickCategoryLine.h  |  7 +++++--
 PickPackageLine.cc  |  6 ++++++
 PickPackageLine.h   |  7 +++++--
 PickView.cc         | 10 +++++-----
 PickView.h          |  2 +-
 8 files changed, 55 insertions(+), 19 deletions(-)

diff --git a/ListView.cc b/ListView.cc
index 0c451d1..b0351cd 100644
--- a/ListView.cc
+++ b/ListView.cc
@@ -56,14 +56,17 @@ ListView::init(HWND parent, int id, HeaderList headers)
   // populate with columns
   initColumns(headers);
 
-  // create a small icon imagelist and assign to listview control
+  // create a small icon imagelist
   // (the order of images matches ListViewLine::State enum)
-  HIMAGELIST hImgList = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
-                                         GetSystemMetrics(SM_CYSMICON),
-                                         ILC_COLOR32, 2, 0);
+  hImgList = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
+                              GetSystemMetrics(SM_CYSMICON),
+                              ILC_COLOR32, 2, 0);
   ImageList_AddIcon(hImgList, LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_TREE_PLUS)));
   ImageList_AddIcon(hImgList, LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_TREE_MINUS)));
-  ListView_SetImageList(hWndListView, hImgList, LVSIL_SMALL);
+
+  // create an empty imagelist, used to reset the indent
+  hEmptyImgList = ImageList_Create(1, 1,
+                                   ILC_COLOR32, 2, 0);
 }
 
 void
@@ -172,7 +175,7 @@ ListView::resizeColumns(void)
 }
 
 void
-ListView::setContents(ListViewContents *_contents)
+ListView::setContents(ListViewContents *_contents, bool tree)
 {
   contents = _contents;
 
@@ -185,15 +188,25 @@ ListView::setContents(ListViewContents *_contents)
 
   empty();
 
+  // assign imagelist to listview control (this also sets the size for indents)
+  if (tree)
+    ListView_SetImageList(hWndListView, hImgList, LVSIL_SMALL);
+  else
+    ListView_SetImageList(hWndListView, hEmptyImgList, LVSIL_SMALL);
+
   size_t i;
   for (i = 0; i < contents->size();  i++)
     {
       LVITEM lvi;
-      lvi.mask = LVIF_TEXT | LVIF_IMAGE;
+      lvi.mask = LVIF_TEXT | (tree ? LVIF_IMAGE | LVIF_INDENT : 0);
       lvi.iItem = i;
       lvi.iSubItem = 0;
       lvi.pszText = LPSTR_TEXTCALLBACK;
-      lvi.iImage = I_IMAGECALLBACK;
+      if (tree)
+        {
+          lvi.iImage = I_IMAGECALLBACK;
+          lvi.iIndent = (*contents)[i]->get_indent();
+        }
 
       ListView_InsertItem(hWndListView, &lvi);
     }
diff --git a/ListView.h b/ListView.h
index f5aa1d9..34d6267 100644
--- a/ListView.h
+++ b/ListView.h
@@ -16,6 +16,7 @@
 
 #include "ActionList.h"
 #include "win32.h"
+#include <commctrl.h>
 #include <vector>
 
 // ---------------------------------------------------------------------------
@@ -32,6 +33,7 @@ class ListViewLine
   virtual ~ListViewLine() {};
   virtual const std::string get_text(int col) const = 0;
   virtual State get_state() const = 0;
+  virtual int get_indent() const = 0;
   virtual ActionList *get_actions(int col) const = 0;
   virtual int do_action(int col, int id) = 0;
 };
@@ -66,7 +68,7 @@ class ListView
   void noteColumnWidthEnd();
   void resizeColumns(void);
 
-  void setContents(ListViewContents *contents);
+  void setContents(ListViewContents *contents, bool tree = false);
   void setEmptyText(const char *text);
 
   bool OnNotify (NMHDR *pNmHdr, LRESULT *pResult);
@@ -75,6 +77,9 @@ class ListView
   HWND hWndParent;
   HWND hWndListView;
   HDC dc;
+  HIMAGELIST hImgList;
+  HIMAGELIST hEmptyImgList;
+
   ListViewContents *contents;
   HeaderList headers;
   const char *empty_list_text;
diff --git a/PickCategoryLine.cc b/PickCategoryLine.cc
index 21795b2..e206aee 100644
--- a/PickCategoryLine.cc
+++ b/PickCategoryLine.cc
@@ -76,3 +76,9 @@ PickCategoryLine::get_state() const
 {
   return cat_tree->collapsed() ? State::collapsed : State::expanded;
 }
+
+int
+PickCategoryLine::get_indent() const
+{
+  return indent;
+}
diff --git a/PickCategoryLine.h b/PickCategoryLine.h
index 6b54c3f..a5daa8c 100644
--- a/PickCategoryLine.h
+++ b/PickCategoryLine.h
@@ -23,10 +23,11 @@
 class PickCategoryLine: public ListViewLine
 {
 public:
-  PickCategoryLine (PickView & aView, CategoryTree * _tree, int _pkgcount) :
+  PickCategoryLine (PickView & aView, CategoryTree * _tree, int _pkgcount, int _indent) :
     cat_tree (_tree),
     pkgcount(_pkgcount),
-    theView (aView)
+    theView (aView),
+    indent(_indent)
   {
   };
   ~PickCategoryLine ()
@@ -35,6 +36,7 @@ public:
 
   const std::string get_text(int col) const;
   State get_state() const;
+  int get_indent() const;
   ActionList *get_actions(int col) const;
   int do_action(int col, int action_id);
 
@@ -42,6 +44,7 @@ private:
   CategoryTree * cat_tree;
   int pkgcount;
   PickView & theView;
+  int indent;
 };
 
 #endif /* SETUP_PICKCATEGORYLINE_H */
diff --git a/PickPackageLine.cc b/PickPackageLine.cc
index 67baad2..a602c90 100644
--- a/PickPackageLine.cc
+++ b/PickPackageLine.cc
@@ -143,3 +143,9 @@ PickPackageLine::get_actions(int col_num) const
 
   return NULL;
 }
+
+int
+PickPackageLine::get_indent() const
+{
+  return indent;
+}
diff --git a/PickPackageLine.h b/PickPackageLine.h
index dacad28..53c389b 100644
--- a/PickPackageLine.h
+++ b/PickPackageLine.h
@@ -24,18 +24,21 @@ class PickView;
 class PickPackageLine: public ListViewLine
 {
 public:
-  PickPackageLine (PickView &aView, packagemeta & apkg) :
+  PickPackageLine (PickView &aView, packagemeta & apkg, int aindent) :
     pkg (apkg),
-    theView (aView)
+    theView (aView),
+    indent (aindent)
   {
   };
   const std::string get_text(int col) const;
   State get_state() const { return State::nothing; }
+  int get_indent() const;
   ActionList *get_actions(int col_num) const;
   int do_action(int col, int action_id);
 private:
   packagemeta & pkg;
   PickView & theView;
+  int indent;
 };
 
 #endif /* SETUP_PICKPACKAGELINE_H */
diff --git a/PickView.cc b/PickView.cc
index 6e1af0c..c7ff054 100644
--- a/PickView.cc
+++ b/PickView.cc
@@ -87,7 +87,7 @@ PickView::setViewMode (views mode)
         }
     }
 
-  listview->setContents(&contents);
+  listview->setContents(&contents, view_mode == PickView::views::Category);
 }
 
 PickView::views
@@ -148,12 +148,12 @@ PickView::setObsolete (bool doit)
 }
 
 void
-PickView::insert_pkg (packagemeta & pkg)
+PickView::insert_pkg (packagemeta & pkg, int indent)
 {
   if (!showObsolete && isObsolete (pkg.categories))
     return;
 
-  contents.push_back(new PickPackageLine(*this, pkg));
+  contents.push_back(new PickPackageLine(*this, pkg, indent));
 }
 
 void
@@ -197,7 +197,7 @@ PickView::insert_category (CategoryTree *cat_tree)
     return;
 
   // insert line for the category
-  contents.push_back(new PickCategoryLine(*this, cat_tree, packageCount));
+  contents.push_back(new PickCategoryLine(*this, cat_tree, packageCount, isAll ? 0 : 1));
 
   // if not collapsed
   if (!cat_tree->collapsed())
@@ -212,7 +212,7 @@ PickView::insert_category (CategoryTree *cat_tree)
                   || (*i
                       && StrStrI ((*i)->name.c_str (), packageFilterString.c_str ())))
                 {
-                  insert_pkg(**i);
+                  insert_pkg(**i, 2);
                 }
             }
         }
diff --git a/PickView.h b/PickView.h
index fc9216e..3a6c602 100644
--- a/PickView.h
+++ b/PickView.h
@@ -67,7 +67,7 @@ private:
   CategoryTree *cat_tree_root;
   Window *parent;
 
-  void insert_pkg (packagemeta &);
+  void insert_pkg (packagemeta &, int indent = 0);
   void insert_category (CategoryTree *);
 };
 
-- 
2.17.0



More information about the Cygwin-apps mailing list