Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

Listowanie zawartości dysku C z użyciem std::filesystem

Ostatnio zmodyfikowano dzisiaj: 19h » 42 min
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
Listowanie zawartości dysku C z użyciem std::filesystem
» 2025-09-30 07:54:43
Witam. Próbuję listować zawartość dysku C. Program częściowo działa - czyli wczytuje parę pozycji, a potem się wywala. Dlaczego?


Loading directory: C:\
C:\$Recycle.Bin
C:\$WinREAgent
C:\appverifUI.dll
C:\Documents and Settings




Na innych lokacjach działa - nie chce działać tylko na dysku C. Poniżej przykład listowania Pulpitu


Jakby potrzebny był projekt do zbudowania to zamieszczam poniżej link do Githuba
https://github.com/tBane1995/Anim-Paint

Jeszcze musi to być tak zrobione, by dodawać na pierwszej pozycji katalog nadrzędny w przypadku gdy obecny katalog nie jest dyskiem. Chodzi o to, że klikając zawsze w pozycję 0 przechodzimy do katalogu nadrzędnego. Jeżeli ktoś będzie w stanie pomóc będę wdzięczny

Zapewne błąd wkradł się gdzieś tutaj ale nie jestem
void Dialog_Save_As::loadDirectory();

C/C++
#include "Dialogs/Dialog_Save_As.hpp"
#include "Window.hpp"
#include "Theme.hpp"
#include "Mouse.hpp"
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <cstdio>
#include <filesystem>
#include <shobjidl.h>    
// IShellLink
#include <objbase.h>     // CoInitializeEx
#include <cwctype>    // iswspace
#include "Time.hpp"


inline std::filesystem::path resolve_lnk( const std::filesystem::path & lnkPath ) {
   
std::filesystem::path result;
   
   
// COINIT_APARTMENTTHREADED jest OK do prostych wywołań Shell
   
HRESULT hrCo = CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED );
   
bool comInit = SUCCEEDED( hrCo );
   
   
IShellLinkW * psl = nullptr;
   
if( SUCCEEDED( CoCreateInstance( CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
   
IID_PPV_ARGS( & psl ) ) ) ) {
       
IPersistFile * ppf = nullptr;
       
if( SUCCEEDED( psl->QueryInterface( IID_PPV_ARGS( & ppf ) ) ) ) {
           
if( SUCCEEDED( ppf->Load( lnkPath.c_str(), STGM_READ ) ) ) {
               
WCHAR szTarget[ MAX_PATH ] = { };
               
WIN32_FIND_DATAW wfd { };
               
// SLGP_UNCPRIORITY -> preferuj ścieżkę UNC, ale SLGP_RAWPATH też jest ok
               
if( SUCCEEDED( psl->GetPath( szTarget, MAX_PATH, & wfd, SLGP_UNCPRIORITY ) ) ) {
                   
result = szTarget;
               
}
            }
           
ppf->Release();
       
}
       
psl->Release();
   
}
   
   
if( comInit ) CoUninitialize();
   
   
return result; // może być pusty, jeśli nie udało się rozwiązać
}


inline bool isExtension( const std::filesystem::path & p, std::wstring extension ) {
   
std::wstring ext = p.extension().wstring();
   
std::transform( ext.begin(), ext.end(), ext.begin(),::towlower );
   
return ext == extension;
}

std::filesystem::path getPath( std::filesystem::directory_entry entry ) {
   
if( isExtension( entry.path(), L".lnk" ) ) {
       
// shortcut file
       
std::filesystem::path path = resolve_lnk( entry.path() );
       
//std::wcout << L"is lnk: " << path.wstring() << std::endl;
       
return path;
   
}
   
else {
       
// other file
        //std::wcout << L"is other: " << entry.path().wstring() << std::endl;
       
return entry.path();
   
}
}

