Listing 5
// main_dlg.cpp
#include "main_dlg.h"
#include "resource.h"
using namespace win32::gui;
// IDC_files - a subclassed ListCtrl
// IDC_filter - the filter edit box
// IDC_path - the path edit box
// IDC_Export - the "Export" button
struct main_dlg_handler : event_handler<main_dlg_handler, dialog,main_dlg> {
// easier access to the files window
wnd<files_filt> files;
main_dlg_handler()
: files( child(IDC_files)) {} // note: automatic cast
// user is modifying the current directory
handle_event on_focus_path() {
// wait for the user to finish modification
signal result = child(IDC_path)->wait(
wait_for::key_pressed(VK_RETURN) ||
wait_for::key_pressed(VK_TAB) ||
wait_for::key_pressed(VK_ESCAPE) || wait_for::kill_focus);
if ( result == wait_for::key_pressed(VK_RETURN) ||
result == wait_for::key_pressed(VK_TAB) )
// the user has pressed Enter or Tab
files->path( child(IDC_path)->text() );
return command<IDC_path,EN_SETFOCUS>().HANDLED_BY(&me::on_focus_path);
}
// user is modifying the filter
handle_event on_focus_filt() {
// wait for the user to finish modification
signal result = // (similar to above)
if ( result == wait_for::key_pressed(VK_RETURN) ||
result == wait_for::key_pressed(VK_TAB) )
// the user has pressed Enter or Tab
files->filter( child(IDC_filter)->text() );
return command<IDC_filter,EN_SETFOCUS>().HANDLED_BY(&me::on_focus_filt);
}
// the user clicked on Export List button
handle_event on_export() {
window()->export(); // call the main_dlg::export
return command<IDC_Export,BN_CLICKED>().HANDLED_BY(&me::on_export);
}
};
main_dlg::main_dlg() {
// subclass the control with id IDC_files to our class
subclass<files_filt>(IDC_files);
child(IDC_path)->text( current_path() );
child(IDC_filter)->text( "*.*");
// show files from current path
child<files_filt>(IDC_files)->path( current_path() );
// auto refresh each 200 ms
child<files_filt>(IDC_files)->set_auto_refresh_time( 200);
}
// exports the list of files
void main_dlg::export() {
wnd<files_filt> files_wnd = child(IDC_files);
std::string file = files_wnd->path() + "/export.txt";
std::vector<std::string> names;
files_wnd->get_file_names( names);
{ std::ofstream out( file.c_str());
std::copy( names.begin(), names.end(),
std::ostream_iterator<std::string>(out, "\n")); }
::ShellExecute( raw_hwnd(),"open",file.c_str(),0,"",SW_SHOW);
}