a9add4a7a0f0d3256f2c5bd5f4aee2e68efcd629
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / bookmarks / bookmark_sync_promo_controller.mm
1 // Copyright 2013 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.
4
5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_sync_promo_controller.h"
6
7 #include "base/strings/sys_string_conversions.h"
8 #include "chrome/browser/signin/signin_promo.h"
9 #include "chrome/browser/ui/chrome_pages.h"
10 #include "chrome/browser/ui/chrome_style.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "skia/ext/skia_utils_mac.h"
13 #include "third_party/skia/include/core/SkColor.h"
14 #import "ui/base/cocoa/controls/hyperlink_text_view.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/l10n/l10n_util_mac.h"
17
18 namespace {
19
20 // Remove underlining from the specified range of characters in a text view.
21 void RemoveUnderlining(NSTextView* textView, int offset, int length) {
22   [textView setLinkTextAttributes:nil];
23   NSTextStorage* text = [textView textStorage];
24   NSRange range = NSMakeRange(offset, length);
25   [text addAttribute:NSUnderlineStyleAttributeName
26                value:[NSNumber numberWithInt:NSUnderlineStyleNone]
27                range:range];
28 }
29
30 const SkColor kTextColor = SkColorSetRGB(0x66, 0x66, 0x66);
31 const SkColor kBackgroundColor = SkColorSetRGB(0xf5, 0xf5, 0xf5);
32 const SkColor kBorderColor = SkColorSetRGB(0xe5, 0xe5, 0xe5);
33
34 // Vertical padding of the promo (dp).
35 const CGFloat kVerticalPadding = 15;
36
37 // Width of the border (dp).
38 const CGFloat kBorderWidth = 1.0;
39
40 // Font size of the promo text (pt).
41 const int kFontSize = 11;
42
43 }  // namespace
44
45 @implementation BookmarkSyncPromoController
46
47 - (id)initWithBrowser:(Browser*)browser {
48   if ((self = [super init])) {
49     browser_ = browser;
50   }
51   return self;
52 }
53
54 - (CGFloat)borderWidth {
55   return kBorderWidth;
56 }
57
58 - (CGFloat)preferredHeightForWidth:(CGFloat)width {
59   CGFloat availableWidth =
60       width - (2 * chrome_style::kHorizontalPadding) - (2 * kBorderWidth);
61   NSRect frame = [[textView_ textStorage]
62       boundingRectWithSize:NSMakeSize(availableWidth, 0.0)
63                    options:NSStringDrawingUsesLineFragmentOrigin];
64   return frame.size.height + (2 * kVerticalPadding) + (2 * kBorderWidth);
65 }
66
67 - (void)loadView {
68   NSBox* promoView = [[[NSBox alloc] init] autorelease];
69   [promoView setBoxType:NSBoxCustom];
70   [promoView setFillColor:gfx::SkColorToDeviceNSColor(kBackgroundColor)];
71   [promoView setContentViewMargins:NSMakeSize(chrome_style::kHorizontalPadding,
72                                               kVerticalPadding)];
73   [promoView setBorderType:NSLineBorder];
74   [promoView setBorderWidth:kBorderWidth];
75   [promoView setBorderColor:gfx::SkColorToDeviceNSColor(kBorderColor)];
76
77   // Add the sync promo text.
78   size_t offset;
79   const base::string16 linkText = l10n_util::GetStringUTF16(
80       IDS_BOOKMARK_SYNC_PROMO_LINK);
81   const base::string16 promoText =  l10n_util::GetStringFUTF16(
82       IDS_BOOKMARK_SYNC_PROMO_MESSAGE,
83       linkText, &offset);
84   const base::string16 promoTextWithoutLink =
85       promoText.substr(0, offset) +
86       promoText.substr(offset + linkText.size());
87
88   NSFont* font = [NSFont labelFontOfSize:kFontSize];
89   NSColor* linkColor = gfx::SkColorToCalibratedNSColor(
90       chrome_style::GetLinkColor());
91
92   textView_.reset([[HyperlinkTextView alloc] init]);
93   [textView_ setMessageAndLink:base::SysUTF16ToNSString(promoTextWithoutLink)
94                       withLink:base::SysUTF16ToNSString(linkText)
95                       atOffset:offset
96                           font:font
97                   messageColor:gfx::SkColorToDeviceNSColor(kTextColor)
98                      linkColor:linkColor];
99   [textView_ setRefusesFirstResponder:YES];
100   [[textView_ textContainer] setLineFragmentPadding:0.0];
101   RemoveUnderlining(textView_, offset, linkText.size());
102   [textView_ setDelegate:self];
103
104   [promoView setContentView:textView_];
105
106   [self setView:promoView];
107 }
108
109 - (BOOL)textView:(NSTextView *)textView
110    clickedOnLink:(id)link
111          atIndex:(NSUInteger)charIndex {
112   chrome::ShowBrowserSignin(browser_, signin::SOURCE_BOOKMARK_BUBBLE);
113   return YES;
114 }
115
116 @end