Page 1 of 1

How to Display the "Select a File" Window?

Posted: Thu Dec 28, 2017 7:56 pm
by ColleenKobe
I am using the wxFilePickerCtrl to allow my user to select a file. When the user types a new filename into the field, or clicks the "..." button to browse and select a new file, the event handler works great.

Now I have a new requirement: when my user clicks on a different button on the screen, a series of tasks has to be done. One of those tasks is letting the user change or select a file. So I need to bring up the same "select a file" window that is called up before my wxFilePickerCtrl is called.

To meet this requirement, I did these things:

1. Created a new global procedure called File_Picker_Changed. The wxEVT_COMMAND_FILEPICKER_CTRL event procedure calls this procedure, so all the steps to be taken when the file is specified take place in a separate procedure. This new procedure works fine when the user modifies the wxFilePickerCtrl control.

2. Added a new button to the window called m_Arm. Gave it a wxEVT_COMMAND_BUTTON_CLICKED event called OnArm_Clicked. OnArm_Clicked contains only one line of code, which is a call to File_Picker_Changed.

3. Rebuilt the workspace and ran the code in the debugger. When I get to the line

Code: Select all

New_Filename = m_File_Picker->GetFileName ();
I had expected the "select a file" window to pop up, like it does in the wxEVT_COMMAND_FILEPICKER_CTRL event procedure. But it doesn't. In fact, I don't see where that "select a file" procedure gets displayed at all.

How do I bring up that "select a file" window separately from using a wxFilePicker?

Thank you!
Colleen

Here is my MainFrame.cpp, if you'd like to look:

Code: Select all

#include "MainFrame.h"                  //  3
#include <wx/aboutdlg.h>                //  6
#include <wx/filename.h>                //  6
#include <wx/msgdlg.h>                  //  6
#include <wx/string.h>                  //  6

#define CREATING_A_BIN_FILE     0
#define CREATING_A_TXT_FILE     1
#define DEFAULT_DIR             "F:\\temp\\"
#define DEFAULT_DRIVE           "F"
#define DEFAULT_FN              "aaa"
#define DEFAULT_FOLDER          "\\Temp"
#define FT_BIN                  "bin"
#define FT_TXT                  "txt"
#define OFFSET                  0x20    // start at '!'
#define QTY_NUMBERS           100
#define WORD_BINARY             "Binary"
#define WORD_TEXT               "Text"

wxFileName  New_bin_Filename;
wxFileName  New_txt_Filename;

int     What_File_Type = CREATING_A_TXT_FILE;

wxString    fpc_Get_New_bin_Filename    (void);

// -----------------------------------------------------------------------------
MainFrame::MainFrame(wxWindow* parent)
    : MainFrameBaseClass(parent)
{
    wxString    a_st    = DEFAULT_DIR;
    wxString    fn_wxSt = "";

    What_File_Type  = CREATING_A_TXT_FILE;

    m_File_Picker->SetInitialDirectory    (a_st);

    // Define the default text file.
    New_txt_Filename.Assign
           (DEFAULT_DRIVE,
            DEFAULT_FOLDER,
            DEFAULT_FN,
            FT_TXT,
            wxPATH_NATIVE);

    // Define the default binary file.
    New_bin_Filename.Assign
           (DEFAULT_DRIVE,
            DEFAULT_FOLDER,
            DEFAULT_FN,
            FT_BIN,
            wxPATH_NATIVE);
    m_File_Picker->SetFileName  (New_bin_Filename);

    // For now, assume the binary file is default.
        What_File_Type      = CREATING_A_BIN_FILE;
        fn_wxSt             = fpc_Get_New_bin_Filename();
        a_st                = WORD_BINARY;
        m_Create_Binary_File->Enable    (true);
        m_Create_Text_File->Disable     ();

    a_st = wxString::Format  ("%s file: %s", a_st, fn_wxSt);
    m_lbl_Filename->SetLabel (a_st);

}

// -----------------------------------------------------------------------------
MainFrame::~MainFrame()
{
}

// -----------------------------------------------------------------------------
wxString    fpc_Get_New_bin_Filename    (void)
{
    wxString    New_bin_Filename_St = New_bin_Filename.GetFullPath();
    return (New_bin_Filename_St);
}