bool sortkey( std::filesystem::directory_entry first, std::filesystem::directory_entry second ) {
   
   
bool firstIsDir = std::filesystem::is_directory( first.path() );
   
bool secondIsDir = std::filesystem::is_directory( second.path() );
   
   
//std::wcout << firstIsDir << " " << secondIsDir << std::endl;
   
   
if( firstIsDir && secondIsDir ) {
       
std::wstring name_1 = first.path().filename().wstring();
       
std::wstring name_2 = second.path().filename().wstring();
       
       
std::transform( name_1.begin(), name_1.end(), name_1.begin(),[ ]( unsigned char c ) { return std::tolower( c ); } );
       
std::transform( name_2.begin(), name_2.end(), name_2.begin(),[ ]( unsigned char c ) { return std::tolower( c ); } );
       
       
if( name_1 < name_2 )
           
 return true;
       
else
           
 return false;
       
   
}
   
else if( firstIsDir && !secondIsDir ) {
       
return true;
   
}
   
else if( !firstIsDir && secondIsDir ) {
       
return false;
   
}
   
else if( !firstIsDir && !secondIsDir ) {
       
// both are file
       
std::wstring name_1 = first.path().filename().wstring();
       
std::wstring name_2 = second.path().filename().wstring();
       
       
std::transform( name_1.begin(), name_1.end(), name_1.begin(),[ ]( unsigned char c ) { return std::tolower( c ); } );
       
std::transform( name_2.begin(), name_2.end(), name_2.begin(),[ ]( unsigned char c ) { return std::tolower( c ); } );
       
       
if( name_1 < name_2 )
           
 return true;
       
else
           
 return false;
       
   
}
   
   
return false;
}

bool onlyWhitespace( const std::wstring & s ) {
   
return std::all_of( s.begin(), s.end(),
   
[ ]( wchar_t ch ) { return std::iswspace( static_cast < unsigned >( ch ) ) != 0; } );
}

bool isDrive( const std::wstring & path ) {
   
UINT type = GetDriveTypeW( path.c_str() );
   
return type != DRIVE_NO_ROOT_DIR && type != DRIVE_UNKNOWN;
}

////////////////////////////////////////////////////////////////////////////

FileRect::FileRect()
    :
ElementGUI()
{
   
_rect = sf::RectangleShape();
   
_rect.setFillColor( sf::Color( 95, 47, 47 ) );
   
_rect.setOutlineThickness( 1.0f );
   
_rect.setOutlineColor( sf::Color( 63, 47, 47 ) );
   
   
_ico = sf::Sprite();
   
   
_text = sf::Text();
   
_text.setFont( basicFont );
   
_text.setCharacterSize( dialog_content_font_size );
   
_text.setFillColor( normal_text_color );
   
_text.setString( L"pathfile/pathfile/file.png" );
   
   
_state = ButtonState::Idle;
   
_clickTime = currentTime;
   
_onclick_func =[ ]() { };
   
}

FileRect::~FileRect() {
   
}

void FileRect::setSize( sf::Vector2f size ) {
   
_rect.setSize( size );
}

void FileRect::setPosition( sf::Vector2f position ) {
   
_rect.setPosition( position );
   
_ico.setPosition( position );
   
_text.setPosition( position + sf::Vector2f( 20 + 4,( 20 - basicFont.getLineSpacing( dialog_content_font_size ) ) / 2.0f ) );
   
}

void FileRect::setText( std::wstring text ) {
   
}

void FileRect::unclick() {
   
_state = ButtonState::Idle;
   
_rect.setFillColor( sf::Color( 63, 47, 47 ) );
}

void FileRect::hover() {
   
_state = ButtonState::Hover;
   
_rect.setFillColor( sf::Color( 95, 47, 47 ) );
}

void FileRect::click() {
   
_state = ButtonState::Pressed;
   
_rect.setFillColor( sf::Color( 79, 47, 47 ) );
   
_clickTime = currentTime;
}


void FileRect::cursorHover() {
   
   
if( _rect.getGlobalBounds().contains( worldMousePosition ) ) {
       
ElementGUI_hovered = this;
   
}
}

void FileRect::handleEvent( sf::Event & event ) {
   
   
   
   
if( _rect.getGlobalBounds().contains( worldMousePosition ) ) {
       
if( event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left ) {
           
ElementGUI_pressed = this;
           
       
}
       
else if( event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left ) {
           
if( ElementGUI_pressed == this ) {
               
click();
           
}
        }
       
    }
}

void FileRect::update() {
   
if( _state == ButtonState::Pressed ) {
       
if(( currentTime - _clickTime ).asSeconds() > 0.05f ) {
           
if( _onclick_func ) {
               
_onclick_func();
           
}
           
ElementGUI_pressed = nullptr;
           
unclick();
       
}
    }
   
else if( ElementGUI_hovered == this ) {
       
hover();
   
}
   
else
       
 unclick();
   
}

