Buttons in Panel in treebook do not work (still a newbe) Topic is solved

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
smf
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Nov 12, 2008 7:04 pm

Buttons in Panel in treebook do not work (still a newbe)

Post by smf »

Hi,

as I am still a wxNewbe, I am stuck again...

I would like to have a config-frame (like that one in Netscape or VLC) for my Program, so this was my try:

Code: Select all

enum
{
    ID_BUTTON_SKIN = (wxID_HIGHEST+1),
};

BEGIN_EVENT_TABLE(config_frame, wxFrame)
END_EVENT_TABLE()

config_frame::config_frame(wxFrame *frame, const wxString& title)
    :wxFrame((wxFrame *)NULL, wxID_ANY, wxEmptyString,
           wxDefaultPosition, wxSize(10, 10) )
{
    // get a vertical top sizer
    wxSizer* my_sizer=new wxBoxSizer(wxVERTICAL);

    // get two child-sizers
    wxSizer* my_sizer_bottom=new wxBoxSizer(wxHORIZONTAL);
    wxSizer* my_sizer_above =new wxBoxSizer(wxVERTICAL);

    // get a treebook
    wxTreebook* my_treebook = new wxTreebook( this, -1, wxDefaultPosition, wxSize(480,480), 0, wxT("name" ) );

    // ========================================================================
    // Define first page of Treebook for General setup parameters
    // ========================================================================
    wxPanel* my_panel1 = new wxPanel(this);
    wxBoxSizer* my_panel1_sizer0 = new wxBoxSizer( wxVERTICAL );
    wxBoxSizer* my_panel1_sizer1 = new wxBoxSizer( wxHORIZONTAL );
    wxTextCtrl* skin_textctrl = new wxTextCtrl( my_panel1,
                                                -1,
                                                wxT("Default"),
                                                wxDefaultPosition,
                                                wxSize(256,24),
                                                0,
                                                wxDefaultValidator,
                                                wxT("name") );
    wxButton* skin_button1 = new wxButton( my_panel1, ID_BUTTON_SKIN, wxT("Select Skin"), wxT("name") );

    my_panel1_sizer1->Add( skin_textctrl );
    my_panel1_sizer1->Add( skin_button1 );
    my_panel1_sizer0->Add( my_panel1_sizer1 );

    my_panel1->SetSizer( my_panel1_sizer0 );

    // leave these empty for the first try...
    wxPanel* my_panel2 = new wxPanel(this);
    wxPanel* my_panel3 = new wxPanel(this);
    wxPanel* my_panel4 = new wxPanel(this);

    my_treebook->AddPage( my_panel1, wxT("plane1"), false, 0 );
    my_treebook->AddPage( my_panel2, wxT("plane2"), false, 0 );
    my_treebook->AddPage( my_panel3, wxT("plane3"), false, 0 );
    my_treebook->AddPage( my_panel4, wxT("plane4"), false, 0 );

    my_sizer_above->Add( my_treebook, 1, wxEXPAND );

    my_sizer_above->Layout();
    my_sizer_above->Fit(this);
    my_sizer_above->SetSizeHints(this);

    // Add a Button to bottom sizer
    my_sizer_bottom->Add(
        new wxButton(this, wxID_ABORT, wxT("Cancel")),
        0,        // make vertically stretchable
        0| // make horizontally stretchable
        wxALL,    // and make border all around
        4  );     // set border width to 4

    my_sizer_bottom->Add(
        new wxButton(this, wxID_OK, wxT("OK")),
        0,        // make vertically stretchable
        0| // make horizontally stretchable
        wxALL,    // and make border all around
        4  );     // set border width to 4

    my_sizer->Add( my_sizer_above  );
    my_sizer->Add( my_sizer_bottom );

    my_sizer->Layout();
    my_sizer->Fit(this);
    my_sizer->SetSizeHints(this);

    // Connect widgets to event-loop
    Connect(ID_BUTTON_SKIN, 
         wxEVT_COMMAND_BUTTON_CLICKED,
            (wxObjectEventFunction)&config_frame::OnButtonPressed);

    Connect(wxID_ABORT, 
         wxEVT_COMMAND_BUTTON_CLICKED,
            (wxObjectEventFunction)&config_frame::OnButtonPressed);

    Connect(wxID_OK, 
         wxEVT_COMMAND_BUTTON_CLICKED,
            (wxObjectEventFunction)&config_frame::OnButtonPressed);

    // Show frame
    Show();
}

