Chromium Code Reviews| 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/base_bubble_controller.h" | |
| 6 | |
| 7 #include "app/l10n_util.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/mac_util.h" | |
| 10 #include "base/string_util.h" | |
| 11 #import "chrome/browser/cocoa/info_bubble_view.h" | |
| 12 #include "grit/generated_resources.h" | |
| 13 | |
| 14 @implementation BaseBubbleController | |
| 15 | |
| 16 @synthesize bubble = bubble_; | |
| 17 | |
| 18 - (id)initWithWindowNibPath:(NSString*)nibPath | |
| 19 parentWindow:(NSWindow*)parentWindow | |
| 20 anchoredAt:(NSPoint)anchoredAt { | |
| 21 nibPath = [mac_util::MainAppBundle() pathForResource:nibPath | |
| 22 ofType:@"nib"]; | |
| 23 if ((self = [super initWithWindowNibPath:nibPath owner:self])) { | |
| 24 parentWindow_ = parentWindow; | |
| 25 anchor_ = anchoredAt; | |
| 26 | |
| 27 // Watch to see if the parent window closes, and if so, close this one. | |
| 28 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; | |
| 29 [center addObserver:self | |
| 30 selector:@selector(parentWindowWillClose:) | |
| 31 name:NSWindowWillCloseNotification | |
| 32 object:parentWindow_]; | |
| 33 } | |
| 34 return self; | |
| 35 } | |
| 36 | |
| 37 - (id)initWithWindowNibPath:(NSString*)nibPath | |
| 38 relativeToView:(NSView*)view | |
| 39 offset:(NSPoint)offset { | |
| 40 DCHECK([view window]); | |
| 41 NSWindow* window = [view window]; | |
| 42 NSRect bounds = [view convertRect:[view bounds] toView:nil]; | |
| 43 NSPoint anchor = NSMakePoint(NSMinX(bounds) + offset.x, | |
| 44 NSMinY(bounds) + offset.y); | |
| 45 anchor = [window convertBaseToScreen:anchor]; | |
| 46 return [self initWithWindowNibPath:nibPath | |
| 47 parentWindow:window | |
| 48 anchoredAt:anchor]; | |
| 49 } | |
| 50 | |
| 51 | |
| 52 - (void)awakeFromNib { | |
| 53 // Check all connections have been made in Interface Builder. | |
| 54 DCHECK([self window]); | |
| 55 DCHECK(bubble_); | |
| 56 DCHECK_EQ(self, [[self window] delegate]); | |
| 57 | |
| 58 [bubble_ setBubbleType:info_bubble::kWhiteInfoBubble]; | |
| 59 [bubble_ setArrowLocation:info_bubble::kTopRight]; | |
| 60 } | |
| 61 | |
| 62 - (void)dealloc { | |
| 63 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 64 [super dealloc]; | |
| 65 } | |
| 66 | |
| 67 - (void)parentWindowWillClose:(NSNotification*)notification { | |
| 68 [self close]; | |
| 69 } | |
| 70 | |
| 71 - (void)windowWillClose:(NSNotification*)notification { | |
| 72 // We caught a close so we don't need to watch for the parent closing. | |
| 73 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 74 [self autorelease]; | |
| 75 } | |
| 76 | |
| 77 // We want this to be a child of a browser window. addChildWindow: | |
| 78 // (called from this function) will bring the window on-screen; | |
| 79 // unfortunately, [NSWindowController showWindow:] will also bring it | |
| 80 // on-screen (but will cause unexpected changes to the window's | |
| 81 // position). We cannot have an addChildWindow: and a subsequent | |
| 82 // showWindow:. Thus, we have our own version. | |
| 83 - (void)showWindow:(id)sender { | |
| 84 NSWindow* window = [self window]; // completes nib load | |
|
jeremy
2010/06/27 12:46:42
nit: Completes nib load.
| |
| 85 | |
| 86 NSPoint origin = anchor_; | |
| 87 NSSize offsets = NSMakeSize(info_bubble::kBubbleArrowXOffset + | |
| 88 info_bubble::kBubbleArrowWidth / 2.0, 0); | |
| 89 offsets = [[parentWindow_ contentView] convertSize:offsets toView:nil]; | |
| 90 origin.x += offsets.width; | |
| 91 if ([bubble_ arrowLocation] == info_bubble::kTopRight) | |
| 92 origin.x -= NSWidth([window frame]); | |
| 93 origin.y -= NSHeight([window frame]); | |
| 94 [window setFrameOrigin:origin]; | |
| 95 [parentWindow_ addChildWindow:window ordered:NSWindowAbove]; | |
| 96 [window makeKeyAndOrderFront:self]; | |
| 97 } | |
| 98 | |
| 99 - (void)close { | |
| 100 [parentWindow_ removeChildWindow:[self window]]; | |
| 101 [super close]; | |
| 102 } | |
| 103 | |
| 104 // The controller is the delegate of the window so it receives did resign key | |
| 105 // notifications. When key is resigned mirror Windows behavior and close the | |
| 106 // window. | |
| 107 - (void)windowDidResignKey:(NSNotification*)notification { | |
| 108 NSWindow* window = [self window]; | |
| 109 DCHECK_EQ([notification object], window); | |
| 110 if ([window isVisible]) { | |
| 111 // If the window isn't visible, it is already closed, and this notification | |
| 112 // has been sent as part of the closing operation, so no need to close. | |
| 113 [self close]; | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 // By implementing this, ESC causes the window to go away. | |
| 118 - (IBAction)cancel:(id)sender { | |
| 119 // This is not a "real" cancel as potential changes to the radio group are not | |
| 120 // undone. That's ok. | |
|
jeremy
2010/06/27 12:46:42
Could you expand this comment: which radio group?
| |
| 121 [self close]; | |
| 122 } | |
| 123 @end // BaseBubbleController | |
| OLD | NEW |