1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef nsMenuX_h_
7 #define nsMenuX_h_
8 
9 #import <Cocoa/Cocoa.h>
10 
11 #include "mozilla/RefPtr.h"
12 #include "mozilla/UniquePtr.h"
13 #include "nsMenuBaseX.h"
14 #include "nsMenuBarX.h"
15 #include "nsMenuGroupOwnerX.h"
16 #include "nsCOMPtr.h"
17 #include "nsChangeObserver.h"
18 
19 class nsMenuX;
20 class nsMenuItemIconX;
21 class nsMenuItemX;
22 class nsIWidget;
23 
24 // MenuDelegate is used to receive Cocoa notifications for setting
25 // up carbon events. Protocol is defined as of 10.6 SDK.
26 @interface MenuDelegate : NSObject<NSMenuDelegate> {
27   nsMenuX* mGeckoMenu;  // weak ref
28 }
29 - (id)initWithGeckoMenu:(nsMenuX*)geckoMenu;
30 @end
31 
32 // Once instantiated, this object lives until its DOM node or its parent window is destroyed.
33 // Do not hold references to this, they can become invalid any time the DOM node can be destroyed.
34 class nsMenuX : public nsMenuObjectX, public nsChangeObserver {
35  public:
36   nsMenuX();
37   virtual ~nsMenuX();
38 
39   // If > 0, the OS is indexing all the app's menus (triggered by opening
40   // Help menu on Leopard and higher).  There are some things that are
41   // unsafe to do while this is happening.
42   static int32_t sIndexingMenuLevel;
43 
44   NS_DECL_CHANGEOBSERVER
45 
46   // nsMenuObjectX
NativeData()47   void* NativeData() override { return (void*)mNativeMenu; }
MenuObjectType()48   nsMenuObjectTypeX MenuObjectType() override { return eSubmenuObjectType; }
IconUpdated()49   void IconUpdated() override { mParent->IconUpdated(); }
50 
51   // nsMenuX
52   nsresult Create(nsMenuObjectX* aParent, nsMenuGroupOwnerX* aMenuGroupOwner, nsIContent* aNode);
53   uint32_t GetItemCount();
54   nsMenuObjectX* GetItemAt(uint32_t aPos);
55   nsresult GetVisibleItemCount(uint32_t& aCount);
56   nsMenuObjectX* GetVisibleItemAt(uint32_t aPos);
57   nsEventStatus MenuOpened();
58   void MenuClosed();
59   void SetRebuild(bool aMenuEvent);
60   NSMenuItem* NativeMenuItem();
61   nsresult SetupIcon();
62 
63   static bool IsXULHelpMenu(nsIContent* aMenuContent);
64 
65  protected:
66   void MenuConstruct();
67   nsresult RemoveAll();
68   nsresult SetEnabled(bool aIsEnabled);
69   nsresult GetEnabled(bool* aIsEnabled);
70   void GetMenuPopupContent(nsIContent** aResult);
71   bool OnOpen();
72   bool OnClose();
73   nsresult AddMenuItem(nsMenuItemX* aMenuItem);
74   nsMenuX* AddMenu(mozilla::UniquePtr<nsMenuX> aMenu);
75   void LoadMenuItem(nsIContent* inMenuItemContent);
76   void LoadSubMenu(nsIContent* inMenuContent);
77   GeckoNSMenu* CreateMenuWithGeckoString(nsString& menuTitle);
78 
79   nsTArray<mozilla::UniquePtr<nsMenuObjectX>> mMenuObjectsArray;
80   nsString mLabel;
81   uint32_t mVisibleItemsCount;         // cache
82   nsMenuObjectX* mParent;              // [weak]
83   nsMenuGroupOwnerX* mMenuGroupOwner;  // [weak]
84   // The icon object should never outlive its creating nsMenuX object.
85   RefPtr<nsMenuItemIconX> mIcon;
86   GeckoNSMenu* mNativeMenu;     // [strong]
87   MenuDelegate* mMenuDelegate;  // [strong]
88   // nsMenuX objects should always have a valid native menu item.
89   NSMenuItem* mNativeMenuItem;  // [strong]
90   bool mIsEnabled;
91   bool mDestroyHandlerCalled;
92   bool mNeedsRebuild;
93   bool mConstructed;
94   bool mVisible;
95   bool mXBLAttached;
96 };
97 
98 #endif  // nsMenuX_h_
99