Bump version to 5.0-14
[LibreOffice.git] / shell / source / win32 / shlxthandler / propsheets / propsheets.cxx
blob6d1a146bfd34809337b376e07f70942f4171fad7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "internal/config.hxx"
21 #include "internal/global.hxx"
23 #include "internal/propsheets.hxx"
24 #include "internal/utilities.hxx"
25 #include "internal/resource.h"
26 #include "listviewbuilder.hxx"
28 #if defined _MSC_VER
29 #pragma warning(push, 1)
30 #endif
31 #include <shellapi.h>
32 #ifdef _WIN32_WINNT_WINBLUE
33 #include <VersionHelpers.h>
34 #endif
35 #if defined _MSC_VER
36 #pragma warning(pop)
37 #endif
39 #include <string>
40 #include <vector>
41 #include <utility>
42 #include <strsafe.h>
45 /*---------------------------------------------
46 INFO - INFO - INFO - INFO - INFO - INFO
48 See MSDN "Using Windows XP Visual Styles"
49 for hints how to enable the new common
50 control library for our property sheet.
52 INFO - INFO - INFO - INFO - INFO - INFO
53 ----------------------------------------------*/
57 CPropertySheet::CPropertySheet(long RefCnt) :
58 m_RefCnt(RefCnt)
60 OutputDebugStringFormat("CPropertySheet::CTor [%d], [%d]", m_RefCnt, g_DllRefCnt );
61 InterlockedIncrement(&g_DllRefCnt);
66 CPropertySheet::~CPropertySheet()
68 OutputDebugStringFormat("CPropertySheet::DTor [%d], [%d]", m_RefCnt, g_DllRefCnt );
69 InterlockedDecrement(&g_DllRefCnt);
73 // IUnknown methods
76 HRESULT STDMETHODCALLTYPE CPropertySheet::QueryInterface(
77 REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject)
79 *ppvObject = 0;
81 IUnknown* pUnk = 0;
82 if (IID_IUnknown == riid || IID_IShellExtInit == riid)
84 pUnk = static_cast<IShellExtInit*>(this);
85 pUnk->AddRef();
86 *ppvObject = pUnk;
87 return S_OK;
89 else if (IID_IShellPropSheetExt == riid)
91 pUnk = static_cast<IShellPropSheetExt*>(this);
92 pUnk->AddRef();
93 *ppvObject = pUnk;
94 return S_OK;
97 return E_NOINTERFACE;
102 ULONG STDMETHODCALLTYPE CPropertySheet::AddRef()
104 OutputDebugStringFormat("CPropertySheet::AddRef [%d]", m_RefCnt );
105 return InterlockedIncrement(&m_RefCnt);
110 ULONG STDMETHODCALLTYPE CPropertySheet::Release()
112 OutputDebugStringFormat("CPropertySheet::Release [%d]", m_RefCnt );
113 long refcnt = InterlockedDecrement(&m_RefCnt);
115 if (0 == refcnt)
116 delete this;
118 return refcnt;
122 // IShellExtInit
125 HRESULT STDMETHODCALLTYPE CPropertySheet::Initialize(
126 LPCITEMIDLIST /*pidlFolder*/, LPDATAOBJECT lpdobj, HKEY /*hkeyProgID*/)
128 InitCommonControls();
130 STGMEDIUM medium;
131 FORMATETC fe = {CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
133 HRESULT hr = lpdobj->GetData(&fe, &medium);
135 // save the file name
136 if (SUCCEEDED(hr) &&
137 (1 == DragQueryFileA(
138 reinterpret_cast<HDROP>(medium.hGlobal),
139 0xFFFFFFFF,
140 NULL,
141 0)))
143 UINT size = DragQueryFile( reinterpret_cast<HDROP>(medium.hGlobal), 0, 0, 0 );
144 if ( size != 0 )
146 TCHAR * buffer = new TCHAR[ size + 1 ];
147 UINT result_size = DragQueryFile( reinterpret_cast<HDROP>(medium.hGlobal),
148 0, buffer, size + 1 );
149 if ( result_size != 0 )
151 std::wstring fname = getShortPathName( buffer );
152 std::string fnameA = WStringToString( fname );
153 ZeroMemory( m_szFileName, sizeof( m_szFileName ) );
154 strncpy( m_szFileName, fnameA.c_str(), ( sizeof( m_szFileName ) - 1 ) );
155 hr = S_OK;
157 else
158 hr = E_INVALIDARG;
159 delete [] buffer;
161 else
162 hr = E_INVALIDARG;
164 else
165 hr = E_INVALIDARG;
167 ReleaseStgMedium(&medium);
169 return hr;
173 // IShellPropSheetExt
176 HRESULT STDMETHODCALLTYPE CPropertySheet::AddPages(LPFNADDPROPSHEETPAGE lpfnAddPage, LPARAM lParam)
178 // the Win32 SDK 8.1 deprecates GetVersionEx()
179 #ifdef _WIN32_WINNT_WINBLUE
180 bool bIsVistaOrLater = IsWindowsVistaOrGreater() ? true : false;
181 #else
182 // Get OS version (we don't need the summary page on Windows Vista or later)
183 OSVERSIONINFO sInfoOS;
185 ZeroMemory( &sInfoOS, sizeof(OSVERSIONINFO) );
186 sInfoOS.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
187 GetVersionEx( &sInfoOS );
188 bool bIsVistaOrLater = (sInfoOS.dwMajorVersion >= 6);
189 #endif
191 std::wstring proppage_header;
193 PROPSHEETPAGE psp;
194 ZeroMemory(&psp, sizeof(PROPSHEETPAGEA));
196 // add the summary property page
197 psp.dwSize = sizeof(PROPSHEETPAGE);
198 psp.dwFlags = PSP_DEFAULT | PSP_USETITLE | PSP_USECALLBACK;
199 psp.hInstance = GetModuleHandle(MODULE_NAME);
200 psp.lParam = reinterpret_cast<LPARAM>(this);
201 psp.pfnCallback = reinterpret_cast<LPFNPSPCALLBACK>(CPropertySheet::PropPageSummaryCallback);
203 HPROPSHEETPAGE hPage = NULL;
205 if ( !bIsVistaOrLater )
207 proppage_header = GetResString(IDS_PROPPAGE_SUMMARY_TITLE);
209 psp.pszTemplate = MAKEINTRESOURCE(IDD_PROPPAGE_SUMMARY);
210 psp.pszTitle = proppage_header.c_str();
211 psp.pfnDlgProc = reinterpret_cast<DLGPROC>(CPropertySheet::PropPageSummaryProc);
213 hPage = CreatePropertySheetPage(&psp);
215 // keep this instance alive, will be released when the
216 // the page is about to be destroyed in the callback function
218 if (hPage)
220 if (lpfnAddPage(hPage, lParam))
221 AddRef();
222 else
223 DestroyPropertySheetPage(hPage);
227 // add the statistics property page
228 proppage_header = GetResString(IDS_PROPPAGE_STATISTICS_TITLE);
230 psp.pszTemplate = MAKEINTRESOURCE(IDD_PROPPAGE_STATISTICS);
231 psp.pszTitle = proppage_header.c_str();
232 psp.pfnDlgProc = reinterpret_cast<DLGPROC>(CPropertySheet::PropPageStatisticsProc);
234 hPage = CreatePropertySheetPage(&psp);
236 if (hPage)
238 if (lpfnAddPage(hPage, lParam))
239 AddRef();
240 else
241 DestroyPropertySheetPage(hPage);
244 // always return success else
245 // no property sheet will be
246 // displayed at all
247 return NOERROR;
252 HRESULT STDMETHODCALLTYPE CPropertySheet::ReplacePage(
253 UINT /*uPageID*/, LPFNADDPROPSHEETPAGE /*lpfnReplaceWith*/, LPARAM /*lParam*/)
255 return E_NOTIMPL;
260 UINT CALLBACK CPropertySheet::PropPageSummaryCallback(
261 HWND /*hwnd*/, UINT uMsg, LPPROPSHEETPAGE ppsp)
263 CPropertySheet* pImpl =
264 reinterpret_cast<CPropertySheet*>(ppsp->lParam);
266 // release this instance, acquired
267 // in the AddPages method
268 if (PSPCB_RELEASE == uMsg)
270 pImpl->Release();
273 return TRUE;
279 BOOL CALLBACK CPropertySheet::PropPageSummaryProc(HWND hwnd, UINT uiMsg, WPARAM /*wParam*/, LPARAM lParam)
281 switch (uiMsg)
283 case WM_INITDIALOG:
285 LPPROPSHEETPAGE psp = reinterpret_cast<LPPROPSHEETPAGE>(lParam);
286 CPropertySheet* pImpl = reinterpret_cast<CPropertySheet*>(psp->lParam);
287 pImpl->InitPropPageSummary(hwnd, psp);
288 return TRUE;
292 return FALSE;
297 BOOL CALLBACK CPropertySheet::PropPageStatisticsProc(HWND hwnd, UINT uiMsg, WPARAM /*wParam*/, LPARAM lParam)
299 switch (uiMsg)
301 case WM_INITDIALOG:
303 LPPROPSHEETPAGE psp = reinterpret_cast<LPPROPSHEETPAGE>(lParam);
304 CPropertySheet* pImpl = reinterpret_cast<CPropertySheet*>(psp->lParam);
305 pImpl->InitPropPageStatistics(hwnd, psp);
306 return TRUE;
310 return FALSE;
313 void CPropertySheet::InitPropPageSummary(HWND hwnd, LPPROPSHEETPAGE /*lppsp*/)
317 CMetaInfoReader metaInfo(m_szFileName);
319 SetWindowText(GetDlgItem(hwnd,IDC_TITLE), metaInfo.getTagData( META_INFO_TITLE ).c_str() );
320 SetWindowText(GetDlgItem(hwnd,IDC_AUTHOR), metaInfo.getTagData( META_INFO_AUTHOR ).c_str() );
321 SetWindowText(GetDlgItem(hwnd,IDC_SUBJECT), metaInfo.getTagData( META_INFO_SUBJECT ).c_str() );
322 SetWindowText(GetDlgItem(hwnd,IDC_KEYWORDS), metaInfo.getTagData( META_INFO_KEYWORDS ).c_str() );
324 // comments read from meta.xml use "\n" for return, but this will not displayable in Edit control, add
325 // "\r" before "\n" to form "\r\n" in order to display return in Edit control.
326 std::wstring tempStr = metaInfo.getTagData( META_INFO_DESCRIPTION );
327 std::wstring::size_type itor = tempStr.find ( L"\n" , 0 );
328 while (itor != std::wstring::npos)
330 tempStr.insert(itor, L"\r");
331 itor = tempStr.find(L"\n", itor + 2);
333 SetWindowText(GetDlgItem(hwnd,IDC_COMMENTS), tempStr.c_str());
335 catch (const std::exception&)
343 void CPropertySheet::InitPropPageStatistics(HWND hwnd, LPPROPSHEETPAGE /*lppsp*/)
347 CMetaInfoReader metaInfo(m_szFileName);
349 document_statistic_reader_ptr doc_stat_reader = create_document_statistic_reader(m_szFileName, &metaInfo);
351 statistic_group_list_t sgl;
352 doc_stat_reader->read(&sgl);
354 list_view_builder_ptr lv_builder = create_list_view_builder(
355 GetDlgItem(hwnd, IDC_STATISTICSLIST),
356 GetResString(IDS_PROPERTY),
357 GetResString(IDS_PROPERTY_VALUE));
359 lv_builder->build(sgl);
361 catch (const std::exception&)
365 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */