Refactor rebase commit list: Don't fill list again and again when reordering commits
[TortoiseGit.git] / src / TortoiseProc / ResizableColumnsListCtrl.h
blob3251cec6776ed0dc02b8a1f00776e0e3e988181e
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2016 - TortoiseGit
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software Foundation,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #pragma once
20 #include "ColumnManager.h"
22 template <typename BaseType> class CResizableColumnsListCtrl : public BaseType
24 public:
25 CResizableColumnsListCtrl()
26 : BaseType()
27 , m_ColumnManager(this)
28 , m_bAllowHiding(true)
31 DECLARE_MESSAGE_MAP()
33 protected:
34 void OnDestroy()
36 SaveColumnWidths();
37 BaseType::OnDestroy();
40 void OnHeaderDblClick(NMHDR* pNMHDR, LRESULT* pResult)
42 m_ColumnManager.OnHeaderDblClick(pNMHDR, pResult);
44 *pResult = FALSE;
47 void OnColumnResized(NMHDR* pNMHDR, LRESULT* pResult)
49 m_ColumnManager.OnColumnResized(pNMHDR, pResult);
51 *pResult = FALSE;
54 void OnColumnMoved(NMHDR* pNMHDR, LRESULT* pResult)
56 m_ColumnManager.OnColumnMoved(pNMHDR, pResult);
58 Invalidate(FALSE);
61 void OnContextMenuHeader(CWnd* pWnd, CPoint point)
63 m_ColumnManager.OnContextMenuHeader(pWnd, point, !!IsGroupViewEnabled());
66 void OnContextMenu(CWnd* pWnd, CPoint point)
68 if (pWnd == GetHeaderCtrl() && m_bAllowHiding)
69 OnContextMenuHeader(pWnd, point);
70 else if (pWnd == this && m_ContextMenuHandler)
71 m_ContextMenuHandler(point);
74 // prevent users from extending our hidden (size 0) columns
75 void OnHdnBegintrack(NMHDR* pNMHDR, LRESULT* pResult)
77 m_ColumnManager.OnHdnBegintrack(pNMHDR, pResult);
80 // prevent any function from extending our hidden (size 0) columns
81 void OnHdnItemchanging(NMHDR* pNMHDR, LRESULT* pResult)
83 if (!m_ColumnManager.OnHdnItemchanging(pNMHDR, pResult))
84 Default();
87 typedef std::function<void(CPoint point)> ContextMenuHandler;
88 ContextMenuHandler m_ContextMenuHandler;
90 public:
91 void Init()
93 CRegDWORD regFullRowSelect(L"Software\\TortoiseGit\\FullRowSelect", TRUE);
94 DWORD exStyle = LVS_EX_HEADERDRAGDROP;
95 if (DWORD(regFullRowSelect))
96 exStyle |= LVS_EX_FULLROWSELECT;
97 SetExtendedStyle(GetExtendedStyle() | exStyle);
100 void SetListContextMenuHandler(ContextMenuHandler pContextMenuHandler)
102 m_ContextMenuHandler = pContextMenuHandler;
105 void AdjustColumnWidths()
107 auto header = GetHeaderCtrl();
108 if (!header)
109 return;
110 int maxcol = header->GetItemCount() - 1;
111 for (int col = 0; col <= maxcol; col++)
112 SetColumnWidth(col, m_ColumnManager.GetWidth(col, true));
114 virtual void SaveColumnWidths()
116 auto header = GetHeaderCtrl();
117 if (!header)
118 return;
119 int maxcol = header->GetItemCount() - 1;
120 for (int col = 0; col <= maxcol; col++)
121 if (m_ColumnManager.IsVisible(col))
122 m_ColumnManager.ColumnResized(col);
124 m_ColumnManager.WriteSettings();
127 bool m_bAllowHiding;
128 ColumnManager m_ColumnManager;
131 BEGIN_TEMPLATE_MESSAGE_MAP(CResizableColumnsListCtrl, BaseType, BaseType)
132 ON_NOTIFY(HDN_BEGINTRACKA, 0, OnHdnBegintrack)
133 ON_NOTIFY(HDN_BEGINTRACKW, 0, OnHdnBegintrack)
134 ON_NOTIFY(HDN_ENDTRACK, 0, OnColumnResized)
135 ON_NOTIFY(HDN_ENDDRAG, 0, OnColumnMoved)
136 ON_NOTIFY(HDN_DIVIDERDBLCLICK, 0, OnHeaderDblClick)
137 ON_NOTIFY(HDN_ITEMCHANGINGA, 0, OnHdnItemchanging)
138 ON_NOTIFY(HDN_ITEMCHANGINGW, 0, OnHdnItemchanging)
139 ON_WM_DESTROY()
140 ON_WM_CONTEXTMENU()
141 END_MESSAGE_MAP()