+ ASSERT_MENU_EVENT_RESULT( menu, "aomobowA" );
+}
+
+// Minimal viable implementations of wxDocument and wxView.
+class EventTestDocument : public wxDocument
+{
+public:
+ EventTestDocument() { }
+
+ wxDECLARE_DYNAMIC_CLASS(EventTestDocument);
+};
+
+class EventTestView : public wxView
+{
+public:
+ EventTestView() { }
+
+ virtual void OnDraw(wxDC*) { }
+
+ wxDECLARE_DYNAMIC_CLASS(EventTestView);
+};
+
+wxIMPLEMENT_DYNAMIC_CLASS(EventTestDocument, wxDocument);
+wxIMPLEMENT_DYNAMIC_CLASS(EventTestView, wxView);
+
+void EventPropagationTestCase::DocView()
+{
+ // Set up the parent frame and its menu bar.
+ wxDocManager docManager;
+
+ wxScopedPtr<wxDocMDIParentFrame>
+ parent(new wxDocMDIParentFrame(&docManager, NULL, wxID_ANY, "Parent"));
+
+ wxMenu* const menu = CreateTestMenu(parent.get());
+
+
+ // Set up the event handlers.
+ TestEvtSink sinkDM('m');
+ docManager.Connect(wxEVT_MENU,
+ wxEventHandler(TestEvtSink::Handle), NULL, &sinkDM);
+
+ TestEvtSink sinkParent('p');
+ parent->Connect(wxEVT_MENU,
+ wxEventHandler(TestEvtSink::Handle), NULL, &sinkParent);
+
+
+ // Check that wxDocManager and wxFrame get the event in order.
+ ASSERT_MENU_EVENT_RESULT( menu, "ampA" );
+
+
+ // Now check what happens if we have an active document.
+ wxDocTemplate docTemplate(&docManager, "Test", "", "", "",
+ "Test Document", "Test View",
+ wxCLASSINFO(EventTestDocument),
+ wxCLASSINFO(EventTestView));
+ wxDocument* const doc = docTemplate.CreateDocument("");
+ wxView* const view = doc->GetFirstView();
+
+ wxScopedPtr<wxMDIChildFrame>
+ child(new wxDocMDIChildFrame(doc, view, parent.get(), wxID_ANY, "Child"));
+
+ wxMenu* const menuChild = CreateTestMenu(child.get());
+
+ // Ensure that the child that we've just created is the active one.
+ child->Activate();
+
+#ifdef __WXGTK__
+ // There are a lot of hacks related to child frame menu bar handling in
+ // wxGTK and, in particular, the code in src/gtk/mdi.cpp relies on getting
+ // idle events to really put everything in place. Moreover, as wxGTK uses
+ // GtkNotebook as its MDI pages container, the frame must be shown for all
+ // this to work as gtk_notebook_set_current_page() doesn't do anything if
+ // called for a hidden window (this incredible fact cost me quite some time
+ // to find empirically -- only to notice its confirmation in GTK+
+ // documentation immediately afterwards). So just do whatever it takes to
+ // make things work "as usual".
+ child->Show();
+ parent->Show();
+ wxYield();
+#endif // __WXGTK__
+
+ TestEvtSink sinkDoc('d');
+ doc->Connect(wxEVT_MENU,
+ wxEventHandler(TestEvtSink::Handle), NULL, &sinkDoc);
+
+ TestEvtSink sinkView('v');
+ view->Connect(wxEVT_MENU,
+ wxEventHandler(TestEvtSink::Handle), NULL, &sinkView);
+
+ TestEvtSink sinkChild('c');
+ child->Connect(wxEVT_MENU,
+ wxEventHandler(TestEvtSink::Handle), NULL, &sinkChild);
+
+ // Check that wxDocument, wxView, wxDocManager, child frame and the parent
+ // get the event in order.
+ ASSERT_MENU_EVENT_RESULT( menuChild, "advmcpA" );
+
+
+#if wxUSE_TOOLBAR
+ // Also check that toolbar events get forwarded to the active child.
+ wxToolBar* const tb = parent->CreateToolBar(wxTB_NOICONS);
+ tb->AddTool(wxID_APPLY, "Apply", wxNullBitmap);
+ tb->Realize();
+
+ // As in CheckMenuEvent(), use toolbar method actually sending the event
+ // instead of bothering with wxUIActionSimulator which would have been
+ // trickier.
+ g_str.clear();
+ tb->OnLeftClick(wxID_APPLY, true /* doesn't matter */);
+
+ CPPUNIT_ASSERT_EQUAL( "advmcpA", g_str );
+#endif // wxUSE_TOOLBAR
+}
+
+#if wxUSE_UIACTIONSIMULATOR
+
+class ContextMenuTestWindow : public wxWindow
+{
+public:
+ ContextMenuTestWindow(wxWindow *parent, char tag)
+ : wxWindow(parent, wxID_ANY),
+ m_tag(tag)
+ {
+ Connect(wxEVT_CONTEXT_MENU,
+ wxContextMenuEventHandler(ContextMenuTestWindow::OnMenu));
+ }
+
+private:
+ void OnMenu(wxContextMenuEvent& event)
+ {
+ g_str += m_tag;
+
+ event.Skip();
+ }
+
+ const char m_tag;
+
+ wxDECLARE_NO_COPY_CLASS(ContextMenuTestWindow);
+};
+
+void EventPropagationTestCase::ContextMenuEvent()
+{
+ ContextMenuTestWindow * const
+ parent = new ContextMenuTestWindow(wxTheApp->GetTopWindow(), 'p');
+ wxON_BLOCK_EXIT_OBJ0( *parent, wxWindow::Destroy );
+
+ ContextMenuTestWindow * const
+ child = new ContextMenuTestWindow(parent, 'c');
+ parent->SetSize(100, 100);
+ child->SetSize(0, 0, 50, 50);
+ child->SetFocus();
+
+ wxUIActionSimulator sim;
+ const wxPoint origin = parent->ClientToScreen(wxPoint(0, 0));
+
+ // Right clicking in the child should generate an event for it and the
+ // parent.
+ g_str.clear();
+ sim.MouseMove(origin + wxPoint(10, 10));
+ sim.MouseClick(wxMOUSE_BTN_RIGHT);
+
+ // At least with MSW, for WM_CONTEXTMENU to be synthesized by the system
+ // from the right mouse click event, we must dispatch the mouse messages.
+ wxYield();
+
+ CPPUNIT_ASSERT_EQUAL( "cp", g_str );
+
+ // Right clicking outside the child should generate the event just in the
+ // parent.
+ g_str.clear();
+ sim.MouseMove(origin + wxPoint(60, 60));
+ sim.MouseClick(wxMOUSE_BTN_RIGHT);
+ wxYield();
+ CPPUNIT_ASSERT_EQUAL( "p", g_str );