Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localize PowerRename #1106

Merged
merged 17 commits into from
Jan 20, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,25 @@ std::wstring get_resource_string(UINT resource_id, HINSTANCE instance, const wch
}
}

// function to return the string as a to a wchar_t* (to enable localization in cases where the return type did not accept wstring)
wchar_t* get_resource_string_wchar(UINT resource_id, HINSTANCE instance)
alekhyareddy28 marked this conversation as resolved.
Show resolved Hide resolved
{
wchar_t* text_ptr;
unsigned int length = LoadStringW(instance, resource_id, reinterpret_cast<wchar_t*>(&text_ptr), 0);
std::wstring res_string = { *reinterpret_cast<wchar_t**>(&text_ptr), length };
length++;

if (length > 1)
{
wchar_t* tmp_res_ptr;
tmp_res_ptr = new wchar_t[length];
wmemcpy(tmp_res_ptr, res_string.c_str(), length);
alekhyareddy28 marked this conversation as resolved.
Show resolved Hide resolved
return tmp_res_ptr;
}

return (wchar_t*)L"test";
alekhyareddy28 marked this conversation as resolved.
Show resolved Hide resolved
}

std::wstring get_module_filename(HMODULE mod)
{
wchar_t buffer[MAX_PATH + 1];
Expand Down
14 changes: 13 additions & 1 deletion src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,16 @@ std::wstring get_resource_string(UINT resource_id, HINSTANCE instance, const wch
// Requires that
// extern "C" IMAGE_DOS_HEADER __ImageBase;
// is added to the .cpp file.
#define GET_RESOURCE_STRING(resource_id) get_resource_string(resource_id, reinterpret_cast<HINSTANCE>(&__ImageBase), L#resource_id)
#define GET_RESOURCE_STRING(resource_id) get_resource_string(resource_id, reinterpret_cast<HINSTANCE>(&__ImageBase), L#resource_id)

// Function which takes a pointer ptr and allocates space before populating it with the string from the resource table
// using this approach as wstring cannot be cast to PCWSTR without allocating space
// hence, we have to malloc and free the pointer after each use
wchar_t* get_resource_string_wchar(UINT resource_id, HINSTANCE instance);

// Wrapper for getting a string from the resource file.
// Requires that
// extern "C" IMAGE_DOS_HEADER __ImageBase;
// is added to the .cpp file.
// used when the return type must be a wchar_t* instead of a wstring.
#define GET_RES_STRING_WCHAR(resource_id) get_resource_string_wchar(resource_id, reinterpret_cast<HINSTANCE>(&__ImageBase))
6 changes: 4 additions & 2 deletions src/modules/powerrename/dll/PowerRenameExt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <PowerRenameItem.h>
#include <PowerRenameManager.h>
#include <trace.h>
#include <common.h>
#include <Helpers.h>
#include <Settings.h>
#include "resource.h"
Expand Down Expand Up @@ -195,7 +196,8 @@ DWORD WINAPI CPowerRenameMenu::s_PowerRenameUIThreadProc(_In_ void* pData)

HRESULT __stdcall CPowerRenameMenu::GetTitle(IShellItemArray* /*psiItemArray*/, LPWSTR* ppszName)
{
return SHStrDup(L"PowerRename", ppszName);
app_name = GET_RES_STRING_WCHAR(IDS_POWERRENAME);
alekhyareddy28 marked this conversation as resolved.
Show resolved Hide resolved
return SHStrDup(app_name, ppszName);
}

HRESULT __stdcall CPowerRenameMenu::GetIcon(IShellItemArray* /*psiItemArray*/, LPWSTR* ppszIcon)
Expand Down Expand Up @@ -268,4 +270,4 @@ HRESULT __stdcall CPowerRenameMenu::EnumSubCommands(IEnumExplorerCommand** ppEnu
{
*ppEnum = nullptr;
return E_NOTIMPL;
}
}
alekhyareddy28 marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions src/modules/powerrename/dll/PowerRenameExt.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ class __declspec(uuid("0440049F-D1DC-4E46-B27B-98393D79486B")) CPowerRenameMenu
std::atomic<long> m_refCount = 1;
HBITMAP m_hbmpIcon = nullptr;
CComPtr<IDataObject> m_spdo;
wchar_t* app_name;
};
Binary file modified src/modules/powerrename/dll/PowerRenameExt.rc
Binary file not shown.
50 changes: 29 additions & 21 deletions src/modules/powerrename/dll/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <settings.h>
#include <trace.h>
#include <common/settings_objects.h>

#include <common/common.h>
#include "resource.h"
#include <atomic>

std::atomic<DWORD> g_dwModuleRefCount = 0;
Expand Down Expand Up @@ -160,12 +161,14 @@ class PowerRenameModule : public PowertoyModuleIface
private:
// Enabled by default
bool m_enabled = true;
wchar_t* app_name;

public:
// Return the display name of the powertoy, this will be cached
virtual PCWSTR get_name() override
{
return L"PowerRename";
app_name = GET_RES_STRING_WCHAR(IDS_POWERRENAME);
alekhyareddy28 marked this conversation as resolved.
Show resolved Hide resolved
return app_name;
}

// Enable the powertoy
Expand Down Expand Up @@ -203,38 +206,38 @@ class PowerRenameModule : public PowertoyModuleIface

// Create a Settings object.
PowerToysSettings::Settings settings(hinstance, get_name());
settings.set_description(L"A Windows Shell Extension for more advanced bulk renaming using search and replace or regular expressions.");
settings.set_icon_key(L"pt-power-rename");
settings.set_description(GET_RESOURCE_STRING(IDS_SETTINGS_DESCRIPTION));
settings.set_icon_key(GET_RESOURCE_STRING(IDS_SETTINGS_ICON));
alekhyareddy28 marked this conversation as resolved.
Show resolved Hide resolved

// Link to the GitHub PowerRename sub-page
settings.set_overview_link(L"https://github.com/microsoft/PowerToys/tree/master/src/modules/powerrename");
settings.set_overview_link(GET_RESOURCE_STRING(IDS_OVERVIEW_LINK));

settings.add_bool_toogle(
L"bool_persist_input",
L"Restore search, replace and flags values on launch from previous run.",
GET_RESOURCE_STRING(IDS_BOOL_PERSIST),
GET_RESOURCE_STRING(IDS_RESTORE_SEARCH),
CSettings::GetPersistState());

settings.add_bool_toogle(
L"bool_mru_enabled",
L"Enable autocomplete and autosuggest of recently used inputs for search and replace values.",
GET_RESOURCE_STRING(IDS_MRU_ENABLED),
GET_RESOURCE_STRING(IDS_ENABLE_AUTO),
CSettings::GetMRUEnabled());

settings.add_int_spinner(
L"int_max_mru_size",
L"Maximum number of items to show in recently used list for autocomplete dropdown.",
GET_RESOURCE_STRING(IDS_MAX_MRU_SIZE),
GET_RESOURCE_STRING(IDS_MAX_ITEMS),
CSettings::GetMaxMRUSize(),
0,
20,
1);

settings.add_bool_toogle(
L"bool_show_icon_on_menu",
L"Show icon on context menu.",
GET_RESOURCE_STRING(IDS_SHOW_ICON),
GET_RESOURCE_STRING(IDS_ICON_CONTEXT_MENU),
CSettings::GetShowIconOnMenu());

settings.add_bool_toogle(
L"bool_show_extended_menu",
L"Only show the PowerRename menu item on the extended context menu (SHIFT + Right-click).",
GET_RESOURCE_STRING(IDS_EXTENDED_MENU),
GET_RESOURCE_STRING(IDS_EXTENDED_MENU_INFO),
CSettings::GetExtendedContextMenuOnly());

return settings.serialize_to_buffer(buffer, buffer_size);
Expand All @@ -250,11 +253,11 @@ class PowerRenameModule : public PowertoyModuleIface
PowerToysSettings::PowerToyValues values =
PowerToysSettings::PowerToyValues::from_json_string(config);

CSettings::SetPersistState(values.get_bool_value(L"bool_persist_input").value());
CSettings::SetMRUEnabled(values.get_bool_value(L"bool_mru_enabled").value());
CSettings::SetMaxMRUSize(values.get_int_value(L"int_max_mru_size").value());
CSettings::SetShowIconOnMenu(values.get_bool_value(L"bool_show_icon_on_menu").value());
CSettings::SetExtendedContextMenuOnly(values.get_bool_value(L"bool_show_extended_menu").value());
CSettings::SetPersistState(values.get_bool_value(GET_RESOURCE_STRING(IDS_BOOL_PERSIST)).value());
CSettings::SetMRUEnabled(values.get_bool_value(GET_RESOURCE_STRING(IDS_MRU_ENABLED)).value());
CSettings::SetMaxMRUSize(values.get_int_value(GET_RESOURCE_STRING(IDS_MAX_MRU_SIZE)).value());
CSettings::SetShowIconOnMenu(values.get_bool_value(GET_RESOURCE_STRING(IDS_SHOW_ICON)).value());
CSettings::SetExtendedContextMenuOnly(values.get_bool_value(GET_RESOURCE_STRING(IDS_EXTENDED_MENU)).value());
}
catch (std::exception)
{
Expand Down Expand Up @@ -299,9 +302,14 @@ class PowerRenameModule : public PowertoyModuleIface
{
init_settings();
}

~PowerRenameModule()
{
delete app_name;
}
};