void FileRect::draw() {
   
window->draw( _rect );
   
window->draw( _ico );
   
window->draw( _text );
   
}

//////////////////////////////////////////////////////////////////////////////

Dialog_Save_As::Dialog_Save_As()
    :
Dialog( L"Save As", sf::Vector2f( 400, 240 ), sf::Vector2f( 8, 120 ) )
{
   
   
_files.clear();
   
   
// current path = desktop
   
const wchar_t * userProfile = _wgetenv( L"USERPROFILE" );
   
   
if( userProfile ) {
       
currentPath = userProfile;
       
currentPath += L"\\Desktop";
   
}
   
currentPath = L"C:\\";
   
   
//std::wcout << L"Current path: " << currentPath << std::endl;
   
createFavorites();
   
createFileRects( 10 );
   
loadDirectory();
   
sf::Vector2f scrollbarPos = sf::Vector2f( _position.x + 400 - 16 - dialog_padding, _position.y + dialog_title_rect_height + dialog_padding );
   
sf::Vector2f scrollbarSize = sf::Vector2f( 16, 240 - dialog_title_rect_height - dialog_padding * 2 );
   
scrollbar = new Scrollbar( scrollbarPos.x, scrollbarPos.y, scrollbarSize.x, scrollbarSize.y, 0, _paths.size() - _files.size(), _files.size(), 0 );
   
scrollbar->_func =[ this ]() { setTheFiles(); };
   
setTheFiles();
   
setPosition( _position );
}

Dialog_Save_As::~Dialog_Save_As() {
   
}

void Dialog_Save_As::createFavorites() {
   
   
const wchar_t * userProfile = _wgetenv( L"USERPROFILE" );
   
std::wstring up( userProfile );
   
   
_favorites.push_back( new LocationRect( up + L"\\Desktop" ) );
   
_favorites.push_back( new LocationRect( up + L"\\Downloads" ) );
   
_favorites.push_back( new LocationRect( up + L"\\AppData\\Roaming\\Microsoft\\Windows\\Recent" ) );
   
   
// for the test - set the location
    //currentPath = _favorites[2]->_path.wstring();
   
}

void Dialog_Save_As::createFileRects( int filesCount ) {
   
for( int i = 0; i < filesCount; i++ ) {
       
FileRect * file = new FileRect();
       
file->setSize( sf::Vector2f( 360, 20 ) );
       
_files.push_back( file );
   
}
}

void Dialog_Save_As::sortTheFiles() {
   
std::sort( _paths.begin(), _paths.end(), sortkey );
}

void Dialog_Save_As::loadDirectory() {
   
std::wcout << L"Loading directory: " << currentPath << std::endl;
   
   
_paths.clear();
   
   
std::error_code error;
   
std::filesystem::directory_options options = std::filesystem::directory_options::skip_permission_denied;
   
   
for( const auto & entry: std::filesystem::directory_iterator( currentPath ) ) {
       
       
if( error ) {
           
std::cout << "Error accessing entry: " << error.message() << std::endl;
           
continue;
       
}
       
       
std::filesystem::directory_entry entryPath( getPath( entry ) );
       
       
auto name = entryPath.path().filename().wstring();
       
if( name.empty() || onlyWhitespace( name ) || name == L"." || name == L".." )
           
 continue;
       
       
_paths.push_back( entryPath );
       
std::wcout << entryPath.path().wstring() << std::endl;
   
}
   
   
std::wcout << L"Total files: " << _paths.size() << std::endl;
   
   
sortTheFiles();
   
   
if( !isDrive( currentPath ) ) {
       
//_paths.insert(_paths.begin(), std::filesystem::directory_entry(std::filesystem::path(currentPath).parent_path()));
   
}
}


