OSDN Git Service

Add swtich fetch branch tag command at ContextMenu.cpp
[tortoisegit/TortoiseGitJp.git] / src / TortoiseShell / ItemIDList.cpp
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2006 - Stefan Kueng\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "StdAfx.h"\r
20 #include "ShellExt.h"\r
21 #include "ItemIDList.h"\r
22 \r
23 \r
24 ItemIDList::ItemIDList(LPCITEMIDLIST item, LPCITEMIDLIST parent) :\r
25           item_ (item)\r
26         , parent_ (parent)\r
27         , count_ (-1)\r
28 {\r
29 }\r
30 \r
31 ItemIDList::~ItemIDList()\r
32 {\r
33 }\r
34 \r
35 int ItemIDList::size() const\r
36 {\r
37         if (count_ == -1)\r
38         {\r
39                 count_ = 0;\r
40                 if (item_)\r
41                 {\r
42                         LPCSHITEMID ptr = &item_->mkid;\r
43                         while (ptr != 0 && ptr->cb != 0)\r
44                         {\r
45                                 ++count_;\r
46                                 LPBYTE byte = (LPBYTE) ptr;\r
47                                 byte += ptr->cb;\r
48                                 ptr = (LPCSHITEMID) byte;\r
49                         }\r
50                 }\r
51         }\r
52         return count_;\r
53 }\r
54 \r
55 LPCSHITEMID ItemIDList::get(int index) const\r
56 {\r
57         int count = 0;\r
58 \r
59         if (item_ == NULL)\r
60                 return NULL;\r
61         LPCSHITEMID ptr = &item_->mkid;\r
62         if (ptr == NULL)\r
63                 return NULL;\r
64         while (ptr->cb != 0)\r
65         {\r
66                 if (count == index)\r
67                         break;\r
68 \r
69                 ++count;\r
70                 LPBYTE byte = (LPBYTE) ptr;\r
71                 byte += ptr->cb;\r
72                 ptr = (LPCSHITEMID) byte;\r
73         }\r
74         return ptr;\r
75 \r
76 }\r
77 stdstring ItemIDList::toString()\r
78 {\r
79         IShellFolder *shellFolder = NULL;\r
80         IShellFolder *parentFolder = NULL;\r
81         STRRET name;\r
82         TCHAR * szDisplayName = NULL;\r
83         stdstring ret;\r
84         HRESULT hr;\r
85 \r
86         hr = ::SHGetDesktopFolder(&shellFolder);\r
87         if (!SUCCEEDED(hr))\r
88                 return ret;\r
89         if (parent_)\r
90         {\r
91                 hr = shellFolder->BindToObject(parent_, 0, IID_IShellFolder, (void**) &parentFolder);\r
92                 if (!SUCCEEDED(hr))\r
93                         parentFolder = shellFolder;\r
94         } \r
95         else \r
96         {\r
97                 parentFolder = shellFolder;\r
98         }\r
99 \r
100         if ((parentFolder != 0)&&(item_ != 0))\r
101         {\r
102                 hr = parentFolder->GetDisplayNameOf(item_, SHGDN_NORMAL | SHGDN_FORPARSING, &name);\r
103                 if (!SUCCEEDED(hr))\r
104                 {\r
105                         parentFolder->Release();\r
106                         return ret;\r
107                 }\r
108                 hr = StrRetToStr (&name, item_, &szDisplayName);\r
109                 if (!SUCCEEDED(hr))\r
110                         return ret;\r
111         }\r
112         parentFolder->Release();\r
113         if (szDisplayName == NULL)\r
114         {\r
115                 CoTaskMemFree(szDisplayName);\r
116                 return ret;                     //to avoid a crash!\r
117         }\r
118         ret = szDisplayName;\r
119         CoTaskMemFree(szDisplayName);\r
120         return ret;\r
121 }\r
122 \r
123 LPCITEMIDLIST ItemIDList::operator& ()\r
124 {\r
125         return item_;\r
126 }\r