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 #import "chrome/browser/ui/cocoa/infobars/alternate_nav_infobar_controller.h"
7 #include "base/logging.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/ui/cocoa/infobars/infobar_cocoa.h"
10 #include "chrome/browser/ui/omnibox/alternate_nav_infobar_delegate.h"
11 #import "ui/base/cocoa/cocoa_base_utils.h"
12 #import "ui/base/cocoa/controls/hyperlink_text_view.h"
13 #include "ui/base/window_open_disposition.h"
15 @implementation AlternateNavInfoBarController
17 // Link infobars have a text message, of which part is linkified. We
18 // use an NSAttributedString to display styled text, and we set a
19 // NSLink attribute on the hyperlink portion of the message. Infobars
20 // use a custom NSTextField subclass, which allows us to override
21 // textView:clickedOnLink:atIndex: and intercept clicks.
23 - (void)addAdditionalControls {
27 AlternateNavInfoBarDelegate* delegate =
28 static_cast<AlternateNavInfoBarDelegate*>([self delegate]);
30 size_t offset = base::string16::npos;
31 base::string16 message = delegate->GetMessageTextWithOffset(&offset);
32 base::string16 link = delegate->GetLinkText();
33 message.insert(offset, link);
34 NSFont* font = [NSFont labelFontOfSize:
35 [NSFont systemFontSizeForControlSize:NSRegularControlSize]];
36 HyperlinkTextView* view = (HyperlinkTextView*)label_.get();
37 [view setMessage:base::SysUTF16ToNSString(message)
39 messageColor:[NSColor blackColor]];
40 [view addLinkRange:NSMakeRange(offset, link.length())
42 linkColor:[NSColor blueColor]];
45 // Called when someone clicks on the link in the infobar. This method
46 // is called by the InfobarTextField on its delegate (the
47 // AlternateNavInfoBarController).
51 WindowOpenDisposition disposition =
52 ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
53 AlternateNavInfoBarDelegate* delegate =
54 static_cast<AlternateNavInfoBarDelegate*>([self delegate]);
55 if (delegate->LinkClicked(disposition))
62 scoped_ptr<infobars::InfoBar> AlternateNavInfoBarDelegate::CreateInfoBar(
63 scoped_ptr<AlternateNavInfoBarDelegate> delegate) {
64 scoped_ptr<InfoBarCocoa> infobar(new InfoBarCocoa(delegate.Pass()));
65 base::scoped_nsobject<AlternateNavInfoBarController> controller(
66 [[AlternateNavInfoBarController alloc] initWithInfoBar:infobar.get()]);
67 infobar->set_controller(controller);
68 return infobar.Pass();