void Dialog_Save_As::setTheFiles() {
   
   
// set the names for the file rects
   
int scrollbarValue = scrollbar->getValue();
   
for( int i = 0; i < _files.size(); i++ ) {
       
if( i + scrollbarValue < _paths.size() ) {
           
           
std::filesystem::path path = getPath( _paths[ i + scrollbarValue ] );
           
           
_files[ i ]->_text.setString(( path == std::filesystem::path( currentPath ).parent_path() ) ? L".."
               
: path.filename().wstring() );
           
std::error_code ec;
           
bool isDir = std::filesystem::is_directory( path, ec );
           
_files[ i ]->_ico.setTexture(( isDir ) ? * getTexture( L"tex\\dialog\\dictionary.png" )->_texture
                : * getTexture( L"tex\\dialog\\file.png" )->_texture );
           
_files[ i ]->_path = path;
           
           
_files[ i ]->_onclick_func =[ this, path ]() {
               
if( std::filesystem::is_directory( path ) ) {
                   
// is directory then open directory
                   
std::wcout << L"click\n";
                   
currentPath = path.wstring();
                   
loadDirectory();
                   
setTheFiles();
                   
this->scrollbar->setMax( _paths.size() - _files.size() );
                   
this->scrollbar->setValue( 0 );
               
}
               
else {
                   
// is file then open file
                   
std::wcout << L"Selected file: " << path.wstring() << std::endl;
               
}
            }
;
           
//std::wcout << path.filename().wstring() << std::endl;
       
}
       
else {
           
_files[ i ]->_text.setString( L"" );
           
_files[ i ]->_path = std::filesystem::path();
           
_files[ i ]->_ico.setTexture( * getTexture( L"tex\\dialog\\empty.png" )->_texture );
           
_files[ i ]->_onclick_func =[ ]() { };
       
}
    }
   
   
/*
 // load the harddrivers
 DWORD drives = GetLogicalDrives();
 for (int i = 0; i < 32; i++)
  if ((drives >> i) & 1)
   printf("%c:\\\n", 'A' + i);
 */
   
}

void Dialog_Save_As::setPosition( sf::Vector2f position ) {
   
Dialog::setPosition( position );
   
   
for( int i = 0; i < _files.size(); i++ ) {
       
_files[ i ]->setPosition( sf::Vector2f( _position.x + dialog_padding, _position.y + dialog_title_rect_height + dialog_padding + i * 20 ) );
   
}
   
   
sf::Vector2f scrollbarPos( _position.x + 400 - 16 - dialog_padding, _position.y + dialog_title_rect_height + dialog_padding );
   
scrollbar->setPosition( scrollbarPos.x, scrollbarPos.y );
}


void Dialog_Save_As::cursorHover() {
   
Dialog::cursorHover();
   
   
if( scrollbar->_state == ScrollbarState::Idle )
   
for( auto * file
            : _files )  file->cursorHover();
   
   
scrollbar->cursorHover();
   
}

void Dialog_Save_As::handleEvent( sf::Event & event ) {
   
Dialog::handleEvent( event );
   
   
for( auto & file: _files )
       
 file->handleEvent( event );
   
   
scrollbar->handleEvent( event );
}

void Dialog_Save_As::update() {
   
Dialog::update();
   
   
for( auto * file: _files )
       
 file->update();
   
   
scrollbar->update();
}

void Dialog_Save_As::draw() {
   
Dialog::draw();
   
   
for( auto * file: _files )
       
 file->draw();
   
   
scrollbar->draw();
}
P-183061
DejaVu
» 2025-09-30 09:26:09
Uruchom debuggera i sprawdź gdzie się wywali. Zapewne wychodzisz poza zakres tablicy.

/edit:
Komunikat sugeruje, że exception leci z std::filesystem - po prostu go przeczytaj.
P-183062
tBane
Temat założony przez niniejszego użytkownika
» 2025-09-30 09:42:02
Nie wychodzę poza zakres tablicy. Wyrzuca mi takie błędy:


[[noreturn]] inline void _Throw_fs_error(const char* _Op, __std_win_error _Error, const path& _Path1) {
    _THROW(filesystem_error(_Op, _Path1, _Make_ec(_Error)));
}



P-183063
tBane
Temat założony przez niniejszego użytkownika
» 2025-09-30 09:58:24
Dobra. ChatGPT pomógł i działa. Problematyczne bylo std::filesystem::directory_entry. Nie można ich zagnieżdżać.

C/C++
#include "Dialogs/Dialog_Save_As.hpp"
#include "Window.hpp"
#include "Theme.hpp"
#include "Mouse.hpp"
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <cstdio>
#include <filesystem>
#include <shobjidl.h>    
// IShellLink
#include <objbase.h>     // CoInitializeEx
#include <cwctype>    // iswspace
#include "Time.hpp"

inline std::filesystem::path resolve_lnk( const std::filesystem::path & lnkPath ) {
   
std::filesystem::path result;
   
   
// COINIT_APARTMENTTHREADED jest OK do prostych wywołań Shell
   
HRESULT hrCo = CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED );
   
