Back to index

lightning-sunbird  0.9+nobinonly
nsMenuBarX.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 nsMenuBarX_h__
00039 #define nsMenuBarX_h__
00040 
00041 #include "nsIMenuBar.h"
00042 #include "nsIMenuListener.h"
00043 #include "nsIDocumentObserver.h"
00044 #include "nsIChangeManager.h"
00045 #include "nsIMenuCommandDispatcher.h"
00046 #include "nsPresContext.h"
00047 #include "nsSupportsArray.h"
00048 #include "nsHashtable.h"
00049 #include "nsWeakReference.h"
00050 #include "nsIContent.h"
00051 
00052 #include <MacTypes.h>
00053 #include <UnicodeConverter.h>
00054 #include <Menus.h>
00055 #include <CarbonEvents.h>
00056 
00057 extern nsWeakPtr gMacMenubarX;
00058 
00059 class nsIWidget;
00060 class nsIDocument;
00061 class nsIDOMNode;
00062 
00063 namespace MenuHelpersX
00064 {
00065     // utility routine for getting a PresContext out of a docShell
00066   nsresult DocShellToPresContext ( nsIDocShell* inDocShell, nsPresContext** outContext ) ;
00067 
00068 }
00069 
00070 
00075 class nsMenuBarX :  public nsIMenuBar,
00076                     public nsIMenuListener,
00077                     public nsIDocumentObserver,
00078                     public nsIChangeManager,
00079                     public nsIMenuCommandDispatcher,
00080                     public nsSupportsWeakReference
00081 {
00082 public:
00083     nsMenuBarX();
00084     virtual ~nsMenuBarX();
00085 
00086     enum { kAppleMenuID = 1 } ;
00087     
00088     NS_DECL_ISUPPORTS
00089     NS_DECL_NSICHANGEMANAGER
00090     NS_DECL_NSIMENUCOMMANDDISPATCHER
00091 
00092     // nsIMenuListener interface
00093     nsEventStatus MenuItemSelected(const nsMenuEvent & aMenuEvent);
00094     nsEventStatus MenuSelected(const nsMenuEvent & aMenuEvent);
00095     nsEventStatus MenuDeselected(const nsMenuEvent & aMenuEvent);
00096     nsEventStatus MenuConstruct(const nsMenuEvent & aMenuEvent, nsIWidget * aParentWindow, 
00097                                 void * menuNode, void * aDocShell);
00098     nsEventStatus MenuDestruct(const nsMenuEvent & aMenuEvent);
00099     nsEventStatus CheckRebuild(PRBool & aMenuEvent);
00100     nsEventStatus SetRebuild(PRBool aMenuEvent);
00101 
00102     // nsIDocumentObserver
00103     NS_DECL_NSIDOCUMENTOBSERVER
00104 
00105     NS_IMETHOD Create(nsIWidget * aParent);
00106 
00107     // nsIMenuBar Methods
00108     NS_IMETHOD GetParent(nsIWidget *&aParent);
00109     NS_IMETHOD SetParent(nsIWidget * aParent);
00110     NS_IMETHOD AddMenu(nsIMenu * aMenu);
00111     NS_IMETHOD GetMenuCount(PRUint32 &aCount);
00112     NS_IMETHOD GetMenuAt(const PRUint32 aCount, nsIMenu *& aMenu);
00113     NS_IMETHOD InsertMenuAt(const PRUint32 aCount, nsIMenu *& aMenu);
00114     NS_IMETHOD RemoveMenu(const PRUint32 aCount);
00115     NS_IMETHOD RemoveAll();
00116     NS_IMETHOD GetNativeData(void*& aData);
00117     NS_IMETHOD Paint();
00118     NS_IMETHOD SetNativeData(void* aData);
00119     
00120 protected:
00121 
00122     void GetDocument ( nsIDocShell* inDocShell, nsIDocument** outDocument ) ;
00123     void RegisterAsDocumentObserver ( nsIDocShell* inDocShell ) ;
00124     
00125       // Make our menubar conform to Aqua UI guidelines
00126     void AquifyMenuBar ( ) ;
00127     void HideItem ( nsIDOMDocument* inDoc, const nsAString & inID, nsIContent** outHiddenNode ) ;
00128     OSStatus InstallCommandEventHandler ( ) ;
00129 
00130       // command handler for some special menu items (prefs/quit/etc)
00131     pascal static OSStatus CommandEventHandler ( EventHandlerCallRef inHandlerChain, 
00132                                                   EventRef inEvent, void* userData ) ;
00133     nsEventStatus ExecuteCommand ( nsIContent* inDispatchTo ) ;
00134     
00135       // build the Apple menu shared by all menu bars.
00136     nsresult CreateAppleMenu ( nsIMenu* inMenu ) ;
00137 
00138     nsHashtable             mObserverTable;     // stores observers for content change notification
00139     nsHashtable             mCommandMapTable;   // maps CommandIDs to content nodes for CarbonEvent item selection
00140     PRUint32                mCurrentCommandID;  // unique command id (per menu-bar) to give to next item that asks
00141 
00142     PRUint32                mNumMenus;
00143     nsSupportsArray         mMenusArray;        // holds refs
00144     nsCOMPtr<nsIContent>    mMenuBarContent;    // menubar content node, strong ref
00145     nsCOMPtr<nsIContent>    mPrefItemContent;   // on X, holds the content node for the prefs item that has
00146                                                 // been removed from the menubar
00147     nsCOMPtr<nsIContent>    mQuitItemContent;   // as above, but for quit
00148     nsIWidget*              mParent;            // weak ref
00149 
00150     PRBool                  mIsMenuBarAdded;
00151 
00152     nsWeakPtr               mDocShellWeakRef;   // weak ref to docshell
00153     nsIDocument*            mDocument;          // pointer to document
00154 
00155     MenuRef                 mRootMenu;          // root menu, representing entire menu bar.
00156     
00157     static MenuRef          sAppleMenu;         // AppleMenu shared by all menubars
00158  
00159     static EventHandlerUPP  sCommandEventHandler;   // carbon event handler for commands, shared
00160 };
00161 
00162 #endif // nsMenuBarX_h__