How to open new frame from button

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.
marcusbarnet
In need of some credit
In need of some credit
Posts: 9
Joined: Sat Jul 27, 2019 3:45 pm

How to open new frame from button

Post by marcusbarnet » Sat Jul 27, 2019 3:53 pm

Hi to all,

this is my first post in this forum and I hope I can find support for my problem.
I'm using CodeBlocks and Wxwidgets to create a buttons-based application where the user can select different buttons in order to interact with the application; when a button is clicked, for example Button1, the current window (isolaFrame) should be replaced by a new window with new buttons.

How can I do this? How can I create a new Frame?

I tried to search a lot both in this forum and on google, but I've found nothing about this; unfortunately, i'm still a newby and so I can't solve this problem by myself.

Code: Select all

 // IsolaAPP.cpp
 
#include "isolaApp.h"

//(*AppHeaders
#include "isolaMain.h"
#include <wx/image.h>
//*)

IMPLEMENT_APP(isolaApp);

bool isolaApp::OnInit()
{

    bool wxsOK = true;
    wxInitAllImageHandlers();
    if ( wxsOK )
    {
    	isolaFrame* Frame = new isolaFrame(1);
    	Frame->Show();
    	SetTopWindow(Frame);
    }

    return wxsOK;

}

Code: Select all

// IsolaMain.cpp
#include "isolaMain.h"
#include <wx/msgdlg.h>

//(*InternalHeaders(isolaFrame)
#include <wx/intl.h>
#include <wx/string.h>
//*)

//helper functions
enum wxbuildinfoformat {
    short_f, long_f };

wxString wxbuildinfo(wxbuildinfoformat format)
{
    wxString wxbuild(wxVERSION_STRING);

    if (format == long_f )
    {
#if defined(__WXMSW__)
        wxbuild << _T("-Windows");
#elif defined(__UNIX__)
        wxbuild << _T("-Linux");
#endif

#if wxUSE_UNICODE
        wxbuild << _T("-Unicode build");
#else
        wxbuild << _T("-ANSI build");
#endif // wxUSE_UNICODE
    }

    return wxbuild;
}

//(*IdInit(isolaFrame)
const long isolaFrame::ID_BUTTON1 = wxNewId();
const long isolaFrame::ID_BUTTON2 = wxNewId();
const long isolaFrame::ID_SASHWINDOW1 = wxNewId();
const long isolaFrame::idMenuQuit = wxNewId();
const long isolaFrame::idMenuAbout = wxNewId();
const long isolaFrame::ID_STATUSBAR1 = wxNewId();
//*)

BEGIN_EVENT_TABLE(isolaFrame,wxFrame)
    //(*EventTable(isolaFrame)
    //*)
END_EVENT_TABLE()

