00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00012
00013 #ifdef __GNUG__
00014 #pragma implementation "CRMenuSelectionEvent.h"
00015 #endif
00016
00017 #include "CRMenuSelectionEvent.h"
00018
00019 #include <wxGuiTest/WxGuiTestHelper.h>
00020 #include <wxGuiTest/CRWindowHierarchyHandler.h>
00021 #include <wxGuiTest/CRCppEmitter.h>
00022
00023 using namespace wxTst;
00024
00025
00026 CRMenuSelectionEvent::CRMenuSelectionEvent (wxEvent *event) :
00027 CRCapturedEvent (event),
00028 m_isTool(false),
00029
00030 m_isFromTopWindow(false),
00031 m_eventObjectIsMenu(false),
00032 m_isFromPopupMenu (false),
00033 m_isChecked(false),
00034 m_isControl(false)
00035 {}
00036
00037
00038 CRMenuSelectionEvent::~CRMenuSelectionEvent ()
00039 {
00040
00041 }
00042
00043
00044 bool CRMenuSelectionEvent::processTopFrame(wxFrame* topFrame) {
00045 wxMenuBar* menuBar = topFrame->GetMenuBar ();
00046 wxASSERT (menuBar != NULL);
00047
00048
00049
00050
00051
00052 if (m_event->GetEventObject () == topFrame) {
00053
00054 m_isFromTopWindow = true;
00055 processMainMenu(menuBar);
00056 return true;
00057 }
00058 return false;
00059
00060 }
00061
00062
00063 bool CRMenuSelectionEvent::processTopMenu(wxMenu* menu) {
00064 m_eventObjectIsMenu = true;
00065
00066
00067 wxMenuBar* menuBar = menu->GetMenuBar ();
00068 wxASSERT (menuBar != NULL);
00069
00070 wxWindow *topWdw = wxTheApp->GetTopWindow ();
00071 wxFrame *topFrame = dynamic_cast< wxFrame * >(topWdw);
00072 if (topFrame != NULL && topFrame->GetMenuBar () == menuBar) {
00073
00074 m_isFromTopWindow = true;
00075 processMainMenu(menuBar);
00076 return true;
00077 }
00078
00079 return false;
00080 }
00081
00082 void CRMenuSelectionEvent::processMainMenu (wxMenuBar* menuBar) {
00083
00084 wxMenu *menu;
00085 wxMenuItem *menuItem = menuBar->FindItem (m_event->GetId (), &menu);
00086 wxASSERT (menuItem != NULL);
00087 wxASSERT (menu != NULL);
00088 bool isCheckable = menuItem->IsCheckable ();
00089
00090 while (menu->GetParent () != NULL) {
00091
00092 menu = menu->GetParent ();
00093 }
00094
00095 bool menuFound = false;
00096 size_t menuIdx = 0;
00097 while ((!menuFound) && (menuIdx < menuBar->GetMenuCount ())) {
00098
00099 if (menuBar->GetMenu (menuIdx) == menu) {
00100
00101 menuFound = true;
00102
00103 } else {
00104
00105 menuIdx++;
00106 }
00107 }
00108 wxASSERT (menuIdx < menuBar->GetMenuCount ());
00109 m_menuLabel = menuBar->GetLabelTop (menuIdx);
00110
00111 m_menuItemLabel = menuBar->GetLabel (m_event->GetId ());
00112
00113
00114 if (isCheckable)
00115 processCheckable();
00116 }
00117
00118 bool CRMenuSelectionEvent::processPopUp (wxMenu* menu) {
00119
00120 wxMenu *menu2;
00121 wxMenuItem *menuItem = menu->FindItem (m_event->GetId (), &menu2);
00122 wxASSERT (menuItem != NULL);
00123 wxASSERT (menu2 != NULL);
00124 bool isCheckable = menuItem->IsCheckable ();
00125
00126
00127
00128
00129
00130
00131 m_popupMenuKey = WxGuiTestHelper::FindPopupMenuKey (menu);
00132 if (m_popupMenuKey.IsEmpty ())
00133 return false;
00134
00135 m_isFromPopupMenu = true;
00136
00137 m_menuLabel = menu->GetTitle ();
00138
00139 m_menuItemLabel = menu->GetLabel (m_event->GetId ());
00140
00141
00142
00143 wxWindow *evtHandlerWdw = WxGuiTestHelper::FindPopupMenuEvtHandlerWdw (m_popupMenuKey);
00144 wxASSERT (evtHandlerWdw != NULL);
00145
00146 CRWindowHierarchyHandler *hierarchy = CRWindowHierarchyHandler::GetInstance ();
00147 wxASSERT (hierarchy != NULL);
00148 m_parentContainerName = hierarchy->FindContainerName (evtHandlerWdw);
00149 wxASSERT (!m_parentContainerName.IsEmpty ());
00150
00151
00152
00153 if (isCheckable)
00154 processCheckable();
00155 return true;
00156 }
00157 bool CRMenuSelectionEvent::processToolBar(wxToolBar *toolBar) {
00158
00159
00160 int id = m_event->GetId();
00161 if (id == wxID_SEPARATOR)
00162 return true;
00163 wxToolBarToolBase* tool = toolBar->FindById(id);
00164 if (tool == NULL) {
00165 wxLogDebug(_T("Tool %d not found"), id);
00166 return true;
00167 }
00168 if (tool->IsSeparator())
00169 return true;
00170
00171 m_isTool = true;
00172
00173 m_toolbarName = toolBar->GetName();
00174 CRWindowHierarchyHandler *hierarchy = CRWindowHierarchyHandler::GetInstance ();
00175 wxASSERT (hierarchy != NULL);
00176 m_parentContainerName = hierarchy->FindContainerName (toolBar);
00177 wxASSERT (!m_parentContainerName.IsEmpty ());
00178
00179 if (tool->IsControl()) {
00180 m_isControl = true;
00181 wxControl* control = tool->GetControl();
00182 m_controlName = control->GetClassInfo()->GetClassName();
00183 wxLogDebug(_T("Tool %d is a %s"), id, m_controlName.c_str());
00184
00185
00186 }
00187 if (tool->CanBeToggled() && tool->IsToggled())
00188 m_isChecked = true;
00189 return true;
00190
00191 }
00192
00193
00194
00195 void CRMenuSelectionEvent::processCheckable() {
00196 wxASSERT (m_event->IsCommandEvent ());
00197 wxCommandEvent *cmdEvt = wxDynamicCast (m_event, wxCommandEvent);
00198 if ((cmdEvt->GetInt () == 1) || (cmdEvt->IsChecked ())) {
00199
00200 m_isChecked = true;
00201 }
00202 }
00203
00204
00205 void CRMenuSelectionEvent::Process (CRCapturedEvent** WXUNUSED(pendingEvt))
00206 {
00207
00208 wxWindow *topWdw = wxTheApp->GetTopWindow ();
00209 wxFrame *topFrame = dynamic_cast< wxFrame * >(topWdw);
00210 if (topFrame != NULL && processTopFrame(topFrame))
00211 return;
00212
00213
00214
00215 wxMenu *menu = wxDynamicCast (m_event->GetEventObject (), wxMenu);
00216 if (menu != NULL && (processTopMenu(menu) || processPopUp(menu)))
00217 return;
00218
00219
00220
00221 wxToolBar *wdwEvtObject = wxDynamicCast (m_event->GetEventObject (),
00222 wxToolBar);
00223 if (wdwEvtObject != NULL && processToolBar(wdwEvtObject))
00224 return;
00225
00226
00227
00228 }
00229
00230
00231 void CRMenuSelectionEvent::EmitCpp ()
00232 {
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 CRCppEmitter *emitter = CRCppEmitter::GetInstance ();
00308 wxString str;
00309
00310 if (!m_isTool) {
00311
00312
00313
00314
00315 wxString eventHdlVarName, menuItemContVarName, menuItemIdVarName;
00316
00317 if (m_isFromTopWindow) {
00318
00319 str.Clear ();
00320 str << _T("CPPUNIT_ASSERT_MESSAGE (\"Application top window invalid\", wxTheApp->GetTopWindow () != NULL);");
00321 emitter->AddCode (str);
00322
00323 eventHdlVarName = _T("topFrame");
00324 str.Clear ();
00325 str << _T("wxFrame *") << eventHdlVarName <<
00326 _T(" = dynamic_cast< wxFrame * >(wxTheApp->GetTopWindow ());");
00327 emitter->AddCode (str);
00328
00329 str.Clear ();
00330 str << _T("CPPUNIT_ASSERT_MESSAGE (\"Top window is not a frame\", ")
00331 << eventHdlVarName << _T(" != NULL);");
00332 emitter->AddCode (str);
00333
00334 }
00335 else if (m_isFromPopupMenu) {
00336
00337 menuItemContVarName = emitter->MakeVarName (m_menuLabel,
00338 _T("PopupMenu"));
00339 str.Clear ();
00340 str << _T("wxMenu *") << menuItemContVarName <<
00341 _T(" = wxTst::WxGuiTestHelper::FindPopupMenu (\"") <<
00342 m_popupMenuKey << _T("\");");
00343 emitter->AddCode (str);
00344
00345 str.Clear ();
00346 str << _T("CPPUNIT_ASSERT_MESSAGE (\"Pop-up menu '") <<
00347 m_popupMenuKey << _T("' not found\", ") <<
00348 menuItemContVarName << _T(" != NULL);");
00349 emitter->AddCode (str);
00350
00351 menuItemIdVarName = emitter->MakeVarName (m_menuItemLabel,
00352 _T("MenuItemId"));
00353 str.Clear ();
00354 str << _T("int ") << menuItemIdVarName << _T(" = ") <<
00355 menuItemContVarName << _T("->FindItem (_(\")_T(") <<
00356 m_menuItemLabel << _T(")\"));");
00357 emitter->AddCode (str);
00358
00359 str.Clear ();
00360 str << _T("CPPUNIT_ASSERT_MESSAGE (\"Menu item ID '") <<
00361 m_menuItemLabel << _T("' not found\", ") << menuItemIdVarName <<
00362 _T(" != wxNOT_FOUND);");
00363 emitter->AddCode (str);
00364
00365 eventHdlVarName = emitter->AddContainerLookupCode (
00366 m_parentContainerName,
00367 wxString::Format (_T("pop-up menu '%s'"),
00368 m_menuLabel.c_str ()));
00369 }
00370
00371 if (
00372 m_isFromTopWindow) {
00373
00374 menuItemContVarName = _T("menuBar");
00375 str.Clear ();
00376 str << _T("wxMenuBar *") << menuItemContVarName << _T(" = ") <<
00377 eventHdlVarName << _T("->GetMenuBar ();");
00378 emitter->AddCode (str);
00379
00380 str.Clear ();
00381 str << _T("CPPUNIT_ASSERT_MESSAGE (\"Menubar not found\", ") <<
00382 menuItemContVarName << _T(" != NULL);");
00383 emitter->AddCode (str);
00384
00385 menuItemIdVarName = emitter->MakeVarName (m_menuItemLabel,
00386 _T("MenuItemId"));
00387 str.Clear ();
00388 str << _T("int ") << menuItemIdVarName << _T(" = ") <<
00389 menuItemContVarName << _T("->FindMenuItem (_(\"") <<
00390 m_menuLabel << _T("\"), _(\"") << m_menuItemLabel << _T("\"));");
00391 emitter->AddCode (str);
00392
00393 str.Clear ();
00394 str << _T("CPPUNIT_ASSERT_MESSAGE (\"Menu item ID '") <<
00395 m_menuItemLabel << _T("' not found\", ") <<
00396 menuItemIdVarName << _T(" != wxNOT_FOUND);");
00397 emitter->AddCode (str);
00398 }
00399
00400 if (m_isChecked) {
00401
00402 wxString menuVarName;
00403 if (m_eventObjectIsMenu) {
00404
00405 menuVarName = emitter->MakeVarName (m_menuLabel,
00406 _T("Menu"));
00407
00408 str.Clear ();
00409 str << _T("wxMenu *") << menuVarName << _T(";");
00410 emitter->AddCode (str);
00411 }
00412
00413 wxString menuItemVarName = emitter->MakeVarName (m_menuItemLabel,
00414 _T("MenuItem"));
00415
00416 str.Clear ();
00417 if (!m_eventObjectIsMenu) {
00418
00419 str << _T("wxMenuItem *") << menuItemVarName << _T(" = ") <<
00420 menuItemContVarName << _T("->FindItem (") <<
00421 menuItemIdVarName << _T(");");
00422
00423 } else {
00424
00425 str << _T("wxMenuItem *") << menuItemVarName << _T(" = ") <<
00426 menuItemContVarName << _T("->FindItem (") <<
00427 menuItemIdVarName << _T(", &") <<
00428 menuVarName << _T(");");
00429 }
00430 emitter->AddCode (str);
00431
00432 str.Clear ();
00433 str << _T("CPPUNIT_ASSERT_MESSAGE (\"Menu item '") <<
00434 m_menuItemLabel << _T("' not found\", ") << menuItemVarName <<
00435 _T(" != NULL);");
00436 emitter->AddCode (str);
00437
00438 if (m_eventObjectIsMenu) {
00439
00440 str.Clear ();
00441 str << _T("CPPUNIT_ASSERT_MESSAGE (\"Menu for menu item '") <<
00442 m_menuItemLabel << _T("' not found\", ") << menuVarName <<
00443 _T(" != NULL);");
00444 emitter->AddCode (str);
00445 }
00446
00447 emitter->AddComment (_T("Check if checkable menu item is not already checked?"));
00448 str.Clear ();
00449 str << _T("if (!") << menuItemVarName << _T("->IsChecked ()) { ...");
00450 emitter->AddComment (str);
00451
00452 str.Clear ();
00453 if (!m_eventObjectIsMenu) {
00454
00455 str << _T("wxTst::EventSimulationHelper::SelectAndCheckMenuItem (") <<
00456 menuItemIdVarName << _T(", ") << eventHdlVarName << _T(");");
00457
00458 } else {
00459
00460 str << _T("wxTst::EventSimulationHelper::SelectAndCheckMenuItem (") <<
00461 menuItemIdVarName << _T(", ") << menuVarName << _T(");");
00462 }
00463 emitter->AddCode (str);
00464
00465 } else {
00466
00467 str.Clear ();
00468 str << _T("wxTst::EventSimulationHelper::SelectMenuItem (")
00469 << menuItemIdVarName << _T(", ") << eventHdlVarName << _T(");");
00470 emitter->AddCode (str);
00471 }
00472
00473 } else {
00474 wxString toolBarVarName = emitter->MakeVarName (m_toolbarName);
00475 wxString containerVarName = emitter->MakeVarName(m_parentContainerName);
00476 wxString containerWdwVarName = emitter->MakeVarName(m_parentContainerName,
00477 _T("Wdw"));
00478
00479 str.Clear ();
00480 str << _T("wxWindow* ") << containerWdwVarName
00481 << _T(" = wxWindow::FindWindowByName( _T(\"")
00482 << m_parentContainerName << _T("\"));");
00483 emitter->AddCode (str);
00484
00485 str.Clear ();
00486 str << _T("CPPUNIT_ASSERT_MESSAGE (\"Parent Window '")
00487 << m_parentContainerName << _T("' not found\", ")
00488 << containerWdwVarName << _T(" != NULL);");
00489 emitter->AddCode (str);
00490
00491 str.Clear ();
00492 str << _T("wxFrame* ") << containerVarName << _T(" = wxDynamicCast(")
00493 << containerWdwVarName << _T(", wxFrame);");
00494 emitter->AddCode (str);
00495
00496 str.Clear ();
00497 str << _T("CPPUNIT_ASSERT_MESSAGE (\"Converting Window for frame '")
00498 << m_parentContainerName << _T("' not found\", ")
00499 << containerVarName << _T(" != NULL);");
00500 emitter->AddCode (str);
00501
00502 str.Clear ();
00503 str << _T("wxToolBar* ") << toolBarVarName
00504 << _T(" = ") << containerVarName << _T("->GetToolBar();");
00505 emitter->AddCode (str);
00506
00507 str.Clear ();
00508 str << _T("CPPUNIT_ASSERT_MESSAGE (\"Toolbar '") << m_toolbarName
00509 << _T("' not found\", ") << toolBarVarName << _T(" != NULL);");
00510 emitter->AddCode (str);
00511
00512 wxString isCheckedBoolStr = m_isChecked ? _T("true") : _T("false");
00513
00514 str.Clear ();
00515 if (!m_isControl) {
00516 str << _T("wxTst::EventSimulationHelper::ToggleTool(")
00517 << m_event->GetId () << _T(", ") << isCheckedBoolStr
00518 << _T(", ") << toolBarVarName << _T(", ") << containerVarName
00519 << _T(");");
00520 emitter->AddCode (str);
00521 }
00522
00523 }
00524 str.Clear ();
00525 str << _T("wxTst::WxGuiTestHelper::FlushEventQueue ();\n");
00526 emitter->AddCode (str);
00527 }
00528
00529
00530