| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/cocoa/notifications/balloon_controller.h" | 5 #include "chrome/browser/cocoa/notifications/balloon_controller.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #import "base/cocoa_protocols_mac.h" | 9 #import "base/cocoa_protocols_mac.h" |
| 10 #include "base/mac_util.h" |
| 10 #import "base/scoped_nsobject.h" | 11 #import "base/scoped_nsobject.h" |
| 11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 12 #import "chrome/browser/cocoa/menu_controller.h" | 13 #import "chrome/browser/cocoa/menu_controller.h" |
| 13 #include "chrome/browser/cocoa/notifications/balloon_view_host_mac.h" | 14 #include "chrome/browser/cocoa/notifications/balloon_view_host_mac.h" |
| 14 #include "chrome/browser/notifications/balloon.h" | 15 #include "chrome/browser/notifications/balloon.h" |
| 15 #include "chrome/browser/notifications/desktop_notification_service.h" | 16 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 16 #include "chrome/browser/notifications/notification_options_menu_model.h" | 17 #include "chrome/browser/notifications/notification_options_menu_model.h" |
| 17 #include "chrome/browser/profile.h" | 18 #include "chrome/browser/profile.h" |
| 18 #include "chrome/browser/renderer_host/render_view_host.h" | 19 #include "chrome/browser/renderer_host/render_view_host.h" |
| 19 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 20 #include "grit/theme_resources.h" | 21 #include "grit/theme_resources.h" |
| 21 | 22 |
| 22 namespace { | 23 namespace { |
| 23 | 24 |
| 24 // Margin, in pixels, between the notification frame and the contents | 25 // Margin, in pixels, between the notification frame and the contents |
| 25 // of the notification. | 26 // of the notification. |
| 26 const int kTopMargin = 1; | 27 const int kTopMargin = 1; |
| 27 const int kBottomMargin = 1; | 28 const int kBottomMargin = 1; |
| 28 const int kLeftMargin = 1; | 29 const int kLeftMargin = 1; |
| 29 const int kRightMargin = 1; | 30 const int kRightMargin = 1; |
| 30 | 31 |
| 31 } // namespace | 32 } // namespace |
| 32 | 33 |
| 33 @implementation BalloonController | 34 @implementation BalloonController |
| 34 | 35 |
| 35 - (id)initWithBalloon:(Balloon*)balloon { | 36 - (id)initWithBalloon:(Balloon*)balloon { |
| 36 if ((self = [super initWithWindowNibName:@"Notification"])) { | 37 NSString* nibpath = |
| 38 [mac_util::MainAppBundle() pathForResource:@"Notification" |
| 39 ofType:@"nib"]; |
| 40 if ((self = [super initWithWindowNibPath:nibpath owner:self])) { |
| 37 balloon_ = balloon; | 41 balloon_ = balloon; |
| 38 [self initializeHost]; | 42 [self initializeHost]; |
| 39 menuModel_.reset(new NotificationOptionsMenuModel(balloon)); | 43 menuModel_.reset(new NotificationOptionsMenuModel(balloon)); |
| 40 menuController_.reset([[MenuController alloc] initWithModel:menuModel_.get() | 44 menuController_.reset([[MenuController alloc] initWithModel:menuModel_.get() |
| 41 useWithPopUpButtonCell:NO]); | 45 useWithPopUpButtonCell:NO]); |
| 42 } | 46 } |
| 43 return self; | 47 return self; |
| 44 } | 48 } |
| 45 | 49 |
| 46 - (void)awakeFromNib { | 50 - (void)awakeFromNib { |
| 47 DCHECK([self window]); | 51 DCHECK([self window]); |
| 48 DCHECK_EQ(self, [[self window] delegate]); | 52 DCHECK_EQ(self, [[self window] delegate]); |
| 49 | 53 |
| 50 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 54 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 51 NSImage* image = rb.GetNSImageNamed(IDR_BALLOON_WRENCH); | 55 NSImage* image = rb.GetNSImageNamed(IDR_BALLOON_WRENCH); |
| 52 DCHECK(image); | 56 DCHECK(image); |
| 53 [optionsButton_ setImage:image]; | 57 [optionsButton_ setImage:image]; |
| 54 | 58 |
| 55 NSString* sourceLabelText = l10n_util::GetNSStringF( | 59 NSString* sourceLabelText = l10n_util::GetNSStringF( |
| 56 IDS_NOTIFICATION_BALLOON_SOURCE_LABEL, | 60 IDS_NOTIFICATION_BALLOON_SOURCE_LABEL, |
| 57 WideToUTF16(balloon_->notification().display_source())); | 61 WideToUTF16(balloon_->notification().display_source())); |
| 58 [originLabel_ setStringValue:sourceLabelText]; | 62 [originLabel_ setStringValue:sourceLabelText]; |
| 59 | 63 |
| 60 gfx::NativeView contents = htmlContents_->native_view(); | 64 // This condition is false in unit tests which have no RVH. |
| 61 [contents setFrame:NSMakeRect(kLeftMargin, kTopMargin, 0, 0)]; | 65 if (htmlContents_.get()) { |
| 62 [[htmlContainer_ superview] addSubview:contents | 66 gfx::NativeView contents = htmlContents_->native_view(); |
| 63 positioned:NSWindowBelow | 67 [contents setFrame:NSMakeRect(kLeftMargin, kTopMargin, 0, 0)]; |
| 64 relativeTo:nil]; | 68 [[htmlContainer_ superview] addSubview:contents |
| 69 positioned:NSWindowBelow |
| 70 relativeTo:nil]; |
| 71 } |
| 65 } | 72 } |
| 66 | 73 |
| 67 - (IBAction)optionsButtonPressed:(id)sender { | 74 - (IBAction)optionsButtonPressed:(id)sender { |
| 68 [NSMenu popUpContextMenu:[menuController_ menu] | 75 [NSMenu popUpContextMenu:[menuController_ menu] |
| 69 withEvent:[NSApp currentEvent] | 76 withEvent:[NSApp currentEvent] |
| 70 forView:optionsButton_]; | 77 forView:optionsButton_]; |
| 71 } | 78 } |
| 72 | 79 |
| 73 - (IBAction)permissionRevoked:(id)sender { | 80 - (IBAction)permissionRevoked:(id)sender { |
| 74 DesktopNotificationService* service = | 81 DesktopNotificationService* service = |
| (...skipping 22 matching lines...) Expand all Loading... |
| 97 balloon_->notification().content_url()); | 104 balloon_->notification().content_url()); |
| 98 } | 105 } |
| 99 | 106 |
| 100 - (void)repositionToBalloon { | 107 - (void)repositionToBalloon { |
| 101 DCHECK(balloon_); | 108 DCHECK(balloon_); |
| 102 int x = balloon_->GetPosition().x(); | 109 int x = balloon_->GetPosition().x(); |
| 103 int y = balloon_->GetPosition().y(); | 110 int y = balloon_->GetPosition().y(); |
| 104 int w = [self desiredTotalWidth]; | 111 int w = [self desiredTotalWidth]; |
| 105 int h = [self desiredTotalHeight]; | 112 int h = [self desiredTotalHeight]; |
| 106 | 113 |
| 107 htmlContents_->UpdateActualSize(balloon_->content_size()); | 114 if (htmlContents_.get()) |
| 115 htmlContents_->UpdateActualSize(balloon_->content_size()); |
| 116 |
| 108 [[self window] setFrame:NSMakeRect(x, y, w, h) | 117 [[self window] setFrame:NSMakeRect(x, y, w, h) |
| 109 display:YES | 118 display:YES |
| 110 animate:YES]; | 119 animate:YES]; |
| 111 } | 120 } |
| 112 | 121 |
| 113 // Returns the total width the view should be to accommodate the balloon. | 122 // Returns the total width the view should be to accommodate the balloon. |
| 114 - (int)desiredTotalWidth { | 123 - (int)desiredTotalWidth { |
| 115 return (balloon_ ? balloon_->content_size().width() : 0) + | 124 return (balloon_ ? balloon_->content_size().width() : 0) + |
| 116 kLeftMargin + kRightMargin; | 125 kLeftMargin + kRightMargin; |
| 117 } | 126 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 132 htmlContents_.reset(new BalloonViewHost(balloon_)); | 141 htmlContents_.reset(new BalloonViewHost(balloon_)); |
| 133 htmlContents_->Init(); | 142 htmlContents_->Init(); |
| 134 } | 143 } |
| 135 | 144 |
| 136 // NSWindowDelegate notification. | 145 // NSWindowDelegate notification. |
| 137 - (void)windowWillClose:(NSNotification*)notif { | 146 - (void)windowWillClose:(NSNotification*)notif { |
| 138 [self autorelease]; | 147 [self autorelease]; |
| 139 } | 148 } |
| 140 | 149 |
| 141 @end | 150 @end |
| OLD | NEW |