1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_shell.hxx"
26 
27 #ifdef _MSC_VER
28 #pragma warning (disable : 4786 4503)
29 #endif
30 
31 //------------------------------------
32 // include
33 //------------------------------------
34 #include "listviewbuilder.hxx"
35 #include "document_statistic.hxx"
36 #include "internal/utilities.hxx"
37 #include "internal/config.hxx"
38 
39 #if defined _MSC_VER
40 #pragma warning(push, 1)
41 #endif
42 #include <commctrl.h>
43 #if defined _MSC_VER
44 #pragma warning(pop)
45 #endif
46 #include <commctrl.h>
47 #include <tchar.h>
48 #include "internal/resource.h"
49 
50 //------------------------------------
51 //
52 //------------------------------------
53 
create_list_view_builder(HWND hwnd_lv,const std::wstring & col1,const std::wstring & col2)54 list_view_builder_ptr create_list_view_builder(
55     HWND hwnd_lv, const std::wstring& col1, const std::wstring& col2)
56 {
57     if (is_windows_xp_or_above())
58         return list_view_builder_ptr(new winxp_list_view_builder(hwnd_lv, col1, col2));
59     else
60         return list_view_builder_ptr(new list_view_builder(hwnd_lv, col1, col2));
61 }
62 
63 //------------------------------------
64 //
65 //------------------------------------
66 
list_view_builder(HWND hwnd_list_view,const std::wstring & column1_title,const std::wstring & column2_title)67 list_view_builder::list_view_builder(
68     HWND hwnd_list_view,
69     const std::wstring& column1_title,
70     const std::wstring& column2_title) :
71     hwnd_list_view_(hwnd_list_view),
72     row_index_(-1),
73     column1_title_(column1_title),
74     column2_title_(column2_title)
75 {
76 }
77 
78 //------------------------------------
79 //
80 //------------------------------------
81 
~list_view_builder()82 list_view_builder::~list_view_builder()
83 {
84 }
85 
86 //------------------------------------
87 //
88 //------------------------------------
89 
build(statistic_group_list_t & gl)90 void list_view_builder::build(statistic_group_list_t& gl)
91 {
92     setup_list_view();
93 
94     statistic_group_list_t::iterator group_iter     = gl.begin();
95     statistic_group_list_t::iterator group_iter_end = gl.end();
96 
97     for (/**/; group_iter != group_iter_end; ++group_iter)
98     {
99         statistic_item_list_t::iterator item_iter     = group_iter->second.begin();
100         statistic_item_list_t::iterator item_iter_end = group_iter->second.end();
101 
102         if (item_iter != item_iter_end)
103             insert_group(group_iter->first);
104 
105         for (/**/; item_iter != item_iter_end; ++item_iter)
106             insert_item(item_iter->title_, item_iter->value_, item_iter->editable_);
107     }
108 }
109 
110 //------------------------------------
111 //
112 //------------------------------------
113 
setup_list_view()114 void list_view_builder::setup_list_view()
115 {
116     HIMAGELIST h_ils = ImageList_Create(16,15,ILC_MASK, 7, 0);
117     HBITMAP    h_bmp = LoadBitmap(GetModuleHandle(MODULE_NAME), MAKEINTRESOURCE(IDB_PROPERTY_IMAGES));
118     ImageList_AddMasked(h_ils, h_bmp, RGB(255, 0, 255));
119 
120     ListView_SetImageList(hwnd_list_view_, h_ils, LVSIL_SMALL);
121 
122 	std::wstring header = GetResString(IDS_PROPERTY);
123 
124     LVCOLUMN lvc;
125 	lvc.mask = LVCF_FMT |
126 			   LVCF_WIDTH |
127      		   LVCF_TEXT |
128 			   LVCF_SUBITEM;
129 
130 	lvc.iSubItem = 0;
131 	lvc.pszText  = const_cast<wchar_t*>(header.c_str());
132 	lvc.cx       = 120;
133 	lvc.fmt      = LVCFMT_LEFT;
134 
135 	ListView_InsertColumn(hwnd_list_view_, 0, &lvc);
136 	lvc.iSubItem = 1;
137 	header = GetResString(IDS_PROPERTY_VALUE);
138 	lvc.pszText = const_cast<wchar_t*>(header.c_str());
139 	ListView_InsertColumn(hwnd_list_view_, 1, &lvc);
140 }
141 
142 //------------------------------------
143 //
144 //------------------------------------
145 
insert_group(const std::wstring &)146 void list_view_builder::insert_group(const std::wstring& /*title*/)
147 {
148     insert_item(L"", L"", false);
149 }
150 
151 //------------------------------------
152 //
153 //------------------------------------
154 
insert_item(const std::wstring & title,const std::wstring & value,bool is_editable)155 void list_view_builder::insert_item(const std::wstring& title, const std::wstring& value, bool is_editable)
156 {
157     LVITEM lvi;
158 
159 	lvi.iItem      = ++row_index_;
160 	lvi.iSubItem   = 0;
161 	lvi.mask       = LVIF_TEXT;
162 	lvi.state      = 0;
163 	lvi.cchTextMax = title.size() + 1;
164 	lvi.stateMask  = 0;
165 	lvi.pszText    = const_cast<wchar_t*>(title.c_str());
166 
167 	if (title.length() > 0)
168 	{
169 		lvi.mask |= LVIF_IMAGE;
170 
171 		if (is_editable)
172 		    lvi.iImage = 4;
173 		else
174 		    lvi.iImage = 3;
175 	}
176 
177 	ListView_InsertItem(hwnd_list_view_, &lvi);
178 
179 	lvi.mask     = LVIF_TEXT;
180 	lvi.iSubItem = 1;
181 	lvi.pszText  = const_cast<wchar_t*>(value.c_str());
182 
183 	ListView_SetItem(hwnd_list_view_, &lvi);
184 }
185 
186 //------------------------------------
187 //
188 //------------------------------------
189 
get_list_view() const190 HWND list_view_builder::get_list_view() const
191 {
192     return hwnd_list_view_;
193 }
194 
195 //------------------------------------
196 //
197 //------------------------------------
198 
winxp_list_view_builder(HWND hwnd_list_view,const std::wstring & column1_title,const std::wstring & column2_title)199 winxp_list_view_builder::winxp_list_view_builder(
200     HWND hwnd_list_view,
201     const std::wstring& column1_title,
202     const std::wstring& column2_title) :
203     list_view_builder(hwnd_list_view, column1_title, column2_title),
204     group_count_(-1),
205     row_count_(0)
206 {
207 }
208 
209 //------------------------------------
210 //
211 //------------------------------------
212 
setup_list_view()213 void winxp_list_view_builder::setup_list_view()
214 {
215     list_view_builder::setup_list_view();
216 
217     ListView_EnableGroupView(get_list_view(), TRUE);
218 }
219 
220 //------------------------------------
221 //
222 //------------------------------------
223 
insert_group(const std::wstring & name)224 void winxp_list_view_builder::insert_group(const std::wstring& name)
225 {
226     LVGROUP lvg;
227 
228     ZeroMemory(&lvg, sizeof(lvg));
229 
230     lvg.cbSize    = sizeof(lvg);
231     lvg.mask      = LVGF_HEADER | LVGF_STATE | LVGF_GROUPID;
232     lvg.pszHeader = const_cast<wchar_t*>(name.c_str());
233     lvg.cchHeader = name.size() + 1;
234     lvg.iGroupId  = ++group_count_;
235     lvg.state     = LVGS_NORMAL;
236     lvg.uAlign    = LVGA_HEADER_CENTER;
237 
238     ListView_InsertGroup(get_list_view(), row_count_++, &lvg);
239 }
240 
241 //------------------------------------
242 //
243 //------------------------------------
244 
insert_item(const std::wstring & title,const std::wstring & value,bool is_editable)245 void winxp_list_view_builder::insert_item(
246     const std::wstring& title, const std::wstring& value, bool is_editable)
247 {
248     LVITEM lvi;
249 
250 	lvi.iItem      = ++row_index_;
251 	lvi.iSubItem   = 0;
252 	lvi.mask       = LVIF_TEXT | LVIF_GROUPID;
253 	lvi.state      = 0;
254 	lvi.stateMask  = 0;
255 	lvi.pszText    = const_cast<wchar_t*>(title.c_str());
256 	lvi.iGroupId   = group_count_;
257 
258 	if (title.length() > 0)
259 	{
260 		lvi.mask |= LVIF_IMAGE;
261 
262 		if (is_editable)
263 		    lvi.iImage = 4;
264 		else
265 		    lvi.iImage = 3;
266 	}
267 
268 	ListView_InsertItem(get_list_view(), &lvi);
269 
270 	lvi.mask     = LVIF_TEXT;
271 	lvi.iSubItem = 1;
272 	lvi.pszText  = const_cast<wchar_t*>(value.c_str());
273 
274 	ListView_SetItem(get_list_view(), &lvi);
275 
276 	row_count_++;
277 }
278