Posting events to notebook tabs 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
Lamego
Earned some good credits
Earned some good credits
Posts: 101
Joined: Wed Jun 29, 2005 10:13 pm
Location: Portugal
Contact:

Posting events to notebook tabs

Post by Lamego »

Hello,
I want to be able to trigger events on pages contained on a notebook, this events must be trigered by the notebook container.
At this time I want this to happen during page changes so I was trying like this:

ChatCentral is the Notebook:
...

Code: Select all

Remote_Refresh = 101,
...
EVT_FLATNOTEBOOK_PAGE_CHANGED(NotebookPage_Notebook, ChatCentral::OnPageChange)
...
void ChatCentral::OnPageChange( wxFlatNotebookEvent& event )
{
  wxLogMessage("Page change");
  int new_page = event.GetSelection();

  if(new_page != wxNOT_FOUND)
  {
    m_notebook->SetPageHighlight(new_page, false);
    wxCommandEvent ev(0, Remote_Refresh);
    wxPostEvent(m_notebook->GetPage(new_page)->GetEventHandler(), ev);
  }
}
RoomWindow is a panel added to the notebook:

Code: Select all

...
    Remote_Refresh = 101,
...
EVT_COMMAND(Remote_Refresh, 0, RoomWindow::OnRemoteRefresh)
...
void RoomWindow::OnRemoteRefresh( wxCommandEvent &event )
{
  wxLogMessage("Got a remote refresh !");
}
The OnPageChange is being called but the event handler on roomwindow is not.<br>I have already used this approach on other cases and it worked fined, what am I missing here ?

Thanks
wxMSW 2.6.3 - GCC 3.4.2 (MinGW/MSYS)
wxGTK 2.6.3 - GCC 4.02 (Ubuntu)
Code::Blocks Win/Lin
PTlink Tech Admin
PTlink Chat Software Lead Developer
http://software.pt-link.net
eranif
Moderator
Moderator
Posts: 610
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Post by eranif »

Hi,

Try this instead:

Code: Select all

void ChatCentral::OnPageChange( wxFlatNotebookEvent& event )
{
  wxLogMessage("Page change");
  int new_page = event.GetSelection();

  if(new_page != wxNOT_FOUND)
  {
    m_notebook->SetPageHighlight(new_page, false);
    m_notebook->GetPage(new_page)->ProcessEvent(event);
  }
}
Eran
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org
Lamego
Earned some good credits
Earned some good credits
Posts: 101
Joined: Wed Jun 29, 2005 10:13 pm
Location: Portugal
Contact:

crash

Post by Lamego »

It crashes with ProcessEvent() (I think it gets into an endless loop, I don't have much info on the backtracke).
wxMSW 2.6.3 - GCC 3.4.2 (MinGW/MSYS)
wxGTK 2.6.3 - GCC 4.02 (Ubuntu)
Code::Blocks Win/Lin
PTlink Tech Admin
PTlink Chat Software Lead Developer
http://software.pt-link.net
eranif
Moderator
Moderator
Posts: 610
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Post by eranif »

Maybe the loop is because the event is returninig to the function that called ProcessEvent().

To avoid this, try to call process event with other event type (maybe like the one you wrote):

On your header file, add:

Code: Select all

DECLARE_EVENT_TYPE(wxREFERSH_PAGE, -1)
On your cpp file, add this once:

Code: Select all

DEFINE_EVENT_TYPE(wxREFERSH_PAGE)
Add this to the event table of the RoomWindow class:

Code: Select all

EVT_COMMAND(wxREFERSH_PAGE, ChatCentral::OnPageChange)

Code: Select all

void ChatCentral::OnPageChange( wxFlatNotebookEvent& event )
{
  wxLogMessage("Page change");
  int new_page = event.GetSelection();

  if(new_page != wxNOT_FOUND)
  {
    wxCommandEvent ev(wxREFERSH_PAGE, GetId());
    ev.SetEventObject(this);
    m_notebook->SetPageHighlight(new_page, false);
    m_notebook->GetPage(new_page)->ProcessEvent(ev);
  }
}

If that will not work, why not simply calling the required function directly?

Eran
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org
Lamego
Earned some good credits
Earned some good credits
Posts: 101
Joined: Wed Jun 29, 2005 10:13 pm
Location: Portugal
Contact:

didn't work

Post by Lamego »

It didn't work.
I do believe my initial approach with wxPost() was correct, it is the expected mechanism to pass events between controls. I don't understand why the event doesn't get called on the tab contained page.

About your suggestion to just call the function I want to trigger the issue is that the tabs have different types of pages which may support or not functions when the page is changed.
I am a bit rusty with C++/OPP but I am not seeing how can I call a function from a child for which I don't know the exact type.

Thanks
wxMSW 2.6.3 - GCC 3.4.2 (MinGW/MSYS)
wxGTK 2.6.3 - GCC 4.02 (Ubuntu)
Code::Blocks Win/Lin
PTlink Tech Admin
PTlink Chat Software Lead Developer
http://software.pt-link.net
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7479
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

Lamego,
Can't you do a wxDynamicCast() and find out the type on the fly?

Thank you.
eranif
Moderator
Moderator
Posts: 610
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Post by eranif »

Lamego,
Can't you do a wxDynamicCast() and find out the type on the fly?
And what if he there are several classes that needs to handle refresh, then what should it cast it to?
this does not sounds like a good idea,

I suggest the following approach:

Create common base class to all Pages on the notebook:

- wxPageBase (which contains virtual functions like RefreshPage(), not pure virtual, with default empty implementation)
+ wxPageRoom
+ wxPageRoomType2

When the event is captured, simplay cast it to the base and call RefreshPage() function which is the member of the base class and no problem.

Eran
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org
Lamego
Earned some good credits
Earned some good credits
Posts: 101
Joined: Wed Jun 29, 2005 10:13 pm
Location: Portugal
Contact:

you rock

Post by Lamego »

Eranif,
your approach worked fine.

Thanks
wxMSW 2.6.3 - GCC 3.4.2 (MinGW/MSYS)
wxGTK 2.6.3 - GCC 4.02 (Ubuntu)
Code::Blocks Win/Lin
PTlink Tech Admin
PTlink Chat Software Lead Developer
http://software.pt-link.net
Post Reply