Console+GUI hybrid app

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 340
Joined: Mon Oct 06, 2008 12:55 pm

Console+GUI hybrid app

Post by samsam598 »

Hello,

I want to create a hybrid app that includes console and GUI together.Below is my homework which I don't quite sure whether it is right.The wrong part is the last line of code (MyApp::OnInit(),right before return true) that execute console output's not executed in the right order,it is before the GUI windows being showing.What's more,if SetExitOnFrameDelete(false), after the GUI windows deletion,the app did not quit,and have no way to quit.

Thanks for your help in advance.

Code: Select all


//This application demonstrates how to show a dialog in a console app.

#include <wx/wx.h>
#include <stdio.h>


enum
{
    ID_Quit,
    ID_CLICK_ME,
    ID_DISABLE_ENABLE,
};
class MyApp : public wxApp
{

public:
    bool  OnInit ();


};
class MyFrame:public wxFrame
{
public:
    MyFrame(const wxString& title,const wxPoint& pos,const wxSize& size);
    void OnQuit(wxCommandEvent& event);
    void OnClickMe(wxCommandEvent& event);
    void OnDisableEnable(wxCommandEvent& event);
    void OnClose(wxCloseEvent& event);



private:
    bool m_enabled;
    wxButton* m_clickButton;
    wxButton* m_enableButton;
    DECLARE_EVENT_TABLE()
};
BEGIN_EVENT_TABLE(MyFrame,wxFrame)
    EVT_MENU(ID_Quit,MyFrame::OnQuit)
    EVT_BUTTON(ID_CLICK_ME,MyFrame::OnClickMe)
    EVT_BUTTON(ID_DISABLE_ENABLE,MyFrame::OnDisableEnable)
    EVT_CLOSE(MyFrame::OnClose)
END_EVENT_TABLE()



IMPLEMENT_APP(MyApp)

MyFrame::MyFrame(const wxString& title,const wxPoint& pos,const wxSize& size):
wxFrame((wxFrame*)NULL,-1,title,pos,size),m_enabled(true)
{
    wxMenu* menu=new wxMenu;
    menu->Append(ID_Quit,"E&xit");
    wxMenuBar* menuBar=new wxMenuBar;
    menuBar->Append(menu,"&File");
    SetMenuBar(menuBar);


    m_clickButton=new wxButton(this,ID_CLICK_ME,"&Click Me");

    wxBoxSizer* topsizer=new wxBoxSizer(wxHORIZONTAL);
    topsizer->Add(m_clickButton,0,wxTOP|wxALL,5);

    m_enableButton=new wxButton(this,ID_DISABLE_ENABLE,"&Disable");
    topsizer->Add(m_enableButton,0,wxTOP|wxALL,5);

    wxBoxSizer* vertsizer=new wxBoxSizer(wxVERTICAL);
    wxStaticText* text=new wxStaticText(this,-1,wxT("Test"),wxDefaultPosition,wxDefaultSize,wxALIGN_CENTRE_HORIZONTAL|wxEXPAND);
    topsizer->Add(vertsizer,1,wxALIGN_CENTER|wxALL,10);
    vertsizer->Add(text,1,wxALIGN_CENTER|wxALL,10);
    CreateStatusBar();
    SetSizer(topsizer);

    //Connect(wxEVT_CLOSE_WINDOW, wxCloseEventHandler(MyFrame::OnClose));

}

void MyFrame::OnQuit(wxCommandEvent& event)
{
    Close(true);
}
void MyFrame::OnClickMe(wxCommandEvent& WXUNUSED(event))
{
    wxMessageBox(wxT("I've been clicked!"));

}
void MyFrame::OnDisableEnable(wxCommandEvent& WXUNUSED(event))
{
    m_enabled=(m_enabled==true?false:true);
    m_clickButton->Enable(m_enabled);

    m_enableButton->SetLabel((m_enabled?"&Disable":"&Enable"));
}

void MyFrame::OnClose(wxCloseEvent& event)
{

    wxMessageDialog *dial = new wxMessageDialog(NULL,
      wxT("Are you sure to quit?"), wxT("Question"),
      wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);

     int ret = dial->ShowModal();
    dial->Destroy();

    if (ret == wxID_YES) {
      event.Skip();//Destroy();

  }


}

bool MyApp::OnInit()
{
    //wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
    printf("This is a line of text from console.\n");
    wxPrintf("Press any key to show a dialog..\n");
    system("pause");

    wxMessageBox(wxT("This is a window."));
    wxPrintf("Press any key to continue...\n");
    system("pause");

    wxPrintf("This is a line of text from console using wxPrintf\n");
    system("pause");

    MyFrame* frame=new MyFrame(wxT("Test"),wxPoint(50,50),wxSize(600,340));

    //SetTopWindow(frame);
    
    //****And below matters a lot!!!
    //SetExitOnFrameDelete(false);
    
    
    frame->Show(true);
    wxPrintf("Now application will quit.\n");
    system("pause");


    return true;
}


Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Console+GUI hybrid app

Post by doublemax »

Sorry, i'm not quite sure what your problem is.

If you want to quit the application when wxApp::OnInit() ends, just return "false" at the end.
Use the source, Luke!
samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 340
Joined: Mon Oct 06, 2008 12:55 pm

Re: Console+GUI hybrid app

Post by samsam598 »

Thanks...yes, return false helps me a lot that app can now quit.

My problem is the execution order with console output and GUI.I want it:
1.console printing something...
2.wxMessageBox shows...
3.wxFrame winodws shows
4.press X to quit wxFrame...
5.console printing something...
7.system("pause")
8.app exit.
But it actually goes:
1,2,
3.wxFrame child controls not all visible and the wxwindows is frozen,push the console to the front,click on the console and now all controls visible and the wxWindow activated now.
5,7,actually don't know 3 &5/7which shows first,almost the same time I guess.
4,8.
Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Console+GUI hybrid app

Post by doublemax »

The problem is probably that wxFrame requires a working event loop to work correctly. But there is no event loop running in wxApp::OnInit().

You could try to use a modal wxDialog instead of a wxFrame, because it creates its own event loop.

Or you could try to move the whole code to wxAppConsole::OnEventLoopEnter()
http://docs.wxwidgets.org/trunk/classwx ... 17e53ae999
Use the source, Luke!
samsam598
Super wx Problem Solver
Super wx Problem Solver
Posts: 340
Joined: Mon Oct 06, 2008 12:55 pm

Re: Console+GUI hybrid app

Post by samsam598 »

Thanks again.
By testing wxAppConsole::EventLoopEnter,it does not work,maybe due to what I did is wrong.
By testing wxDialog,everything is fine,except the difference between wxDialog and wxFrame,that means with wxDialog I can do fewer things on it,for example,it has no menubar/menu.
Regards,
Sam
-------------------------------------------------------------------
Windows 10 64bit
VS Community 2019
msys2-mingw13.2.0 C::B character set: UTF-8/GBK(Chinese)
wxWidgets 3.3/3.2.4 Unicode Mono Static gcc static build
Post Reply