Use correct length for buffer
[TortoiseGit.git] / src / TortoiseProc / GitStatusListCtrlHelpers.cpp
blobb83614d95cd3ba2923e1b351ec8c97fee3fc4a07
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 both GitLogListBase and GitStatusListCtrl
27 #define GITSLC_COL_VERSION 6
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 columns[i].adjusted = false;
130 power *= 2;
133 // userProps.clear();
135 // where the settings are stored within the registry
137 registryPrefix = _T("Software\\TortoiseGit\\StatusColumns\\") + containerName;
139 // we accept settings of current version only
140 bool valid = (DWORD)CRegDWORD (registryPrefix + _T("Version"), 0xff) == GITSLC_COL_VERSION;
141 if (valid)
143 // read (possibly different) column selection
145 selectedStandardColumns = CRegDWORD (registryPrefix, selectedStandardColumns) & ~hideColumns;
147 // read column widths
149 CString colWidths = CRegString (registryPrefix + _T("_Width"));
151 ParseWidths (colWidths);
154 // process old-style visibility setting
156 SetStandardColumnVisibility (selectedStandardColumns);
158 // clear all previously set header columns
160 int c = control->GetHeaderCtrl()->GetItemCount() - 1;
161 while (c>=0)
162 control->DeleteColumn(c--);
164 // create columns
166 for (int i = 0, count = GetColumnCount(); i < count; ++i)
167 control->InsertColumn (i, GetName(i), LVCFMT_LEFT, IsVisible(i)&&IsRelevant(i) ? -1 : GetVisibleWidth(i, false));
169 // restore column ordering
171 if (valid)
172 ParseColumnOrder (CRegString (registryPrefix + _T("_Order")));
173 else
174 ParseColumnOrder (CString());
176 ApplyColumnOrder();
178 // auto-size the columns so we can see them while fetching status
179 // (seems the same values will not take affect in InsertColumn)
181 for (int i = 0, count = GetColumnCount(); i < count; ++i)
182 if (IsVisible(i))
183 control->SetColumnWidth (i, GetVisibleWidth (i, true));
186 void ColumnManager::WriteSettings() const
188 CRegDWORD regVersion (registryPrefix + _T("Version"), 0, TRUE);
189 regVersion = GITSLC_COL_VERSION;
191 // write (possibly different) column selection
193 CRegDWORD regStandardColumns (registryPrefix, 0, TRUE);
194 regStandardColumns = GetSelectedStandardColumns();
196 // write column widths
198 CRegString regWidths (registryPrefix + _T("_Width"), CString(), TRUE);
199 regWidths = GetWidthString();
201 // write column ordering
203 CRegString regColumnOrder (registryPrefix + _T("_Order"), CString(), TRUE);
204 regColumnOrder = GetColumnOrderString();
207 // read column definitions
209 int ColumnManager::GetColumnCount() const
211 return static_cast<int>(columns.size());
214 bool ColumnManager::IsVisible (int column) const
216 size_t index = static_cast<size_t>(column);
217 assert (columns.size() > index);
219 return columns[index].visible;
222 int ColumnManager::GetInvisibleCount() const
224 int invisibleCount = 0;
225 for (const auto& column : columns)
227 if (!column.visible)
228 invisibleCount++;
230 return invisibleCount;
233 bool ColumnManager::IsRelevant (int column) const
235 size_t index = static_cast<size_t>(column);
236 assert (columns.size() > index);
238 return columns[index].relevant;
241 int ColumnManager::SetNames(UINT* buffer, int size)
243 itemName.clear();
244 for (int i = 0; i < size; ++i)
245 itemName.push_back(*buffer++);
246 return 0;
249 void ColumnManager::SetRightAlign(int column) const
251 assert(column < columns.size());
253 LVCOLUMN col = { 0 };
254 col.mask = LVCF_FMT;
255 col.fmt = LVCFMT_RIGHT;
256 control->SetColumn(column, &col);
258 control->Invalidate(FALSE);
261 CString ColumnManager::GetName (int column) const
263 // standard columns
264 size_t index = static_cast<size_t>(column);
265 if (index < itemName.size())
267 CString result;
268 result.LoadString (itemName[index]);
269 return result;
272 // user-prop columns
274 // if (index < columns.size())
275 // return userProps[columns[index].index - SVNSLC_USERPROPCOLOFFSET].name;
277 // default: empty
279 return CString();
282 int ColumnManager::GetWidth (int column, bool useDefaults) const
284 size_t index = static_cast<size_t>(column);
285 assert (columns.size() > index);
287 int width = columns[index].width;
288 if ((width == 0) && useDefaults)
290 if (index > 0)
292 int cx = control->GetStringWidth(GetName(column)) + 20; // 20 pixels for col separator and margin
294 for (int i = 0, itemCnt = control->GetItemCount(); i < itemCnt; ++i)
296 // get the width of the string and add 14 pixels for the column separator and margins
297 int linewidth = control->GetStringWidth(control->GetItemText(i, column)) + 14;
298 if (cx < linewidth)
299 cx = linewidth;
301 return cx;
303 return LVSCW_AUTOSIZE_USEHEADER;
305 return width;
308 int ColumnManager::GetVisibleWidth (int column, bool useDefaults) const
310 return IsVisible (column)
311 ? GetWidth (column, useDefaults)
312 : 0;
315 // switch columns on and off
317 void ColumnManager::SetVisible
318 ( int column
319 , bool visible)
321 size_t index = static_cast<size_t>(column);
322 assert (index < columns.size());
324 if (columns[index].visible != visible)
326 columns[index].visible = visible;
327 columns[index].relevant |= visible;
328 if (!visible)
329 columns[index].width = 0;
331 control->SetColumnWidth (column, GetVisibleWidth (column, true));
332 ApplyColumnOrder();
334 control->Invalidate (FALSE);
338 // tracking column modifications
340 void ColumnManager::ColumnMoved (int column, int position)
342 // in front of what column has it been inserted?
344 int index = columns[column].index;
346 std::vector<int> gridColumnOrder = GetGridColumnOrder();
348 size_t visiblePosition = static_cast<size_t>(position);
349 size_t columnCount = gridColumnOrder.size();
351 int next = -1;
352 if (visiblePosition < columnCount - 1)
354 // the new position (visiblePosition) is the column id w/o the moved column
355 gridColumnOrder.erase(std::find(gridColumnOrder.cbegin(), gridColumnOrder.cend(), index));
356 next = gridColumnOrder[visiblePosition];
359 // move logical column index just in front of that "next" column
361 columnOrder.erase(std::find(columnOrder.cbegin(), columnOrder.cend(), index));
362 columnOrder.insert(std::find(columnOrder.cbegin(), columnOrder.cend(), next), index);
364 // make sure, invisible columns are still put in front of all others
366 ApplyColumnOrder();
369 void ColumnManager::ColumnResized(int column, int manual)
371 size_t index = static_cast<size_t>(column);
372 assert (index < columns.size());
373 assert (columns[index].visible);
375 int width = control->GetColumnWidth (column);
376 if (manual != 0)
377 columns[index].adjusted = (manual < 3);
378 if (manual == 2)
380 control->SetColumnWidth(column, LVSCW_AUTOSIZE);
381 columns[index].width = control->GetColumnWidth(column);
383 else if (manual == 3)
385 columns[index].width = 0;
386 control->SetColumnWidth(column, GetWidth(column, true));
388 else
389 columns[index].width = width;
391 control->Invalidate (FALSE);
394 void ColumnManager::RemoveUnusedProps()
396 // determine what column indexes / IDs to keep.
397 // map them onto new IDs (we may delete some IDs in between)
399 std::map<int, int> validIndices;
401 for (size_t i = 0, count = columns.size(); i < count; ++i)
403 int index = columns[i].index;
405 if (itemProps.find (GetName((int)i)) != itemProps.end()
406 || columns[i].visible)
408 validIndices[index] = index;
412 // remove everything else:
414 // remove from columns and control.
415 // also update index values in columns
417 for (size_t i = columns.size(); i > 0; --i)
419 std::map<int, int>::const_iterator iter
420 = validIndices.find (columns[i-1].index);
422 if (iter == validIndices.end())
424 control->DeleteColumn (static_cast<int>(i-1));
425 columns.erase(columns.cbegin() + i - 1);
427 else
429 columns[i-1].index = iter->second;
433 // remove from and update column order
435 for (size_t i = columnOrder.size(); i > 0; --i)
437 std::map<int, int>::const_iterator iter
438 = validIndices.find (columnOrder[i-1]);
440 if (iter == validIndices.end())
441 columnOrder.erase(columnOrder.cbegin() + i - 1);
442 else
443 columnOrder[i-1] = iter->second;
447 // bring everything back to its "natural" order
449 void ColumnManager::ResetColumns (DWORD defaultColumns)
451 // update internal data
453 std::sort(columnOrder.begin(), columnOrder.end());
455 for (size_t i = 0, count = columns.size(); i < count; ++i)
457 columns[i].width = 0;
458 columns[i].visible = (i < 32) && (((defaultColumns >> i) & 1) != 0);
459 columns[i].adjusted = false;
462 // update UI
464 for (int i = 0, count = GetColumnCount(); i < count; ++i)
465 control->SetColumnWidth (i, GetVisibleWidth (i, true));
467 ApplyColumnOrder();
469 control->Invalidate (FALSE);
472 // initialization utilities
474 void ColumnManager::ParseWidths (const CString& widths)
476 for (int i = 0, count = widths.GetLength() / 8; i < count; ++i)
478 long width = _tcstol (widths.Mid (i * 8, 8), nullptr, 16);
479 if (i < (int)itemName.size())
481 // a standard column
482 if (width != MAXLONG)
484 columns[i].width = width;
485 columns[i].adjusted = true;
488 else
490 // there is no such column
492 assert (width == 0);
497 void ColumnManager::SetStandardColumnVisibility
498 (DWORD visibility)
500 for (size_t i = 0; i < itemName.size(); ++i)
502 columns[i].visible = (visibility & 1) > 0;
503 visibility /= 2;
507 void ColumnManager::ParseColumnOrder
508 (const CString& widths)
510 std::set<int> alreadyPlaced;
511 columnOrder.clear();
513 // place columns according to valid entries in orderString
515 for (int i = 0, count = widths.GetLength() / 2; i < count; ++i)
517 int index = _tcstol (widths.Mid (i * 2, 2), nullptr, 16);
518 if ((index < (int)itemName.size()))
520 alreadyPlaced.insert (index);
521 columnOrder.push_back (index);
525 // place the remaining colums behind it
527 for (int i = 0; i < (int)itemName.size(); ++i)
528 if (alreadyPlaced.find (i) == alreadyPlaced.end())
529 columnOrder.push_back (i);
532 // map internal column order onto visible column order
533 // (all invisibles in front)
535 std::vector<int> ColumnManager::GetGridColumnOrder() const
537 // extract order of used columns from order of all columns
539 std::vector<int> result;
540 result.reserve (GITSLC_MAXCOLUMNCOUNT+1);
542 size_t colCount = columns.size();
543 bool visible = false;
547 // put invisible cols in front
549 for (size_t i = 0, count = columnOrder.size(); i < count; ++i)
551 int index = columnOrder[i];
552 for (size_t k = 0; k < colCount; ++k)
554 const ColumnInfo& column = columns[k];
555 if ((column.index == index) && (column.visible == visible))
556 result.push_back (static_cast<int>(k));
560 visible = !visible;
562 while (visible);
564 return result;
567 void ColumnManager::ApplyColumnOrder()
569 // extract order of used columns from order of all columns
571 int order[GITSLC_MAXCOLUMNCOUNT + 1] = { 0 };
573 std::vector<int> gridColumnOrder = GetGridColumnOrder();
574 std::copy(gridColumnOrder.cbegin(), gridColumnOrder.cend(), stdext::checked_array_iterator<int*>(&order[0], sizeof(order)));
576 // we must have placed all columns or something is really fishy ..
578 assert (gridColumnOrder.size() == columns.size());
579 assert(GetColumnCount() == control->GetHeaderCtrl()->GetItemCount());
581 // o.k., apply our column ordering
583 control->SetColumnOrderArray (GetColumnCount(), order);
586 // utilities used when writing data to the registry
588 DWORD ColumnManager::GetSelectedStandardColumns() const
590 DWORD result = 0;
591 for (size_t i = itemName.size(); i > 0; --i)
592 result = result * 2 + (columns[i-1].visible ? 1 : 0);
594 return result;
597 CString ColumnManager::GetWidthString() const
599 CString result;
601 // regular columns
603 TCHAR buf[10] = { 0 };
604 for (size_t i = 0; i < itemName.size(); ++i)
606 _stprintf_s (buf, 10, L"%08X", columns[i].adjusted ? columns[i].width : MAXLONG);
607 result += buf;
610 return result;
613 CString ColumnManager::GetColumnOrderString() const
615 CString result;
617 TCHAR buf[3] = { 0 };
618 for (size_t i = 0, count = columnOrder.size(); i < count; ++i)
620 _stprintf_s (buf, 3, _T("%02X"), columnOrder[i]);
621 result += buf;
624 return result;
627 // sorter utility class, only used by GitStatusList!
629 CSorter::CSorter ( ColumnManager* columnManager
630 , int sortedColumn
631 , bool ascending)
632 : columnManager (columnManager)
633 , sortedColumn (sortedColumn)
634 , ascending (ascending)
638 bool CSorter::operator() (const CTGitPath* entry1 , const CTGitPath* entry2) const
640 #define SGN(x) ((x)==0?0:((x)>0?1:-1))
642 int result = 0;
643 switch (sortedColumn)
645 case 7: // File size
647 if (result == 0)
649 __int64 fileSize1 = entry1->IsDirectory() ? 0 : entry1->GetFileSize();
650 __int64 fileSize2 = entry2->IsDirectory() ? 0 : entry2->GetFileSize();
652 result = int(fileSize1 - fileSize2);
654 break;
656 case 6: //Last Modification Date
658 if (result == 0)
660 __int64 writetime1 = entry1->GetLastWriteTime();
661 __int64 writetime2 = entry2->GetLastWriteTime();
663 FILETIME* filetime1 = (FILETIME*)(__int64*)&writetime1;
664 FILETIME* filetime2 = (FILETIME*)(__int64*)&writetime2;
666 result = CompareFileTime(filetime1, filetime2);
668 break;
670 case 5: //Del Number
672 if (result == 0)
674 // result = entry1->lock_comment.CompareNoCase(entry2->lock_comment);
675 result = A2L(entry1->m_StatDel)-A2L(entry2->m_StatDel);
677 break;
679 case 4: //Add Number
681 if (result == 0)
683 //result = entry1->lock_owner.CompareNoCase(entry2->lock_owner);
684 result = A2L(entry1->m_StatAdd)-A2L(entry2->m_StatAdd);
686 break;
689 case 3: // Status
691 if (result == 0)
693 result = entry1->GetActionName(entry1->m_Action).CompareNoCase(entry2->GetActionName(entry2->m_Action));
695 break;
697 case 2: //Ext file
699 if (result == 0)
701 result = entry1->GetFileExtension().CompareNoCase(entry2->GetFileExtension());
703 break;
705 case 1: // File name
707 if (result == 0)
709 result = entry1->GetFileOrDirectoryName().CompareNoCase(entry2->GetFileOrDirectoryName());
711 break;
713 case 0: // Full path column
715 if (result == 0)
717 result = CTGitPath::Compare(entry1->GetGitPathString(), entry2->GetGitPathString());
719 break;
721 } // switch (m_nSortedColumn)
722 // sort by path name as second priority
723 if (sortedColumn > 0 && result == 0)
724 result = CTGitPath::Compare(entry1->GetGitPathString(), entry2->GetGitPathString());
725 if (!ascending)
726 result = -result;
728 return result < 0;