extern "C" __declspec(dllexport) PowertoyModuleIface* __cdecl powertoy_create()
{
return new PowerRenameModule();
}
}
alekhyareddy28 marked this conversation as resolved.
Show resolved Hide resolved
25 changes: 20 additions & 5 deletions src/modules/powerrename/dll/resource.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
#define IDS_POWERRENAME 801
#define IDI_RENAME 132
#define IDS_SETTINGS_DESCRIPTION 2101
#define IDS_SETTINGS_ICON 2102
#define IDS_OVERVIEW_LINK 2103
#define IDS_BOOL_PERSIST 2104
#define IDS_RESTORE_SEARCH 2105
#define IDS_MRU_ENABLED 2106
#define IDS_ENABLE_AUTO 2107
#define IDS_MAX_MRU_SIZE 2108
#define IDS_MAX_ITEMS 2109
#define IDS_SHOW_ICON 2110
#define IDS_ICON_CONTEXT_MENU 2111
#define IDS_EXTENDED_MENU 2112
#define IDS_EXTENDED_MENU_INFO 2113

// Next default values for new objects
//

//

#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
73 changes: 73 additions & 0 deletions src/modules/powerrename/lib/PowerRenameLib.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (United States) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE 9, 1

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE
BEGIN
"resource.h\0"
END

