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 "base/mac/mac_util.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #import "chrome/browser/ui/cocoa/animatable_view.h"
12 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
13 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h"
14 #import "chrome/browser/ui/cocoa/image_button_cell.h"
15 #include "chrome/browser/ui/cocoa/infobars/infobar_cocoa.h"
16 #import "chrome/browser/ui/cocoa/infobars/infobar_container_cocoa.h"
17 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h"
18 #import "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h"
19 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
20 #include "grit/theme_resources.h"
21 #include "grit/ui_resources.h"
22 #include "ui/gfx/image/image.h"
24 @interface InfoBarController ()
25 // Sets |label_| based on |labelPlaceholder_|, sets |labelPlaceholder_| to nil.
26 - (void)initializeLabel;
29 @implementation InfoBarController
31 @synthesize containerController = containerController_;
33 - (id)initWithInfoBar:(InfoBarCocoa*)infobar {
34 if ((self = [super initWithNibName:@"InfoBar"
35 bundle:base::mac::FrameworkBundle()])) {
37 infobar_ = infobar->GetWeakPtr();
42 // All infobars have an icon, so we set up the icon in the base class
44 - (void)awakeFromNib {
45 [[closeButton_ cell] setImageID:IDR_CLOSE_1
46 forButtonState:image_button_cell::kDefaultState];
47 [[closeButton_ cell] setImageID:IDR_CLOSE_1_H
48 forButtonState:image_button_cell::kHoverState];
49 [[closeButton_ cell] setImageID:IDR_CLOSE_1_P
50 forButtonState:image_button_cell::kPressedState];
51 [[closeButton_ cell] setImageID:IDR_CLOSE_1
52 forButtonState:image_button_cell::kDisabledState];
54 if (![self delegate]->GetIcon().IsEmpty()) {
55 [image_ setImage:[self delegate]->GetIcon().ToNSImage()];
57 // No icon, remove it from the view and grow the textfield to include the
59 NSRect imageFrame = [image_ frame];
60 NSRect labelFrame = [labelPlaceholder_ frame];
61 labelFrame.size.width += NSMinX(imageFrame) - NSMinX(labelFrame);
62 labelFrame.origin.x = imageFrame.origin.x;
63 [image_ removeFromSuperview];
65 [labelPlaceholder_ setFrame:labelFrame];
67 [self initializeLabel];
69 [self addAdditionalControls];
71 [infoBarView_ setInfobarType:[self delegate]->GetInfoBarType()];
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 - (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 label_.reset([[HyperlinkTextView alloc]
175 initWithFrame:[labelPlaceholder_ frame]]);
176 [label_.get() setAutoresizingMask:[labelPlaceholder_ autoresizingMask]];
177 [[labelPlaceholder_ superview]
178 replaceSubview:labelPlaceholder_ with:label_.get()];
179 labelPlaceholder_ = nil; // Now released.
180 [label_.get() setDelegate:self];