// -----------------------------------------------------------------------------
wxString    fpc_Get_New_txt_Filename    (void)
{
    wxString    New_txt_Filename_St = New_txt_Filename.GetFullPath();
    return (New_txt_Filename_St);
}

// -----------------------------------------------------------------------------
void MainFrame::OnExit(wxCommandEvent& event)
{
    wxUnusedVar(event);
    Close();
}

// -----------------------------------------------------------------------------
void MainFrame::OnAbout(wxCommandEvent& event)
{
    wxUnusedVar(event);
    wxAboutDialogInfo info;
    info.SetCopyright(_("My MainFrame"));
    info.SetLicence(_("GPL v2 or later"));
    info.SetDescription(_("Short description goes here"));
    ::wxAboutBox(info);
}

// -----------------------------------------------------------------------------
// void MainFrame::File_Picker_Changed (wxFileDirPickerEvent& event)
void MainFrame::File_Picker_Changed (void)
{
    wxFileName  New_Filename;
    wxString    a_st            = "";
    wxString    fn_wxSt         = "";
    wxString    New_File_Ext    = "";

    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    New_Filename    = m_File_Picker->GetFileName ();        // <--- THIS IS IT!!! <======
    // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    // Change the extension to lower case.  Save it in the original file name.

    New_File_Ext    = New_Filename.GetExt   ();
    New_File_Ext    = New_File_Ext.Lower    (); // change type to lower case
    New_Filename.SetExt (New_File_Ext);

    if (New_File_Ext == FT_BIN)
    {
        What_File_Type      = CREATING_A_BIN_FILE;
        New_bin_Filename    = New_Filename;
        fn_wxSt             = fpc_Get_New_bin_Filename();
        a_st                = WORD_BINARY;
        m_Create_Binary_File->Enable    (true);
        m_Create_Text_File->Disable     ();
    }
    else if (New_File_Ext == FT_TXT)
    {
        What_File_Type      = CREATING_A_TXT_FILE;
        New_txt_Filename    = New_Filename;
        fn_wxSt             = fpc_Get_New_txt_Filename  ();
        a_st                = WORD_TEXT;
        m_Create_Binary_File->Disable   ();
        m_Create_Text_File->Enable      (true);
    }

    // Load the new filename, with its lower-case extension, into the
    // file picker field.
    m_File_Picker->SetFileName  (fn_wxSt);

    // Display the new filename below the file picker field.
    a_st = wxString::Format  ("%s file: %s", a_st, fn_wxSt);
    m_lbl_Filename->SetLabel (a_st);

}   // File_Picker_Changed

// -----------------------------------------------------------------------------
void MainFrame::OnFile_Picker_Changed   (wxFileDirPickerEvent& event)
{

    File_Picker_Changed ();

}   // OnFile_Picker_Changed

