1 // Copyright (c) 2012 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.
5 #import "chrome/browser/ui/cocoa/infobars/infobar_controller.h"
7 #include "base/logging.h"
8 #include "base/mac/bundle_locations.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #import "chrome/browser/ui/cocoa/animatable_view.h"
11 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
12 #import "chrome/browser/ui/cocoa/image_button_cell.h"
13 #include "chrome/browser/ui/cocoa/infobars/infobar_cocoa.h"
14 #import "chrome/browser/ui/cocoa/infobars/infobar_container_cocoa.h"
15 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h"
16 #import "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h"
17 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
18 #include "grit/theme_resources.h"
19 #import "ui/base/cocoa/controls/hyperlink_text_view.h"
20 #include "ui/gfx/image/image.h"
21 #include "ui/resources/grit/ui_resources.h"
23 @interface InfoBarController ()
24 // Sets |label_| based on |labelPlaceholder_|, sets |labelPlaceholder_| to nil.
25 - (void)initializeLabel;
28 @implementation InfoBarController
30 @synthesize containerController = containerController_;
32 - (id)initWithInfoBar:(InfoBarCocoa*)infobar {
33 if ((self = [super initWithNibName:@"InfoBar"
34 bundle:base::mac::FrameworkBundle()])) {
36 infobar_ = infobar->GetWeakPtr();
41 // All infobars have an icon, so we set up the icon in the base class
43 - (void)awakeFromNib {
44 [[closeButton_ cell] setImageID:IDR_CLOSE_1
45 forButtonState:image_button_cell::kDefaultState];
46 [[closeButton_ cell] setImageID:IDR_CLOSE_1_H
47 forButtonState:image_button_cell::kHoverState];
48 [[closeButton_ cell] setImageID:IDR_CLOSE_1_P
49 forButtonState:image_button_cell::kPressedState];
50 [[closeButton_ cell] setImageID:IDR_CLOSE_1
51 forButtonState:image_button_cell::kDisabledState];
53 if (![self delegate]->GetIcon().IsEmpty()) {
54 [image_ setImage:[self delegate]->GetIcon().ToNSImage()];
56 // No icon, remove it from the view and grow the textfield to include the
58 NSRect imageFrame = [image_ frame];
59 NSRect labelFrame = [labelPlaceholder_ frame];
60 labelFrame.size.width += NSMinX(imageFrame) - NSMinX(labelFrame);
61 labelFrame.origin.x = imageFrame.origin.x;
62 [image_ removeFromSuperview];
64 [labelPlaceholder_ setFrame:labelFrame];
66 [self initializeLabel];
68 [self addAdditionalControls];
70 [infoBarView_ setInfobarType:[self delegate]->GetInfoBarType()];
71 [infoBarView_ setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
75 [okButton_ setTarget:nil];
76 [cancelButton_ setTarget:nil];
77 [closeButton_ setTarget:nil];
81 - (InfoBarCocoa*)infobar {
82 return infobar_.get();
85 // Called when someone clicks on the embedded link.
86 - (BOOL)textView:(NSTextView*)textView
87 clickedOnLink:(id)link
88 atIndex:(NSUInteger)charIndex {
89 if ([self respondsToSelector:@selector(linkClicked)])
90 [self performSelector:@selector(linkClicked)];
95 return infobar_ && infobar_->OwnerCocoa() != NULL;
98 // Called when someone clicks on the ok button.
99 - (void)ok:(id)sender {
100 // Subclasses must override this method if they do not hide the ok button.
104 // Called when someone clicks on the cancel button.
105 - (void)cancel:(id)sender {
106 // Subclasses must override this method if they do not hide the cancel button.
110 // Called when someone clicks on the close button.
111 - (void)dismiss:(id)sender {
114 [self delegate]->InfoBarDismissed();
119 infobar_->RemoveSelf();
122 - (void)addAdditionalControls {
123 // Default implementation does nothing.
126 - (void)infobarWillHide {
129 - (void)infobarWillClose {
132 - (void)removeButtons {
133 // Extend the label all the way across.
134 NSRect labelFrame = [label_.get() frame];
135 labelFrame.size.width = NSMaxX([cancelButton_ frame]) - NSMinX(labelFrame);
136 [okButton_ removeFromSuperview];
138 [cancelButton_ removeFromSuperview];
140 [label_.get() setFrame:labelFrame];
143 - (void)layoutArrow {
144 [infoBarView_ setArrowHeight:infobar_->arrow_height()];
145 [infoBarView_ setArrowHalfWidth:infobar_->arrow_half_width()];
146 [infoBarView_ setHasTip:![containerController_ shouldSuppressTopInfoBarTip]];
148 // Convert from window to view coordinates.
149 NSPoint point = NSMakePoint([containerController_ infobarArrowX], 0);
150 point = [infoBarView_ convertPoint:point fromView:nil];
151 [infoBarView_ setArrowX:point.x];
154 - (void)disablePopUpMenu:(NSMenu*)menu {
155 // If the menu is re-opened, prevent queries to update items.
156 [menu setDelegate:nil];
158 // Prevent target/action messages to the controller.
159 for (NSMenuItem* item in [menu itemArray]) {
160 [item setEnabled:NO];
161 [item setTarget:nil];
165 - (infobars::InfoBarDelegate*)delegate {
166 return infobar_->delegate();
169 - (void)initializeLabel {
170 // Replace the label placeholder NSTextField with the real label NSTextView.
171 // The former doesn't show links in a nice way, but the latter can't be added
172 // in IB without a containing scroll view, so create the NSTextView
174 base::scoped_nsobject<HyperlinkTextView> newLabel(
175 [[HyperlinkTextView alloc] initWithFrame:[labelPlaceholder_ frame]]);
176 [newLabel setDrawsBackgroundUsingSuperview:YES];
177 [newLabel setAutoresizingMask:[labelPlaceholder_ autoresizingMask]];
178 [[labelPlaceholder_ superview]
179 replaceSubview:labelPlaceholder_ with:newLabel];
180 labelPlaceholder_ = nil; // Now released.
181 [newLabel setDelegate:self];
183 label_.reset(newLabel.release());