Minimal solution to use wxWidgets: wxTextControl to emulate a C++ std::cin and std::cin(getline) stream

251 views Asked by At

I have transcribed a c++ console app to GUI using wxWidgets. Most of my functions were written for the commandline flow. I am creating handles and additional code to interface GUI with the functions. I have emulated a console for output stream, which works good (using wxStreamToTextRedirector). However, I can't find a simple solution to take user input from a textcontrol and substitute the std::cin command in cases like the code below. Event handlers and GUI controls for my frame are in MainFrame.cpp and object data and functions in another Data.cpp

I have a function in Data.cpp which will be called on a button press event from MainFrame.cpp:

bool Data::run_yn_prompt()
{   //Run Y or N input prompt
    do {
            std::string input = "";
            std::cout << "\n Input Y/y to proceed, N/n to cancel:"; **//OUTPUT TO DISPLAY CONSOLE**
            std::getline(std::cin, input);          **//Need to fetch wxTextCtrl input at this point only**
            std::cout << input;
            if ( (input=="Y") || (input=="y") ) return true;
            if ( (input=="N") || (input=="n") ) return false;
        } while(1==1);
}

The problem is I need the input to be fetched only after:

  1. the previous std::cout is run and
  2. the user enters the input and presses ENTER (to be signaled by wxEVT_TEXT_ENTER event handler)

The only way I guess I could do it is by adding a lot of conditionals and boolean flags to watch for user input and enter press. Is there any simple strategy to make this work? My goal is not to publish this app, but learn to get wxWidgets elements to work as per my need. I have so many std::cin in the middle of my code like this. This problem has stopped me from moving forward with wxWidgets. And I would like to keep everything in a single frame without additional dialogs.

enter image description here

1

There are 1 answers

0
VZ. On

The only way to preserve the code using std::cin in a GUI program is to use modal dialogs for text entry (e.g. wxGetTextFromUser()) instead. This is not going to be nearly as convenient for the user as using a text control inside the main program window, but it's the only way to preserve the existing control flow.