bool comInit = SUCCEEDED( hrCo );
   
   
IShellLinkW * psl = nullptr;
   
if( SUCCEEDED( CoCreateInstance( CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER,
   
IID_PPV_ARGS( & psl ) ) ) ) {
       
IPersistFile * ppf = nullptr;
       
if( SUCCEEDED( psl->QueryInterface( IID_PPV_ARGS( & ppf ) ) ) ) {
           
if( SUCCEEDED( ppf->Load( lnkPath.c_str(), STGM_READ ) ) ) {
               
WCHAR szTarget[ MAX_PATH ] = { };
               
WIN32_FIND_DATAW wfd { };
               
// SLGP_UNCPRIORITY -> preferuj ścieżkę UNC, ale SLGP_RAWPATH też jest ok
               
if( SUCCEEDED( psl->GetPath( szTarget, MAX_PATH, & wfd, SLGP_UNCPRIORITY ) ) ) {
                   
result = szTarget;
               
}
            }
           
ppf->Release();
       
}
       
psl->Release();
   
}
   
   
if( comInit ) CoUninitialize();
   
   
return result; // może być pusty, jeśli nie udało się rozwiązać
}


inline bool isExtension( const std::filesystem::path & p, std::wstring extension ) {
   
std::wstring ext = p.extension().wstring();
   
std::transform( ext.begin(), ext.end(), ext.begin(),::towlower );
   
return ext == extension;
}

std::filesystem::path getPath( std::filesystem::path p ) {
   
if( isExtension( p, L".lnk" ) ) {
       
return resolve_lnk( p ); // może zwrócić pustą ścieżkę
   
}
   
return p;
}

static inline std::wstring to_lower_w( std::wstring s ) {
   
std::transform( s.begin(), s.end(), s.begin(),
   
[ ]( wchar_t c ) { return static_cast < wchar_t >( std::towlower( c ) ); } );
   
return s;
}

bool sortkey( std::filesystem::path a, std::filesystem::path b )
{
   
std::error_code ea, eb;
   
bool ad = std::filesystem::is_directory( a, ea );
   
bool bd = std::filesystem::is_directory( b, eb );
   
if( ea ) ad = false; // w razie błędu traktuj jak plik
   
   
if( eb ) bd = false;
   
   
// katalogi najpierw
   
if( ad != bd ) return ad;
   
   
// porównanie nazw case-insensitive (na wstring!)
   
std::wstring na = to_lower_w( a.filename().wstring() );
   
std::wstring nb = to_lower_w( b.filename().wstring() );
   
if( na != nb ) return na < nb;
   
   
// tie-breaker żeby utrzymać ścisłe porządki (ważne dla std::sort)
   
return a.native() < b.native();
}

bool onlyWhitespace( const std::wstring & s ) {
   
return std::all_of( s.begin(), s.end(),
   
[ ]( wchar_t ch ) { return std::iswspace( ch ) != 0; } );
}

bool isDrive( const std::wstring & path ) {
   
UINT type = GetDriveTypeW( path.c_str() );
   
return type != DRIVE_NO_ROOT_DIR && type != DRIVE_UNKNOWN;
}

////////////////////////////////////////////////////////////////////////////

FileRect::FileRect()
    :
ElementGUI()
{
   
_rect = sf::RectangleShape();
   
_rect.setFillColor( sf::Color( 95, 47, 47 ) );
   
_rect.setOutlineThickness( 1.0f );
   
_rect.setOutlineColor( sf::Color( 63, 47, 47 ) );
   
   
_ico = sf::Sprite();
   
   
_text = sf::Text();
   
_text.setFont( basicFont );
   
_text.setCharacterSize( dialog_content_font_size );
   
_text.setFillColor( normal_text_color );
   
_text.setString( L"pathfile/pathfile/file.png" );
   
   
_state = ButtonState::Idle;
   
_clickTime = currentTime;
   
_onclick_func =[ ]() { };
   
}

FileRect::~FileRect() {
   
}

void FileRect::setSize( sf::Vector2f size ) {
   
_rect.setSize( size );
}

void FileRect::setPosition( sf::Vector2f position ) {
   
_rect.setPosition( position );
   
_ico.setPosition( position );
   
_text.setPosition( position + sf::Vector2f( 20 + 4,( 20 - basicFont.getLineSpacing( dialog_content_font_size ) ) / 2.0f ) );
   
}

