Back to index

lightning-sunbird  0.9+nobinonly
nsMenuX.h
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.org 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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsMenuX_h__
00039 #define nsMenuX_h__
00040 
00041 #include "nsCOMPtr.h"
00042 #include "nsIMenu.h"
00043 #include "nsSupportsArray.h"
00044 #include "nsIMenuListener.h"
00045 #include "nsIChangeManager.h"
00046 #include "nsWeakReference.h"
00047 
00048 #include <Menus.h>
00049 #include <UnicodeConverter.h>
00050 #include <CarbonEvents.h>
00051 
00052 
00053 class nsIMenuBar;
00054 class nsIMenuListener;
00055 
00056 
00057 //static PRInt16      mMacMenuIDCount;    // use GetUniqueMenuID()
00058 extern PRInt16 mMacMenuIDCount;// = kMacMenuID;
00059 
00060 
00061 class nsMenuX : public nsIMenu,
00062                 public nsIMenuListener,
00063                 public nsIChangeObserver,
00064                 public nsSupportsWeakReference
00065 {
00066 
00067 public:
00068     nsMenuX();
00069     virtual ~nsMenuX();
00070 
00071     NS_DECL_ISUPPORTS
00072     NS_DECL_NSICHANGEOBSERVER
00073 
00074     // nsIMenuListener methods
00075     nsEventStatus MenuItemSelected(const nsMenuEvent & aMenuEvent); 
00076     nsEventStatus MenuSelected(const nsMenuEvent & aMenuEvent); 
00077     nsEventStatus MenuDeselected(const nsMenuEvent & aMenuEvent); 
00078     nsEventStatus MenuConstruct( const nsMenuEvent & aMenuEvent, nsIWidget * aParentWindow, 
00079                                 void * menuNode, void * aDocShell);
00080     nsEventStatus MenuDestruct(const nsMenuEvent & aMenuEvent);
00081     nsEventStatus CheckRebuild(PRBool & aMenuEvent);
00082     nsEventStatus SetRebuild(PRBool aMenuEvent);
00083 
00084     // nsIMenu Methods
00085     NS_IMETHOD Create ( nsISupports * aParent, const nsAString &aLabel, const nsAString &aAccessKey, 
00086                         nsIChangeManager* aManager, nsIDocShell* aShell, nsIContent* aNode ) ;
00087     NS_IMETHOD GetParent(nsISupports *&aParent);
00088     NS_IMETHOD GetLabel(nsString &aText);
00089     NS_IMETHOD SetLabel(const nsAString &aText);
00090     NS_IMETHOD GetAccessKey(nsString &aText);
00091     NS_IMETHOD SetAccessKey(const nsAString &aText);
00092     NS_IMETHOD AddItem(nsISupports* aText);
00093     NS_IMETHOD AddSeparator();
00094     NS_IMETHOD GetItemCount(PRUint32 &aCount);
00095     NS_IMETHOD GetItemAt(const PRUint32 aPos, nsISupports *& aMenuItem);
00096     NS_IMETHOD InsertItemAt(const PRUint32 aPos, nsISupports * aMenuItem);
00097     NS_IMETHOD RemoveItem(const PRUint32 aPos);
00098     NS_IMETHOD RemoveAll();
00099     NS_IMETHOD GetNativeData(void** aData);
00100     NS_IMETHOD SetNativeData(void* aData);
00101     NS_IMETHOD AddMenuListener(nsIMenuListener * aMenuListener);
00102     NS_IMETHOD RemoveMenuListener(nsIMenuListener * aMenuListener);
00103     NS_IMETHOD GetMenuContent(nsIContent ** aMenuNode);
00104     NS_IMETHOD SetEnabled(PRBool aIsEnabled);
00105     NS_IMETHOD GetEnabled(PRBool* aIsEnabled);
00106     NS_IMETHOD IsHelpMenu(PRBool* aIsEnabled);
00107 
00108     NS_IMETHOD AddMenuItem(nsIMenuItem * aMenuItem);
00109     NS_IMETHOD AddMenu(nsIMenu * aMenu);
00110 
00111 protected:
00112       // Determines how many menus are visible among the siblings that are before me.
00113       // It doesn't matter if I am visible.
00114     nsresult CountVisibleBefore ( PRUint32* outVisibleBefore ) ;
00115 
00116     // fetch the content node associated with the menupopup item
00117     void GetMenuPopupContent ( nsIContent** aResult ) ;
00118 
00119       // Insert a new item in this menu with index |inItemIndex| with the text |inItemLabel|,
00120       // middle-truncated to a certain pixel width with an elipsis.
00121     void InsertMenuItemWithTruncation ( nsAutoString & inItemLabel, 
00122                                           PRUint32 inItemIndex ) ;
00123     
00124     // fire handlers for oncreate/ondestroy
00125     PRBool OnDestroy() ;
00126     PRBool OnCreate() ;
00127     PRBool OnDestroyed() ;
00128     PRBool OnCreated() ;
00129 
00130     void LoadMenuItem ( nsIMenu* pParentMenu, nsIContent* menuitemContent );  
00131     void LoadSubMenu ( nsIMenu * pParentMenu, nsIContent* menuitemContent );
00132     void LoadSeparator ( nsIContent* menuitemContent );
00133 
00134     MenuHandle NSStringNewMenu(short menuID, nsString& menuTitle);
00135 
00136 protected:
00137     nsString                    mLabel;
00138     PRUint32                    mNumMenuItems;
00139     nsSupportsArray             mMenuItemsArray;        // array holds refs
00140 
00141     nsISupports*                mParent;                // weak, my parent owns me
00142     nsIChangeManager*           mManager;               // weak ref, it will outlive us [menubar]
00143     nsWeakPtr                   mDocShellWeakRef;       // weak ref to docshell
00144     nsCOMPtr<nsIContent>        mMenuContent;           // the |menu| tag, strong ref
00145     nsCOMPtr<nsIMenuListener>   mListener;              // strong ref
00146 
00147     // MacSpecific
00148     PRInt16                     mMacMenuID;
00149     MenuHandle                  mMacMenuHandle;
00150     PRPackedBool                mIsEnabled;
00151     PRPackedBool                mDestroyHandlerCalled;
00152     PRPackedBool                mNeedsRebuild;
00153     PRPackedBool                mConstructed;
00154     PRPackedBool                mVisible;               // are we visible to the user?
00155     
00156     EventHandlerRef             mHandler;               // our event handler
00157 };
00158 
00159 #endif // nsMenuX_h__