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 #include "nsMenuGroupOwnerX.h"
7 #include "nsMenuBarX.h"
9 #include "nsMenuItemX.h"
10 #include "nsMenuUtilsX.h"
11 #include "nsCocoaUtils.h"
12 #include "nsCocoaWindow.h"
16 #include "nsObjCExceptions.h"
17 #include "nsThreadUtils.h"
19 #include "mozilla/dom/Element.h"
20 #include "nsIWidget.h"
21 #include "mozilla/dom/Document.h"
25 using namespace mozilla;
27 NS_IMPL_ISUPPORTS(nsMenuGroupOwnerX, nsIMutationObserver)
29 nsMenuGroupOwnerX::nsMenuGroupOwnerX() : mCurrentCommandID(eCommand_ID_Last) {
30 mInfoSet = [[NSMutableSet setWithCapacity:10] retain];
33 nsMenuGroupOwnerX::~nsMenuGroupOwnerX() {
34 MOZ_ASSERT(mContentToObserverTable.Count() == 0, "have outstanding mutation observers!\n");
36 // The MenuItemInfo objects in mInfoSet may live longer than we do. So when
37 // we get destroyed we need to invalidate all their mMenuGroupOwner pointers.
38 NSEnumerator* counter = [mInfoSet objectEnumerator];
40 while ((info = (MenuItemInfo*)[counter nextObject])) {
41 [info setMenuGroupOwner:nil];
46 nsresult nsMenuGroupOwnerX::Create(mozilla::dom::Element* aContent) {
47 if (!aContent) return NS_ERROR_INVALID_ARG;
55 // nsIMutationObserver
58 void nsMenuGroupOwnerX::CharacterDataWillChange(nsIContent* aContent,
59 const CharacterDataChangeInfo&) {}
61 void nsMenuGroupOwnerX::CharacterDataChanged(nsIContent* aContent, const CharacterDataChangeInfo&) {
64 void nsMenuGroupOwnerX::ContentAppended(nsIContent* aFirstNewContent) {
65 for (nsIContent* cur = aFirstNewContent; cur; cur = cur->GetNextSibling()) {
70 void nsMenuGroupOwnerX::NodeWillBeDestroyed(const nsINode* aNode) {}
72 void nsMenuGroupOwnerX::AttributeWillChange(dom::Element* aContent, int32_t aNameSpaceID,
73 nsAtom* aAttribute, int32_t aModType) {}
75 void nsMenuGroupOwnerX::NativeAnonymousChildListChange(nsIContent* aContent, bool aIsRemove) {}
77 void nsMenuGroupOwnerX::AttributeChanged(dom::Element* aElement, int32_t aNameSpaceID,
78 nsAtom* aAttribute, int32_t aModType,
79 const nsAttrValue* aOldValue) {
80 nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this);
81 nsChangeObserver* obs = LookupContentChangeObserver(aElement);
82 if (obs) obs->ObserveAttributeChanged(aElement->OwnerDoc(), aElement, aAttribute);
85 void nsMenuGroupOwnerX::ContentRemoved(nsIContent* aChild, nsIContent* aPreviousSibling) {
86 nsIContent* container = aChild->GetParent();
91 nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this);
92 nsChangeObserver* obs = LookupContentChangeObserver(container);
94 obs->ObserveContentRemoved(aChild->OwnerDoc(), container, aChild, aPreviousSibling);
95 else if (container != mContent) {
96 // We do a lookup on the parent container in case things were removed
97 // under a "menupopup" item. That is basically a wrapper for the contents
99 nsCOMPtr<nsIContent> parent = container->GetParent();
101 obs = LookupContentChangeObserver(parent);
103 obs->ObserveContentRemoved(aChild->OwnerDoc(), aChild->GetParent(), aChild,
109 void nsMenuGroupOwnerX::ContentInserted(nsIContent* aChild) {
110 nsIContent* container = aChild->GetParent();
115 nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this);
116 nsChangeObserver* obs = LookupContentChangeObserver(container);
118 obs->ObserveContentInserted(aChild->OwnerDoc(), container, aChild);
119 else if (container != mContent) {
120 // We do a lookup on the parent container in case things were removed
121 // under a "menupopup" item. That is basically a wrapper for the contents
123 nsCOMPtr<nsIContent> parent = container->GetParent();
125 obs = LookupContentChangeObserver(parent);
126 if (obs) obs->ObserveContentInserted(aChild->OwnerDoc(), container, aChild);
131 void nsMenuGroupOwnerX::ParentChainChanged(nsIContent* aContent) {}
133 // For change management, we don't use a |nsSupportsHashtable| because
134 // we know that the lifetime of all these items is bounded by the
135 // lifetime of the menubar. No need to add any more strong refs to the
136 // picture because the containment hierarchy already uses strong refs.
137 void nsMenuGroupOwnerX::RegisterForContentChanges(nsIContent* aContent,
138 nsChangeObserver* aMenuObject) {
139 if (!mContentToObserverTable.Contains(aContent)) {
140 aContent->AddMutationObserver(this);
142 mContentToObserverTable.Put(aContent, aMenuObject);
145 void nsMenuGroupOwnerX::UnregisterForContentChanges(nsIContent* aContent) {
146 if (mContentToObserverTable.Contains(aContent)) {
147 aContent->RemoveMutationObserver(this);
149 mContentToObserverTable.Remove(aContent);
152 nsChangeObserver* nsMenuGroupOwnerX::LookupContentChangeObserver(nsIContent* aContent) {
153 nsChangeObserver* result;
154 if (mContentToObserverTable.Get(aContent, &result))
160 // Given a menu item, creates a unique 4-character command ID and
161 // maps it to the item. Returns the id for use by the client.
162 uint32_t nsMenuGroupOwnerX::RegisterForCommand(nsMenuItemX* inMenuItem) {
163 // no real need to check for uniqueness. We always start afresh with each
164 // window at 1. Even if we did get close to the reserved Apple command id's,
165 // those don't start until at least ' ', which is integer 538976288. If
166 // we have that many menu items in one window, I think we have other
172 mCommandToMenuObjectTable.Put(mCurrentCommandID, inMenuItem);
174 return mCurrentCommandID;
177 // Removes the mapping between the given 4-character command ID
178 // and its associated menu item.
179 void nsMenuGroupOwnerX::UnregisterCommand(uint32_t inCommandID) {
180 mCommandToMenuObjectTable.Remove(inCommandID);
183 nsMenuItemX* nsMenuGroupOwnerX::GetMenuItemForCommandID(uint32_t inCommandID) {
185 if (mCommandToMenuObjectTable.Get(inCommandID, &result))
191 void nsMenuGroupOwnerX::AddMenuItemInfoToSet(MenuItemInfo* info) { [mInfoSet addObject:info]; }