void FileRect::setText( std::wstring text ) {
   
}

void FileRect::unclick() {
   
_state = ButtonState::Idle;
   
_rect.setFillColor( sf::Color( 63, 47, 47 ) );
}

void FileRect::hover() {
   
_state = ButtonState::Hover;
   
_rect.setFillColor( sf::Color( 95, 47, 47 ) );
}

void FileRect::click() {
   
_state = ButtonState::Pressed;
   
_rect.setFillColor( sf::Color( 79, 47, 47 ) );
   
_clickTime = currentTime;
}


void FileRect::cursorHover() {
   
   
if( _rect.getGlobalBounds().contains( worldMousePosition ) ) {
       
ElementGUI_hovered = this;
   
}
}

void FileRect::handleEvent( sf::Event & event ) {
   
   
   
   
if( _rect.getGlobalBounds().contains( worldMousePosition ) ) {
       
if( event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left ) {
           
ElementGUI_pressed = this;
           
       
}
       
else if( event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left ) {
           
if( ElementGUI_pressed == this ) {
               
click();
           
}
        }
       
    }
}

void FileRect::update() {
   
if( _state == ButtonState::Pressed ) {
       
if(( currentTime - _clickTime ).asSeconds() > 0.05f ) {
           
if( _onclick_func ) {
               
_onclick_func();
           
}
           
ElementGUI_pressed = nullptr;
           
unclick();
       
}
    }
   
else if( ElementGUI_hovered == this ) {
       
hover();
   
}
   
else
       
 unclick();
   
}

void FileRect::draw() {
   
window->draw( _rect );
   
window->draw( _ico );
   
window->draw( _text );
   
}

//////////////////////////////////////////////////////////////////////////////

Dialog_Save_As::Dialog_Save_As()
    :
Dialog( L"Save As", sf::Vector2f( 400, 240 ), sf::Vector2f( 8, 120 ) )
{
   
   
_files.clear();
   
   
// current path = desktop
   
const wchar_t * userProfile = _wgetenv( L"USERPROFILE" );
   
   
if( userProfile ) {
       
currentPath = userProfile;
       
currentPath += L"\\Desktop";
   
}
   
currentPath = L"C:\\";
   
   
//std::wcout << L"Current path: " << currentPath << std::endl;
   
createFavorites();
   
createFileRects( 10 );
   
loadDirectory();
   
sf::Vector2f scrollbarPos = sf::Vector2f( _position.x + 400 - 16 - dialog_padding, _position.y + dialog_title_rect_height + dialog_padding );
   
sf::Vector2f scrollbarSize = sf::Vector2f( 16, 240 - dialog_title_rect_height - dialog_padding * 2 );
   
scrollbar = new Scrollbar( scrollbarPos.x, scrollbarPos.y, scrollbarSize.x, scrollbarSize.y, 0, _paths.size() - _files.size(), _files.size(), 0 );
   
scrollbar->_func =[ this ]() { setTheFiles(); };
   
setTheFiles();
   
setPosition( _position );
}

Dialog_Save_As::~Dialog_Save_As() {
   
}

void Dialog_Save_As::createFavorites() {
   
   
const wchar_t * userProfile = _wgetenv( L"USERPROFILE" );
   
std::wstring up( userProfile );
   
   
_favorites.push_back( new LocationRect( up + L"\\Desktop" ) );
   
_favorites.push_back( new LocationRect( up + L"\\Downloads" ) );
   
_favorites.push_back( new LocationRect( up + L"\\AppData\\Roaming\\Microsoft\\Windows\\Recent" ) );
   
   
// for the test - set the location
    //currentPath = _favorites[2]->_path.wstring();
   
}

void Dialog_Save_As::createFileRects( int filesCount ) {
   
for( int i = 0; i < filesCount; i++ ) {
       
FileRect * file = new FileRect();
       
file->setSize( sf::Vector2f( 360, 20 ) );
       
_files.push_back( file );
   
}
}

void Dialog_Save_As::sortTheFiles() {
   
std::sort( _paths.begin(), _paths.end(), sortkey );
}