2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END

3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END

#endif // APSTUDIO_INVOKED

#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////

STRINGTABLE
BEGIN
IDS_ROOT_PATH L"Software\\Microsoft\\PowerRename"
IDS_SEARCH_PATH L"SearchMRU"
IDS_REPLACE_PATH L"ReplaceMRU"
IDS_ENABLED L"Enabled"
IDS_SHOW_ICON L"ShowIcon"
IDS_CONTEXT_MENU L"ExtendedContextMenuOnly"
IDS_PERSIST_STATE L"PersistState"
IDS_MAX_MRU_SIZE L"MaxMRUSize"
IDS_FLAGS L"Flags"
IDS_SEARCH_TEXT L"SearchText"
IDS_REPLACE_TEXT L"ReplaceText"
IDS_MRU_ENABLED L"MRUEnabled"
END

#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED
4 changes: 4 additions & 0 deletions src/modules/powerrename/lib/PowerRenameLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
<ClInclude Include="PowerRenameInterfaces.h" />
<ClInclude Include="PowerRenameManager.h" />
<ClInclude Include="PowerRenameRegEx.h" />
<ClInclude Include="resource.h" />
<ClInclude Include="Settings.h" />
<ClInclude Include="srwlock.h" />
<ClInclude Include="stdafx.h" />
Expand All @@ -167,6 +168,9 @@
</ClCompile>
<ClCompile Include="trace.cpp" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="PowerRenameLib.rc" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
29 changes: 15 additions & 14 deletions src/modules/powerrename/lib/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
#include <commctrl.h>
#include "Settings.h"
#include "PowerRenameInterfaces.h"

