Right align column contents
[TortoiseGit.git] / src / TortoiseProc / GitStatusListCtrlHelpers.cpp
blob489dbda7c5a2fa7bb23c950326c36b9265240a6b
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008, 2014 - TortoiseSVN
4 // Copyright (C) 2008-2016 - TortoiseGit
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "stdafx.h"
22 #include "resource.h"
23 #include "GitStatusListCtrl.h"
24 #include <iterator>
26 // registry version number of column-settings of GitLogListBase
27 #define GITSLC_COL_VERSION 5
29 #ifndef assert
30 #define assert(x) ATLASSERT(x)
31 #endif
32 // assign property list
33 #if 0
34 PropertyList&
35 PropertyList::operator= (const char* rhs)
37 // do you really want to replace the property list?
39 assert (properties.empty());
40 properties.clear();
42 // add all properties in the list
44 while (rhs && *rhs)
46 const char* next = strchr (rhs, ' ');
48 CString name (rhs, static_cast<int>(!next ? strlen (rhs) : next - rhs));
49 properties.insert (std::make_pair (name, CString()));
51 rhs = !next ? nullptr : next + 1;
54 // done
56 return *this;
59 // collect property names in a set
61 void PropertyList::GetPropertyNames (std::set<CString>& names)
63 for (CIT iter = properties.cbegin(), end = properties.cend()
64 ; iter != end
65 ; ++iter)
67 names.insert (iter->first);
71 // get a property value.
73 CString PropertyList::operator[](const CString& name) const
75 CIT iter = properties.find (name);
77 return iter == properties.end()
78 ? CString()
79 : iter->second;
82 // set a property value.
84 CString& PropertyList::operator[](const CString& name)
86 return properties[name];
89 /// check whether that property has been set on this item.
91 bool PropertyList::HasProperty (const CString& name) const
93 return properties.find (name) != properties.end();
96 // due to frequent use: special check for svn:needs-lock
98 bool PropertyList::IsNeedsLockSet() const
100 static const CString svnNeedsLock = _T("svn:needs-lock");
101 return HasProperty (svnNeedsLock);
104 #endif
105 // registry access
107 void ColumnManager::ReadSettings
108 ( DWORD defaultColumns
109 , DWORD hideColumns
110 , const CString& containerName
111 , int maxsize
112 , int * widthlist)
114 // defaults
115 DWORD selectedStandardColumns = defaultColumns & ~hideColumns;
116 m_dwDefaultColumns = defaultColumns & ~hideColumns;
118 columns.resize (maxsize);
119 int power = 1;
120 for (int i = 0; i < maxsize; ++i)
122 columns[i].index = static_cast<int>(i);
123 if (!widthlist)
124 columns[i].width = 0;
125 else
126 columns[i].width = widthlist[i];
127 columns[i].visible = true;
128 columns[i].relevant = !(hideColumns & power);
129 power *= 2;
132 // userProps.clear();
134 // where the settings are stored within the registry
136 registryPrefix = _T("Software\\TortoiseGit\\StatusColumns\\") + containerName;
138 // we accept settings of current version only
139 bool valid = (DWORD)CRegDWORD (registryPrefix + _T("Version"), 0xff) == GITSLC_COL_VERSION;
140 if (valid)
142 // read (possibly different) column selection
144 selectedStandardColumns = CRegDWORD (registryPrefix, selectedStandardColumns) & ~hideColumns;
146 // read column widths
148 CString colWidths = CRegString (registryPrefix + _T("_Width"));
150 ParseWidths (colWidths);
153 // process old-style visibility setting
155 SetStandardColumnVisibility (selectedStandardColumns);
157 // clear all previously set header columns
159 int c = control->GetHeaderCtrl()->GetItemCount() - 1;
160 while (c>=0)
161 control->DeleteColumn(c--);
163 // create columns
165 for (int i = 0, count = GetColumnCount(); i < count; ++i)
166 control->InsertColumn (i, GetName(i), LVCFMT_LEFT, IsVisible(i)&&IsRelevant(i) ? -1 : GetVisibleWidth(i, false));
168 // restore column ordering
170 if (valid)
171 ParseColumnOrder (CRegString (registryPrefix + _T("_Order")));
172 else
173 ParseColumnOrder (CString());
175 ApplyColumnOrder();
177 // auto-size the columns so we can see them while fetching status
178 // (seems the same values will not take affect in InsertColumn)
180 for (int i = 0, count = GetColumnCount(); i < count; ++i)
181 if (IsVisible(i))
182 control->SetColumnWidth (i, GetVisibleWidth (i, true));
185 void ColumnManager::WriteSettings() const
187 CRegDWORD regVersion (registryPrefix + _T("Version"), 0, TRUE);
188 regVersion = GITSLC_COL_VERSION;
190 // write (possibly different) column selection
192 CRegDWORD regStandardColumns (registryPrefix, 0, TRUE);
193 regStandardColumns = GetSelectedStandardColumns();
195 // write column widths
197 CRegString regWidths (registryPrefix + _T("_Width"), CString(), TRUE);
198 regWidths = GetWidthString();
200 // write column ordering
202 CRegString regColumnOrder (registryPrefix + _T("_Order"), CString(), TRUE);
203 regColumnOrder = GetColumnOrderString();
206 // read column definitions
208 int ColumnManager::GetColumnCount() const
210 return static_cast<int>(columns.size());
213 bool ColumnManager::IsVisible (int column) const
215 size_t index = static_cast<size_t>(column);
216 assert (columns.size() > index);
218 return columns[index].visible;
221 int ColumnManager::GetInvisibleCount() const
223 int invisibleCount = 0;
224 for (const auto& column : columns)
226 if (!column.visible)
227 invisibleCount++;
229 return invisibleCount;
232 bool ColumnManager::IsRelevant (int column) const
234 size_t index = static_cast<size_t>(column);
235 assert (columns.size() > index);
237 return columns[index].relevant;
240 int ColumnManager::SetNames(UINT* buffer, int size)
242 itemName.clear();
243 for (int i = 0; i < size; ++i)
244 itemName.push_back(*buffer++);
245 return 0;
248 void ColumnManager::SetRightAlign(int column) const
250 assert(column < columns.size());
252 if (!IsVisible(column))
253 return;
255 LVCOLUMN col = { 0 };
256 col.mask = LVCF_FMT;
257 col.fmt = LVCFMT_RIGHT;
258 control->SetColumn(column, &col);
260 control->Invalidate(FALSE);
263 CString ColumnManager::GetName (int column) const
265 // standard columns
266 size_t index = static_cast<size_t>(column);
267 if (index < itemName.size())
269 CString result;
270 result.LoadString (itemName[index]);
271 return result;
274 // user-prop columns
276 // if (index < columns.size())
277 // return userProps[columns[index].index - SVNSLC_USERPROPCOLOFFSET].name;
279 // default: empty
281 return CString();
284 int ColumnManager::GetWidth (int column, bool useDefaults) const
286 size_t index = static_cast<size_t>(column);
287 assert (columns.size() > index);
289 int width = columns[index].width;
290 if ((width == 0) && useDefaults)
291 width = LVSCW_AUTOSIZE;
293 return width;
296 int ColumnManager::GetVisibleWidth (int column, bool useDefaults) const
298 return IsVisible (column)
299 ? GetWidth (column, useDefaults)
300 : 0;
303 // switch columns on and off
305 void ColumnManager::SetVisible
306 ( int column
307 , bool visible)
309 size_t index = static_cast<size_t>(column);
310 assert (index < columns.size());
312 if (columns[index].visible != visible)
314 columns[index].visible = visible;
315 columns[index].relevant |= visible;
316 if (!visible)
317 columns[index].width = 0;
319 control->SetColumnWidth (column, GetVisibleWidth (column, true));
320 ApplyColumnOrder();
322 control->Invalidate (FALSE);
326 // tracking column modifications
328 void ColumnManager::ColumnMoved (int column, int position)
330 // in front of what column has it been inserted?
332 int index = columns[column].index;
334 std::vector<int> gridColumnOrder = GetGridColumnOrder();
336 size_t visiblePosition = static_cast<size_t>(position);
337 size_t columnCount = gridColumnOrder.size();
339 int next = -1;
340 if (visiblePosition < columnCount - 1)
342 // the new position (visiblePosition) is the column id w/o the moved column
343 gridColumnOrder.erase(std::find(gridColumnOrder.cbegin(), gridColumnOrder.cend(), index));
344 next = gridColumnOrder[visiblePosition];
347 // move logical column index just in front of that "next" column
349 columnOrder.erase(std::find(columnOrder.cbegin(), columnOrder.cend(), index));
350 columnOrder.insert(std::find(columnOrder.cbegin(), columnOrder.cend(), next), index);
352 // make sure, invisible columns are still put in front of all others
354 ApplyColumnOrder();
357 void ColumnManager::ColumnResized (int column)
359 size_t index = static_cast<size_t>(column);
360 assert (index < columns.size());
361 assert (columns[index].visible);
363 int width = control->GetColumnWidth (column);
364 columns[index].width = width;
366 control->Invalidate (FALSE);
369 void ColumnManager::RemoveUnusedProps()
371 // determine what column indexes / IDs to keep.
372 // map them onto new IDs (we may delete some IDs in between)
374 std::map<int, int> validIndices;
376 for (size_t i = 0, count = columns.size(); i < count; ++i)
378 int index = columns[i].index;
380 if (itemProps.find (GetName((int)i)) != itemProps.end()
381 || columns[i].visible)
383 validIndices[index] = index;
387 // remove everything else:
389 // remove from columns and control.
390 // also update index values in columns
392 for (size_t i = columns.size(); i > 0; --i)
394 std::map<int, int>::const_iterator iter
395 = validIndices.find (columns[i-1].index);
397 if (iter == validIndices.end())
399 control->DeleteColumn (static_cast<int>(i-1));
400 columns.erase(columns.cbegin() + i - 1);
402 else
404 columns[i-1].index = iter->second;
408 // remove from and update column order
410 for (size_t i = columnOrder.size(); i > 0; --i)
412 std::map<int, int>::const_iterator iter
413 = validIndices.find (columnOrder[i-1]);
415 if (iter == validIndices.end())
416 columnOrder.erase(columnOrder.cbegin() + i - 1);
417 else
418 columnOrder[i-1] = iter->second;
422 // bring everything back to its "natural" order
424 void ColumnManager::ResetColumns (DWORD defaultColumns)
426 // update internal data
428 std::sort(columnOrder.begin(), columnOrder.end());
430 for (size_t i = 0, count = columns.size(); i < count; ++i)
432 columns[i].width = 0;
433 columns[i].visible = (i < 32) && (((defaultColumns >> i) & 1) != 0);
436 // update UI
438 for (int i = 0, count = GetColumnCount(); i < count; ++i)
439 control->SetColumnWidth (i, GetVisibleWidth (i, true));
441 ApplyColumnOrder();
443 control->Invalidate (FALSE);
446 // initialization utilities
448 void ColumnManager::ParseWidths (const CString& widths)
450 for (int i = 0, count = widths.GetLength() / 8; i < count; ++i)
452 long width = _tcstol (widths.Mid (i * 8, 8), nullptr, 16);
453 if (i < (int)itemName.size())
455 // a standard column
457 columns[i].width = width;
459 else
461 // there is no such column
463 assert (width == 0);
468 void ColumnManager::SetStandardColumnVisibility
469 (DWORD visibility)
471 for (size_t i = 0; i < itemName.size(); ++i)
473 columns[i].visible = (visibility & 1) > 0;
474 visibility /= 2;
478 void ColumnManager::ParseColumnOrder
479 (const CString& widths)
481 std::set<int> alreadyPlaced;
482 columnOrder.clear();
484 // place columns according to valid entries in orderString
486 for (int i = 0, count = widths.GetLength() / 2; i < count; ++i)
488 int index = _tcstol (widths.Mid (i * 2, 2), nullptr, 16);
489 if ((index < (int)itemName.size()))
491 alreadyPlaced.insert (index);
492 columnOrder.push_back (index);
496 // place the remaining colums behind it
498 for (int i = 0; i < (int)itemName.size(); ++i)
499 if (alreadyPlaced.find (i) == alreadyPlaced.end())
500 columnOrder.push_back (i);
503 // map internal column order onto visible column order
504 // (all invisibles in front)
506 std::vector<int> ColumnManager::GetGridColumnOrder() const
508 // extract order of used columns from order of all columns
510 std::vector<int> result;
511 result.reserve (GITSLC_MAXCOLUMNCOUNT+1);
513 size_t colCount = columns.size();
514 bool visible = false;
518 // put invisible cols in front
520 for (size_t i = 0, count = columnOrder.size(); i < count; ++i)
522 int index = columnOrder[i];
523 for (size_t k = 0; k < colCount; ++k)
525 const ColumnInfo& column = columns[k];
526 if ((column.index == index) && (column.visible == visible))
527 result.push_back (static_cast<int>(k));
531 visible = !visible;
533 while (visible);
535 return result;
538 void ColumnManager::ApplyColumnOrder()
540 // extract order of used columns from order of all columns
542 int order[GITSLC_MAXCOLUMNCOUNT + 1] = { 0 };
544 std::vector<int> gridColumnOrder = GetGridColumnOrder();
545 std::copy(gridColumnOrder.cbegin(), gridColumnOrder.cend(), stdext::checked_array_iterator<int*>(&order[0], sizeof(order)));
547 // we must have placed all columns or something is really fishy ..
549 assert (gridColumnOrder.size() == columns.size());
550 assert(GetColumnCount() == control->GetHeaderCtrl()->GetItemCount());
552 // o.k., apply our column ordering
554 control->SetColumnOrderArray (GetColumnCount(), order);
557 // utilities used when writing data to the registry
559 DWORD ColumnManager::GetSelectedStandardColumns() const
561 DWORD result = 0;
562 for (size_t i = itemName.size(); i > 0; --i)
563 result = result * 2 + (columns[i-1].visible ? 1 : 0);
565 return result;
568 CString ColumnManager::GetWidthString() const
570 CString result;
572 // regular columns
574 TCHAR buf[10] = { 0 };
575 for (size_t i = 0; i < itemName.size(); ++i)
577 _stprintf_s (buf, 10, _T("%08X"), columns[i].width);
578 result += buf;
581 return result;
584 CString ColumnManager::GetColumnOrderString() const
586 CString result;
588 TCHAR buf[3] = { 0 };
589 for (size_t i = 0, count = columnOrder.size(); i < count; ++i)
591 _stprintf_s (buf, 3, _T("%02X"), columnOrder[i]);
592 result += buf;
595 return result;
598 // sorter utility class, only used by GitStatusList!
600 CSorter::CSorter ( ColumnManager* columnManager
601 , int sortedColumn
602 , bool ascending)
603 : columnManager (columnManager)
604 , sortedColumn (sortedColumn)
605 , ascending (ascending)
609 bool CSorter::operator() (const CTGitPath* entry1 , const CTGitPath* entry2) const
611 #define SGN(x) ((x)==0?0:((x)>0?1:-1))
613 int result = 0;
614 switch (sortedColumn)
616 case 7: // File size
618 if (result == 0)
620 __int64 fileSize1 = entry1->IsDirectory() ? 0 : entry1->GetFileSize();
621 __int64 fileSize2 = entry2->IsDirectory() ? 0 : entry2->GetFileSize();
623 result = int(fileSize1 - fileSize2);
625 break;
627 case 6: //Last Modification Date
629 if (result == 0)
631 __int64 writetime1 = entry1->GetLastWriteTime();
632 __int64 writetime2 = entry2->GetLastWriteTime();
634 FILETIME* filetime1 = (FILETIME*)(__int64*)&writetime1;
635 FILETIME* filetime2 = (FILETIME*)(__int64*)&writetime2;
637 result = CompareFileTime(filetime1, filetime2);
639 break;
641 case 5: //Del Number
643 if (result == 0)
645 // result = entry1->lock_comment.CompareNoCase(entry2->lock_comment);
646 result = A2L(entry1->m_StatDel)-A2L(entry2->m_StatDel);
648 break;
650 case 4: //Add Number
652 if (result == 0)
654 //result = entry1->lock_owner.CompareNoCase(entry2->lock_owner);
655 result = A2L(entry1->m_StatAdd)-A2L(entry2->m_StatAdd);
657 break;
660 case 3: // Status
662 if (result == 0)
664 result = entry1->GetActionName(entry1->m_Action).CompareNoCase(entry2->GetActionName(entry2->m_Action));
666 break;
668 case 2: //Ext file
670 if (result == 0)
672 result = entry1->GetFileExtension().CompareNoCase(entry2->GetFileExtension());
674 break;
676 case 1: // File name
678 if (result == 0)
680 result = entry1->GetFileOrDirectoryName().CompareNoCase(entry2->GetFileOrDirectoryName());
682 break;
684 case 0: // Full path column
686 if (result == 0)
688 result = CTGitPath::Compare(entry1->GetGitPathString(), entry2->GetGitPathString());
690 break;
692 } // switch (m_nSortedColumn)
693 // sort by path name as second priority
694 if (sortedColumn > 0 && result == 0)
695 result = CTGitPath::Compare(entry1->GetGitPathString(), entry2->GetGitPathString());
696 if (!ascending)
697 result = -result;
699 return result < 0;