summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/sad_tab_view.mm
blob: 1143025bd2578c15ec65434dcb167dc7c5d5e4e6 (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
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2009 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.

#include "chrome/browser/cocoa/sad_tab_view.h"

#include "app/l10n_util_mac.h"
#include "app/resource_bundle.h"
#include "base/sys_string_conversions.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"

static const int kSadTabOffset = -64;
static const int kIconTitleSpacing = 20;
static const int kTitleMessageSpacing = 15;

@implementation SadTabView

- (void)drawRect:(NSRect)dirtyRect {
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  NSImage* sadTabImage = rb.GetNSImageNamed(IDR_SAD_TAB);
  DCHECK(sadTabImage);
  NSString* title = l10n_util::GetNSStringWithFixup(IDS_SAD_TAB_TITLE);
  NSString* message = l10n_util::GetNSStringWithFixup(IDS_SAD_TAB_MESSAGE);

  NSColor* textColor = [NSColor whiteColor];
  NSColor* backgroundColor = [NSColor colorWithCalibratedRed:(35.0f/255.0f)
                                                       green:(48.0f/255.0f)
                                                        blue:(64.0f/255.0f)
                                                       alpha:1.0];

  // Layout
  NSFont* titleFont = [NSFont boldSystemFontOfSize:[NSFont systemFontSize]];
  NSFont* messageFont = [NSFont systemFontOfSize:[NSFont smallSystemFontSize]];

  NSDictionary* titleAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                              titleFont, NSFontAttributeName,
                              textColor, NSForegroundColorAttributeName,
                              nil];
  NSDictionary* messageAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
                                messageFont, NSFontAttributeName,
                                textColor, NSForegroundColorAttributeName,
                                nil];

  NSAttributedString* titleString =
      [[[NSAttributedString alloc] initWithString:title
                                       attributes:titleAttrs] autorelease];
  NSAttributedString* messageString =
      [[[NSAttributedString alloc] initWithString:message
                                       attributes:messageAttrs] autorelease];

  NSRect viewBounds = [self bounds];

  NSSize sadTabImageSize = [sadTabImage size];
  CGFloat iconWidth = sadTabImageSize.width;
  CGFloat iconHeight = sadTabImageSize.height;
  CGFloat iconX = (viewBounds.size.width - iconWidth) / 2;
  CGFloat iconY =
      ((viewBounds.size.height - iconHeight) / 2) - kSadTabOffset;

  NSSize titleSize = [titleString size];
  CGFloat titleX = (viewBounds.size.width - titleSize.width) / 2;
  CGFloat titleY = iconY - kIconTitleSpacing - titleSize.height;

  NSSize messageSize = [messageString size];
  CGFloat messageX = (viewBounds.size.width - messageSize.width) / 2;
  CGFloat messageY = titleY - kTitleMessageSpacing - messageSize.height;

  // Paint
  [backgroundColor set];
  NSRectFill(viewBounds);

  [sadTabImage drawAtPoint:NSMakePoint(iconX, iconY)
                  fromRect:NSZeroRect
                 operation:NSCompositeSourceOver
                  fraction:1.0f];
  [titleString drawAtPoint:NSMakePoint(titleX, titleY)];
  [messageString drawAtPoint:NSMakePoint(messageX, messageY)];
}

@end