const wchar_t c_rootRegPath[] = L"Software\\Microsoft\\PowerRename";
const wchar_t c_mruSearchRegPath[] = L"SearchMRU";
const wchar_t c_mruReplaceRegPath[] = L"ReplaceMRU";

const wchar_t c_enabled[] = L"Enabled";
const wchar_t c_showIconOnMenu[] = L"ShowIcon";
const wchar_t c_extendedContextMenuOnly[] = L"ExtendedContextMenuOnly";
const wchar_t c_persistState[] = L"PersistState";
const wchar_t c_maxMRUSize[] = L"MaxMRUSize";
const wchar_t c_flags[] = L"Flags";
const wchar_t c_searchText[] = L"SearchText";
const wchar_t c_replaceText[] = L"ReplaceText";
const wchar_t c_mruEnabled[] = L"MRUEnabled";
#include "resource.h"
#include <common.h>

const wchar_t* c_rootRegPath = GET_RESOURCE_STRING(IDS_ROOT_PATH).c_str();
alekhyareddy28 marked this conversation as resolved.
Show resolved Hide resolved
const wchar_t* c_mruSearchRegPath = GET_RESOURCE_STRING(IDS_SEARCH_PATH).c_str();
const wchar_t* c_mruReplaceRegPath = GET_RESOURCE_STRING(IDS_REPLACE_PATH).c_str();
const wchar_t* c_enabled = GET_RESOURCE_STRING(IDS_ENABLED).c_str();
const wchar_t* c_showIconOnMenu = GET_RESOURCE_STRING(IDS_SHOW_ICON).c_str();
const wchar_t* c_extendedContextMenuOnly = GET_RESOURCE_STRING(IDS_CONTEXT_MENU).c_str();
const wchar_t* c_persistState = GET_RESOURCE_STRING(IDS_PERSIST_STATE).c_str();
const wchar_t* c_maxMRUSize = GET_RESOURCE_STRING(IDS_MAX_MRU_SIZE).c_str();
const wchar_t* c_flags = GET_RESOURCE_STRING(IDS_FLAGS).c_str();
const wchar_t* c_searchText = GET_RESOURCE_STRING(IDS_SEARCH_TEXT).c_str();
const wchar_t* c_replaceText = GET_RESOURCE_STRING(IDS_REPLACE_TEXT).c_str();
const wchar_t* c_mruEnabled = GET_RESOURCE_STRING(IDS_MRU_ENABLED).c_str();

const bool c_enabledDefault = true;
const bool c_showIconOnMenuDefault = true;
Expand Down
28 changes: 28 additions & 0 deletions src/modules/powerrename/lib/resource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by PowerRenameLib.rc

#define IDS_ROOT_PATH 100
#define IDS_SEARCH_PATH 101
#define IDS_REPLACE_PATH 102
#define IDS_ENABLED 103
#define IDS_SHOW_ICON 104
#define IDS_CONTEXT_MENU 105
#define IDS_PERSIST_STATE 106
#define IDS_MAX_MRU_SIZE 107
#define IDS_FLAGS 108
#define IDS_SEARCH_TEXT 109
#define IDS_REPLACE_TEXT 110
#define IDS_MRU_ENABLED 111


// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
1 change: 1 addition & 0 deletions src/modules/powerrename/testapp/PowerRenameTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <PowerRenameUI.h>
#include <PowerRenameManager.h>
#include <Shobjidl.h>
#include <common.h>

#pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

Expand Down
5 changes: 5 additions & 0 deletions src/modules/powerrename/testapp/PowerRenameTest.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@
<ItemGroup>
<ResourceCompile Include="PowerRenameTest.rc" />
</ItemGroup>
<ItemGroup>
yuyoyuppe marked this conversation as resolved.
Show resolved Hide resolved
<ProjectReference Include="..\..\..\common\common.vcxproj">
<Project>{74485049-c722-400f-abe5-86ac52d929b3}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down