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