From e7440e6a239091d5364937161058007f2557b0a0 Mon Sep 17 00:00:00 2001 From: "pinkerton%netscape.com" Date: Sat, 26 Feb 2000 00:52:56 +0000 Subject: [PATCH] fix for 29257, onDestroy being called after the window had gone away. also calling onDestroy when menu item is selected. r=saari, a=jar. --- widget/src/mac/nsMenu.cpp | 82 ++++++++++++++++++++++++++------------- widget/src/mac/nsMenu.h | 1 + 2 files changed, 55 insertions(+), 28 deletions(-) diff --git a/widget/src/mac/nsMenu.cpp b/widget/src/mac/nsMenu.cpp index 52a75ec34c40..1cb8122cd4bf 100644 --- a/widget/src/mac/nsMenu.cpp +++ b/widget/src/mac/nsMenu.cpp @@ -93,6 +93,7 @@ nsMenu::nsMenu() : nsIMenu() mIsEnabled = PR_TRUE; mListener = nsnull; mConstructed = nsnull; + mDestroyHandlerCalled = PR_FALSE; mDOMNode = nsnull; mDOMElement = nsnull; @@ -584,6 +585,11 @@ nsEventStatus nsMenu::MenuItemSelected(const nsMenuEvent & aMenuEvent) eventStatus = listener->MenuSelected(event); if(eventStatus != nsEventStatus_eIgnore) { + // call our ondestroy handler now because the menu is going away. + // do it now before sending the event into the dom in case our window + // goes away. + OnDestroy(); + /* call back into this method with the proper "this" */ eventStatus = listener->MenuItemSelected(aMenuEvent); NS_RELEASE(menu); @@ -604,6 +610,11 @@ nsEventStatus nsMenu::MenuItemSelected(const nsMenuEvent & aMenuEvent) if(mMenuItemVoidArray[menuItemID-1]) { ((nsIMenuItem*)mMenuItemVoidArray[menuItemID-1])->QueryInterface(NS_GET_IID(nsIMenuListener), &menuListener); if(menuListener) { + // call our ondestroy handler now because the menu is going away. + // do it now before sending the event into the dom in case our window + // goes away. + OnDestroy(); + eventStatus = menuListener->MenuItemSelected(aMenuEvent); NS_IF_RELEASE(menuListener); if(nsEventStatus_eIgnore != eventStatus) @@ -624,6 +635,11 @@ nsEventStatus nsMenu::MenuItemSelected(const nsMenuEvent & aMenuEvent) nsIMenuListener * menuListener = nsnull; ((nsISupports*)mMenuItemVoidArray[i-1])->QueryInterface(NS_GET_IID(nsIMenuListener), &menuListener); if(menuListener){ + // call our ondestroy handler now because the menu is going away. + // do it now before sending the event into the dom in case our window + // goes away. + OnDestroy(); + eventStatus = menuListener->MenuItemSelected(aMenuEvent); NS_IF_RELEASE(menuListener); if(nsEventStatus_eIgnore != eventStatus) @@ -723,6 +739,9 @@ nsEventStatus nsMenu::MenuConstruct( void * menuNode, void * aWebShell) { + // reset destroy handler flag so that we'll know to fire it next time this menu goes away. + mDestroyHandlerCalled = PR_FALSE; + //printf("nsMenu::MenuConstruct called for %s = %d \n", mLabel.ToNewCString(), mMacMenuHandle); // Begin menuitem inner loop @@ -1589,21 +1608,22 @@ nsMenu::OnCreate() nsCOMPtr presContext; MenuHelpers::WebShellToPresContext ( mWebShell, getter_AddRefs(presContext) ); - - nsresult rv; - nsCOMPtr menuPopup; - GetMenuPopupElement(getter_AddRefs(menuPopup)); - nsCOMPtr popupContent ( do_QueryInterface(menuPopup) ); - if ( popupContent ) - rv = popupContent->HandleDOMEvent(presContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); - else { - nsCOMPtr me ( do_QueryInterface(mDOMNode) ); - if ( me ) - rv = me->HandleDOMEvent(presContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); - } + if ( presContext ) { + nsresult rv; + nsCOMPtr menuPopup; + GetMenuPopupElement(getter_AddRefs(menuPopup)); + nsCOMPtr popupContent ( do_QueryInterface(menuPopup) ); + if ( popupContent ) + rv = popupContent->HandleDOMEvent(presContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); + else { + nsCOMPtr me ( do_QueryInterface(mDOMNode) ); + if ( me ) + rv = me->HandleDOMEvent(presContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); + } + if ( NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault ) + return PR_FALSE; + } - if ( NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault ) - return PR_FALSE; return PR_TRUE; } @@ -1617,6 +1637,9 @@ nsMenu::OnCreate() PRBool nsMenu::OnDestroy() { + if ( mDestroyHandlerCalled ) + return PR_TRUE; + nsEventStatus status = nsEventStatus_eIgnore; nsMouseEvent event; event.eventStructType = NS_EVENT; @@ -1630,21 +1653,24 @@ nsMenu::OnDestroy() nsCOMPtr presContext; MenuHelpers::WebShellToPresContext ( mWebShell, getter_AddRefs(presContext) ); - - nsresult rv; - nsCOMPtr menuPopup; - GetMenuPopupElement(getter_AddRefs(menuPopup)); - nsCOMPtr popupContent ( do_QueryInterface(menuPopup) ); - if ( popupContent ) - rv = popupContent->HandleDOMEvent(presContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); - else { - nsCOMPtr me ( do_QueryInterface(mDOMNode) ); - if ( me ) - rv = me->HandleDOMEvent(presContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); - } + if ( presContext ) { + nsresult rv; + nsCOMPtr menuPopup; + GetMenuPopupElement(getter_AddRefs(menuPopup)); + nsCOMPtr popupContent ( do_QueryInterface(menuPopup) ); + if ( popupContent ) + rv = popupContent->HandleDOMEvent(presContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); + else { + nsCOMPtr me ( do_QueryInterface(mDOMNode) ); + if ( me ) + rv = me->HandleDOMEvent(presContext, &event, nsnull, NS_EVENT_FLAG_INIT, &status); + } - if ( NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault ) - return PR_FALSE; + mDestroyHandlerCalled = PR_TRUE; + + if ( NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault ) + return PR_FALSE; + } return PR_TRUE; } diff --git a/widget/src/mac/nsMenu.h b/widget/src/mac/nsMenu.h index 93a772f1b69d..8b0b748c4ed7 100644 --- a/widget/src/mac/nsMenu.h +++ b/widget/src/mac/nsMenu.h @@ -183,6 +183,7 @@ protected: PRInt16 mHelpMenuOSItemsCount; PRBool mIsHelpMenu; PRBool mIsEnabled; + PRBool mDestroyHandlerCalled; // fetch the content node associated with the menupopup item void GetMenuPopupElement ( nsIDOMNode** aResult ) ;