// -----------------------------------------------------------------------------
void MainFrame::Create_the_File (int What_File_Type)
{

    wxString    a_st    = "";
    wxString    b_st    = "";
    wxString    fn_wxSt = "";

    int i = 0;

    // CreateFile arguments.
    HANDLE                  fh                      = NULL;
    DWORD                   dwDesiredAccess         = GENERIC_WRITE;
    DWORD                   dwShareMode             = 0;
    LPSECURITY_ATTRIBUTES   lpSecurityAttributes    = NULL;
    DWORD                   dwCreationDisposition   = OPEN_ALWAYS;
    DWORD                   dwFlagsAndAttributes    = FILE_ATTRIBUTE_NORMAL;
    HANDLE                  hTemplateFile           = NULL;

    // WriteFile arguments.
    bool                    rc_from_WriteFile       = TRUE;
    wxString                lpBuffer_wxSt           = "";
    DWORD                   lpBuffer_dw [QTY_NUMBERS];
    DWORD                   nNumberOfBytesToWrite   = 0;
    DWORD                   NumberOfBytesWritten    = 0;
    LPOVERLAPPED            lpOverlapped            = NULL;

    // CloseHandle arguments.
    bool    rc_from_CloseHandle = FALSE;

    // -------------------------------------------------------------------------
    //  Get the desired filename.
    // -------------------------------------------------------------------------
    if (What_File_Type == CREATING_A_BIN_FILE)
    {

        for (i = 0; i < QTY_NUMBERS; i++)   // Initialize the WORD array to zeroes.
            lpBuffer_dw [i] = 0;

        for (i = 0; i < QTY_NUMBERS; i++)   // Fill the WORD array with binary values.
            lpBuffer_dw [i] = i + OFFSET;

        nNumberOfBytesToWrite   = sizeof (DWORD) * QTY_NUMBERS;

        fn_wxSt = fpc_Get_New_bin_Filename();
        b_st    = WORD_BINARY;
    }
    else
    {
        lpBuffer_wxSt  =               // Fill the string with text values.
            "Some years ago--never mind how long precisely--having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world.";

        nNumberOfBytesToWrite   = lpBuffer_wxSt.length   ();

        fn_wxSt = fpc_Get_New_txt_Filename();
        b_st    = WORD_TEXT;
    }

    // -------------------------------------------------------------------------
    //  Create the file.
    // -------------------------------------------------------------------------

    fh  =   CreateFile                  //   HANDLE WINAPI CreateFile(
           ((LPCTSTR) fn_wxSt,          //     _In_     LPCTSTR               lpFileName,
            dwDesiredAccess,            //     _In_     DWORD                 dwDesiredAccess,
            dwShareMode,                //     _In_     DWORD                 dwShareMode,
            lpSecurityAttributes,       //     _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
            dwCreationDisposition,      //     _In_     DWORD                 dwCreationDisposition,
            dwFlagsAndAttributes,       //     _In_     DWORD                 dwFlagsAndAttributes,
            hTemplateFile);             //     _In_opt_ HANDLE                hTemplateFile

    if (fh == INVALID_HANDLE_VALUE)
    {
        wxMessageBox    ("File wasn't created.", "Debug", wxICON_INFORMATION);
        return;
    }

    // -------------------------------------------------------------------------
    //  Write a little something to it.
    // -------------------------------------------------------------------------
    if (What_File_Type == CREATING_A_BIN_FILE)
    {
        rc_from_WriteFile   = WriteFile
           (fh,
            lpBuffer_dw,                    // Write an array of data words.
            nNumberOfBytesToWrite,
            &NumberOfBytesWritten,
            lpOverlapped);
    }
    else
    {
        rc_from_WriteFile   = WriteFile
           (fh,
            lpBuffer_wxSt,                  // Write an ASCII string.
            nNumberOfBytesToWrite,
            &NumberOfBytesWritten,
            lpOverlapped);
    }

    if (rc_from_WriteFile == FALSE)
    {
        wxMessageBox    ("Didn't write to file.", "Debug", wxICON_INFORMATION);
        return;
    }

    // -------------------------------------------------------------------------
    //  Close the file.
    // -------------------------------------------------------------------------

    rc_from_CloseHandle =   CloseHandle (fh);
    if (rc_from_CloseHandle == 0)
    {
        wxMessageBox    ("Couldn't close file.", "Debug", wxICON_INFORMATION);
        return;
    }

    a_st    = wxString::Format  ("%d/%d bytes successfully written to %s.\nThe file is closed.\nCheck it out!",
        NumberOfBytesWritten,
        nNumberOfBytesToWrite,
        fn_wxSt);
    wxMessageBox    (a_st, "Debug", wxICON_INFORMATION);
}   // Create_the_File

// -----------------------------------------------------------------------------
void MainFrame::OnCreate_a_Binary_File_Clicked(wxCommandEvent& event)
{
    Create_the_File (CREATING_A_BIN_FILE);
}

// -----------------------------------------------------------------------------
void MainFrame::OnCreate_a_Text_File_Clicked(wxCommandEvent& event)
{
    Create_the_File (CREATING_A_TXT_FILE);
}

// -----------------------------------------------------------------------------
void MainFrame::OnArm_Clicked(wxCommandEvent& event)
{
    File_Picker_Changed ();
}

Re: How to Display the "Select a File" Window?

Posted: Thu Dec 28, 2017 8:22 pm
by eranif
What you are looking for is wxFileSelector function:
http://docs.wxwidgets.org/3.1.0/group__ ... 86b01928bf