void config_frame::OnButtonPressed(wxCommandEvent& event)
{
    std::cerr << "Button pressed in Config-screen...\n";
    std::cerr << event.GetId() << "\n";
}
While this does show me the expected layout of a treebook with four panels and a textctrl and a button on the first panel, it does not react like I would expected it (on Win32... have not tried that with wxGTK). I can click the OK and the CANCEL Button and get a call to On ButtonPressed(). But the SELECT Button does not respond, however?

It seems like I have not fully understood, what I need to do here, to make this work... (BTW: I have taken a peek at the notebook-sample, but because of the lot's of #ifdefs inside I am as clueless as before... *sigh*)

Any help would be greatly appreciated...
Stefan :?
JimFairway
wxWorld Domination!
wxWorld Domination!
Posts: 1059
Joined: Sun Dec 30, 2007 6:40 pm
Location: Canada

Post by JimFairway »

Hi Stefan,

Given that you are using Connect for only some of the buttons, I can presume you're using static tables for the others, which would explain why some work and others don't.

I believe you need to call Connect for the object that will generate the event.
Instead of:

Code: Select all

Connect(ID_BUTTON_SKIN,
         wxEVT_COMMAND_BUTTON_CLICKED,
            (wxObjectEventFunction)&config_frame::OnButtonPressed); 
Try the following:

Code: Select all

skin_button1->Connect(ID_BUTTON_SKIN,
         wxEVT_COMMAND_BUTTON_CLICKED,
            wxCommandEventHandler(config_frame::OnButtonPressed), NULL, this);
The last parameter tells the event logic that your frame object will handle the event on behalf of the button.

Hope that helps,

Jim
OS: Vista SP1, wxWidgets 2.8.7.
smf
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Nov 12, 2008 7:04 pm

Post by smf »

Hi Jim,
JimFairway wrote:
I believe you need to call Connect for the object that will generate the event.

...

The last parameter tells the event logic that your frame object will handle the event on behalf of the button.
no, I am sorry,... it is behaving absolutely the same with the changed event-connect. It is like that: I can switch the panels with the tree but the widgets inside the panels remain completely inactive (just if they were just images).

Stefan

PS: I tried to compile it under Linux with GTK in the meantime and both variants (yours and mine) in fact do work there... but not under Win32. So, I guess that it must be something which doesn't work under Win the way I am doing it...
tan
wxWorld Domination!
wxWorld Domination!
Posts: 1471
Joined: Tue Nov 14, 2006 7:58 am
Location: Saint-Petersburg, Russia

Post by tan »

Hi,
i don't sure, but try to make my_treebook as a parent for the panels:

Code: Select all

wxPanel* my_panel1 = new wxPanel(my_treebook);
OS: Windows XP Pro
Compiler: MSVC++ 7.1
wxWidgets: 2.8.10
smf
Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Nov 12, 2008 7:04 pm

Post by smf »

tan wrote:Hi,
i don't sure, but try to make my_treebook as a parent for the panels:

Code: Select all

wxPanel* my_panel1 = new wxPanel(my_treebook);
O_O *WOW* that was it! Many many thanks! I overlooked that one (I am pretty sure it would have taken me ages to find this...) What I have learned from this one: ever check that the parents are correct, if something doesn't work...

so many thanks!
Stefan
Post Reply