summaryrefslogtreecommitdiffstats
path: root/ui/base/cocoa
diff options
context:
space:
mode:
authoravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-05 15:05:07 +0000
committeravi@chromium.org <avi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-05 15:05:07 +0000
commit264f19c80c7fcb5244efd6772953e4cf94025b9f (patch)
tree0609858a044bb58ad15d49d06eb6dedaa6c3f2c6 /ui/base/cocoa
parent7e8b061fdcc4022734ca746ee76154d0b9ef9411 (diff)
downloadchromium_src-264f19c80c7fcb5244efd6772953e4cf94025b9f.zip
chromium_src-264f19c80c7fcb5244efd6772953e4cf94025b9f.tar.gz
chromium_src-264f19c80c7fcb5244efd6772953e4cf94025b9f.tar.bz2
Make common utility to load views from nibs.
BUG=120526 TEST=no change Review URL: https://chromiumcodereview.appspot.com/9982014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@130922 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/base/cocoa')
-rw-r--r--ui/base/cocoa/nib_loading.h21
-rw-r--r--ui/base/cocoa/nib_loading.mm45
2 files changed, 66 insertions, 0 deletions
diff --git a/ui/base/cocoa/nib_loading.h b/ui/base/cocoa/nib_loading.h
new file mode 100644
index 0000000..653aba4
--- /dev/null
+++ b/ui/base/cocoa/nib_loading.h
@@ -0,0 +1,21 @@
+// Copyright (c) 2012 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.
+
+#ifndef UI_BASE_COCOA_NIB_LOADING_H_
+#define UI_BASE_COCOA_NIB_LOADING_H_
+#pragma once
+
+#import <Cocoa/Cocoa.h>
+
+#include "ui/base/ui_export.h"
+
+namespace ui {
+
+// Given the name of a nib file, gets an unowned reference to the NSView in the
+// nib. Requires a nib with just a single root view.
+UI_EXPORT NSView* GetViewFromNib(NSString* name);
+
+} // namespace ui
+
+#endif // UI_BASE_COCOA_NIB_LOADING_H_
diff --git a/ui/base/cocoa/nib_loading.mm b/ui/base/cocoa/nib_loading.mm
new file mode 100644
index 0000000..9430a76
--- /dev/null
+++ b/ui/base/cocoa/nib_loading.mm
@@ -0,0 +1,45 @@
+// Copyright (c) 2012 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 "ui/base/cocoa/nib_loading.h"
+
+#include "base/mac/bundle_locations.h"
+#include "base/memory/scoped_nsobject.h"
+
+namespace ui {
+
+NSView* GetViewFromNib(NSString* name) {
+ scoped_nsobject<NSNib> nib(
+ [[NSNib alloc] initWithNibNamed:name
+ bundle:base::mac::FrameworkBundle()]);
+ if (!nib)
+ return nil;
+
+ NSArray* objects;
+ BOOL success = [nib instantiateNibWithOwner:nil
+ topLevelObjects:&objects];
+ if (!success)
+ return nil;
+
+ // When loading a nib manually (as opposed to using an NSWindowController or
+ // NSViewController), all the top-level objects need to be explicitly
+ // released. See
+ // http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW10
+ // for more information.
+ [objects makeObjectsPerformSelector:@selector(release)];
+
+ // For some strange reason, even nibs that appear to have but one top-level
+ // object often have more (an NSApplication, etc.). Filter out what isn't
+ // desired.
+ for (NSView* view in objects) {
+ if (![view isKindOfClass:[NSView class]])
+ continue;
+
+ return [[view retain] autorelease];
+ }
+
+ return nil;
+}
+
+} // namespace ui