void Dialog_Save_As::loadDirectory() {
   
std::wcout << L"Loading directory: " << currentPath << std::endl;
   
_paths.clear();
   
   
std::error_code ec;
   
auto opts = std::filesystem::directory_options::skip_permission_denied;
   
   
for( std::filesystem::directory_iterator it( currentPath, opts, ec ), end;
   
it != end; it.increment( ec ) ) {
       
if( ec ) { ec.clear(); continue; }
       
       
std::filesystem::path p = getPath( it->path() );
       
if( p.empty() ) continue; // nieudany .lnk
       
       
auto name = p.filename().wstring();
       
if( name.empty() || onlyWhitespace( name ) || name == L"." || name == L".." )
           
 continue;
       
       
_paths.push_back( p );
       
std::wcout << p.wstring() << std::endl;
   
}
   
   
sortTheFiles();
   
   
if( !isDrive( currentPath ) )
       
 _paths.insert( _paths.begin(), std::filesystem::path( currentPath ).parent_path() );
   
   
std::wcout << L"Total files: " << _paths.size() << std::endl;
   
}


void Dialog_Save_As::setTheFiles() {
   
   
// set the names for the file rects
   
int scrollbarValue = scrollbar->getValue();
   
for( int i = 0; i < _files.size(); i++ ) {
       
if( i + scrollbarValue < _paths.size() ) {
           
           
std::filesystem::path path = _paths[ i + scrollbarValue ];
           
           
if( i + scrollbarValue == 0 && !isDrive( currentPath ) )
               
 _files[ i ]->_text.setString( L".." );
           
else
               
 _files[ i ]->_text.setString( path.filename().wstring() );
           
           
std::error_code ec;
           
bool isDir = std::filesystem::is_directory( path, ec );
           
_files[ i ]->_ico.setTexture(( isDir ) ? * getTexture( L"tex\\dialog\\dictionary.png" )->_texture
                : * getTexture( L"tex\\dialog\\file.png" )->_texture );
           
_files[ i ]->_path = path;
           
           
_files[ i ]->_onclick_func =[ this, path ]() {
               
if( std::filesystem::is_directory( path ) ) {
                   
// is directory then open directory
                   
std::wcout << L"click\n";
                   
currentPath = path.wstring();
                   
loadDirectory();
                   
                   
this->scrollbar->setMax( _paths.size() - _files.size() );
                   
this->scrollbar->setValue( 0 );
                   
setTheFiles();
               
}
               
else {
                   
// is file then open file
                   
std::wcout << L"Selected file: " << path.wstring() << std::endl;
               
}
            }
;
           
//std::wcout << path.filename().wstring() << std::endl;
       
}
       
else {
           
_files[ i ]->_text.setString( L"" );
           
_files[ i ]->_path = std::filesystem::path();
           
_files[ i ]->_ico.setTexture( * getTexture( L"tex\\dialog\\empty.png" )->_texture );
           
_files[ i ]->_onclick_func =[ ]() { };
       
}
    }
   
   
/*
 // load the harddrivers
 DWORD drives = GetLogicalDrives();
 for (int i = 0; i < 32; i++)
  if ((drives >> i) & 1)
   printf("%c:\\\n", 'A' + i);
 */
   
}

void Dialog_Save_As::setPosition( sf::Vector2f position ) {
   
Dialog::setPosition( position );
   
   
for( int i = 0; i < _files.size(); i++ ) {
       
_files[ i ]->setPosition( sf::Vector2f( _position.x + dialog_padding, _position.y + dialog_title_rect_height + dialog_padding + i * 20 ) );
   
}
   
   
sf::Vector2f scrollbarPos( _position.x + 400 - 16 - dialog_padding, _position.y + dialog_title_rect_height + dialog_padding );
   
scrollbar->setPosition( scrollbarPos.x, scrollbarPos.y );
}


void Dialog_Save_As::cursorHover() {
   
Dialog::cursorHover();
   
   
if( scrollbar->_state == ScrollbarState::Idle )
   
for( auto * file
            : _files )  file->cursorHover();
   
   
scrollbar->cursorHover();
   
}

void Dialog_Save_As::handleEvent( sf::Event & event ) {
   
Dialog::handleEvent( event );
   
   
for( auto & file: _files )
       
 file->handleEvent( event );
   
   
scrollbar->handleEvent( event );
}

void Dialog_Save_As::update() {
   
Dialog::update();
   
   
for( auto * file: _files )
       
 file->update();
   
   
scrollbar->update();
}

void Dialog_Save_As::draw() {
   
Dialog::draw();
   
   
for( auto * file: _files )
       
 file->draw();
   
   
scrollbar->draw();
}
P-183064
« 1 »
  Strona 1 z 1