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 "nsAutoPtr.h"
00043 #include "nsIMenu.h"
00044 #include "nsSupportsArray.h"
00045 #include "nsIMenuListener.h"
00046 #include "nsIChangeManager.h"
00047 #include "nsWeakReference.h"
00048 
00049 #include <Menus.h>
00050 #include <UnicodeConverter.h>
00051 #include <CarbonEvents.h>
00052 
00053 
00054 class nsIMenuBar;
00055 class nsIMenuListener;
00056 class nsMenuItemIcon;
00057 
00058 
00059 //static PRInt16      mMacMenuIDCount;    // use GetUniqueMenuID()
00060 extern PRInt16 mMacMenuIDCount;// = kMacMenuID;
00061 
00062 #if DEBUG
00063 // utility instance counter class
00064 class nsInstanceCounter
00065 {
00066 public:
00067         nsInstanceCounter(const char* inDesc)
00068         : mInstanceCount(0)
00069         , mDescription(inDesc)
00070         {
00071         }
00072         
00073         ~nsInstanceCounter()
00074         {
00075           printf("%s %d\n", mDescription, mInstanceCount);
00076         }
00077 
00078         nsInstanceCounter& operator ++()          // prefix
00079         {
00080           ++ mInstanceCount;
00081           return *this;                
00082         }
00083 
00084         nsInstanceCounter& operator -- ()        // prefix
00085         {
00086           -- mInstanceCount;
00087           return *this;
00088         }
00089 
00090 protected:
00091 
00092   PRInt32     mInstanceCount;
00093   const char* mDescription;
00094 
00095 };
00096 #endif
00097 
00098 class nsMenuX : public nsIMenu_MOZILLA_1_8_BRANCH,
00099                 public nsIMenuListener,
00100                 public nsIChangeObserver,
00101                 public nsSupportsWeakReference
00102 {
00103 
00104 public:
00105     nsMenuX();
00106     virtual ~nsMenuX();
00107 
00108     NS_DECL_ISUPPORTS
00109     NS_DECL_NSICHANGEOBSERVER
00110 
00111     // nsIMenuListener methods
00112     nsEventStatus MenuItemSelected(const nsMenuEvent & aMenuEvent); 
00113     nsEventStatus MenuSelected(const nsMenuEvent & aMenuEvent); 
00114     nsEventStatus MenuDeselected(const nsMenuEvent & aMenuEvent); 
00115     nsEventStatus MenuConstruct( const nsMenuEvent & aMenuEvent, nsIWidget * aParentWindow, 
00116                                 void * menuNode, void * aDocShell);
00117     nsEventStatus MenuDestruct(const nsMenuEvent & aMenuEvent);
00118     nsEventStatus CheckRebuild(PRBool & aMenuEvent);
00119     nsEventStatus SetRebuild(PRBool aMenuEvent);
00120 
00121     // nsIMenu Methods
00122     NS_IMETHOD Create ( nsISupports * aParent, const nsAString &aLabel, const nsAString &aAccessKey, 
00123                         nsIChangeManager* aManager, nsIDocShell* aShell, nsIContent* aNode ) ;
00124     NS_IMETHOD GetParent(nsISupports *&aParent);
00125     NS_IMETHOD GetLabel(nsString &aText);
00126     NS_IMETHOD SetLabel(const nsAString &aText);
00127     NS_IMETHOD GetAccessKey(nsString &aText);
00128     NS_IMETHOD SetAccessKey(const nsAString &aText);
00129     NS_IMETHOD AddItem(nsISupports* aText);
00130     NS_IMETHOD AddSeparator();
00131     NS_IMETHOD GetItemCount(PRUint32 &aCount);
00132     NS_IMETHOD GetItemAt(const PRUint32 aPos, nsISupports *& aMenuItem);
00133     NS_IMETHOD InsertItemAt(const PRUint32 aPos, nsISupports * aMenuItem);
00134     NS_IMETHOD RemoveItem(const PRUint32 aPos);
00135     NS_IMETHOD RemoveAll();
00136     NS_IMETHOD GetNativeData(void** aData);
00137     NS_IMETHOD SetNativeData(void* aData);
00138     NS_IMETHOD AddMenuListener(nsIMenuListener * aMenuListener);
00139     NS_IMETHOD RemoveMenuListener(nsIMenuListener * aMenuListener);
00140     NS_IMETHOD GetMenuContent(nsIContent ** aMenuNode);
00141     NS_IMETHOD SetEnabled(PRBool aIsEnabled);
00142     NS_IMETHOD GetEnabled(PRBool* aIsEnabled);
00143     NS_IMETHOD IsHelpMenu(PRBool* aIsEnabled);
00144 
00145     NS_IMETHOD AddMenuItem(nsIMenuItem * aMenuItem);
00146     NS_IMETHOD AddMenu(nsIMenu * aMenu);
00147     NS_IMETHOD ChangeNativeEnabledStatusForMenuItem(nsIMenuItem* aMenuItem, PRBool aEnabled);
00148     NS_IMETHOD GetMenuRefAndItemIndexForMenuItem(nsISupports* aMenuItem,
00149                                                  void**       aMenuRef,
00150                                                  PRUint16*    aMenuItemIndex);
00151     NS_IMETHOD SetupIcon();
00152 
00153 protected:
00154       // Determines how many menus are visible among the siblings that are before me.
00155       // It doesn't matter if I am visible.
00156     nsresult CountVisibleBefore ( PRUint32* outVisibleBefore ) ;
00157 
00158     // fetch the content node associated with the menupopup item
00159     void GetMenuPopupContent ( nsIContent** aResult ) ;
00160 
00161       // Insert a new item in this menu with index |inItemIndex| with the text |inItemLabel|,
00162       // middle-truncated to a certain pixel width with an elipsis.
00163     void InsertMenuItemWithTruncation ( nsAutoString & inItemLabel, 
00164                                           PRUint32 inItemIndex ) ;
00165     
00166     // fire handlers for oncreate/ondestroy
00167     PRBool OnDestroy() ;
00168     PRBool OnCreate() ;
00169     PRBool OnDestroyed() ;
00170     PRBool OnCreated() ;
00171 
00172     void LoadMenuItem ( nsIMenu* pParentMenu, nsIContent* menuitemContent );  
00173     void LoadSubMenu ( nsIMenu * pParentMenu, nsIContent* menuitemContent );
00174     void LoadSeparator ( nsIContent* menuitemContent );
00175 
00176     MenuHandle NSStringNewMenu(short menuID, nsString& menuTitle);
00177 
00178 protected:
00179     nsString                    mLabel;
00180     PRUint32                    mNumMenuItems;
00181     nsSupportsArray             mMenuItemsArray;        // array holds refs
00182 
00183     nsISupports*                mParent;                // weak, my parent owns me
00184     nsIChangeManager*           mManager;               // weak ref, it will outlive us [menubar]
00185     nsWeakPtr                   mDocShellWeakRef;       // weak ref to docshell
00186     nsCOMPtr<nsIContent>        mMenuContent;           // the |menu| tag, strong ref
00187     nsCOMPtr<nsIMenuListener>   mListener;              // strong ref
00188     nsRefPtr<nsMenuItemIcon>    mIcon;
00189 
00190     // MacSpecific
00191     PRInt16                     mMacMenuID;
00192     MenuHandle                  mMacMenuHandle;
00193     PRPackedBool                mIsEnabled;
00194     PRPackedBool                mDestroyHandlerCalled;
00195     PRPackedBool                mNeedsRebuild;
00196     PRPackedBool                mConstructed;
00197     PRPackedBool                mVisible;               // are we visible to the user?
00198     
00199     EventHandlerRef             mHandler;               // our event handler
00200 };
00201 
00202 #endif // nsMenuX_h__