|
|
|
@@ -0,0 +1,418 @@
|
|
|
|
|
enum class EnvironmentScope { user, system, process };
|
|
|
|
|
|
|
|
|
|
constexpr wchar_t userEnvironmentPath[]=L"Environment";
|
|
|
|
|
constexpr wchar_t systemEnvironmentPath[]=L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment";
|
|
|
|
|
constexpr unsigned environmentValueLimit=32767;
|
|
|
|
|
|
|
|
|
|
const wchar_t *environmentScopeName(EnvironmentScope scope)
|
|
|
|
|
{
|
|
|
|
|
switch(scope)
|
|
|
|
|
{
|
|
|
|
|
case EnvironmentScope::user: return L"User";
|
|
|
|
|
case EnvironmentScope::system: return L"System";
|
|
|
|
|
case EnvironmentScope::process: return L"Process";
|
|
|
|
|
}
|
|
|
|
|
return L"";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::wstring environmentSource(EnvironmentScope scope)
|
|
|
|
|
{
|
|
|
|
|
if(scope==EnvironmentScope::process) return L"Current KSwitcher.Files process";
|
|
|
|
|
return scope==EnvironmentScope::user?L"HKCU\\"+std::wstring(userEnvironmentPath):
|
|
|
|
|
L"HKLM\\"+std::wstring(systemEnvironmentPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct EnvironmentKey
|
|
|
|
|
{
|
|
|
|
|
HKEY value{};
|
|
|
|
|
explicit EnvironmentKey(EnvironmentScope scope,bool write=false)
|
|
|
|
|
{
|
|
|
|
|
const bool user=scope==EnvironmentScope::user;
|
|
|
|
|
const auto root=user?HKEY_CURRENT_USER:HKEY_LOCAL_MACHINE;
|
|
|
|
|
const auto path=user?userEnvironmentPath:systemEnvironmentPath;
|
|
|
|
|
const auto access=KEY_QUERY_VALUE|(write?KEY_SET_VALUE:0);
|
|
|
|
|
const auto result=user && write?
|
|
|
|
|
RegCreateKeyExW(root,path,0,nullptr,0,access,nullptr,&value,nullptr):
|
|
|
|
|
RegOpenKeyExW(root,path,0,access,&value);
|
|
|
|
|
if(result!=ERROR_SUCCESS && (result!=ERROR_FILE_NOT_FOUND || !user || write))
|
|
|
|
|
throw std::system_error(result,std::system_category(),"Open the environment registry key");
|
|
|
|
|
}
|
|
|
|
|
EnvironmentKey(const EnvironmentKey &)=delete;
|
|
|
|
|
~EnvironmentKey(){if(value) RegCloseKey(value);}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct EnvironmentValue
|
|
|
|
|
{
|
|
|
|
|
DWORD type{};
|
|
|
|
|
std::wstring text;
|
|
|
|
|
bool supported{};
|
|
|
|
|
bool operator==(const EnvironmentValue &) const = default;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct EnvironmentVariable
|
|
|
|
|
{
|
|
|
|
|
std::wstring name;
|
|
|
|
|
EnvironmentValue value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::optional<EnvironmentValue> readEnvironmentValue(HKEY key,const std::wstring &name)
|
|
|
|
|
{
|
|
|
|
|
if(!key) return {};
|
|
|
|
|
DWORD size{},type{};
|
|
|
|
|
auto result=RegQueryValueExW(key,name.c_str(),nullptr,&type,nullptr,&size);
|
|
|
|
|
if(result==ERROR_FILE_NOT_FOUND) return {};
|
|
|
|
|
if(result!=ERROR_SUCCESS) throw std::system_error(result,std::system_category(),"Read the environment definition");
|
|
|
|
|
for(int attempt=0;attempt<4;++attempt)
|
|
|
|
|
{
|
|
|
|
|
if(type!=REG_SZ && type!=REG_EXPAND_SZ) return EnvironmentValue{type,{},false};
|
|
|
|
|
if(size%sizeof(wchar_t) || size>2*(environmentValueLimit+1))
|
|
|
|
|
return EnvironmentValue{type,{},false};
|
|
|
|
|
std::wstring text(size/sizeof(wchar_t)+1,L'\0');
|
|
|
|
|
result=RegQueryValueExW(key,name.c_str(),nullptr,&type,reinterpret_cast<BYTE*>(text.data()),&size);
|
|
|
|
|
if(result==ERROR_FILE_NOT_FOUND) return {};
|
|
|
|
|
if(result==ERROR_MORE_DATA) continue;
|
|
|
|
|
if(result!=ERROR_SUCCESS) throw std::system_error(result,std::system_category(),"Read the environment definition");
|
|
|
|
|
if((type!=REG_SZ && type!=REG_EXPAND_SZ) || size%sizeof(wchar_t))
|
|
|
|
|
return EnvironmentValue{type,{},false};
|
|
|
|
|
text.resize(size/sizeof(wchar_t));
|
|
|
|
|
while(!text.empty() && text.back()==L'\0') text.pop_back();
|
|
|
|
|
if(text.find(L'\0')!=text.npos) return EnvironmentValue{type,{},false};
|
|
|
|
|
return EnvironmentValue{type,std::move(text),true};
|
|
|
|
|
}
|
|
|
|
|
throw std::runtime_error("The environment definition changed while Windows was reading it.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<EnvironmentVariable> readEnvironmentScope(EnvironmentScope scope)
|
|
|
|
|
{
|
|
|
|
|
std::vector<EnvironmentVariable> entries;
|
|
|
|
|
if(scope==EnvironmentScope::process)
|
|
|
|
|
{
|
|
|
|
|
const auto block=GetEnvironmentStringsW();
|
|
|
|
|
if(!block) throw std::system_error(GetLastError(),std::system_category(),"Read the current process environment");
|
|
|
|
|
const std::unique_ptr<wchar_t,decltype(&FreeEnvironmentStringsW)> owned(block,FreeEnvironmentStringsW);
|
|
|
|
|
for(auto offset=block;*offset;)
|
|
|
|
|
{
|
|
|
|
|
const std::wstring_view line=offset;
|
|
|
|
|
const auto equals=line.find(L'=');
|
|
|
|
|
if(equals!=line.npos && equals>0)
|
|
|
|
|
entries.push_back({std::wstring(line.substr(0,equals)),{REG_SZ,std::wstring(line.substr(equals+1)),true}});
|
|
|
|
|
offset+=line.size()+1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
const EnvironmentKey key(scope);
|
|
|
|
|
if(!key.value) return {};
|
|
|
|
|
std::vector<wchar_t> name(16384);
|
|
|
|
|
for(DWORD index=0;;++index)
|
|
|
|
|
{
|
|
|
|
|
DWORD length=static_cast<DWORD>(name.size());
|
|
|
|
|
const auto result=RegEnumValueW(key.value,index,name.data(),&length,nullptr,nullptr,nullptr,nullptr);
|
|
|
|
|
if(result==ERROR_NO_MORE_ITEMS) break;
|
|
|
|
|
if(result!=ERROR_SUCCESS) throw std::system_error(result,std::system_category(),"List environment definitions");
|
|
|
|
|
if(!length) continue;
|
|
|
|
|
std::wstring label(name.data(),length);
|
|
|
|
|
if(label.find(L'=')!=label.npos) continue;
|
|
|
|
|
if(auto value=readEnvironmentValue(key.value,label);value && value->supported)
|
|
|
|
|
entries.push_back({std::move(label),std::move(*value)});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
std::sort(entries.begin(),entries.end(),[](const auto &left,const auto &right) {
|
|
|
|
|
return CompareStringOrdinal(left.name.c_str(),-1,right.name.c_str(),-1,TRUE)==CSTR_LESS_THAN;
|
|
|
|
|
});
|
|
|
|
|
return entries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::wstring expandedEnvironmentValue(EnvironmentScope scope,const EnvironmentValue &value)
|
|
|
|
|
{
|
|
|
|
|
if(value.type!=REG_EXPAND_SZ) return value.text;
|
|
|
|
|
Handle token{};
|
|
|
|
|
if(scope==EnvironmentScope::user &&
|
|
|
|
|
!OpenProcessToken(GetCurrentProcess(),TOKEN_QUERY|TOKEN_IMPERSONATE|TOKEN_DUPLICATE,&token.value))
|
|
|
|
|
throw std::system_error(GetLastError(),std::system_category(),"Open the current user's environment");
|
|
|
|
|
std::wstring expanded(environmentValueLimit+1,L'\0');
|
|
|
|
|
if(!ExpandEnvironmentStringsForUserW(token.value,value.text.c_str(),expanded.data(),static_cast<DWORD>(expanded.size())))
|
|
|
|
|
throw std::system_error(GetLastError(),std::system_category(),"Expand the environment definition");
|
|
|
|
|
expanded.resize(wcslen(expanded.c_str()));
|
|
|
|
|
return expanded;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void writeEnvironmentValue(EnvironmentScope scope,const std::wstring &name,const EnvironmentValue &replacement,
|
|
|
|
|
const std::optional<EnvironmentValue> &expected)
|
|
|
|
|
{
|
|
|
|
|
if(scope==EnvironmentScope::process) throw std::runtime_error("The current process environment is read-only here.");
|
|
|
|
|
if(replacement.text.size()>environmentValueLimit) throw std::runtime_error("The value exceeds the Windows environment limit.");
|
|
|
|
|
const EnvironmentKey key(scope,true);
|
|
|
|
|
if(readEnvironmentValue(key.value,name)!=expected)
|
|
|
|
|
throw std::runtime_error("Windows changed this definition while it was open. Reload and review the current value.");
|
|
|
|
|
if(expected==replacement) return;
|
|
|
|
|
const auto bytes=static_cast<DWORD>((replacement.text.size()+1)*sizeof(wchar_t));
|
|
|
|
|
const auto result=RegSetValueExW(key.value,name.c_str(),0,replacement.type,
|
|
|
|
|
reinterpret_cast<const BYTE*>(replacement.text.c_str()),bytes);
|
|
|
|
|
if(result!=ERROR_SUCCESS) throw std::system_error(result,std::system_category(),"Save the environment definition");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
std::thread([] {
|
|
|
|
|
DWORD_PTR reply{};
|
|
|
|
|
SendMessageTimeoutW(HWND_BROADCAST,WM_SETTINGCHANGE,0,reinterpret_cast<LPARAM>(L"Environment"),
|
|
|
|
|
SMTO_ABORTIFHUNG,250,&reply);
|
|
|
|
|
}).detach();
|
|
|
|
|
}
|
|
|
|
|
catch(const std::exception &) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const wchar_t *environmentValueType(const EnvironmentValue &value)
|
|
|
|
|
{
|
|
|
|
|
return value.type==REG_EXPAND_SZ?L"REG_EXPAND_SZ":L"REG_SZ";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct EnvironmentEdit
|
|
|
|
|
{
|
|
|
|
|
EnvironmentScope scope;
|
|
|
|
|
EnvironmentVariable original;
|
|
|
|
|
std::wstring replacement;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
INT_PTR CALLBACK environmentEditDialog(HWND dialog,UINT message,WPARAM wp,LPARAM lp)
|
|
|
|
|
{
|
|
|
|
|
constexpr int label=630,body=632,footer=633;
|
|
|
|
|
if(message==WM_INITDIALOG)
|
|
|
|
|
{
|
|
|
|
|
const auto &editor=*reinterpret_cast<const EnvironmentEdit *>(lp);
|
|
|
|
|
SetWindowLongPtrW(dialog,DWLP_USER,lp);
|
|
|
|
|
prepareTextDialog(dialog);
|
|
|
|
|
layoutDialogContent(dialog,label);
|
|
|
|
|
const auto title=editor.original.name+L" · "+environmentScopeName(editor.scope)+L" · "+
|
|
|
|
|
environmentValueType(editor.original.value);
|
|
|
|
|
SetDlgItemTextW(dialog,label,title.c_str());
|
|
|
|
|
SendDlgItemMessageW(dialog,body,EM_SETLIMITTEXT,environmentValueLimit,0);
|
|
|
|
|
SetDlgItemTextW(dialog,body,editor.original.value.text.c_str());
|
|
|
|
|
layoutDialogButtons(dialog,footer,{IDOK,IDCANCEL});
|
|
|
|
|
SendMessageW(dialog,DM_SETDEFID,IDOK,0);
|
|
|
|
|
SetFocus(GetDlgItem(dialog,body));
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
if(message==WM_COMMAND && LOWORD(wp)==IDOK)
|
|
|
|
|
{
|
|
|
|
|
auto &editor=*reinterpret_cast<EnvironmentEdit *>(GetWindowLongPtrW(dialog,DWLP_USER));
|
|
|
|
|
editor.replacement=dialogFieldText(GetDlgItem(dialog,body));
|
|
|
|
|
FilesModalDialog::finish(dialog,IDOK);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
if(message==WM_CLOSE || (message==WM_COMMAND && LOWORD(wp)==IDCANCEL))
|
|
|
|
|
{
|
|
|
|
|
FilesModalDialog::finish(dialog,IDCANCEL);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
return paintCopyDialog(dialog,message,wp,lp,L" Edit environment definition ",false,{footer});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constexpr int envScope=611,envList=612,envSource=613,envSelected=614,envRaw=615,envExpanded=616,
|
|
|
|
|
envStatus=617,envFooter=618,envEdit=622,envCopyUser=623,envCopySystem=624,envRefresh=625;
|
|
|
|
|
|
|
|
|
|
struct EnvironmentPanel
|
|
|
|
|
{
|
|
|
|
|
EnvironmentScope scope=EnvironmentScope::user;
|
|
|
|
|
std::vector<EnvironmentVariable> entries;
|
|
|
|
|
std::wstring readFailure,notice;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::wstring environmentErrorText(const std::exception &error)
|
|
|
|
|
{
|
|
|
|
|
if(const auto *system=dynamic_cast<const std::system_error *>(&error)) return fileError(*system);
|
|
|
|
|
return ks::wide(error.what());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const EnvironmentVariable *selectedEnvironmentVariable(HWND dialog,const EnvironmentPanel &panel)
|
|
|
|
|
{
|
|
|
|
|
const auto row=SendDlgItemMessageW(dialog,envList,LB_GETCURSEL,0,0);
|
|
|
|
|
if(row<0) return nullptr;
|
|
|
|
|
const auto index=SendDlgItemMessageW(dialog,envList,LB_GETITEMDATA,row,0);
|
|
|
|
|
return index>=0 && static_cast<size_t>(index)<panel.entries.size()?&panel.entries[index]:nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setEnvironmentDetails(HWND dialog,const EnvironmentPanel &panel)
|
|
|
|
|
{
|
|
|
|
|
const auto *entry=selectedEnvironmentVariable(dialog,panel);
|
|
|
|
|
const bool registry=panel.scope!=EnvironmentScope::process;
|
|
|
|
|
SetDlgItemTextW(dialog,envSource,environmentSource(panel.scope).c_str());
|
|
|
|
|
SetDlgItemTextW(dialog,envSelected,entry?(entry->name+L" · "+
|
|
|
|
|
(registry?environmentValueType(entry->value):L"Read-only")).c_str():L"");
|
|
|
|
|
SetDlgItemTextW(dialog,envRaw,entry?entry->value.text.c_str():L"");
|
|
|
|
|
std::wstring expanded;
|
|
|
|
|
if(entry)
|
|
|
|
|
{
|
|
|
|
|
try {expanded=expandedEnvironmentValue(panel.scope,entry->value);}
|
|
|
|
|
catch(const std::exception &error) {expanded=L"Unavailable · "+environmentErrorText(error);}
|
|
|
|
|
}
|
|
|
|
|
SetDlgItemTextW(dialog,envExpanded,expanded.c_str());
|
|
|
|
|
EnableWindow(GetDlgItem(dialog,envEdit),entry && registry);
|
|
|
|
|
EnableWindow(GetDlgItem(dialog,envCopyUser),entry && panel.scope!=EnvironmentScope::user);
|
|
|
|
|
EnableWindow(GetDlgItem(dialog,envCopySystem),entry && panel.scope!=EnvironmentScope::system);
|
|
|
|
|
const auto status=!panel.readFailure.empty()?panel.readFailure:!panel.notice.empty()?panel.notice:
|
|
|
|
|
!entry?std::wstring(L"No environment variables in this scope."):
|
|
|
|
|
registry?L"Existing PowerShell sessions keep their environment. Restart Files for embedded PowerShell.":
|
|
|
|
|
L"These values belong to the running Files process.";
|
|
|
|
|
SetDlgItemTextW(dialog,envStatus,status.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void reloadEnvironmentPanel(HWND dialog,EnvironmentPanel &panel,const std::wstring &preferred={})
|
|
|
|
|
{
|
|
|
|
|
panel.readFailure.clear();
|
|
|
|
|
try {panel.entries=readEnvironmentScope(panel.scope);}
|
|
|
|
|
catch(const std::exception &error)
|
|
|
|
|
{
|
|
|
|
|
panel.entries.clear();
|
|
|
|
|
panel.readFailure=environmentErrorText(error);
|
|
|
|
|
}
|
|
|
|
|
const auto list=GetDlgItem(dialog,envList);
|
|
|
|
|
SendMessageW(list,WM_SETREDRAW,FALSE,0);
|
|
|
|
|
SendMessageW(list,LB_RESETCONTENT,0,0);
|
|
|
|
|
LRESULT selected=0;
|
|
|
|
|
for(size_t index=0;index<panel.entries.size();++index)
|
|
|
|
|
{
|
|
|
|
|
const auto &name=panel.entries[index].name;
|
|
|
|
|
const auto row=SendMessageW(list,LB_ADDSTRING,0,reinterpret_cast<LPARAM>(name.c_str()));
|
|
|
|
|
if(row<0) continue;
|
|
|
|
|
SendMessageW(list,LB_SETITEMDATA,row,index);
|
|
|
|
|
if(CompareStringOrdinal(name.c_str(),-1,preferred.c_str(),-1,TRUE)==CSTR_EQUAL) selected=row;
|
|
|
|
|
}
|
|
|
|
|
if(!panel.entries.empty()) SendMessageW(list,LB_SETCURSEL,selected,0);
|
|
|
|
|
SendMessageW(list,WM_SETREDRAW,TRUE,0);
|
|
|
|
|
InvalidateRect(list,nullptr,TRUE);
|
|
|
|
|
setEnvironmentDetails(dialog,panel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void saveEnvironmentDefinition(HWND dialog,EnvironmentPanel &panel,const EnvironmentVariable &entry,
|
|
|
|
|
std::optional<EnvironmentScope> copyTo={})
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if(copyTo)
|
|
|
|
|
{
|
|
|
|
|
const EnvironmentKey key(*copyTo);
|
|
|
|
|
const auto previous=readEnvironmentValue(key.value,entry.name);
|
|
|
|
|
if(previous && !previous->supported)
|
|
|
|
|
throw std::runtime_error("The destination already has a registry value of an unsupported type.");
|
|
|
|
|
const auto question=previous?L"Replace "+entry.name+L" in "+environmentScopeName(*copyTo)+L"?":
|
|
|
|
|
L"Copy "+entry.name+L" to "+environmentScopeName(*copyTo)+L"?";
|
|
|
|
|
if(showFilesNotice(dialog,question.c_str(),L"Copy environment definition",
|
|
|
|
|
{.confirm=true,.tone=previous?FilesNoticeTone::warning:FilesNoticeTone::dialog})!=IDYES) return;
|
|
|
|
|
if(panel.scope!=EnvironmentScope::process)
|
|
|
|
|
{
|
|
|
|
|
const EnvironmentKey source(panel.scope);
|
|
|
|
|
if(readEnvironmentValue(source.value,entry.name)!=entry.value)
|
|
|
|
|
throw std::runtime_error("The source definition changed. Reload and review it before copying.");
|
|
|
|
|
}
|
|
|
|
|
writeEnvironmentValue(*copyTo,entry.name,entry.value,previous);
|
|
|
|
|
panel.scope=*copyTo;
|
|
|
|
|
SendDlgItemMessageW(dialog,envScope,CB_SETCURSEL,static_cast<WPARAM>(panel.scope),0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if(panel.scope==EnvironmentScope::process) return;
|
|
|
|
|
EnvironmentEdit editor{panel.scope,entry};
|
|
|
|
|
if(FilesModalDialog::run(GetModuleHandleW(nullptr),MAKEINTRESOURCEW(128),dialog,environmentEditDialog,
|
|
|
|
|
reinterpret_cast<LPARAM>(&editor))!=IDOK || editor.replacement==entry.value.text) return;
|
|
|
|
|
writeEnvironmentValue(panel.scope,entry.name,{entry.value.type,editor.replacement,true},entry.value);
|
|
|
|
|
}
|
|
|
|
|
panel.notice=L"Saved. Restart Files for embedded PowerShell to inherit the new value.";
|
|
|
|
|
}
|
|
|
|
|
catch(const std::exception &error)
|
|
|
|
|
{
|
|
|
|
|
panel.notice.clear();
|
|
|
|
|
const auto details=environmentErrorText(error);
|
|
|
|
|
showFilesNotice(dialog,details.c_str(),L"Could not save environment definition",{.tone=FilesNoticeTone::warning});
|
|
|
|
|
}
|
|
|
|
|
reloadEnvironmentPanel(dialog,panel,entry.name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
INT_PTR CALLBACK environmentPanelDialog(HWND dialog,UINT message,WPARAM wp,LPARAM lp)
|
|
|
|
|
{
|
|
|
|
|
if(message==WM_INITDIALOG)
|
|
|
|
|
{
|
|
|
|
|
auto &panel=*reinterpret_cast<EnvironmentPanel *>(lp);
|
|
|
|
|
SetWindowLongPtrW(dialog,DWLP_USER,lp);
|
|
|
|
|
prepareTextDialog(dialog);
|
|
|
|
|
layoutDialogContent(dialog,610);
|
|
|
|
|
const auto scope=GetDlgItem(dialog,envScope);
|
|
|
|
|
for(const auto item:{EnvironmentScope::user,EnvironmentScope::system,EnvironmentScope::process})
|
|
|
|
|
SendMessageW(scope,CB_ADDSTRING,0,reinterpret_cast<LPARAM>(environmentScopeName(item)));
|
|
|
|
|
SendMessageW(scope,CB_SETCURSEL,0,0);
|
|
|
|
|
installFarCombo(scope,GetDlgItem(dialog,610));
|
|
|
|
|
const auto list=GetDlgItem(dialog,envList);
|
|
|
|
|
const auto bounds=dialogControlBounds(dialog,envList);
|
|
|
|
|
SetWindowPos(list,nullptr,0,0,std::max(1L,bounds.right-bounds.left-scrollBarWidth(list)),
|
|
|
|
|
bounds.bottom-bounds.top,SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE);
|
|
|
|
|
installScrollBar(list,dialog);
|
|
|
|
|
layoutDialogButtons(dialog,envFooter,{envEdit,envCopyUser,envCopySystem,envRefresh,IDOK});
|
|
|
|
|
SendMessageW(dialog,DM_SETDEFID,IDOK,0);
|
|
|
|
|
reloadEnvironmentPanel(dialog,panel);
|
|
|
|
|
SetFocus(scope);
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
auto *panel=reinterpret_cast<EnvironmentPanel *>(GetWindowLongPtrW(dialog,DWLP_USER));
|
|
|
|
|
if(!panel) return FALSE;
|
|
|
|
|
if(message==WM_DRAWITEM && wp==envScope)
|
|
|
|
|
{
|
|
|
|
|
drawFarComboRow(*reinterpret_cast<const DRAWITEMSTRUCT *>(lp));
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
if(message==WM_CTLCOLORLISTBOX && reinterpret_cast<HWND>(lp)!=GetDlgItem(dialog,envList))
|
|
|
|
|
{
|
|
|
|
|
const auto dc=reinterpret_cast<HDC>(wp);
|
|
|
|
|
SetTextColor(dc,colors[Text]);
|
|
|
|
|
SetBkColor(dc,colors[Dialog]);
|
|
|
|
|
return reinterpret_cast<INT_PTR>(teal);
|
|
|
|
|
}
|
|
|
|
|
if(message==WM_COMMAND)
|
|
|
|
|
{
|
|
|
|
|
const auto id=LOWORD(wp),notification=HIWORD(wp);
|
|
|
|
|
if(id==envScope && (notification==CBN_SELENDOK ||
|
|
|
|
|
(notification==CBN_SELCHANGE && !SendDlgItemMessageW(dialog,envScope,CB_GETDROPPEDSTATE,0,0))))
|
|
|
|
|
{
|
|
|
|
|
const auto scope=SendDlgItemMessageW(dialog,envScope,CB_GETCURSEL,0,0);
|
|
|
|
|
if(scope>=0 && scope<=static_cast<LRESULT>(EnvironmentScope::process) &&
|
|
|
|
|
static_cast<EnvironmentScope>(scope)!=panel->scope)
|
|
|
|
|
{
|
|
|
|
|
const auto *entry=selectedEnvironmentVariable(dialog,*panel);
|
|
|
|
|
const auto selected=entry?entry->name:std::wstring{};
|
|
|
|
|
panel->scope=static_cast<EnvironmentScope>(scope);
|
|
|
|
|
reloadEnvironmentPanel(dialog,*panel,selected);
|
|
|
|
|
}
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
if(id==envRefresh || (id==envList && notification==LBN_SELCHANGE))
|
|
|
|
|
{
|
|
|
|
|
if(id==envRefresh)
|
|
|
|
|
{
|
|
|
|
|
const auto *entry=selectedEnvironmentVariable(dialog,*panel);
|
|
|
|
|
const auto name=entry?entry->name:std::wstring{};
|
|
|
|
|
reloadEnvironmentPanel(dialog,*panel,name);
|
|
|
|
|
}
|
|
|
|
|
else setEnvironmentDetails(dialog,*panel);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
if(id==envEdit || id==envCopyUser || id==envCopySystem)
|
|
|
|
|
{
|
|
|
|
|
if(const auto *entry=selectedEnvironmentVariable(dialog,*panel))
|
|
|
|
|
saveEnvironmentDefinition(dialog,*panel,EnvironmentVariable(*entry),id==envEdit?std::nullopt:
|
|
|
|
|
std::optional(id==envCopyUser?EnvironmentScope::user:EnvironmentScope::system));
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
if(id==IDOK || id==IDCANCEL)
|
|
|
|
|
{
|
|
|
|
|
FilesModalDialog::finish(dialog,IDCANCEL);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(message==WM_CLOSE) {FilesModalDialog::finish(dialog,IDCANCEL);return TRUE;}
|
|
|
|
|
return paintCopyDialog(dialog,message,wp,lp,L" Environment Variables ",false,{envFooter},false,GetDlgItem(dialog,envList));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void showEnvironmentPanel(HWND owner)
|
|
|
|
|
{
|
|
|
|
|
EnvironmentPanel panel;
|
|
|
|
|
FilesModalDialog::run(GetModuleHandleW(nullptr),MAKEINTRESOURCEW(127),owner,environmentPanelDialog,
|
|
|
|
|
reinterpret_cast<LPARAM>(&panel));
|
|
|
|
|
}
|