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 nsMenuItemX_h_
7 #define nsMenuItemX_h_
8 
9 #include "mozilla/RefPtr.h"
10 #include "nsISupports.h"
11 #include "nsMenuGroupOwnerX.h"
12 #include "nsMenuItemIconX.h"
13 #include "nsChangeObserver.h"
14 #include "nsStringFwd.h"
15 
16 #import <Cocoa/Cocoa.h>
17 
18 class nsMenuItemIconX;
19 class nsMenuX;
20 class nsMenuParentX;
21 
22 namespace mozilla {
23 namespace dom {
24 class Element;
25 }
26 }  // namespace mozilla
27 
28 enum {
29   knsMenuItemNoModifier = 0,
30   knsMenuItemShiftModifier = (1 << 0),
31   knsMenuItemAltModifier = (1 << 1),
32   knsMenuItemControlModifier = (1 << 2),
33   knsMenuItemCommandModifier = (1 << 3)
34 };
35 
36 enum EMenuItemType {
37   eRegularMenuItemType = 0,
38   eCheckboxMenuItemType,
39   eRadioMenuItemType,
40   eSeparatorMenuItemType
41 };
42 
43 // Once instantiated, this object lives until its DOM node or its parent window
44 // is destroyed. Do not hold references to this, they can become invalid any
45 // time the DOM node can be destroyed.
46 class nsMenuItemX final : public nsChangeObserver,
47                           public nsMenuItemIconX::Listener {
48  public:
49   nsMenuItemX(nsMenuX* aParent, const nsString& aLabel, EMenuItemType aItemType,
50               nsMenuGroupOwnerX* aMenuGroupOwner, nsIContent* aNode);
51 
IsVisible()52   bool IsVisible() const { return mIsVisible; }
53 
54   // Unregisters nsMenuX from the nsMenuGroupOwner, and nulls out the group
55   // owner pointer. This is needed because nsMenuX is reference-counted and can
56   // outlive its owner, and the menu group owner asserts that everything has
57   // been unregistered when it is destroyed.
58   void DetachFromGroupOwner();
59 
60   // Nulls out our reference to the parent.
61   // This is needed because nsMenuX is reference-counted and can outlive its
62   // parent.
DetachFromParent()63   void DetachFromParent() { mMenuParent = nullptr; }
64 
65   NS_INLINE_DECL_REFCOUNTING(nsMenuItemX)
66 
67   NS_DECL_CHANGEOBSERVER
68 
69   // nsMenuItemIconX::Listener
70   void IconUpdated() override;
71 
72   // nsMenuItemX
73   nsresult SetChecked(bool aIsChecked);
74   EMenuItemType GetMenuItemType();
75   void DoCommand(NSEventModifierFlags aModifierFlags, int16_t aButton);
76   nsresult DispatchDOMEvent(const nsString& eventName,
77                             bool* preventDefaultCalled);
78   void SetupIcon();
Content()79   nsIContent* Content() { return mContent; }
NativeNSMenuItem()80   NSMenuItem* NativeNSMenuItem() { return mNativeMenuItem; }
81 
82   void Dump(uint32_t aIndent) const;
83 
84  protected:
85   virtual ~nsMenuItemX();
86 
87   void UncheckRadioSiblings(nsIContent* aCheckedElement);
88   void SetKeyEquiv();
89 
90   nsCOMPtr<nsIContent> mContent;  // XUL <menuitem> or <menuseparator>
91 
92   EMenuItemType mType;
93 
94   // nsMenuItemX objects should always have a valid native menu item.
95   NSMenuItem* mNativeMenuItem = nil;             // [strong]
96   nsMenuX* mMenuParent = nullptr;                // [weak]
97   nsMenuGroupOwnerX* mMenuGroupOwner = nullptr;  // [weak]
98   RefPtr<mozilla::dom::Element> mCommandElement;
99   mozilla::UniquePtr<nsMenuItemIconX> mIcon;  // always non-null
100   bool mIsChecked = false;
101   bool mIsVisible = false;
102 };
103 
104 #endif  // nsMenuItemX_h_
105