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