Back to index

lightning-sunbird  0.9+nobinonly
nsMenuDismissalListener.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Dean Tessman <dean_tessman@hotmail.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsMenuDismissalListener.h"
00040 #include "nsIMenuParent.h"
00041 #include "nsMenuFrame.h"
00042 
00043 /*
00044  * nsMenuDismissalListener implementation
00045  */
00046 
00047 NS_IMPL_ADDREF(nsMenuDismissalListener)
00048 NS_IMPL_RELEASE(nsMenuDismissalListener)
00049 NS_IMPL_QUERY_INTERFACE3(nsMenuDismissalListener, nsIDOMMouseListener, nsIMenuRollup, nsIRollupListener)
00050 
00051 
00053 
00054 nsMenuDismissalListener::nsMenuDismissalListener() :
00055   mEnabled(PR_TRUE)
00056 {
00057   mMenuParent = nsnull;
00058 }
00059 
00061 nsMenuDismissalListener::~nsMenuDismissalListener() 
00062 {
00063 }
00064 
00066 nsresult
00067 nsMenuDismissalListener::MouseDown(nsIDOMEvent* aMouseEvent)
00068 {
00069   return NS_OK;
00070 }
00071 
00072 nsIMenuParent*
00073 nsMenuDismissalListener::GetCurrentMenuParent()
00074 {
00075   return mMenuParent;
00076 }
00077 
00078 void
00079 nsMenuDismissalListener::SetCurrentMenuParent(nsIMenuParent* aMenuParent)
00080 {
00081   if (aMenuParent == mMenuParent)
00082     return;
00083 
00084   nsCOMPtr<nsIRollupListener> kungFuDeathGrip = this;
00085   Unregister();
00086   
00087   mMenuParent = aMenuParent;
00088   if (!aMenuParent)
00089     return;
00090 
00091   nsCOMPtr<nsIWidget> widget;
00092   aMenuParent->GetWidget(getter_AddRefs(widget));
00093   if (!widget)
00094     return;
00095 
00096   PRBool consumeOutsideClicks = PR_FALSE;
00097   aMenuParent->ConsumeOutsideClicks(consumeOutsideClicks);
00098   widget->CaptureRollupEvents(this, PR_TRUE, consumeOutsideClicks);
00099   mWidget = widget;
00100 
00101   NS_ADDREF(nsMenuFrame::sDismissalListener = this);
00102 }
00103 
00104 NS_IMETHODIMP
00105 nsMenuDismissalListener::Rollup()
00106 {
00107   if (mEnabled) {
00108     if (mMenuParent) {
00109       AddRef();
00110       mMenuParent->HideChain();
00111       mMenuParent->DismissChain();
00112       Release();
00113     }
00114     else
00115       Unregister();
00116   }
00117   return NS_OK;
00118 }
00119 
00121 NS_IMETHODIMP nsMenuDismissalListener::ShouldRollupOnMouseWheelEvent(PRBool *aShouldRollup) 
00122 { 
00123   *aShouldRollup = PR_FALSE; 
00124   return NS_OK;
00125 }
00126 
00127 
00128 // uggggh.
00129 
00130 // a menu should not roll up if activated by a mouse activate message (eg. X-mouse)
00131 NS_IMETHODIMP nsMenuDismissalListener::ShouldRollupOnMouseActivate(PRBool *aShouldRollup) 
00132 { 
00133   *aShouldRollup = PR_FALSE; 
00134   return NS_OK;
00135 }
00136 
00137 NS_IMETHODIMP
00138 nsMenuDismissalListener::GetSubmenuWidgetChain(nsISupportsArray **_retval)
00139 {
00140   NS_NewISupportsArray ( _retval );
00141   nsIMenuParent *curr = mMenuParent;
00142   while ( curr ) {
00143     nsCOMPtr<nsIWidget> widget;
00144     curr->GetWidget ( getter_AddRefs(widget) );
00145     nsCOMPtr<nsISupports> genericWidget ( do_QueryInterface(widget) );
00146     (**_retval).AppendElement ( genericWidget );
00147     
00148     // move up the chain
00149     nsIFrame* currAsFrame = nsnull;
00150     if ( NS_SUCCEEDED(CallQueryInterface(curr, &currAsFrame)) ) {
00151       nsIMenuFrame *menuFrame = nsnull;
00152       nsIFrame *parentFrame = currAsFrame->GetParent();
00153       if (parentFrame) {
00154         CallQueryInterface(parentFrame, &menuFrame);
00155       }
00156       if ( menuFrame ) {
00157         curr = menuFrame->GetMenuParent ();       // Advance to next parent
00158       }
00159       else {
00160         // we are a menuParent but not a menuFrame. This is probably the case
00161         // of the menu bar. Nothing to do here, really.
00162         return NS_OK;
00163       }
00164     }
00165     else {
00166       // We've run into a menu parent that isn't a frame at all. Not good.
00167       NS_WARNING ( "nsIMenuParent that is not a nsIFrame" );
00168       return NS_ERROR_FAILURE;
00169     }
00170   } // foreach parent menu
00171   
00172   return NS_OK; 
00173 }
00174 
00175 #if 0
00176 NS_IMETHODIMP
00177 nsMenuDismissalListener::FirstMenuParent(nsIMenuParent * *_retval)
00178 {
00179   NS_IF_ADDREF(*_retval = mMenuParent);
00180   
00181   return NS_OK;
00182 }
00183 
00184 
00185 NS_IMETHODIMP
00186 nsMenuDismissalListener::NextMenuParent(nsIMenuParent * inCurrent, nsIMenuParent * *_retval)
00187 {
00188 //XXX for now don't return anything in the chain
00189   *_retval = nsnull;
00190   return NS_OK;
00191 }
00192 #endif
00193 
00194 NS_IMETHODIMP
00195 nsMenuDismissalListener::Unregister()
00196 {
00197   if (mWidget) {
00198     mWidget->CaptureRollupEvents(this, PR_FALSE, PR_FALSE);
00199     mWidget = nsnull;
00200   }
00201   
00202   NS_RELEASE(nsMenuFrame::sDismissalListener);
00203   return NS_OK;
00204 }
00205 
00206 NS_IMETHODIMP
00207 nsMenuDismissalListener::EnableListener(PRBool aEnabled)
00208 {
00209   mEnabled = aEnabled;
00210   return NS_OK;
00211 }
00212