Notify Windows shell about file associations changes

This is documented as being required in the MSDN and, in practice, is
necessary for Windows to update the icon cache and show the new icon.

Closes https://github.com/wxWidgets/wxWidgets/pull/195
This commit is contained in:
Maarten Bent
2016-02-03 19:01:46 +01:00
committed by Vadim Zeitlin
parent 948126a375
commit 90386df305
3 changed files with 63 additions and 6 deletions

View File

@@ -5,7 +5,7 @@
// Modified by:
// Created: 23.09.98
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
// Licence: wxWindows licence (part of wxExtra library)
// Licence: wxWidgets licence (part of base library)
/////////////////////////////////////////////////////////////////////////////
// for compilers that support precompilation, includes "wx.h".
@@ -40,6 +40,7 @@
#include "wx/msw/registry.h"
#include "wx/msw/private.h"
#include <shlwapi.h>
#include <shlobj.h>
// For MSVC we can link in the required library explicitly, for the other
// compilers (e.g. MinGW) this needs to be done at makefiles level.
@@ -146,6 +147,8 @@ void wxFileTypeImpl::Init(const wxString& strFileType, const wxString& ext)
if ( !strFileType ) {
m_strFileType = m_ext.AfterFirst('.') + wxT("_auto_file");
}
m_suppressNotify = false;
}
wxString wxFileTypeImpl::GetVerbPath(const wxString& verb) const
@@ -210,6 +213,17 @@ size_t wxFileTypeImpl::GetAllCommands(wxArrayString *verbs,
return count;
}
void wxFileTypeImpl::MSWNotifyShell()
{
if (!m_suppressNotify)
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT, NULL, NULL);
}
void wxFileTypeImpl::MSWSuppressNotifications(bool supress)
{
m_suppressNotify = supress;
}
// ----------------------------------------------------------------------------
// modify the registry database
// ----------------------------------------------------------------------------
@@ -676,12 +690,15 @@ wxFileType *wxMimeTypesManagerImpl::Associate(const wxFileTypeInfo& ftInfo)
if (ft)
{
ft->m_impl->MSWSuppressNotifications(true);
if (! ftInfo.GetOpenCommand ().empty() ) ft->SetCommand (ftInfo.GetOpenCommand (), wxT("open" ) );
if (! ftInfo.GetPrintCommand().empty() ) ft->SetCommand (ftInfo.GetPrintCommand(), wxT("print" ) );
// chris: I don't like the ->m_impl-> here FIX this ??
if (! ftInfo.GetDescription ().empty() ) ft->m_impl->SetDescription (ftInfo.GetDescription ()) ;
if (! ftInfo.GetIconFile().empty() ) ft->SetDefaultIcon (ftInfo.GetIconFile(), ftInfo.GetIconIndex() );
ft->m_impl->MSWSuppressNotifications(false);
ft->m_impl->MSWNotifyShell();
}
return ft;
@@ -702,7 +719,12 @@ bool wxFileTypeImpl::SetCommand(const wxString& cmd,
// TODO:
// 1. translate '%s' to '%1' instead of always adding it
// 2. create DDEExec value if needed (undo GetCommand)
return rkey.Create() && rkey.SetValue(wxEmptyString, cmd + wxT(" \"%1\"") );
bool result = rkey.Create() && rkey.SetValue(wxEmptyString, cmd + wxT(" \"%1\"") );
if ( result )
MSWNotifyShell();
return result;
}
bool wxFileTypeImpl::SetDefaultIcon(const wxString& cmd, int index)
@@ -718,9 +740,14 @@ bool wxFileTypeImpl::SetDefaultIcon(const wxString& cmd, int index)
wxRegKey rkey(wxRegKey::HKCU,
CLASSES_ROOT_KEY + m_strFileType + wxT("\\DefaultIcon"));
return rkey.Create() &&
bool result = rkey.Create() &&
rkey.SetValue(wxEmptyString,
wxString::Format(wxT("%s,%d"), cmd.c_str(), index));
if ( result )
MSWNotifyShell();
return result;
}
bool wxFileTypeImpl::SetDescription (const wxString& desc)
@@ -743,6 +770,7 @@ bool wxFileTypeImpl::SetDescription (const wxString& desc)
bool wxFileTypeImpl::Unassociate()
{
MSWSuppressNotifications(true);
bool result = true;
if ( !RemoveOpenCommand() )
result = false;
@@ -753,6 +781,9 @@ bool wxFileTypeImpl::Unassociate()
if ( !RemoveDescription() )
result = false;
MSWSuppressNotifications(false);
MSWNotifyShell();
return result;
}
@@ -769,7 +800,12 @@ bool wxFileTypeImpl::RemoveCommand(const wxString& verb)
wxRegKey rkey(wxRegKey::HKCU, CLASSES_ROOT_KEY + GetVerbPath(verb));
// if the key already doesn't exist, it's a success
return !rkey.Exists() || rkey.DeleteSelf();
bool result = !rkey.Exists() || rkey.DeleteSelf();
if ( result )
MSWNotifyShell();
return result;
}
bool wxFileTypeImpl::RemoveMimeType()
@@ -787,7 +823,12 @@ bool wxFileTypeImpl::RemoveDefaultIcon()
wxRegKey rkey (wxRegKey::HKCU,
CLASSES_ROOT_KEY + m_strFileType + wxT("\\DefaultIcon"));
return !rkey.Exists() || rkey.DeleteSelf();
bool result = !rkey.Exists() || rkey.DeleteSelf();
if ( result )
MSWNotifyShell();
return result;
}
bool wxFileTypeImpl::RemoveDescription()