summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/infobars/alternate_nav_infobar_controller.mm
blob: 6a7585f9560fe277a61bdfc6fee45972fe484ac8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "chrome/browser/ui/cocoa/infobars/alternate_nav_infobar_controller.h"

#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
#import "chrome/browser/ui/cocoa/hyperlink_text_view.h"
#include "chrome/browser/ui/cocoa/infobars/infobar_cocoa.h"
#include "chrome/browser/ui/omnibox/alternate_nav_infobar_delegate.h"
#import "ui/base/cocoa/cocoa_event_utils.h"
#include "ui/base/window_open_disposition.h"

@implementation AlternateNavInfoBarController

// Link infobars have a text message, of which part is linkified.  We
// use an NSAttributedString to display styled text, and we set a
// NSLink attribute on the hyperlink portion of the message.  Infobars
// use a custom NSTextField subclass, which allows us to override
// textView:clickedOnLink:atIndex: and intercept clicks.
//
- (void)addAdditionalControls {
  // No buttons.
  [self removeButtons];

  AlternateNavInfoBarDelegate* delegate =
      static_cast<AlternateNavInfoBarDelegate*>([self delegate]);
  DCHECK(delegate);
  size_t offset = base::string16::npos;
  base::string16 message = delegate->GetMessageTextWithOffset(&offset);
  base::string16 link = delegate->GetLinkText();
  NSFont* font = [NSFont labelFontOfSize:
                  [NSFont systemFontSizeForControlSize:NSRegularControlSize]];
  HyperlinkTextView* view = (HyperlinkTextView*)label_.get();
  [view setMessageAndLink:base::SysUTF16ToNSString(message)
                 withLink:base::SysUTF16ToNSString(link)
                 atOffset:offset
                     font:font
             messageColor:[NSColor blackColor]
                linkColor:[NSColor blueColor]];
}

// Called when someone clicks on the link in the infobar.  This method
// is called by the InfobarTextField on its delegate (the
// AlternateNavInfoBarController).
- (void)linkClicked {
  if (![self isOwned])
    return;
  WindowOpenDisposition disposition =
      ui::WindowOpenDispositionFromNSEvent([NSApp currentEvent]);
  AlternateNavInfoBarDelegate* delegate =
      static_cast<AlternateNavInfoBarDelegate*>([self delegate]);
  if (delegate->LinkClicked(disposition))
    [self removeSelf];
}

@end

// static
scoped_ptr<InfoBar> AlternateNavInfoBarDelegate::CreateInfoBar(
    scoped_ptr<AlternateNavInfoBarDelegate> delegate) {
  scoped_ptr<InfoBarCocoa> infobar(
      new InfoBarCocoa(delegate.PassAs<InfoBarDelegate>()));
  base::scoped_nsobject<AlternateNavInfoBarController> controller(
      [[AlternateNavInfoBarController alloc] initWithInfoBar:infobar.get()]);
  infobar->set_controller(controller);
  return infobar.PassAs<InfoBar>();
}