1 // Copyright 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 #include "chrome/browser/ui/cocoa/infobars/confirm_infobar_controller.h"
7 #include "base/logging.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/browser/ui/cocoa/infobars/infobar_cocoa.h"
11 #include "components/infobars/core/confirm_infobar_delegate.h"
12 #include "third_party/google_toolbox_for_mac/src/AppKit/GTMUILocalizerAndLayoutTweaker.h"
13 #import "ui/base/cocoa/cocoa_base_utils.h"
14 #import "ui/base/cocoa/controls/hyperlink_text_view.h"
15 #include "ui/base/window_open_disposition.h"
17 @implementation ConfirmInfoBarController
19 // Called when someone clicks on the "OK" button.
20 - (IBAction)ok:(id)sender {
23 if ([self delegate]->AsConfirmInfoBarDelegate()->Accept())
27 // Called when someone clicks on the "Cancel" button.
28 - (IBAction)cancel:(id)sender {
31 if ([self delegate]->AsConfirmInfoBarDelegate()->Cancel())
35 // Confirm infobars can have OK and/or cancel buttons, depending on
36 // the return value of GetButtons(). We create each button if
37 // required and position them to the left of the close button.
38 - (void)addAdditionalControls {
39 ConfirmInfoBarDelegate* delegate =
40 [self delegate]->AsConfirmInfoBarDelegate();
42 int visibleButtons = delegate->GetButtons();
44 NSRect okButtonFrame = [okButton_ frame];
45 NSRect cancelButtonFrame = [cancelButton_ frame];
47 DCHECK(NSMaxX(cancelButtonFrame) < NSMinX(okButtonFrame))
48 << "Ok button expected to be on the right of the Cancel button in nib";
50 CGFloat rightEdge = NSMaxX(okButtonFrame);
51 CGFloat spaceBetweenButtons =
52 NSMinX(okButtonFrame) - NSMaxX(cancelButtonFrame);
53 CGFloat spaceBeforeButtons =
54 NSMinX(cancelButtonFrame) - NSMaxX([label_.get() frame]);
56 // Update and position the OK button if needed. Otherwise, hide it.
57 if (visibleButtons & ConfirmInfoBarDelegate::BUTTON_OK) {
58 [okButton_ setTitle:base::SysUTF16ToNSString(
59 delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_OK))];
60 [GTMUILocalizerAndLayoutTweaker sizeToFitView:okButton_];
61 okButtonFrame = [okButton_ frame];
63 // Position the ok button to the left of the Close button.
64 okButtonFrame.origin.x = rightEdge - okButtonFrame.size.width;
65 [okButton_ setFrame:okButtonFrame];
67 // Update the rightEdge
68 rightEdge = NSMinX(okButtonFrame);
70 [okButton_ removeFromSuperview];
74 // Update and position the Cancel button if needed. Otherwise, hide it.
75 if (visibleButtons & ConfirmInfoBarDelegate::BUTTON_CANCEL) {
76 [cancelButton_ setTitle:base::SysUTF16ToNSString(
77 delegate->GetButtonLabel(ConfirmInfoBarDelegate::BUTTON_CANCEL))];
78 [GTMUILocalizerAndLayoutTweaker sizeToFitView:cancelButton_];
79 cancelButtonFrame = [cancelButton_ frame];
81 // If we had a Ok button, leave space between the buttons.
82 if (visibleButtons & ConfirmInfoBarDelegate::BUTTON_OK) {
83 rightEdge -= spaceBetweenButtons;
86 // Position the Cancel button on our current right edge.
87 cancelButtonFrame.origin.x = rightEdge - cancelButtonFrame.size.width;
88 [cancelButton_ setFrame:cancelButtonFrame];
90 // Update the rightEdge.
91 rightEdge = NSMinX(cancelButtonFrame);
93 [cancelButton_ removeFromSuperview];
97 // If we had either button, leave space before the edge of the textfield.
98 if ((visibleButtons & ConfirmInfoBarDelegate::BUTTON_CANCEL) ||
99 (visibleButtons & ConfirmInfoBarDelegate::BUTTON_OK)) {
100 rightEdge -= spaceBeforeButtons;
103 NSRect frame = [label_.get() frame];
104 DCHECK(rightEdge > NSMinX(frame))
105 << "Need to make the xib larger to handle buttons with text this long";
106 frame.size.width = rightEdge - NSMinX(frame);
107 [label_.get() setFrame:frame];
109 // Set the text and link.
110 NSString* message = base::SysUTF16ToNSString(delegate->GetMessageText());
111 NSString* link = base::SysUTF16ToNSString(delegate->GetLinkText());
112 NSUInteger linkOffset = [message length];
113 NSUInteger linkLength = [link length];
114 if (linkLength != 0) {
115 // Add spacing between the label and the link.
116 message = [message stringByAppendingFormat:@" %@", link];
117 linkOffset = [message length] - [link length];
119 NSFont* font = [NSFont labelFontOfSize:
120 [NSFont systemFontSizeForControlSize:NSRegularControlSize]];
121 HyperlinkTextView* view = (HyperlinkTextView*)label_.get();
122 [view setMessage:message withFont:font messageColor:[NSColor blackColor]];
123 if (linkLength != 0) {
124 [view addLinkRange:NSMakeRange(linkOffset, linkLength)
126 linkColor:[NSColor blueColor]];
130 // Called when someone clicks on the link in the infobar. This method
131 // is called by the InfobarTextField on its delegate (the
132 // AlternateNavInfoBarController).
133 - (void)linkClicked {
136 WindowOpenDisposition disposition =
137 ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
138 if ([self delegate]->AsConfirmInfoBarDelegate()->LinkClicked(disposition))
144 scoped_ptr<infobars::InfoBar> InfoBarService::CreateConfirmInfoBar(
145 scoped_ptr<ConfirmInfoBarDelegate> delegate) {
146 scoped_ptr<InfoBarCocoa> infobar(new InfoBarCocoa(delegate.Pass()));
147 base::scoped_nsobject<ConfirmInfoBarController> controller(
148 [[ConfirmInfoBarController alloc] initWithInfoBar:infobar.get()]);
149 infobar->set_controller(controller);
150 return infobar.Pass();