Backed out changeset b4a0f8afc02e (bug 1857946) for causing bc failures at browser...
[gecko.git] / widget / cocoa / nsMenuItemX.h
blob980105e123d1cef71cf0f06a68fcaab27ea5e37d
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/. */
6 #ifndef nsMenuItemX_h_
7 #define nsMenuItemX_h_
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"
16 #import <Cocoa/Cocoa.h>
18 class nsMenuItemIconX;
19 class nsMenuX;
20 class nsMenuParentX;
22 namespace mozilla {
23 namespace dom {
24 class Element;
26 } // namespace mozilla
28 enum {
29 knsMenuItemNoModifier = 0,
30 knsMenuItemShiftModifier = (1 << 0),
31 knsMenuItemAltModifier = (1 << 1),
32 knsMenuItemControlModifier = (1 << 2),
33 knsMenuItemCommandModifier = (1 << 3)
36 enum EMenuItemType {
37 eRegularMenuItemType = 0,
38 eCheckboxMenuItemType,
39 eRadioMenuItemType,
40 eSeparatorMenuItemType
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);
52 bool IsVisible() const { return mIsVisible; }
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();
60 // Nulls out our reference to the parent.
61 // This is needed because nsMenuX is reference-counted and can outlive its
62 // parent.
63 void DetachFromParent() { mMenuParent = nullptr; }
65 NS_INLINE_DECL_REFCOUNTING(nsMenuItemX)
67 NS_DECL_CHANGEOBSERVER
69 // nsMenuItemIconX::Listener
70 void IconUpdated() override;
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();
79 nsMenuX* ParentMenu() { return mMenuParent; }
80 nsIContent* Content() { return mContent; }
81 NSMenuItem* NativeNSMenuItem() { return mNativeMenuItem; }
83 void Dump(uint32_t aIndent) const;
85 protected:
86 virtual ~nsMenuItemX();
88 void UncheckRadioSiblings(nsIContent* aCheckedElement);
89 void SetKeyEquiv();
91 nsCOMPtr<nsIContent> mContent; // XUL <menuitem> or <menuseparator>
93 EMenuItemType mType;
95 // nsMenuItemX objects should always have a valid native menu item.
96 NSMenuItem* mNativeMenuItem = nil; // [strong]
97 nsMenuX* mMenuParent = nullptr; // [weak]
98 nsMenuGroupOwnerX* mMenuGroupOwner = nullptr; // [weak]
99 RefPtr<mozilla::dom::Element> mCommandElement;
100 mozilla::UniquePtr<nsMenuItemIconX> mIcon; // always non-null
101 bool mIsChecked = false;
102 bool mIsVisible = false;
105 #endif // nsMenuItemX_h_