isolaFrame::isolaFrame(wxWindow* parent,wxWindowID id)
{
    //(*Initialize(isolaFrame)
    wxMenu* Menu1;
    wxMenu* Menu2;
    wxMenuBar* MenuBar1;
    wxMenuItem* MenuItem1;
    wxMenuItem* MenuItem2;

    Create(parent, id, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, _T("id"));
    SashWindow1 = new wxSashWindow(this, ID_SASHWINDOW1, wxPoint(168,72), wxDefaultSize, wxSW_3D|wxCLIP_CHILDREN, _T("ID_SASHWINDOW1"));
    Button1 = new wxButton(SashWindow1, ID_BUTTON1, _("Label"), wxPoint(143,163), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
    Button2 = new wxButton(SashWindow1, ID_BUTTON2, _("Label"), wxPoint(143,225), wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
    SashWindow1->SetSashVisible(wxSASH_TOP,    true);
    SashWindow1->SetSashVisible(wxSASH_BOTTOM, true);
    SashWindow1->SetSashVisible(wxSASH_LEFT,   true);
    SashWindow1->SetSashVisible(wxSASH_RIGHT,  true);
    MenuBar1 = new wxMenuBar();
    Menu1 = new wxMenu();
    MenuItem1 = new wxMenuItem(Menu1, idMenuQuit, _("Quit\tAlt-F4"), _("Quit the application"), wxITEM_NORMAL);
    Menu1->Append(MenuItem1);
    MenuBar1->Append(Menu1, _("&File"));
    Menu2 = new wxMenu();
    MenuItem2 = new wxMenuItem(Menu2, idMenuAbout, _("About\tF1"), _("Show info about this application"), wxITEM_NORMAL);
    Menu2->Append(MenuItem2);
    MenuBar1->Append(Menu2, _("Help"));
    SetMenuBar(MenuBar1);
    StatusBar1 = new wxStatusBar(this, ID_STATUSBAR1, 0, _T("ID_STATUSBAR1"));
    int __wxStatusBarWidths_1[1] = { -1 };
    int __wxStatusBarStyles_1[1] = { wxSB_NORMAL };
    StatusBar1->SetFieldsCount(1,__wxStatusBarWidths_1);
    StatusBar1->SetStatusStyles(1,__wxStatusBarStyles_1);
    SetStatusBar(StatusBar1);

    Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&isolaFrame::OnButton1Click);
    Connect(idMenuQuit,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&isolaFrame::OnQuit);
    Connect(idMenuAbout,wxEVT_COMMAND_MENU_SELECTED,(wxObjectEventFunction)&isolaFrame::OnAbout);
    //*)
}

isolaFrame::~isolaFrame()
{
    //(*Destroy(isolaFrame)
    //*)
}

void isolaFrame::OnQuit(wxCommandEvent& event)
{
    Close();
}

void isolaFrame::OnAbout(wxCommandEvent& event)
{
    wxString msg = wxbuildinfo(long_f);
    wxMessageBox(msg, _("Welcome to..."));
}

void isolaFrame::OnButton1Click(wxCommandEvent& event)
{
    // I want to show a new window by replacing the current one here!
}

User avatar
doublemax
Moderator
Moderator
Posts: 13879
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to open new frame from button

Post by doublemax » Sat Jul 27, 2019 4:21 pm

Code: Select all

    	isolaFrame* Frame = new isolaFrame(NULL, 1);
    	Frame->Show();
This is everything you need to create a new frame and show it. Are you sure you want to destroy the current frame? If yes, just call Close() and make sure you pass NULL as parent for the new frame.

BTW: I assume the new isolaFrame(1) was a typo as it doesn't make sense.
Use the source, Luke!

Kvaz1r
Earned some good credits
Earned some good credits
Posts: 119
Joined: Tue Jun 07, 2016 1:07 pm

Re: How to open new frame from button

Post by Kvaz1r » Sat Jul 27, 2019 4:23 pm

Hello. What is purpose of the second frame? Is it dialog? Or it's step-by-step process ot something else?

marcusbarnet
In need of some credit
In need of some credit
Posts: 9
Joined: Sat Jul 27, 2019 3:45 pm

Re: How to open new frame from button

Post by marcusbarnet » Sat Jul 27, 2019 4:36 pm

Thank you for your support, guys! i really appreciate it.

My application should work in this way:
  • There is the initial window with three buttons
  • The user clicks on the first button -> a new window with new buttons asks the user to press one of them to make a selection
  • as soon as the user click on one of these new buttons, the window closes and the program returns to the initial window
  • the user click on the second button.. a new window open and so on..
Obviously, I need to keep traces of all the user's selections so I should be able to exchange variable between different windows.

The solution based on creating new windows is correct? Or is there any other easier solution?

User avatar
doublemax
Moderator
Moderator
Posts: 13879
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to open new frame from button

Post by doublemax » Sat Jul 27, 2019 4:43 pm

The user clicks on the first button -> a new window with new buttons asks the user to press one of them to make a selection

as soon as the user click on one of these new buttons, the window closes and the program returns to the initial window
Ok. In that case you don't want to open a new wxFrame, you want a modal wxDialog. Depending you what exactly it contains, you either build a custom dialog or use wxMessageDialog: https://docs.wxwidgets.org/trunk/classw ... ialog.html
Use the source, Luke!

marcusbarnet
In need of some credit
In need of some credit
Posts: 9
Joined: Sat Jul 27, 2019 3:45 pm

Re: How to open new frame from button

Post by marcusbarnet » Sat Jul 27, 2019 4:49 pm

Thank you!

So, can I add new buttons on a new wxDialog? Or is it possible to add only text messages?

User avatar
doublemax
Moderator
Moderator
Posts: 13879
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to open new frame from button

Post by doublemax » Sat Jul 27, 2019 4:52 pm

If you only need a maximum of 3 buttons, you can set custom labels for the standard buttons:
https://docs.wxwidgets.org/trunk/classw ... 423e30f534

Otherwise you'll need a custom dialog. But that wouldn't be much more difficult.
Use the source, Luke!

marcusbarnet
In need of some credit
In need of some credit
Posts: 9
Joined: Sat Jul 27, 2019 3:45 pm

Re: How to open new frame from button

Post by marcusbarnet » Sat Jul 27, 2019 7:50 pm

I tried to study the suggested solutions, but I think these are not what I would like to implement.

I do not want to display popup windows, but I want to always display the content in the same window.
It should be like when you visit a web site, you click on a link and a new page is loaded by replacing the initial one instead of opening a new browser window.

Kvaz1r
Earned some good credits
Earned some good credits
Posts: 119
Joined: Tue Jun 07, 2016 1:07 pm

Re: How to open new frame from button

Post by Kvaz1r » Sat Jul 27, 2019 8:26 pm

For this purpose there is wxWizard class.
Tutorial
wxWiki page

User avatar
doublemax
Moderator
Moderator
Posts: 13879
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to open new frame from button

Post by doublemax » Sat Jul 27, 2019 8:28 pm

That sounds like a very unusual GUI design for a desktop application.

Anyway, if that's what you want, i would suggest to build the pages as individual wxPanels and put them all into a wxSimpleBook. This lets you switch between the pages programmatically, but still gives access to all controls on all pages.

https://docs.wxwidgets.org/trunk/classw ... ebook.html
Use the source, Luke!

marcusbarnet
In need of some credit
In need of some credit
Posts: 9
Joined: Sat Jul 27, 2019 3:45 pm

Re: How to open new frame from button

Post by marcusbarnet » Sat Jul 27, 2019 8:50 pm

Thank you, wxSimplebook seems very close to what I would like to obtain.

I looked on google, but I wasn't able to find any good tutorial to understand how to implement it. I only found the class reference but it is too hard for me to derive an usable code from there.

User avatar
doublemax
Moderator
Moderator
Posts: 13879
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to open new frame from button

Post by doublemax » Sat Jul 27, 2019 10:05 pm

I don't use wxSmith, but after i quick look i couldn't find support for wxSimplebook, only wxNotebook. So i don't know the best way to do it there.

When using code, it would look like this:

Code: Select all

// member variable in wxFrame
// wxSimplebook *m_simplebook;

// following code assumed to be in wxFrame ctor
// store pointer in member variable to acess it later
m_simplebook = new wxSimplebook(this, wxID_ANY);

  wxPanel *panel1 = new wxPanel(m_simplebook);
    // add controls to panel1
    // in real code sizers would be used instead of absolute positioning
    wxButton *button1 = new wxButton(panel1, ID_BUTTON1, "Switch to page 2", wxPoint(10,10));
  m_simplebook->AddPage( panel1, "Panel 1", true );

  wxPanel *panel2 = new wxPanel(m_simplebook);
    // add controls to panel2
    // in real code sizers would be used instead of absolute positioning
    wxButton *button2 = new wxButton(panel2, ID_BUTTON2, "Switch to page 1", wxPoint(10,10));
  m_simplebook->AddPage( panel2, "Panel 2", false );


Button event handler:

Code: Select all

void MyFrame::OnButton(wxCommandEvent &event)
{
  switch( event.GetId() )
  {
    case ID_BUTTON1:
      m_simplebook->ChangeSelection(1);
      break;

    case ID_BUTTON2:
      m_simplebook->ChangeSelection(0);
      break;

    default:
      event.Skip();
  }
}
Use the source, Luke!

marcusbarnet
In need of some credit
In need of some credit
Posts: 9
Joined: Sat Jul 27, 2019 3:45 pm

Re: How to open new frame from button

Post by marcusbarnet » Sun Jul 28, 2019 8:33 am

I've read that wxlistbook can have some problems with wxsmith even if on the code blocks page it says that it is supported: http://wiki.codeblocks.org/index.php/Co ... h_features

May be I should switch to wxformbuilder.
I currently can't see the wxlistbook icon container in code blocks.

User avatar
doublemax
Moderator
Moderator
Posts: 13879
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to open new frame from button

Post by doublemax » Sun Jul 28, 2019 8:41 am

wxListbook and wxNotebook are definitely there ("Standard" tab in the wxSmith version that comes with CB 17.12). But it probably won't help you very much in the end. While you could change wxNotebook to wxSimplebook in the generated source code, i would be overwritten every time you change something in the GUI.
Use the source, Luke!

marcusbarnet
In need of some credit
In need of some credit
Posts: 9
Joined: Sat Jul 27, 2019 3:45 pm

Re: How to open new frame from button

Post by marcusbarnet » Sun Jul 28, 2019 9:00 am

What is your suggestion? Would it be better for me to switch to wxformbuilder?

I spent this morning looking for a solution but I really found almost nothing about wxlistbook. It is so strange to me..

Post Reply