Search code examples
c++linuxwxwidgets

New Window wxWidgets


I really did search a lot before ask this question. Sorry if it's a simple question.

In my wxWidgets app, I want to new a wxHtmlWindow from a menu choice. (I've already got my main app window showed up correctly).

I set up EVENT_TABLE :

EVT_MENU(wxID_HELP, MyFrame::OnHelp)

and then the Function (I got a pointer html_help in the class private):

void MyFrame::OnHelp(wxCommandEvent& event)
// Callback for the Help menu item
{
    cout << "Enter OnHelp" << endl;
    wxWindow* help_win = new wxWindow(NULL, -1, wxDefaultPosition, wxSize(400, 600));
    cout << "New window" << endl;
    help_win->Show(true);
    cout << "Show" << endl;
    html_help = new wxHtmlWindow(help_win, -1);
    cout << "After new" << endl;
    wxString m_Name = wxT("help.html");
    //SetTopWindow(html_help);
    bool hel = html_help->LoadPage(m_Name);
    cout << "After Loadpage" << endl;
}

Sorry for the couts. The program throw out segmentation fault after new htmlwindow.

The first time I tried is just new wxHtmlWindow(NULL) with no other wxWindows, but seems not working, the ->LoadPage has segfault . Then I tried new wxHtmlWindow(this), the htmlwindow showed on current window, not I want .

You may just ignore all the codes, and tell me how to new a wxHtmlWindow (maybe in a new frame? or not) and can actually do the LoadPage work.

Thank you very much!

BTW, I'm using 2.812 wxWidgets, with C++.


Solution

  • wxWindow is a base class for all the other widgets, there are rare occasions when you need to and can use it directly.

    For your case you need a top-level window (that is a wxFrame or wxDialog) as the parent for your wxHtmlWindow. And BTW, only top-level windows can have NULL parent.

    To load the html you would probably do better with html_help->LoadFile(wxFileName("help.html")). The docs are online for both functions.