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