From 7d791652c7ede4209a2014d885148e2713f49bce Mon Sep 17 00:00:00 2001 From: "ben@chromium.org" Date: Wed, 1 Dec 2010 16:34:49 +0000 Subject: Move browser/cocoa to browser/ui/cocoa BUG=none TEST=none TBR=brettw git-svn-id: svn://svn.chromium.org/chrome/trunk/src@67854 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/ui/cocoa/view_id_util.mm | 87 +++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 chrome/browser/ui/cocoa/view_id_util.mm (limited to 'chrome/browser/ui/cocoa/view_id_util.mm') diff --git a/chrome/browser/ui/cocoa/view_id_util.mm b/chrome/browser/ui/cocoa/view_id_util.mm new file mode 100644 index 0000000..df8f079 --- /dev/null +++ b/chrome/browser/ui/cocoa/view_id_util.mm @@ -0,0 +1,87 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import "chrome/browser/ui/cocoa/view_id_util.h" + +#import + +#include +#include + +#include "base/logging.h" +#include "base/singleton.h" +#import "chrome/browser/ui/cocoa/browser_window_controller.h" +#import "chrome/browser/ui/cocoa/tab_strip_controller.h" + +namespace { + +// TODO(suzhe): After migrating to Mac OS X 10.6, we may use Objective-C's new +// "Associative References" feature to attach the ViewID to the view directly +// rather than using a separated map. +typedef std::map ViewIDMap; + +// Returns the view's nearest descendant (including itself) with a specific +// ViewID, or nil if no subview has that ViewID. +NSView* FindViewWithID(NSView* view, ViewID viewID) { + if ([view viewID] == viewID) + return view; + + for (NSView* subview in [view subviews]) { + NSView* result = FindViewWithID(subview, viewID); + if (result != nil) + return result; + } + return nil; +} + +} // anonymous namespace + +namespace view_id_util { + +void SetID(NSView* view, ViewID viewID) { + DCHECK(view); + DCHECK(viewID != VIEW_ID_NONE); + // We handle VIEW_ID_TAB_0 to VIEW_ID_TAB_LAST in GetView() function directly. + DCHECK(!((viewID >= VIEW_ID_TAB_0) && (viewID <= VIEW_ID_TAB_LAST))); + (*Singleton::get())[view] = viewID; +} + +void UnsetID(NSView* view) { + DCHECK(view); + Singleton::get()->erase(view); +} + +NSView* GetView(NSWindow* window, ViewID viewID) { + DCHECK(viewID != VIEW_ID_NONE); + DCHECK(window); + + // As tabs can be created, destroyed or rearranged dynamically, we handle them + // here specially. + if (viewID >= VIEW_ID_TAB_0 && viewID <= VIEW_ID_TAB_LAST) { + BrowserWindowController* windowController = [window windowController]; + DCHECK([windowController isKindOfClass:[BrowserWindowController class]]); + TabStripController* tabStripController = + [windowController tabStripController]; + DCHECK(tabStripController); + NSUInteger count = [tabStripController viewsCount]; + DCHECK(count); + NSUInteger index = + (viewID == VIEW_ID_TAB_LAST ? count - 1 : viewID - VIEW_ID_TAB_0); + return index < count ? [tabStripController viewAtIndex:index] : nil; + } + + return FindViewWithID([[window contentView] superview], viewID); +} + +} // namespace view_id_util + +@implementation NSView (ViewID) + +- (ViewID)viewID { + ViewIDMap* map = Singleton::get(); + ViewIDMap::const_iterator iter = map->find(self); + return iter != map->end() ? iter->second : VIEW_ID_NONE; +} + +@end -- cgit v1.1