Imported Upstream version 9.20
[platform/upstream/7zip.git] / CPP / 7zip / UI / FileManager / PanelSort.cpp
1 // PanelSort.cpp\r
2 \r
3 #include "StdAfx.h"\r
4 \r
5 #include "Windows/PropVariant.h"\r
6 \r
7 #include "../../PropID.h"\r
8 \r
9 #include "Panel.h"\r
10 \r
11 using namespace NWindows;\r
12 \r
13 static UString GetExtension(const UString &name)\r
14 {\r
15   int dotPos = name.ReverseFind(L'.');\r
16   if (dotPos < 0)\r
17     return UString();\r
18   return name.Mid(dotPos);\r
19 }\r
20 \r
21 int CALLBACK CompareItems2(LPARAM lParam1, LPARAM lParam2, LPARAM lpData)\r
22 {\r
23   if (lpData == NULL)\r
24     return 0;\r
25   CPanel *panel = (CPanel*)lpData;\r
26   \r
27   switch(panel->_sortID)\r
28   {\r
29     // if (panel->_sortIndex == 0)\r
30     case kpidName:\r
31     {\r
32       const UString name1 = panel->GetItemName((int)lParam1);\r
33       const UString name2 = panel->GetItemName((int)lParam2);\r
34       int res = name1.CompareNoCase(name2);\r
35       /*\r
36       if (res != 0 || !panel->_flatMode)\r
37         return res;\r
38       const UString prefix1 = panel->GetItemPrefix(lParam1);\r
39       const UString prefix2 = panel->GetItemPrefix(lParam2);\r
40       return res = prefix1.CompareNoCase(prefix2);\r
41       */\r
42       return res;\r
43     }\r
44     case kpidNoProperty:\r
45     {\r
46       return MyCompare(lParam1, lParam2);\r
47     }\r
48     case kpidExtension:\r
49     {\r
50       const UString ext1 = GetExtension(panel->GetItemName((int)lParam1));\r
51       const UString ext2 = GetExtension(panel->GetItemName((int)lParam2));\r
52       return ext1.CompareNoCase(ext2);\r
53     }\r
54   }\r
55   /*\r
56   if (panel->_sortIndex == 1)\r
57     return MyCompare(file1.Size, file2.Size);\r
58   return ::CompareFileTime(&file1.MTime, &file2.MTime);\r
59   */\r
60 \r
61   // PROPID propID = panel->_properties[panel->_sortIndex].ID;\r
62   PROPID propID = panel->_sortID;\r
63 \r
64   NCOM::CPropVariant prop1, prop2;\r
65   // Name must be first property\r
66   panel->_folder->GetProperty((UINT32)lParam1, propID, &prop1);\r
67   panel->_folder->GetProperty((UINT32)lParam2, propID, &prop2);\r
68   if (prop1.vt != prop2.vt)\r
69   {\r
70     return MyCompare(prop1.vt, prop2.vt);\r
71   }\r
72   if (prop1.vt == VT_BSTR)\r
73   {\r
74     return _wcsicmp(prop1.bstrVal, prop2.bstrVal);\r
75   }\r
76   return prop1.Compare(prop2);\r
77   // return 0;\r
78 }\r
79 \r
80 int CALLBACK CompareItems(LPARAM lParam1, LPARAM lParam2, LPARAM lpData)\r
81 {\r
82   if (lpData == NULL) return 0;\r
83   if (lParam1 == kParentIndex) return -1;\r
84   if (lParam2 == kParentIndex) return 1;\r
85 \r
86   CPanel *panel = (CPanel*)lpData;\r
87 \r
88   bool isDir1 = panel->IsItemFolder((int)lParam1);\r
89   bool isDir2 = panel->IsItemFolder((int)lParam2);\r
90   \r
91   if (isDir1 && !isDir2) return -1;\r
92   if (isDir2 && !isDir1) return 1;\r
93 \r
94   int result = CompareItems2(lParam1, lParam2, lpData);\r
95   return panel->_ascending ? result: (-result);\r
96 }\r
97 \r
98 \r
99 /*\r
100 void CPanel::SortItems(int index)\r
101 {\r
102   if (index == _sortIndex)\r
103     _ascending = !_ascending;\r
104   else\r
105   {\r
106     _sortIndex = index;\r
107     _ascending = true;\r
108     switch (_properties[_sortIndex].ID)\r
109     {\r
110       case kpidSize:\r
111       case kpidPackedSize:\r
112       case kpidCTime:\r
113       case kpidATime:\r
114       case kpidMTime:\r
115       _ascending = false;\r
116       break;\r
117     }\r
118   }\r
119   _listView.SortItems(CompareItems, (LPARAM)this);\r
120   _listView.EnsureVisible(_listView.GetFocusedItem(), false);\r
121 }\r
122 void CPanel::SortItemsWithPropID(PROPID propID)\r
123 {\r
124   int index = _properties.FindItemWithID(propID);\r
125   if (index >= 0)\r
126     SortItems(index);\r
127 }\r
128 */\r
129 void CPanel::SortItemsWithPropID(PROPID propID)\r
130 {\r
131   if (propID == _sortID)\r
132     _ascending = !_ascending;\r
133   else\r
134   {\r
135     _sortID = propID;\r
136     _ascending = true;\r
137     switch (propID)\r
138     {\r
139       case kpidSize:\r
140       case kpidPackSize:\r
141       case kpidCTime:\r
142       case kpidATime:\r
143       case kpidMTime:\r
144         _ascending = false;\r
145       break;\r
146     }\r
147   }\r
148   _listView.SortItems(CompareItems, (LPARAM)this);\r
149   _listView.EnsureVisible(_listView.GetFocusedItem(), false);\r
150 }\r
151 \r
152 \r
153 void CPanel::OnColumnClick(LPNMLISTVIEW info)\r
154 {\r
155   /*\r
156   int index = _properties.FindItemWithID(_visibleProperties[info->iSubItem].ID);\r
157   SortItems(index);\r
158   */\r
159   SortItemsWithPropID(_visibleProperties[info->iSubItem].ID);\r
160 }\r