widget/cocoa/nsMenuX.mm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <dlfcn.h>
michael@0 7
michael@0 8 #include "nsMenuX.h"
michael@0 9 #include "nsMenuItemX.h"
michael@0 10 #include "nsMenuUtilsX.h"
michael@0 11 #include "nsMenuItemIconX.h"
michael@0 12 #include "nsStandaloneNativeMenu.h"
michael@0 13
michael@0 14 #include "nsObjCExceptions.h"
michael@0 15
michael@0 16 #include "nsToolkit.h"
michael@0 17 #include "nsCocoaFeatures.h"
michael@0 18 #include "nsCocoaUtils.h"
michael@0 19 #include "nsCOMPtr.h"
michael@0 20 #include "prinrval.h"
michael@0 21 #include "nsString.h"
michael@0 22 #include "nsReadableUtils.h"
michael@0 23 #include "nsUnicharUtils.h"
michael@0 24 #include "plstr.h"
michael@0 25 #include "nsGkAtoms.h"
michael@0 26 #include "nsCRT.h"
michael@0 27 #include "nsBaseWidget.h"
michael@0 28
michael@0 29 #include "nsIDocument.h"
michael@0 30 #include "nsIContent.h"
michael@0 31 #include "nsIDOMDocument.h"
michael@0 32 #include "nsIDocumentObserver.h"
michael@0 33 #include "nsIComponentManager.h"
michael@0 34 #include "nsIRollupListener.h"
michael@0 35 #include "nsIDOMElement.h"
michael@0 36 #include "nsBindingManager.h"
michael@0 37 #include "nsIServiceManager.h"
michael@0 38 #include "nsXULPopupManager.h"
michael@0 39 #include "nsCxPusher.h"
michael@0 40
michael@0 41 #include "jsapi.h"
michael@0 42 #include "nsIScriptGlobalObject.h"
michael@0 43 #include "nsIScriptContext.h"
michael@0 44 #include "nsIXPConnect.h"
michael@0 45
michael@0 46 #include "mozilla/MouseEvents.h"
michael@0 47
michael@0 48 using namespace mozilla;
michael@0 49
michael@0 50 static bool gConstructingMenu = false;
michael@0 51 static bool gMenuMethodsSwizzled = false;
michael@0 52
michael@0 53 int32_t nsMenuX::sIndexingMenuLevel = 0;
michael@0 54 using mozilla::AutoPushJSContext;
michael@0 55
michael@0 56
michael@0 57 //
michael@0 58 // Objective-C class used for representedObject
michael@0 59 //
michael@0 60
michael@0 61 @implementation MenuItemInfo
michael@0 62
michael@0 63 - (id) initWithMenuGroupOwner:(nsMenuGroupOwnerX *)aMenuGroupOwner
michael@0 64 {
michael@0 65 if ((self = [super init]) != nil) {
michael@0 66 mMenuGroupOwner = nullptr;
michael@0 67 [self setMenuGroupOwner:aMenuGroupOwner];
michael@0 68 }
michael@0 69 return self;
michael@0 70 }
michael@0 71
michael@0 72 - (void) dealloc
michael@0 73 {
michael@0 74 [self setMenuGroupOwner:nullptr];
michael@0 75 [super dealloc];
michael@0 76 }
michael@0 77
michael@0 78 - (nsMenuGroupOwnerX *) menuGroupOwner
michael@0 79 {
michael@0 80 return mMenuGroupOwner;
michael@0 81 }
michael@0 82
michael@0 83 - (void) setMenuGroupOwner:(nsMenuGroupOwnerX *)aMenuGroupOwner
michael@0 84 {
michael@0 85 // weak reference as the nsMenuGroupOwnerX owns all of its sub-objects
michael@0 86 mMenuGroupOwner = aMenuGroupOwner;
michael@0 87 }
michael@0 88
michael@0 89 @end
michael@0 90
michael@0 91
michael@0 92 //
michael@0 93 // nsMenuX
michael@0 94 //
michael@0 95
michael@0 96 nsMenuX::nsMenuX()
michael@0 97 : mVisibleItemsCount(0), mParent(nullptr), mMenuGroupOwner(nullptr),
michael@0 98 mNativeMenu(nil), mNativeMenuItem(nil), mIsEnabled(true),
michael@0 99 mDestroyHandlerCalled(false), mNeedsRebuild(true),
michael@0 100 mConstructed(false), mVisible(true), mXBLAttached(false)
michael@0 101 {
michael@0 102 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
michael@0 103
michael@0 104 if (!gMenuMethodsSwizzled) {
michael@0 105 nsToolkit::SwizzleMethods([NSMenu class], @selector(_addItem:toTable:),
michael@0 106 @selector(nsMenuX_NSMenu_addItem:toTable:), true);
michael@0 107 nsToolkit::SwizzleMethods([NSMenu class], @selector(_removeItem:fromTable:),
michael@0 108 @selector(nsMenuX_NSMenu_removeItem:fromTable:), true);
michael@0 109 // On SnowLeopard the Shortcut framework (which contains the
michael@0 110 // SCTGRLIndex class) is loaded on demand, whenever the user first opens
michael@0 111 // a menu (which normally hasn't happened yet). So we need to load it
michael@0 112 // here explicitly.
michael@0 113 dlopen("/System/Library/PrivateFrameworks/Shortcut.framework/Shortcut", RTLD_LAZY);
michael@0 114 Class SCTGRLIndexClass = ::NSClassFromString(@"SCTGRLIndex");
michael@0 115 nsToolkit::SwizzleMethods(SCTGRLIndexClass, @selector(indexMenuBarDynamically),
michael@0 116 @selector(nsMenuX_SCTGRLIndex_indexMenuBarDynamically));
michael@0 117
michael@0 118 gMenuMethodsSwizzled = true;
michael@0 119 }
michael@0 120
michael@0 121 mMenuDelegate = [[MenuDelegate alloc] initWithGeckoMenu:this];
michael@0 122
michael@0 123 if (!nsMenuBarX::sNativeEventTarget)
michael@0 124 nsMenuBarX::sNativeEventTarget = [[NativeMenuItemTarget alloc] init];
michael@0 125
michael@0 126 MOZ_COUNT_CTOR(nsMenuX);
michael@0 127
michael@0 128 NS_OBJC_END_TRY_ABORT_BLOCK;
michael@0 129 }
michael@0 130
michael@0 131 nsMenuX::~nsMenuX()
michael@0 132 {
michael@0 133 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
michael@0 134
michael@0 135 // Prevent the icon object from outliving us.
michael@0 136 if (mIcon)
michael@0 137 mIcon->Destroy();
michael@0 138
michael@0 139 RemoveAll();
michael@0 140
michael@0 141 [mNativeMenu setDelegate:nil];
michael@0 142 [mNativeMenu release];
michael@0 143 [mMenuDelegate release];
michael@0 144 // autorelease the native menu item so that anything else happening to this
michael@0 145 // object happens before the native menu item actually dies
michael@0 146 [mNativeMenuItem autorelease];
michael@0 147
michael@0 148 // alert the change notifier we don't care no more
michael@0 149 if (mContent)
michael@0 150 mMenuGroupOwner->UnregisterForContentChanges(mContent);
michael@0 151
michael@0 152 MOZ_COUNT_DTOR(nsMenuX);
michael@0 153
michael@0 154 NS_OBJC_END_TRY_ABORT_BLOCK;
michael@0 155 }
michael@0 156
michael@0 157 nsresult nsMenuX::Create(nsMenuObjectX* aParent, nsMenuGroupOwnerX* aMenuGroupOwner, nsIContent* aNode)
michael@0 158 {
michael@0 159 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 160
michael@0 161 mContent = aNode;
michael@0 162 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::label, mLabel);
michael@0 163 mNativeMenu = CreateMenuWithGeckoString(mLabel);
michael@0 164
michael@0 165 // register this menu to be notified when changes are made to our content object
michael@0 166 mMenuGroupOwner = aMenuGroupOwner; // weak ref
michael@0 167 NS_ASSERTION(mMenuGroupOwner, "No menu owner given, must have one");
michael@0 168 mMenuGroupOwner->RegisterForContentChanges(mContent, this);
michael@0 169
michael@0 170 mParent = aParent;
michael@0 171 // our parent could be either a menu bar (if we're toplevel) or a menu (if we're a submenu)
michael@0 172
michael@0 173 #ifdef DEBUG
michael@0 174 nsMenuObjectTypeX parentType =
michael@0 175 #endif
michael@0 176 mParent->MenuObjectType();
michael@0 177 NS_ASSERTION((parentType == eMenuBarObjectType || parentType == eSubmenuObjectType || parentType == eStandaloneNativeMenuObjectType),
michael@0 178 "Menu parent not a menu bar, menu, or native menu!");
michael@0 179
michael@0 180 if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(mContent))
michael@0 181 mVisible = false;
michael@0 182 if (mContent->GetChildCount() == 0)
michael@0 183 mVisible = false;
michael@0 184
michael@0 185 NSString *newCocoaLabelString = nsMenuUtilsX::GetTruncatedCocoaLabel(mLabel);
michael@0 186 mNativeMenuItem = [[NSMenuItem alloc] initWithTitle:newCocoaLabelString action:nil keyEquivalent:@""];
michael@0 187 [mNativeMenuItem setSubmenu:mNativeMenu];
michael@0 188
michael@0 189 SetEnabled(!mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
michael@0 190 nsGkAtoms::_true, eCaseMatters));
michael@0 191
michael@0 192 // We call MenuConstruct here because keyboard commands are dependent upon
michael@0 193 // native menu items being created. If we only call MenuConstruct when a menu
michael@0 194 // is actually selected, then we can't access keyboard commands until the
michael@0 195 // menu gets selected, which is bad.
michael@0 196 MenuConstruct();
michael@0 197
michael@0 198 mIcon = new nsMenuItemIconX(this, mContent, mNativeMenuItem);
michael@0 199
michael@0 200 return NS_OK;
michael@0 201
michael@0 202 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 203 }
michael@0 204
michael@0 205 nsresult nsMenuX::AddMenuItem(nsMenuItemX* aMenuItem)
michael@0 206 {
michael@0 207 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 208
michael@0 209 if (!aMenuItem)
michael@0 210 return NS_ERROR_INVALID_ARG;
michael@0 211
michael@0 212 mMenuObjectsArray.AppendElement(aMenuItem);
michael@0 213 if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(aMenuItem->Content()))
michael@0 214 return NS_OK;
michael@0 215 ++mVisibleItemsCount;
michael@0 216
michael@0 217 NSMenuItem* newNativeMenuItem = (NSMenuItem*)aMenuItem->NativeData();
michael@0 218
michael@0 219 // add the menu item to this menu
michael@0 220 [mNativeMenu addItem:newNativeMenuItem];
michael@0 221
michael@0 222 // set up target/action
michael@0 223 [newNativeMenuItem setTarget:nsMenuBarX::sNativeEventTarget];
michael@0 224 [newNativeMenuItem setAction:@selector(menuItemHit:)];
michael@0 225
michael@0 226 // set its command. we get the unique command id from the menubar
michael@0 227 [newNativeMenuItem setTag:mMenuGroupOwner->RegisterForCommand(aMenuItem)];
michael@0 228 MenuItemInfo * info = [[MenuItemInfo alloc] initWithMenuGroupOwner:mMenuGroupOwner];
michael@0 229 [newNativeMenuItem setRepresentedObject:info];
michael@0 230 [info release];
michael@0 231
michael@0 232 return NS_OK;
michael@0 233
michael@0 234 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 235 }
michael@0 236
michael@0 237 nsresult nsMenuX::AddMenu(nsMenuX* aMenu)
michael@0 238 {
michael@0 239 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 240
michael@0 241 // Add a submenu
michael@0 242 if (!aMenu)
michael@0 243 return NS_ERROR_NULL_POINTER;
michael@0 244
michael@0 245 mMenuObjectsArray.AppendElement(aMenu);
michael@0 246 if (nsMenuUtilsX::NodeIsHiddenOrCollapsed(aMenu->Content()))
michael@0 247 return NS_OK;
michael@0 248 ++mVisibleItemsCount;
michael@0 249
michael@0 250 // We have to add a menu item and then associate the menu with it
michael@0 251 NSMenuItem* newNativeMenuItem = aMenu->NativeMenuItem();
michael@0 252 if (!newNativeMenuItem)
michael@0 253 return NS_ERROR_FAILURE;
michael@0 254 [mNativeMenu addItem:newNativeMenuItem];
michael@0 255
michael@0 256 [newNativeMenuItem setSubmenu:(NSMenu*)aMenu->NativeData()];
michael@0 257
michael@0 258 return NS_OK;
michael@0 259
michael@0 260 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 261 }
michael@0 262
michael@0 263 // Includes all items, including hidden/collapsed ones
michael@0 264 uint32_t nsMenuX::GetItemCount()
michael@0 265 {
michael@0 266 return mMenuObjectsArray.Length();
michael@0 267 }
michael@0 268
michael@0 269 // Includes all items, including hidden/collapsed ones
michael@0 270 nsMenuObjectX* nsMenuX::GetItemAt(uint32_t aPos)
michael@0 271 {
michael@0 272 if (aPos >= (uint32_t)mMenuObjectsArray.Length())
michael@0 273 return NULL;
michael@0 274
michael@0 275 return mMenuObjectsArray[aPos];
michael@0 276 }
michael@0 277
michael@0 278 // Only includes visible items
michael@0 279 nsresult nsMenuX::GetVisibleItemCount(uint32_t &aCount)
michael@0 280 {
michael@0 281 aCount = mVisibleItemsCount;
michael@0 282 return NS_OK;
michael@0 283 }
michael@0 284
michael@0 285 // Only includes visible items. Note that this is provides O(N) access
michael@0 286 // If you need to iterate or search, consider using GetItemAt and doing your own filtering
michael@0 287 nsMenuObjectX* nsMenuX::GetVisibleItemAt(uint32_t aPos)
michael@0 288 {
michael@0 289
michael@0 290 uint32_t count = mMenuObjectsArray.Length();
michael@0 291 if (aPos >= mVisibleItemsCount || aPos >= count)
michael@0 292 return NULL;
michael@0 293
michael@0 294 // If there are no invisible items, can provide direct access
michael@0 295 if (mVisibleItemsCount == count)
michael@0 296 return mMenuObjectsArray[aPos];
michael@0 297
michael@0 298 // Otherwise, traverse the array until we find the the item we're looking for.
michael@0 299 nsMenuObjectX* item;
michael@0 300 uint32_t visibleNodeIndex = 0;
michael@0 301 for (uint32_t i = 0; i < count; i++) {
michael@0 302 item = mMenuObjectsArray[i];
michael@0 303 if (!nsMenuUtilsX::NodeIsHiddenOrCollapsed(item->Content())) {
michael@0 304 if (aPos == visibleNodeIndex) {
michael@0 305 // we found the visible node we're looking for, return it
michael@0 306 return item;
michael@0 307 }
michael@0 308 visibleNodeIndex++;
michael@0 309 }
michael@0 310 }
michael@0 311
michael@0 312 return NULL;
michael@0 313 }
michael@0 314
michael@0 315 nsresult nsMenuX::RemoveAll()
michael@0 316 {
michael@0 317 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
michael@0 318
michael@0 319 if (mNativeMenu) {
michael@0 320 // clear command id's
michael@0 321 int itemCount = [mNativeMenu numberOfItems];
michael@0 322 for (int i = 0; i < itemCount; i++)
michael@0 323 mMenuGroupOwner->UnregisterCommand((uint32_t)[[mNativeMenu itemAtIndex:i] tag]);
michael@0 324 // get rid of Cocoa menu items
michael@0 325 for (int i = [mNativeMenu numberOfItems] - 1; i >= 0; i--)
michael@0 326 [mNativeMenu removeItemAtIndex:i];
michael@0 327 }
michael@0 328
michael@0 329 mMenuObjectsArray.Clear();
michael@0 330 mVisibleItemsCount = 0;
michael@0 331
michael@0 332 return NS_OK;
michael@0 333
michael@0 334 NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
michael@0 335 }
michael@0 336
michael@0 337 nsEventStatus nsMenuX::MenuOpened()
michael@0 338 {
michael@0 339 // Open the node.
michael@0 340 mContent->SetAttr(kNameSpaceID_None, nsGkAtoms::open, NS_LITERAL_STRING("true"), true);
michael@0 341
michael@0 342 // Fire a handler. If we're told to stop, don't build the menu at all
michael@0 343 bool keepProcessing = OnOpen();
michael@0 344
michael@0 345 if (!mNeedsRebuild || !keepProcessing)
michael@0 346 return nsEventStatus_eConsumeNoDefault;
michael@0 347
michael@0 348 if (!mConstructed || mNeedsRebuild) {
michael@0 349 if (mNeedsRebuild)
michael@0 350 RemoveAll();
michael@0 351
michael@0 352 MenuConstruct();
michael@0 353 mConstructed = true;
michael@0 354 }
michael@0 355
michael@0 356 nsEventStatus status = nsEventStatus_eIgnore;
michael@0 357 WidgetMouseEvent event(true, NS_XUL_POPUP_SHOWN, nullptr,
michael@0 358 WidgetMouseEvent::eReal);
michael@0 359
michael@0 360 nsCOMPtr<nsIContent> popupContent;
michael@0 361 GetMenuPopupContent(getter_AddRefs(popupContent));
michael@0 362 nsIContent* dispatchTo = popupContent ? popupContent : mContent;
michael@0 363 dispatchTo->DispatchDOMEvent(&event, nullptr, nullptr, &status);
michael@0 364
michael@0 365 return nsEventStatus_eConsumeNoDefault;
michael@0 366 }
michael@0 367
michael@0 368 void nsMenuX::MenuClosed()
michael@0 369 {
michael@0 370 if (mConstructed) {
michael@0 371 // Don't close if a handler tells us to stop.
michael@0 372 if (!OnClose())
michael@0 373 return;
michael@0 374
michael@0 375 if (mNeedsRebuild)
michael@0 376 mConstructed = false;
michael@0 377
michael@0 378 mContent->UnsetAttr(kNameSpaceID_None, nsGkAtoms::open, true);
michael@0 379
michael@0 380 nsEventStatus status = nsEventStatus_eIgnore;
michael@0 381 WidgetMouseEvent event(true, NS_XUL_POPUP_HIDDEN, nullptr,
michael@0 382 WidgetMouseEvent::eReal);
michael@0 383
michael@0 384 nsCOMPtr<nsIContent> popupContent;
michael@0 385 GetMenuPopupContent(getter_AddRefs(popupContent));
michael@0 386 nsIContent* dispatchTo = popupContent ? popupContent : mContent;
michael@0 387 dispatchTo->DispatchDOMEvent(&event, nullptr, nullptr, &status);
michael@0 388
michael@0 389 mDestroyHandlerCalled = true;
michael@0 390 mConstructed = false;
michael@0 391 }
michael@0 392 }
michael@0 393
michael@0 394 void nsMenuX::MenuConstruct()
michael@0 395 {
michael@0 396 mConstructed = false;
michael@0 397 gConstructingMenu = true;
michael@0 398
michael@0 399 // reset destroy handler flag so that we'll know to fire it next time this menu goes away.
michael@0 400 mDestroyHandlerCalled = false;
michael@0 401
michael@0 402 //printf("nsMenuX::MenuConstruct called for %s = %d \n", NS_LossyConvertUTF16toASCII(mLabel).get(), mNativeMenu);
michael@0 403
michael@0 404 // Retrieve our menupopup.
michael@0 405 nsCOMPtr<nsIContent> menuPopup;
michael@0 406 GetMenuPopupContent(getter_AddRefs(menuPopup));
michael@0 407 if (!menuPopup) {
michael@0 408 gConstructingMenu = false;
michael@0 409 return;
michael@0 410 }
michael@0 411
michael@0 412 // bug 365405: Manually wrap the menupopup node to make sure it's bounded
michael@0 413 if (!mXBLAttached) {
michael@0 414 nsresult rv;
michael@0 415 nsCOMPtr<nsIXPConnect> xpconnect =
michael@0 416 do_GetService(nsIXPConnect::GetCID(), &rv);
michael@0 417 if (NS_SUCCEEDED(rv)) {
michael@0 418 nsIDocument* ownerDoc = menuPopup->OwnerDoc();
michael@0 419 nsCOMPtr<nsIScriptGlobalObject> sgo;
michael@0 420 if (ownerDoc && (sgo = do_QueryInterface(ownerDoc->GetWindow()))) {
michael@0 421 nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
michael@0 422 JSObject* global = sgo->GetGlobalJSObject();
michael@0 423 if (scriptContext && global) {
michael@0 424 AutoPushJSContext cx(scriptContext->GetNativeContext());
michael@0 425 if (cx) {
michael@0 426 nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
michael@0 427 xpconnect->WrapNative(cx, global,
michael@0 428 menuPopup, NS_GET_IID(nsISupports),
michael@0 429 getter_AddRefs(wrapper));
michael@0 430 mXBLAttached = true;
michael@0 431 }
michael@0 432 }
michael@0 433 }
michael@0 434 }
michael@0 435 }
michael@0 436
michael@0 437 // Iterate over the kids
michael@0 438 uint32_t count = menuPopup->GetChildCount();
michael@0 439 for (uint32_t i = 0; i < count; i++) {
michael@0 440 nsIContent *child = menuPopup->GetChildAt(i);
michael@0 441 if (child) {
michael@0 442 // depending on the type, create a menu item, separator, or submenu
michael@0 443 nsIAtom *tag = child->Tag();
michael@0 444 if (tag == nsGkAtoms::menuitem || tag == nsGkAtoms::menuseparator)
michael@0 445 LoadMenuItem(child);
michael@0 446 else if (tag == nsGkAtoms::menu)
michael@0 447 LoadSubMenu(child);
michael@0 448 }
michael@0 449 } // for each menu item
michael@0 450
michael@0 451 gConstructingMenu = false;
michael@0 452 mNeedsRebuild = false;
michael@0 453 // printf("Done building, mMenuObjectsArray.Count() = %d \n", mMenuObjectsArray.Count());
michael@0 454 }
michael@0 455
michael@0 456 void nsMenuX::SetRebuild(bool aNeedsRebuild)
michael@0 457 {
michael@0 458 if (!gConstructingMenu)
michael@0 459 mNeedsRebuild = aNeedsRebuild;
michael@0 460 }
michael@0 461
michael@0 462 nsresult nsMenuX::SetEnabled(bool aIsEnabled)
michael@0 463 {
michael@0 464 if (aIsEnabled != mIsEnabled) {
michael@0 465 // we always want to rebuild when this changes
michael@0 466 mIsEnabled = aIsEnabled;
michael@0 467 [mNativeMenuItem setEnabled:(BOOL)mIsEnabled];
michael@0 468 }
michael@0 469 return NS_OK;
michael@0 470 }
michael@0 471
michael@0 472 nsresult nsMenuX::GetEnabled(bool* aIsEnabled)
michael@0 473 {
michael@0 474 NS_ENSURE_ARG_POINTER(aIsEnabled);
michael@0 475 *aIsEnabled = mIsEnabled;
michael@0 476 return NS_OK;
michael@0 477 }
michael@0 478
michael@0 479 GeckoNSMenu* nsMenuX::CreateMenuWithGeckoString(nsString& menuTitle)
michael@0 480 {
michael@0 481 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
michael@0 482
michael@0 483 NSString* title = [NSString stringWithCharacters:(UniChar*)menuTitle.get() length:menuTitle.Length()];
michael@0 484 GeckoNSMenu* myMenu = [[GeckoNSMenu alloc] initWithTitle:title];
michael@0 485 [myMenu setDelegate:mMenuDelegate];
michael@0 486
michael@0 487 // We don't want this menu to auto-enable menu items because then Cocoa
michael@0 488 // overrides our decisions and things get incorrectly enabled/disabled.
michael@0 489 [myMenu setAutoenablesItems:NO];
michael@0 490
michael@0 491 // we used to install Carbon event handlers here, but since NSMenu* doesn't
michael@0 492 // create its underlying MenuRef until just before display, we delay until
michael@0 493 // that happens. Now we install the event handlers when Cocoa notifies
michael@0 494 // us that a menu is about to display - see the Cocoa MenuDelegate class.
michael@0 495
michael@0 496 return myMenu;
michael@0 497
michael@0 498 NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
michael@0 499 }
michael@0 500
michael@0 501 void nsMenuX::LoadMenuItem(nsIContent* inMenuItemContent)
michael@0 502 {
michael@0 503 if (!inMenuItemContent)
michael@0 504 return;
michael@0 505
michael@0 506 nsAutoString menuitemName;
michael@0 507 inMenuItemContent->GetAttr(kNameSpaceID_None, nsGkAtoms::label, menuitemName);
michael@0 508
michael@0 509 // printf("menuitem %s \n", NS_LossyConvertUTF16toASCII(menuitemName).get());
michael@0 510
michael@0 511 EMenuItemType itemType = eRegularMenuItemType;
michael@0 512 if (inMenuItemContent->Tag() == nsGkAtoms::menuseparator) {
michael@0 513 itemType = eSeparatorMenuItemType;
michael@0 514 }
michael@0 515 else {
michael@0 516 static nsIContent::AttrValuesArray strings[] =
michael@0 517 {&nsGkAtoms::checkbox, &nsGkAtoms::radio, nullptr};
michael@0 518 switch (inMenuItemContent->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::type,
michael@0 519 strings, eCaseMatters)) {
michael@0 520 case 0: itemType = eCheckboxMenuItemType; break;
michael@0 521 case 1: itemType = eRadioMenuItemType; break;
michael@0 522 }
michael@0 523 }
michael@0 524
michael@0 525 // Create the item.
michael@0 526 nsMenuItemX* menuItem = new nsMenuItemX();
michael@0 527 if (!menuItem)
michael@0 528 return;
michael@0 529
michael@0 530 nsresult rv = menuItem->Create(this, menuitemName, itemType, mMenuGroupOwner, inMenuItemContent);
michael@0 531 if (NS_FAILED(rv)) {
michael@0 532 delete menuItem;
michael@0 533 return;
michael@0 534 }
michael@0 535
michael@0 536 AddMenuItem(menuItem);
michael@0 537
michael@0 538 // This needs to happen after the nsIMenuItem object is inserted into
michael@0 539 // our item array in AddMenuItem()
michael@0 540 menuItem->SetupIcon();
michael@0 541 }
michael@0 542
michael@0 543 void nsMenuX::LoadSubMenu(nsIContent* inMenuContent)
michael@0 544 {
michael@0 545 nsAutoPtr<nsMenuX> menu(new nsMenuX());
michael@0 546 if (!menu)
michael@0 547 return;
michael@0 548
michael@0 549 nsresult rv = menu->Create(this, mMenuGroupOwner, inMenuContent);
michael@0 550 if (NS_FAILED(rv))
michael@0 551 return;
michael@0 552
michael@0 553 AddMenu(menu);
michael@0 554
michael@0 555 // This needs to happen after the nsIMenu object is inserted into
michael@0 556 // our item array in AddMenu()
michael@0 557 menu->SetupIcon();
michael@0 558
michael@0 559 menu.forget();
michael@0 560 }
michael@0 561
michael@0 562 // This menu is about to open. Returns TRUE if we should keep processing the event,
michael@0 563 // FALSE if the handler wants to stop the opening of the menu.
michael@0 564 bool nsMenuX::OnOpen()
michael@0 565 {
michael@0 566 nsEventStatus status = nsEventStatus_eIgnore;
michael@0 567 WidgetMouseEvent event(true, NS_XUL_POPUP_SHOWING, nullptr,
michael@0 568 WidgetMouseEvent::eReal);
michael@0 569
michael@0 570 nsCOMPtr<nsIContent> popupContent;
michael@0 571 GetMenuPopupContent(getter_AddRefs(popupContent));
michael@0 572
michael@0 573 nsresult rv = NS_OK;
michael@0 574 nsIContent* dispatchTo = popupContent ? popupContent : mContent;
michael@0 575 rv = dispatchTo->DispatchDOMEvent(&event, nullptr, nullptr, &status);
michael@0 576 if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
michael@0 577 return false;
michael@0 578
michael@0 579 // If the open is going to succeed we need to walk our menu items, checking to
michael@0 580 // see if any of them have a command attribute. If so, several attributes
michael@0 581 // must potentially be updated.
michael@0 582
michael@0 583 // Get new popup content first since it might have changed as a result of the
michael@0 584 // NS_XUL_POPUP_SHOWING event above.
michael@0 585 GetMenuPopupContent(getter_AddRefs(popupContent));
michael@0 586 if (!popupContent)
michael@0 587 return true;
michael@0 588
michael@0 589 nsXULPopupManager* pm = nsXULPopupManager::GetInstance();
michael@0 590 if (pm) {
michael@0 591 pm->UpdateMenuItems(popupContent);
michael@0 592 }
michael@0 593
michael@0 594 return true;
michael@0 595 }
michael@0 596
michael@0 597 // Returns TRUE if we should keep processing the event, FALSE if the handler
michael@0 598 // wants to stop the closing of the menu.
michael@0 599 bool nsMenuX::OnClose()
michael@0 600 {
michael@0 601 if (mDestroyHandlerCalled)
michael@0 602 return true;
michael@0 603
michael@0 604 nsEventStatus status = nsEventStatus_eIgnore;
michael@0 605 WidgetMouseEvent event(true, NS_XUL_POPUP_HIDING, nullptr,
michael@0 606 WidgetMouseEvent::eReal);
michael@0 607
michael@0 608 nsCOMPtr<nsIContent> popupContent;
michael@0 609 GetMenuPopupContent(getter_AddRefs(popupContent));
michael@0 610
michael@0 611 nsresult rv = NS_OK;
michael@0 612 nsIContent* dispatchTo = popupContent ? popupContent : mContent;
michael@0 613 rv = dispatchTo->DispatchDOMEvent(&event, nullptr, nullptr, &status);
michael@0 614
michael@0 615 mDestroyHandlerCalled = true;
michael@0 616
michael@0 617 if (NS_FAILED(rv) || status == nsEventStatus_eConsumeNoDefault)
michael@0 618 return false;
michael@0 619
michael@0 620 return true;
michael@0 621 }
michael@0 622
michael@0 623 // Find the |menupopup| child in the |popup| representing this menu. It should be one
michael@0 624 // of a very few children so we won't be iterating over a bazillion menu items to find
michael@0 625 // it (so the strcmp won't kill us).
michael@0 626 void nsMenuX::GetMenuPopupContent(nsIContent** aResult)
michael@0 627 {
michael@0 628 if (!aResult)
michael@0 629 return;
michael@0 630 *aResult = nullptr;
michael@0 631
michael@0 632 // Check to see if we are a "menupopup" node (if we are a native menu).
michael@0 633 {
michael@0 634 int32_t dummy;
michael@0 635 nsCOMPtr<nsIAtom> tag = mContent->OwnerDoc()->BindingManager()->ResolveTag(mContent, &dummy);
michael@0 636 if (tag == nsGkAtoms::menupopup) {
michael@0 637 *aResult = mContent;
michael@0 638 NS_ADDREF(*aResult);
michael@0 639 return;
michael@0 640 }
michael@0 641 }
michael@0 642
michael@0 643 // Otherwise check our child nodes.
michael@0 644
michael@0 645 uint32_t count = mContent->GetChildCount();
michael@0 646
michael@0 647 for (uint32_t i = 0; i < count; i++) {
michael@0 648 int32_t dummy;
michael@0 649 nsIContent *child = mContent->GetChildAt(i);
michael@0 650 nsCOMPtr<nsIAtom> tag = child->OwnerDoc()->BindingManager()->ResolveTag(child, &dummy);
michael@0 651 if (tag == nsGkAtoms::menupopup) {
michael@0 652 *aResult = child;
michael@0 653 NS_ADDREF(*aResult);
michael@0 654 return;
michael@0 655 }
michael@0 656 }
michael@0 657 }
michael@0 658
michael@0 659 NSMenuItem* nsMenuX::NativeMenuItem()
michael@0 660 {
michael@0 661 return mNativeMenuItem;
michael@0 662 }
michael@0 663
michael@0 664 bool nsMenuX::IsXULHelpMenu(nsIContent* aMenuContent)
michael@0 665 {
michael@0 666 bool retval = false;
michael@0 667 if (aMenuContent) {
michael@0 668 nsAutoString id;
michael@0 669 aMenuContent->GetAttr(kNameSpaceID_None, nsGkAtoms::id, id);
michael@0 670 if (id.Equals(NS_LITERAL_STRING("helpMenu")))
michael@0 671 retval = true;
michael@0 672 }
michael@0 673 return retval;
michael@0 674 }
michael@0 675
michael@0 676 //
michael@0 677 // nsChangeObserver
michael@0 678 //
michael@0 679
michael@0 680 void nsMenuX::ObserveAttributeChanged(nsIDocument *aDocument, nsIContent *aContent,
michael@0 681 nsIAtom *aAttribute)
michael@0 682 {
michael@0 683 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
michael@0 684
michael@0 685 // ignore the |open| attribute, which is by far the most common
michael@0 686 if (gConstructingMenu || (aAttribute == nsGkAtoms::open))
michael@0 687 return;
michael@0 688
michael@0 689 nsMenuObjectTypeX parentType = mParent->MenuObjectType();
michael@0 690
michael@0 691 if (aAttribute == nsGkAtoms::disabled) {
michael@0 692 SetEnabled(!mContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::disabled,
michael@0 693 nsGkAtoms::_true, eCaseMatters));
michael@0 694 }
michael@0 695 else if (aAttribute == nsGkAtoms::label) {
michael@0 696 mContent->GetAttr(kNameSpaceID_None, nsGkAtoms::label, mLabel);
michael@0 697
michael@0 698 // invalidate my parent. If we're a submenu parent, we have to rebuild
michael@0 699 // the parent menu in order for the changes to be picked up. If we're
michael@0 700 // a regular menu, just change the title and redraw the menubar.
michael@0 701 if (parentType == eMenuBarObjectType) {
michael@0 702 // reuse the existing menu, to avoid rebuilding the root menu bar.
michael@0 703 NS_ASSERTION(mNativeMenu, "nsMenuX::AttributeChanged: invalid menu handle.");
michael@0 704 NSString *newCocoaLabelString = nsMenuUtilsX::GetTruncatedCocoaLabel(mLabel);
michael@0 705 [mNativeMenu setTitle:newCocoaLabelString];
michael@0 706 }
michael@0 707 else if (parentType == eSubmenuObjectType) {
michael@0 708 static_cast<nsMenuX*>(mParent)->SetRebuild(true);
michael@0 709 }
michael@0 710 else if (parentType == eStandaloneNativeMenuObjectType) {
michael@0 711 static_cast<nsStandaloneNativeMenu*>(mParent)->GetMenuXObject()->SetRebuild(true);
michael@0 712 }
michael@0 713 }
michael@0 714 else if (aAttribute == nsGkAtoms::hidden || aAttribute == nsGkAtoms::collapsed) {
michael@0 715 SetRebuild(true);
michael@0 716
michael@0 717 bool contentIsHiddenOrCollapsed = nsMenuUtilsX::NodeIsHiddenOrCollapsed(mContent);
michael@0 718
michael@0 719 // don't do anything if the state is correct already
michael@0 720 if (contentIsHiddenOrCollapsed != mVisible)
michael@0 721 return;
michael@0 722
michael@0 723 if (contentIsHiddenOrCollapsed) {
michael@0 724 if (parentType == eMenuBarObjectType ||
michael@0 725 parentType == eSubmenuObjectType ||
michael@0 726 parentType == eStandaloneNativeMenuObjectType) {
michael@0 727 NSMenu* parentMenu = (NSMenu*)mParent->NativeData();
michael@0 728 // An exception will get thrown if we try to remove an item that isn't
michael@0 729 // in the menu.
michael@0 730 if ([parentMenu indexOfItem:mNativeMenuItem] != -1)
michael@0 731 [parentMenu removeItem:mNativeMenuItem];
michael@0 732 mVisible = false;
michael@0 733 }
michael@0 734 }
michael@0 735 else {
michael@0 736 if (parentType == eMenuBarObjectType ||
michael@0 737 parentType == eSubmenuObjectType ||
michael@0 738 parentType == eStandaloneNativeMenuObjectType) {
michael@0 739 int insertionIndex = nsMenuUtilsX::CalculateNativeInsertionPoint(mParent, this);
michael@0 740 if (parentType == eMenuBarObjectType) {
michael@0 741 // Before inserting we need to figure out if we should take the native
michael@0 742 // application menu into account.
michael@0 743 nsMenuBarX* mb = static_cast<nsMenuBarX*>(mParent);
michael@0 744 if (mb->MenuContainsAppMenu())
michael@0 745 insertionIndex++;
michael@0 746 }
michael@0 747 NSMenu* parentMenu = (NSMenu*)mParent->NativeData();
michael@0 748 [parentMenu insertItem:mNativeMenuItem atIndex:insertionIndex];
michael@0 749 [mNativeMenuItem setSubmenu:mNativeMenu];
michael@0 750 mVisible = true;
michael@0 751 }
michael@0 752 }
michael@0 753 }
michael@0 754 else if (aAttribute == nsGkAtoms::image) {
michael@0 755 SetupIcon();
michael@0 756 }
michael@0 757
michael@0 758 NS_OBJC_END_TRY_ABORT_BLOCK;
michael@0 759 }
michael@0 760
michael@0 761 void nsMenuX::ObserveContentRemoved(nsIDocument *aDocument, nsIContent *aChild,
michael@0 762 int32_t aIndexInContainer)
michael@0 763 {
michael@0 764 if (gConstructingMenu)
michael@0 765 return;
michael@0 766
michael@0 767 SetRebuild(true);
michael@0 768 mMenuGroupOwner->UnregisterForContentChanges(aChild);
michael@0 769 }
michael@0 770
michael@0 771 void nsMenuX::ObserveContentInserted(nsIDocument *aDocument, nsIContent* aContainer,
michael@0 772 nsIContent *aChild)
michael@0 773 {
michael@0 774 if (gConstructingMenu)
michael@0 775 return;
michael@0 776
michael@0 777 SetRebuild(true);
michael@0 778 }
michael@0 779
michael@0 780 nsresult nsMenuX::SetupIcon()
michael@0 781 {
michael@0 782 // In addition to out-of-memory, menus that are children of the menu bar
michael@0 783 // will not have mIcon set.
michael@0 784 if (!mIcon)
michael@0 785 return NS_ERROR_OUT_OF_MEMORY;
michael@0 786
michael@0 787 return mIcon->SetupIcon();
michael@0 788 }
michael@0 789
michael@0 790 //
michael@0 791 // MenuDelegate Objective-C class, used to set up Carbon events
michael@0 792 //
michael@0 793
michael@0 794 @implementation MenuDelegate
michael@0 795
michael@0 796 - (id)initWithGeckoMenu:(nsMenuX*)geckoMenu
michael@0 797 {
michael@0 798 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
michael@0 799
michael@0 800 if ((self = [super init])) {
michael@0 801 NS_ASSERTION(geckoMenu, "Cannot initialize native menu delegate with NULL gecko menu! Will crash!");
michael@0 802 mGeckoMenu = geckoMenu;
michael@0 803 }
michael@0 804 return self;
michael@0 805
michael@0 806 NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
michael@0 807 }
michael@0 808
michael@0 809 - (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item
michael@0 810 {
michael@0 811 if (!menu || !item || !mGeckoMenu)
michael@0 812 return;
michael@0 813
michael@0 814 nsMenuObjectX* target = mGeckoMenu->GetVisibleItemAt((uint32_t)[menu indexOfItem:item]);
michael@0 815 if (target && (target->MenuObjectType() == eMenuItemObjectType)) {
michael@0 816 nsMenuItemX* targetMenuItem = static_cast<nsMenuItemX*>(target);
michael@0 817 bool handlerCalledPreventDefault; // but we don't actually care
michael@0 818 targetMenuItem->DispatchDOMEvent(NS_LITERAL_STRING("DOMMenuItemActive"), &handlerCalledPreventDefault);
michael@0 819 }
michael@0 820 }
michael@0 821
michael@0 822 - (void)menuWillOpen:(NSMenu *)menu
michael@0 823 {
michael@0 824 if (!mGeckoMenu)
michael@0 825 return;
michael@0 826
michael@0 827 // Don't do anything while the OS is (re)indexing our menus (on Leopard and
michael@0 828 // higher). This stops the Help menu from being able to search in our
michael@0 829 // menus, but it also resolves many other problems.
michael@0 830 if (nsMenuX::sIndexingMenuLevel > 0)
michael@0 831 return;
michael@0 832
michael@0 833 nsIRollupListener* rollupListener = nsBaseWidget::GetActiveRollupListener();
michael@0 834 if (rollupListener) {
michael@0 835 nsCOMPtr<nsIWidget> rollupWidget = rollupListener->GetRollupWidget();
michael@0 836 if (rollupWidget) {
michael@0 837 rollupListener->Rollup(0, nullptr, nullptr);
michael@0 838 [menu cancelTracking];
michael@0 839 return;
michael@0 840 }
michael@0 841 }
michael@0 842 mGeckoMenu->MenuOpened();
michael@0 843 }
michael@0 844
michael@0 845 - (void)menuDidClose:(NSMenu *)menu
michael@0 846 {
michael@0 847 if (!mGeckoMenu)
michael@0 848 return;
michael@0 849
michael@0 850 // Don't do anything while the OS is (re)indexing our menus (on Leopard and
michael@0 851 // higher). This stops the Help menu from being able to search in our
michael@0 852 // menus, but it also resolves many other problems.
michael@0 853 if (nsMenuX::sIndexingMenuLevel > 0)
michael@0 854 return;
michael@0 855
michael@0 856 mGeckoMenu->MenuClosed();
michael@0 857 }
michael@0 858
michael@0 859 @end
michael@0 860
michael@0 861 // OS X Leopard (at least as of 10.5.2) has an obscure bug triggered by some
michael@0 862 // behavior that's present in Mozilla.org browsers but not (as best I can
michael@0 863 // tell) in Apple products like Safari. (It's not yet clear exactly what this
michael@0 864 // behavior is.)
michael@0 865 //
michael@0 866 // The bug is that sometimes you crash on quit in nsMenuX::RemoveAll(), on a
michael@0 867 // call to [NSMenu removeItemAtIndex:]. The crash is caused by trying to
michael@0 868 // access a deleted NSMenuItem object (sometimes (perhaps always?) by trying
michael@0 869 // to send it a _setChangedFlags: message). Though this object was deleted
michael@0 870 // some time ago, it remains registered as a potential target for a particular
michael@0 871 // key equivalent. So when [NSMenu removeItemAtIndex:] removes the current
michael@0 872 // target for that same key equivalent, the OS tries to "activate" the
michael@0 873 // previous target.
michael@0 874 //
michael@0 875 // The underlying reason appears to be that NSMenu's _addItem:toTable: and
michael@0 876 // _removeItem:fromTable: methods (which are used to keep a hashtable of
michael@0 877 // registered key equivalents) don't properly "retain" and "release"
michael@0 878 // NSMenuItem objects as they are added to and removed from the hashtable.
michael@0 879 //
michael@0 880 // Our (hackish) workaround is to shadow the OS's hashtable with another
michael@0 881 // hastable of our own (gShadowKeyEquivDB), and use it to "retain" and
michael@0 882 // "release" NSMenuItem objects as needed. This resolves bmo bugs 422287 and
michael@0 883 // 423669. When (if) Apple fixes this bug, we can remove this workaround.
michael@0 884
michael@0 885 static NSMutableDictionary *gShadowKeyEquivDB = nil;
michael@0 886
michael@0 887 // Class for values in gShadowKeyEquivDB.
michael@0 888
michael@0 889 @interface KeyEquivDBItem : NSObject
michael@0 890 {
michael@0 891 NSMenuItem *mItem;
michael@0 892 NSMutableSet *mTables;
michael@0 893 }
michael@0 894
michael@0 895 - (id)initWithItem:(NSMenuItem *)aItem table:(NSMapTable *)aTable;
michael@0 896 - (BOOL)hasTable:(NSMapTable *)aTable;
michael@0 897 - (int)addTable:(NSMapTable *)aTable;
michael@0 898 - (int)removeTable:(NSMapTable *)aTable;
michael@0 899
michael@0 900 @end
michael@0 901
michael@0 902 @implementation KeyEquivDBItem
michael@0 903
michael@0 904 - (id)initWithItem:(NSMenuItem *)aItem table:(NSMapTable *)aTable
michael@0 905 {
michael@0 906 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
michael@0 907
michael@0 908 if (!gShadowKeyEquivDB)
michael@0 909 gShadowKeyEquivDB = [[NSMutableDictionary alloc] init];
michael@0 910 self = [super init];
michael@0 911 if (aItem && aTable) {
michael@0 912 mTables = [[NSMutableSet alloc] init];
michael@0 913 mItem = [aItem retain];
michael@0 914 [mTables addObject:[NSValue valueWithPointer:aTable]];
michael@0 915 } else {
michael@0 916 mTables = nil;
michael@0 917 mItem = nil;
michael@0 918 }
michael@0 919 return self;
michael@0 920
michael@0 921 NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
michael@0 922 }
michael@0 923
michael@0 924 - (void)dealloc
michael@0 925 {
michael@0 926 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
michael@0 927
michael@0 928 if (mTables)
michael@0 929 [mTables release];
michael@0 930 if (mItem)
michael@0 931 [mItem release];
michael@0 932 [super dealloc];
michael@0 933
michael@0 934 NS_OBJC_END_TRY_ABORT_BLOCK;
michael@0 935 }
michael@0 936
michael@0 937 - (BOOL)hasTable:(NSMapTable *)aTable
michael@0 938 {
michael@0 939 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
michael@0 940
michael@0 941 return [mTables member:[NSValue valueWithPointer:aTable]] ? YES : NO;
michael@0 942
michael@0 943 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(NO);
michael@0 944 }
michael@0 945
michael@0 946 // Does nothing if aTable (its index value) is already present in mTables.
michael@0 947 - (int)addTable:(NSMapTable *)aTable
michael@0 948 {
michael@0 949 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
michael@0 950
michael@0 951 if (aTable)
michael@0 952 [mTables addObject:[NSValue valueWithPointer:aTable]];
michael@0 953 return [mTables count];
michael@0 954
michael@0 955 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
michael@0 956 }
michael@0 957
michael@0 958 - (int)removeTable:(NSMapTable *)aTable
michael@0 959 {
michael@0 960 NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
michael@0 961
michael@0 962 if (aTable) {
michael@0 963 NSValue *objectToRemove =
michael@0 964 [mTables member:[NSValue valueWithPointer:aTable]];
michael@0 965 if (objectToRemove)
michael@0 966 [mTables removeObject:objectToRemove];
michael@0 967 }
michael@0 968 return [mTables count];
michael@0 969
michael@0 970 NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(0);
michael@0 971 }
michael@0 972
michael@0 973 @end
michael@0 974
michael@0 975 @interface NSMenu (MethodSwizzling)
michael@0 976 + (void)nsMenuX_NSMenu_addItem:(NSMenuItem *)aItem toTable:(NSMapTable *)aTable;
michael@0 977 + (void)nsMenuX_NSMenu_removeItem:(NSMenuItem *)aItem fromTable:(NSMapTable *)aTable;
michael@0 978 @end
michael@0 979
michael@0 980 @implementation NSMenu (MethodSwizzling)
michael@0 981
michael@0 982 + (void)nsMenuX_NSMenu_addItem:(NSMenuItem *)aItem toTable:(NSMapTable *)aTable
michael@0 983 {
michael@0 984 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
michael@0 985
michael@0 986 if (aItem && aTable) {
michael@0 987 NSValue *key = [NSValue valueWithPointer:aItem];
michael@0 988 KeyEquivDBItem *shadowItem = [gShadowKeyEquivDB objectForKey:key];
michael@0 989 if (shadowItem) {
michael@0 990 [shadowItem addTable:aTable];
michael@0 991 } else {
michael@0 992 shadowItem = [[KeyEquivDBItem alloc] initWithItem:aItem table:aTable];
michael@0 993 [gShadowKeyEquivDB setObject:shadowItem forKey:key];
michael@0 994 // Release after [NSMutableDictionary setObject:forKey:] retains it (so
michael@0 995 // that it will get dealloced when removeObjectForKey: is called).
michael@0 996 [shadowItem release];
michael@0 997 }
michael@0 998 }
michael@0 999
michael@0 1000 NS_OBJC_END_TRY_ABORT_BLOCK;
michael@0 1001
michael@0 1002 [self nsMenuX_NSMenu_addItem:aItem toTable:aTable];
michael@0 1003 }
michael@0 1004
michael@0 1005 + (void)nsMenuX_NSMenu_removeItem:(NSMenuItem *)aItem fromTable:(NSMapTable *)aTable
michael@0 1006 {
michael@0 1007 [self nsMenuX_NSMenu_removeItem:aItem fromTable:aTable];
michael@0 1008
michael@0 1009 NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
michael@0 1010
michael@0 1011 if (aItem && aTable) {
michael@0 1012 NSValue *key = [NSValue valueWithPointer:aItem];
michael@0 1013 KeyEquivDBItem *shadowItem = [gShadowKeyEquivDB objectForKey:key];
michael@0 1014 if (shadowItem && [shadowItem hasTable:aTable]) {
michael@0 1015 if (![shadowItem removeTable:aTable])
michael@0 1016 [gShadowKeyEquivDB removeObjectForKey:key];
michael@0 1017 }
michael@0 1018 }
michael@0 1019
michael@0 1020 NS_OBJC_END_TRY_ABORT_BLOCK;
michael@0 1021 }
michael@0 1022
michael@0 1023 @end
michael@0 1024
michael@0 1025 // This class is needed to keep track of when the OS is (re)indexing all of
michael@0 1026 // our menus. This appears to only happen on Leopard and higher, and can
michael@0 1027 // be triggered by opening the Help menu. Some operations are unsafe while
michael@0 1028 // this is happening -- notably the calls to [[NSImage alloc]
michael@0 1029 // initWithSize:imageRect.size] and [newImage lockFocus] in nsMenuItemIconX::
michael@0 1030 // OnStopFrame(). But we don't yet have a complete list, and Apple doesn't
michael@0 1031 // yet have any documentation on this subject. (Apple also doesn't yet have
michael@0 1032 // any documented way to find the information we seek here.) The "original"
michael@0 1033 // of this class (the one whose indexMenuBarDynamically method we hook) is
michael@0 1034 // defined in the Shortcut framework in /System/Library/PrivateFrameworks.
michael@0 1035 @interface NSObject (SCTGRLIndexMethodSwizzling)
michael@0 1036 - (void)nsMenuX_SCTGRLIndex_indexMenuBarDynamically;
michael@0 1037 @end
michael@0 1038
michael@0 1039 @implementation NSObject (SCTGRLIndexMethodSwizzling)
michael@0 1040
michael@0 1041 - (void)nsMenuX_SCTGRLIndex_indexMenuBarDynamically
michael@0 1042 {
michael@0 1043 // This method appears to be called (once) whenever the OS (re)indexes our
michael@0 1044 // menus. sIndexingMenuLevel is a int32_t just in case it might be
michael@0 1045 // reentered. As it's running, it spawns calls to two undocumented
michael@0 1046 // HIToolbox methods (_SimulateMenuOpening() and _SimulateMenuClosed()),
michael@0 1047 // which "simulate" the opening and closing of our menus without actually
michael@0 1048 // displaying them.
michael@0 1049 ++nsMenuX::sIndexingMenuLevel;
michael@0 1050 [self nsMenuX_SCTGRLIndex_indexMenuBarDynamically];
michael@0 1051 --nsMenuX::sIndexingMenuLevel;
michael@0 1052 }
michael@0 1053
michael@0 1054 @end

mercurial