383 lines
14 KiB
C++
383 lines
14 KiB
C++
|
|
bool pathNavigationAvailable(const Panel &panel)
|
|
{
|
|
return !transferProfileList(panel) && !panel.findOrigin && !panel.archive && !panel.directory.empty();
|
|
}
|
|
|
|
void navigatePanelPath(Panel &panel,const fs::path &path)
|
|
{
|
|
if(path.empty()) return;
|
|
const auto current=folderLocation(panel,panel.directory);
|
|
auto target=folderLocation(panel,path);
|
|
if(sameFolderLocation(current,target)) return;
|
|
closePanelSearch();
|
|
openFolderLocation(panel,target);
|
|
}
|
|
|
|
void navigatePanelHistory(Panel &panel,int direction)
|
|
{
|
|
if(const auto next=panel.navigation.neighbor(direction))
|
|
{
|
|
closePanelSearch();
|
|
openFolderLocation(panel,panel.navigation.history[*next],next);
|
|
}
|
|
}
|
|
|
|
constexpr int addressField=485,addressLabel=486,addressFooter=487;
|
|
|
|
struct PathPrompt
|
|
{
|
|
fs::path base, result;
|
|
std::optional<FilesPeer> peer;
|
|
std::optional<TransferProfile> ftp;
|
|
std::wstring completionInput;
|
|
std::future<std::wstring> completion;
|
|
};
|
|
|
|
constexpr UINT addressComplete=WM_APP+29;
|
|
constexpr UINT_PTR addressCompletionTimer=1;
|
|
|
|
fs::path enteredPanelPath(const fs::path &base,std::wstring text,bool ftp)
|
|
{
|
|
if(text.size()>=2 && text.front()==L'"' && text.back()==L'"')
|
|
text=text.substr(1,text.size()-2);
|
|
if(ftp && text.find_first_of(L":\r\n")!=text.npos) return {};
|
|
auto path=fs::path(std::move(text));
|
|
if(ftp)
|
|
{
|
|
if(path.has_root_name()) return {};
|
|
return (path.has_root_directory()?path:base/path).lexically_normal();
|
|
}
|
|
return path.is_absolute()?path:base/path;
|
|
}
|
|
|
|
std::wstring panelPathCompletion(const fs::path &base,const std::wstring &text,
|
|
const std::optional<FilesPeer> &peer,const std::optional<TransferProfile> &ftp)
|
|
{
|
|
const auto path=enteredPanelPath(base,text,ftp.has_value());
|
|
if(path.empty()) return {};
|
|
const auto directory=path.has_filename()?path.parent_path():path;
|
|
const auto prefix=path.filename().native();
|
|
fs::path best;
|
|
const auto accept=[&](const fs::path &candidate)
|
|
{
|
|
const auto name=candidate.filename().native();
|
|
if(name.size()<prefix.size() || (!prefix.empty() && CompareStringOrdinal(name.data(),
|
|
static_cast<int>(prefix.size()),prefix.data(),static_cast<int>(prefix.size()),ftp?FALSE:TRUE)!=CSTR_EQUAL))
|
|
return;
|
|
if(best.empty() || candidate<best) best=candidate;
|
|
};
|
|
const auto visit=[&](const fs::path &candidate,const FileInfo &info)
|
|
{
|
|
if(info.attributes&FILE_ATTRIBUTE_DIRECTORY) accept(candidate);
|
|
return true;
|
|
};
|
|
if(peer)
|
|
{
|
|
FilesConnection host(&*peer);
|
|
listFiles(host,directory,visit);
|
|
}
|
|
else if(ftp)
|
|
{
|
|
TransferSession host(*ftp);
|
|
host.list(directory,visit);
|
|
}
|
|
else
|
|
{
|
|
std::error_code error;
|
|
for(fs::directory_iterator it(directory,error),end;!error && it!=end;it.increment(error))
|
|
{
|
|
std::error_code typeError;
|
|
if(it->is_directory(typeError)) accept(it->path());
|
|
}
|
|
}
|
|
if(best.empty()) return {};
|
|
return ftp?ftpPath(best)+L"/":best.native()+L"\\";
|
|
}
|
|
|
|
LRESULT CALLBACK addressFieldKeys(HWND field,UINT message,WPARAM wp,LPARAM lp,UINT_PTR,DWORD_PTR)
|
|
{
|
|
if(message==WM_GETDLGCODE)
|
|
{
|
|
const auto *key=reinterpret_cast<const MSG *>(lp);
|
|
if(key && key->wParam==VK_TAB && !(GetKeyState(VK_SHIFT)&0x8000))
|
|
return DefSubclassProc(field,message,wp,lp)|DLGC_WANTTAB;
|
|
}
|
|
if(message==WM_KEYDOWN && wp==VK_TAB && !(GetKeyState(VK_SHIFT)&0x8000))
|
|
{
|
|
SendMessageW(GetParent(field),addressComplete,0,0);
|
|
return 0;
|
|
}
|
|
if(message==WM_CHAR && wp==VK_TAB) return 0;
|
|
return DefSubclassProc(field,message,wp,lp);
|
|
}
|
|
|
|
INT_PTR CALLBACK editPathDialog(HWND dialog,UINT message,WPARAM wp,LPARAM lp)
|
|
{
|
|
if(message==WM_DRAWITEM)
|
|
{
|
|
paintDialogButton(*reinterpret_cast<const DRAWITEMSTRUCT *>(lp),RGB(192,192,192),RGB(0,0,0),true);
|
|
return TRUE;
|
|
}
|
|
if(const auto painted=paintCopyDialog(dialog,message,wp,lp,L" Go to folder ",false,{addressFooter}))
|
|
return painted;
|
|
if(message==WM_INITDIALOG)
|
|
{
|
|
auto &prompt=*reinterpret_cast<PathPrompt *>(lp);
|
|
SetWindowLongPtrW(dialog,DWLP_USER,lp);
|
|
prepareTextDialog(dialog);
|
|
layoutDialogContent(dialog,addressLabel);
|
|
layoutDialogButtons(dialog,addressFooter,{IDOK,IDCANCEL});
|
|
const auto field=GetDlgItem(dialog,addressField);
|
|
SendMessageW(field,EM_SETLIMITTEXT,32767,0);
|
|
const auto initial=prompt.base/fs::path{};
|
|
SetWindowTextW(field,(prompt.ftp?ftpPath(initial):initial.native()).c_str());
|
|
SetWindowSubclass(field,addressFieldKeys,4,0);
|
|
SetFocus(field);
|
|
SendMessageW(field,EM_SETSEL,0,-1);
|
|
return FALSE;
|
|
}
|
|
if(message==addressComplete)
|
|
{
|
|
auto &prompt=*reinterpret_cast<PathPrompt *>(GetWindowLongPtrW(dialog,DWLP_USER));
|
|
if(prompt.completion.valid()) return TRUE;
|
|
prompt.completionInput=shortcutField(dialog,addressField);
|
|
std::packaged_task<std::wstring()> task([base=prompt.base,text=prompt.completionInput,
|
|
peer=prompt.peer,ftp=prompt.ftp]
|
|
{
|
|
return panelPathCompletion(base,text,peer,ftp);
|
|
});
|
|
if(!SetTimer(dialog,addressCompletionTimer,30,nullptr)) return TRUE;
|
|
prompt.completion=task.get_future();
|
|
std::thread(std::move(task)).detach();
|
|
return TRUE;
|
|
}
|
|
if(message==WM_TIMER && wp==addressCompletionTimer)
|
|
{
|
|
auto &prompt=*reinterpret_cast<PathPrompt *>(GetWindowLongPtrW(dialog,DWLP_USER));
|
|
if(!prompt.completion.valid() ||
|
|
prompt.completion.wait_for(std::chrono::seconds(0))!=std::future_status::ready) return TRUE;
|
|
KillTimer(dialog,addressCompletionTimer);
|
|
try
|
|
{
|
|
const auto value=prompt.completion.get();
|
|
if(!value.empty() && shortcutField(dialog,addressField)==prompt.completionInput)
|
|
{
|
|
const auto field=GetDlgItem(dialog,addressField);
|
|
SetWindowTextW(field,value.c_str());
|
|
SendMessageW(field,EM_SETSEL,value.size(),value.size());
|
|
SendMessageW(field,EM_SCROLLCARET,0,0);
|
|
}
|
|
}
|
|
catch(const std::exception &) {}
|
|
return TRUE;
|
|
}
|
|
if(message==WM_COMMAND && LOWORD(wp)==IDOK)
|
|
{
|
|
auto &prompt=*reinterpret_cast<PathPrompt *>(GetWindowLongPtrW(dialog,DWLP_USER));
|
|
auto path=enteredPanelPath(prompt.base,shortcutField(dialog,addressField),prompt.ftp.has_value()).lexically_normal();
|
|
if(prompt.ftp?!isFtpFolderPath(path):!path.is_absolute())
|
|
{
|
|
showFilesNotice(dialog,L"Enter a folder path.",L"Go to folder");
|
|
return TRUE;
|
|
}
|
|
if(!path.has_filename() && path.has_relative_path()) path=path.parent_path();
|
|
prompt.result=std::move(path);
|
|
FilesModalDialog::finish(dialog,IDOK);
|
|
return TRUE;
|
|
}
|
|
if(message==WM_CLOSE || (message==WM_COMMAND && LOWORD(wp)==IDCANCEL))
|
|
{
|
|
FilesModalDialog::finish(dialog,IDCANCEL);
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
void editPanelPath(Panel &panel)
|
|
{
|
|
PathPrompt prompt{panel.directory,{},panel.peer,panel.ftp};
|
|
if(FilesModalDialog::run(GetModuleHandleW(nullptr),MAKEINTRESOURCEW(117),GetParent(panel.list),
|
|
editPathDialog,reinterpret_cast<LPARAM>(&prompt))==IDOK)
|
|
navigatePanelPath(panel,prompt.result);
|
|
}
|
|
|
|
void showFolderPicker(HWND owner,int side,const wchar_t *caption,std::vector<FolderLocation> locations,
|
|
RECT anchor)
|
|
{
|
|
hidePicker();
|
|
showMenu(false);
|
|
commandPicker=false;
|
|
pickerPanel=side;
|
|
folderPicker=FolderPicker{caption,std::move(locations)};
|
|
SendMessageW(picker,LB_RESETCONTENT,0,0);
|
|
size_t longest=wcslen(caption);
|
|
for(const auto &location:folderPicker->locations)
|
|
{
|
|
auto label=isTransferFolder(location.provider)?ftpPath(location.directory):location.directory.native();
|
|
longest=std::max(longest,label.size());
|
|
SendMessageW(picker,LB_ADDSTRING,0,reinterpret_cast<LPARAM>(label.c_str()));
|
|
}
|
|
RECT client{};
|
|
GetClientRect(owner,&client);
|
|
const int inset=dialogFrameMetrics(GetDpiForWindow(owner)).content();
|
|
const int width=std::max(1,std::min(static_cast<int>(client.right)-2*inset,static_cast<int>(longest+3)*cellWidth));
|
|
const int x=std::clamp(static_cast<int>(anchor.left),inset,std::max(inset,static_cast<int>(client.right)-width-inset));
|
|
const int y=anchor.bottom+inset;
|
|
const int row=std::max(1,static_cast<int>(SendMessageW(picker,LB_GETITEMHEIGHT,0,0)));
|
|
RECT input{};
|
|
GetWindowRect(terminalInput,&input);
|
|
MapWindowPoints(nullptr,owner,reinterpret_cast<POINT *>(&input),2);
|
|
const int bottom=std::min(client.bottom,input.top);
|
|
const int shown=std::clamp((bottom-y-inset)/row,1,static_cast<int>(folderPicker->locations.size()));
|
|
showFramedList(picker,pickerFrame,x,y,width,row*shown,inset);
|
|
SendMessageW(picker,LB_SETCURSEL,0,0);
|
|
SetFocus(picker);
|
|
}
|
|
|
|
void showRecentFolders(Panel &panel)
|
|
{
|
|
PanelListing listing;
|
|
listing.recent=readRecentFolders();
|
|
listing.rows.reserve(listing.recent.size());
|
|
for(size_t i=0;i<listing.recent.size();++i)
|
|
{
|
|
auto &location=listing.recent[i];
|
|
auto label=location.directory.native();
|
|
if(location.provider!=FolderProvider::local)
|
|
{
|
|
const wchar_t *kind=L"LAN";
|
|
if(isTransferFolder(location.provider))
|
|
{
|
|
const auto protocol=folderTransferProtocol(location.provider);
|
|
kind=transferProtocolName(protocol);
|
|
if(loadTransferProfiles(protocol))
|
|
if(const auto *profile=findTransferProfile(location.id,protocol)) location.label=profile->name;
|
|
label=ftpPath(location.directory);
|
|
}
|
|
if(!location.label.empty()) label=location.label+L" · "+label;
|
|
label=std::wstring(kind)+L" · "+label;
|
|
}
|
|
listing.rows.emplace_back(std::to_wstring(i),std::move(label));
|
|
}
|
|
openPanelListing(panel,std::move(listing));
|
|
}
|
|
|
|
void openRecentFolder(Panel &panel)
|
|
{
|
|
const auto row=currentPanelRow(panel);
|
|
if(row!=LB_ERR && !panel.scan.valid())
|
|
openFolderLocation(panel,panel.listing->recent[row]);
|
|
}
|
|
|
|
void removeRecentFolder(Panel &panel)
|
|
{
|
|
const auto row=currentPanelRow(panel);
|
|
if(row==LB_ERR || panel.scan.valid()) return;
|
|
if(!updateRecentFolders(panel.listing->recent[row],false))
|
|
{
|
|
setStatus(L"Could not remove the recent folder.");
|
|
return;
|
|
}
|
|
for(auto &other:panels)
|
|
if(recentFolderList(other) && !other.scan.valid()) showRecentFolders(other);
|
|
}
|
|
|
|
void showHiddenPathParts(HWND window,int side,const PathPart &part)
|
|
{
|
|
const auto owner=GetParent(window);
|
|
auto anchor=part.rect;
|
|
MapWindowPoints(window,owner,reinterpret_cast<POINT*>(&anchor),2);
|
|
std::vector<FolderLocation> locations;
|
|
locations.reserve(part.hidden.size());
|
|
for(const auto &path:part.hidden) locations.push_back(folderLocation(panels[side],path));
|
|
showFolderPicker(owner,side,part.action==PathAction::ancestors?L"Parent folders":L"Previous path",
|
|
std::move(locations),anchor);
|
|
}
|
|
|
|
LRESULT CALLBACK pathBarInput(HWND window,UINT message,WPARAM wp,LPARAM lp,UINT_PTR,DWORD_PTR)
|
|
{
|
|
if(message==WM_LBUTTONDOWN)
|
|
{
|
|
SetCapture(window);
|
|
return 0;
|
|
}
|
|
if(message==WM_LBUTTONUP && GetCapture()==window)
|
|
{
|
|
ReleaseCapture();
|
|
const int index=GetDlgCtrlID(window)-142;
|
|
if(index<0 || index>=2) return 0;
|
|
panelClipboardContext=true;
|
|
auto &panel=panels[index];
|
|
if(!pathNavigationAvailable(panel))
|
|
{
|
|
selectPanelEdge(panel,true);
|
|
return 0;
|
|
}
|
|
activatePanel(index);
|
|
RECT bounds{};
|
|
GetClientRect(window,&bounds);
|
|
const POINT mouse{GET_X_LPARAM(lp),GET_Y_LPARAM(lp)};
|
|
const PathBarMetrics metrics(window,bounds);
|
|
const auto dc=metrics.compact?GetDC(window):nullptr;
|
|
if(metrics.compact && !dc) return 0;
|
|
const auto parts=panelPathParts(dc,panel,bounds,metrics);
|
|
if(dc) ReleaseDC(window,dc);
|
|
for(const auto &part:parts)
|
|
{
|
|
if(!part.enabled || !PtInRect(&part.rect,mouse)) continue;
|
|
switch(part.action)
|
|
{
|
|
case PathAction::back: navigatePanelHistory(panel,-1); break;
|
|
case PathAction::forward: navigatePanelHistory(panel,1); break;
|
|
case PathAction::up: case PathAction::folder: navigatePanelPath(panel,part.target); break;
|
|
case PathAction::edit: editPanelPath(panel); break;
|
|
case PathAction::source:
|
|
if(panel.peer) chooseDrive(index,false,true);
|
|
else
|
|
{
|
|
const auto protocol=panel.ftp->protocol;
|
|
const auto *profile=findTransferProfile(panel.ftp->id,protocol);
|
|
showTransferProfiles(panel,protocol,profile?
|
|
ftpRoot/std::to_wstring(profile-transferProfiles(protocol).rows.data()):fs::path{});
|
|
}
|
|
break;
|
|
case PathAction::ancestors: case PathAction::previous: showHiddenPathParts(window,index,part); break;
|
|
}
|
|
break;
|
|
}
|
|
return 0;
|
|
}
|
|
return DefSubclassProc(window,message,wp,lp);
|
|
}
|
|
|
|
bool handlePanelPathKey(HWND owner,const MSG &message)
|
|
{
|
|
if(!commandLineAvailable(owner) || terminalVisible ||
|
|
(message.hwnd!=terminalInput && message.hwnd!=panels[active].list) ||
|
|
((GetKeyState(VK_SHIFT)|GetKeyState(VK_LWIN)|GetKeyState(VK_RWIN))&0x8000)) return false;
|
|
auto &panel=panels[active];
|
|
if(panel.archive)
|
|
{
|
|
if(message.message!=WM_SYSKEYDOWN || message.wParam!=VK_UP ||
|
|
!(message.lParam&(1LL<<29)) || (GetKeyState(VK_CONTROL)&0x8000)) return false;
|
|
if(!(message.lParam&(1LL<<30))) parentArchive(panel);
|
|
return true;
|
|
}
|
|
if(!pathNavigationAvailable(panel)) return false;
|
|
if(message.message==WM_KEYDOWN && message.wParam=='L' &&
|
|
(GetKeyState(VK_CONTROL)&0x8000) && !(GetKeyState(VK_MENU)&0x8000))
|
|
{
|
|
if(!(message.lParam&(1LL<<30))) editPanelPath(panel);
|
|
return true;
|
|
}
|
|
if(message.message!=WM_SYSKEYDOWN || !(message.lParam&(1LL<<29)) ||
|
|
(GetKeyState(VK_CONTROL)&0x8000)) return false;
|
|
if(message.wParam==VK_UP)
|
|
navigatePanelPath(panel,panelParentPath(panel.directory,!panel.peer && !panel.ftp));
|
|
else if(message.wParam==VK_LEFT || message.wParam==VK_RIGHT)
|
|
navigatePanelHistory(panel,message.wParam==VK_LEFT?-1:1);
|
|
else return false;
|
|
return true;
|
|
}
|