| OLD | NEW | 
|---|
| (Empty) |  | 
|  | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 
|  | 2 // Use of this source code is governed by a BSD-style license that can be | 
|  | 3 // found in the LICENSE file. | 
|  | 4 | 
|  | 5 #import "chrome/browser/cocoa/view_id_util.h" | 
|  | 6 | 
|  | 7 #import <Cocoa/Cocoa.h> | 
|  | 8 | 
|  | 9 #include <map> | 
|  | 10 #include <utility> | 
|  | 11 | 
|  | 12 #include "base/logging.h" | 
|  | 13 #include "base/singleton.h" | 
|  | 14 #import "chrome/browser/cocoa/browser_window_controller.h" | 
|  | 15 #import "chrome/browser/cocoa/tab_strip_controller.h" | 
|  | 16 | 
|  | 17 namespace { | 
|  | 18 | 
|  | 19 // TODO(suzhe): After migrating to Mac OS X 10.6, we may use Objective-C's new | 
|  | 20 // "Associative References" feature to attach the ViewID to the view directly | 
|  | 21 // rather than using a separated map. | 
|  | 22 typedef std::map<NSView*, ViewID> ViewIDMap; | 
|  | 23 | 
|  | 24 // Returns the view's nearest descendant (including itself) with a specific | 
|  | 25 // ViewID, or nil if no subview has that ViewID. | 
|  | 26 NSView* FindViewWithID(NSView* view, ViewID viewID) { | 
|  | 27   if ([view viewID] == viewID) | 
|  | 28     return view; | 
|  | 29 | 
|  | 30   for (NSView* subview in [view subviews]) { | 
|  | 31     NSView* result = FindViewWithID(subview, viewID); | 
|  | 32     if (result != nil) | 
|  | 33       return result; | 
|  | 34   } | 
|  | 35   return nil; | 
|  | 36 } | 
|  | 37 | 
|  | 38 }  // anonymous namespace | 
|  | 39 | 
|  | 40 namespace view_id_util { | 
|  | 41 | 
|  | 42 void SetID(NSView* view, ViewID viewID) { | 
|  | 43   DCHECK(view); | 
|  | 44   DCHECK(viewID != VIEW_ID_NONE); | 
|  | 45   // We handle VIEW_ID_TAB_0 to VIEW_ID_TAB_LAST in GetView() function directly. | 
|  | 46   DCHECK(!((viewID >= VIEW_ID_TAB_0) && (viewID <= VIEW_ID_TAB_LAST))); | 
|  | 47   (*Singleton<ViewIDMap>::get())[view] = viewID; | 
|  | 48 } | 
|  | 49 | 
|  | 50 void UnsetID(NSView* view) { | 
|  | 51   DCHECK(view); | 
|  | 52   Singleton<ViewIDMap>::get()->erase(view); | 
|  | 53 } | 
|  | 54 | 
|  | 55 NSView* GetView(NSWindow* window, ViewID viewID) { | 
|  | 56   DCHECK(viewID != VIEW_ID_NONE); | 
|  | 57   DCHECK(window); | 
|  | 58 | 
|  | 59   // As tabs can be created, destroyed or rearranged dynamically, we handle them | 
|  | 60   // here specially. | 
|  | 61   if (viewID >= VIEW_ID_TAB_0 && viewID <= VIEW_ID_TAB_LAST) { | 
|  | 62     BrowserWindowController* windowController = [window windowController]; | 
|  | 63     DCHECK([windowController isKindOfClass:[BrowserWindowController class]]); | 
|  | 64     TabStripController* tabStripController = | 
|  | 65         [windowController tabStripController]; | 
|  | 66     DCHECK(tabStripController); | 
|  | 67     NSUInteger count = [tabStripController viewsCount]; | 
|  | 68     DCHECK(count); | 
|  | 69     NSUInteger index = | 
|  | 70         (viewID == VIEW_ID_TAB_LAST ? count - 1 : viewID - VIEW_ID_TAB_0); | 
|  | 71     return index < count ? [tabStripController viewAtIndex:index] : nil; | 
|  | 72   } | 
|  | 73 | 
|  | 74   return FindViewWithID([[window contentView] superview], viewID); | 
|  | 75 } | 
|  | 76 | 
|  | 77 }  // namespace view_id_util | 
|  | 78 | 
|  | 79 @implementation NSView (ViewID) | 
|  | 80 | 
|  | 81 - (ViewID)viewID { | 
|  | 82   ViewIDMap* map = Singleton<ViewIDMap>::get(); | 
|  | 83   ViewIDMap::const_iterator iter = map->find(self); | 
|  | 84   return iter != map->end() ? iter->second : VIEW_ID_NONE; | 
|  | 85 } | 
|  | 86 | 
|  | 87 @end | 
| OLD | NEW | 
|---|